/*
* Copyright, Aspect Security, Inc.
*
* This file is part of JavaSnoop.
*
* JavaSnoop is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JavaSnoop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JavaSnoop. If not, see <http://www.gnu.org/licenses/>.
*/
package com.aspect.snoop.ui.condition;
import com.aspect.snoop.Condition;
import com.aspect.snoop.Condition.Operator;
import com.aspect.snoop.util.ModelUIUtil;
import com.aspect.snoop.util.ReflectionUtil;
public class AddEditConditionView extends javax.swing.JDialog {
private int parameter;
private String operand;
private Operator operator;
public AddEditConditionView(java.awt.Frame parent, boolean modal, Class[] parameterTypes) {
this(parent,modal,parameterTypes, null);
}
public AddEditConditionView(java.awt.Frame parent, boolean modal, Class[] parameterTypes, Condition condition) {
super(parent, modal);
initComponents();
lstParameters.removeAllItems();
for(int i=0;i<parameterTypes.length;i++) {
Class type = parameterTypes[i];
String simple = type.getSimpleName();
if ( ! type.getName().startsWith("java.lang.") || type.getName().indexOf(".") != -1 ) {
simple = ReflectionUtil.getSimpleClassName(type.getName());
}
lstParameters.insertItemAt("Parameter " + (i+1) + ": " + simple, i);
}
if (condition != null) {
setTitle("Editing condition");
btnAddCondition.setText("Save condition");
txtOperand.setText(condition.getOperand());
lstOperator.setSelectedItem( ModelUIUtil.getDescriptionByOperator( condition.getOperator()) );
lstParameters.setSelectedIndex(condition.getParameter());
}
txtOperand.requestFocus();
parameter = -1;
operand = null;
operator = null;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
lstParameters = new javax.swing.JComboBox();
lstOperator = new javax.swing.JComboBox();
txtOperand = new javax.swing.JTextField();
btnAddCondition = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.aspect.snoop.JavaSnoop.class).getContext().getResourceMap(AddEditConditionView.class);
setTitle(resourceMap.getString("Form.title")); // NOI18N
setModal(true);
setName("Form"); // NOI18N
setResizable(false);
lstParameters.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Parameter 1 (String)", "Parameter 2 (int)" }));
lstParameters.setName("lstParameters"); // NOI18N
lstOperator.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "equals", "contains", "starts with", "ends with", "greater than", "less than" }));
lstOperator.setName("lstOperator"); // NOI18N
txtOperand.setText(resourceMap.getString("txtOperand.text")); // NOI18N
txtOperand.setName("txtOperand"); // NOI18N
txtOperand.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtOperandActionPerformed(evt);
}
});
btnAddCondition.setFont(resourceMap.getFont("btnAddCondition.font")); // NOI18N
btnAddCondition.setText(resourceMap.getString("btnAddCondition.text")); // NOI18N
btnAddCondition.setToolTipText(resourceMap.getString("btnAddCondition.toolTipText")); // NOI18N
btnAddCondition.setName("btnAddCondition"); // NOI18N
btnAddCondition.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddConditionActionPerformed(evt);
}
});
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
jLabel2.setName("jLabel2"); // NOI18N
jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
jLabel3.setName("jLabel3"); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnAddCondition, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lstParameters, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jLabel3))
.addGroup(layout.createSequentialGroup()
.addComponent(lstOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtOperand, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lstParameters, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lstOperator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtOperand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btnAddCondition, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btnAddConditionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddConditionActionPerformed
if ( lstParameters.getSelectedIndex() != -1 && ! "".equals(txtOperand.getText()) ) { // make sure entry isn't blank
operator = ModelUIUtil.getOperatorByDescription((String)lstOperator.getSelectedItem());
if ( operator.equals(Operator.GreaterThan) || operator.equals(Operator.LessThan) ) {
if ( ! isInt(operand) && ! isFloat(operand) && ! isDouble(operand) ) {
return;
}
}
finalizeSelection();
dispose();
}
}//GEN-LAST:event_btnAddConditionActionPerformed
private void txtOperandActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtOperandActionPerformed
// user pressed enter -> click the save button
btnAddCondition.doClick();
}//GEN-LAST:event_txtOperandActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnAddCondition;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JComboBox lstOperator;
private javax.swing.JComboBox lstParameters;
private javax.swing.JTextField txtOperand;
// End of variables declaration//GEN-END:variables
private void finalizeSelection() {
operator = ModelUIUtil.getOperatorByDescription((String)lstOperator.getSelectedItem());
operand = txtOperand.getText();
parameter = lstParameters.getSelectedIndex();
}
/**
* @return the parameter
*/
public int getParameter() {
return parameter;
}
/**
* @return the operand
*/
public String getOperand() {
return operand;
}
/**
* @return the operator
*/
public Operator getOperator() {
return operator;
}
private boolean isInt(String operand) {
try {
Integer.parseInt(operand);
return true;
} catch (Exception e) { }
return false;
}
private boolean isFloat(String operand) {
try {
Float.parseFloat(operand);
return true;
} catch (Exception e) { }
return false;
}
private boolean isDouble(String operand) {
try {
Double.parseDouble(operand);
return true;
} catch (Exception e) { }
return false;
}
}