package agg.gui.treeview.dialog;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JCheckBox;
import javax.swing.border.TitledBorder;
import agg.editor.impl.EdGraGra;
import agg.editor.impl.EdRule;
import agg.cons.Formula;
/**
* This class provides a window for a user dialog. This dialog is necessary to
* select atomic constraints and formulas to generate post application
* conditions for a rule.
*
* @author $Author: olga $
* @version $Id
*/
@SuppressWarnings("serial")
public class RuleConstraintsDialog extends JDialog implements ActionListener {
private JPanel contentPane;
private JPanel panel;
private JPanel formulaPanel;
private JPanel buttonPanel;
private JScrollPane formulaScrollPane;
private Vector<Formula> allFormulas;
private Vector<JCheckBox> checkBox;
private JButton allFormulaButton;
private JButton closeButton;
private JButton cancelButton;
private boolean isCancelled;
private EdGraGra gragra;
private EdRule rule;
private Vector<Formula> resultFormulas;
/**
* Creates a rule constraints (formulas) dialog.
*
* @param parent
* The parent frame of this dialog.
* @param r
* The rule for each the constraints have to be hold.
*/
public RuleConstraintsDialog(JFrame parent, EdRule r) {
super(parent, true);
setTitle(" Formulas ");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
exitForm(evt);
}
});
if (parent != null)
setLocationRelativeTo(parent);
else
setLocation(300, 100);
if (r != null && r.getBasisRule() != null) {
this.rule = r;
this.gragra = r.getGraGra();
this.allFormulas = new Vector<Formula>();
Enumeration<Formula> en = this.gragra.getBasisGraGra().getConstraints();
while (en.hasMoreElements()) {
Formula f = en.nextElement();
if (f.isEnabled())
this.allFormulas.addElement(f);
}
this.checkBox = new Vector<JCheckBox>();
initComponents(this.rule.getBasisRule().getUsedFormulas());
}
}
/**
* 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 FormEditor.
*/
private void initComponents(Vector<Formula> formulas) {
this.contentPane = new JPanel(new BorderLayout());
this.contentPane.setBackground(Color.lightGray);
this.panel = new JPanel(new BorderLayout());
this.panel.setBackground(Color.orange);
this.panel.setBorder(new TitledBorder("Select Formula"));
this.formulaPanel = new JPanel(new GridLayout(this.allFormulas.size(), 1));
for (int i = 0; i < this.allFormulas.size(); i++) {
Formula f = this.allFormulas.elementAt(i);
JCheckBox cb = new JCheckBox(f.getName(), null, true);
cb.addActionListener(this);
if (formulas.contains(f)) {
cb.setSelected(true);
} else {
cb.setSelected(false);
}
this.checkBox.addElement(cb);
this.formulaPanel.add(cb);
}
this.formulaScrollPane = new JScrollPane(this.formulaPanel);
this.formulaScrollPane.setPreferredSize(new Dimension(100, 100));
this.allFormulaButton = new JButton();
this.allFormulaButton.setActionCommand("allItems");
this.allFormulaButton.setText("Select All");
this.allFormulaButton.addActionListener(this);
this.panel.add(this.formulaScrollPane, BorderLayout.CENTER);
this.panel.add(this.allFormulaButton, BorderLayout.SOUTH);
this.buttonPanel = new JPanel(new GridBagLayout());
this.closeButton = new JButton();
this.closeButton.setActionCommand("close");
this.closeButton.setText("Close");
this.closeButton.setToolTipText("Create Post Application Condition of selected items and close.");
this.closeButton.addActionListener(this);
this.cancelButton = new JButton();
this.isCancelled = false;
this.cancelButton.setActionCommand("cancel");
this.cancelButton.setText("Cancel");
this.cancelButton.addActionListener(this);
constrainBuild(this.buttonPanel, this.closeButton, 0, 0, 1, 1,
GridBagConstraints.BOTH, GridBagConstraints.CENTER, 1.0, 0.0,
5, 10, 10, 5);
constrainBuild(this.buttonPanel, this.cancelButton, 1, 0, 1, 1,
GridBagConstraints.BOTH, GridBagConstraints.CENTER, 1.0, 0.0,
5, 5, 10, 10);
this.contentPane.add(this.panel, BorderLayout.CENTER);
this.contentPane.add(this.buttonPanel, BorderLayout.SOUTH);
this.contentPane.revalidate();
setContentPane(this.contentPane);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
validate();
pack();
}
/** Exit the Application */
void exitForm(WindowEvent evt) {
setVisible(false);
dispose();
}
public void showGUI() {
setVisible(true);
}
/**
* This handels the clicks on the different buttons.
*
* @param e
* The event from the buttons.
*/
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == this.allFormulaButton) {
// System.out.println("Select All button");
for (int j = 0; j < this.checkBox.size(); j++) {
JCheckBox cb = this.checkBox.elementAt(j);
cb.setSelected(true);
}
} else if (source == this.closeButton) {
// System.out.println("Close button");
this.resultFormulas = new Vector<Formula>();
for (int j = 0; j < this.checkBox.size(); j++) {
JCheckBox cb = this.checkBox.elementAt(j);
if (cb.isSelected())
this.resultFormulas.addElement(this.allFormulas.elementAt(j));
}
// System.out.println("Used Formulas: "+resultFormulas);
// if(!resultFormulas.isEmpty())
// rule.getBasisRule().setUsedFormulas(resultFormulas);
setVisible(false);
dispose();
} else if (source == this.cancelButton) {
this.isCancelled = true;
setVisible(false);
dispose();
}
}
public void setRule(EdRule rule) {
this.rule = rule;
this.gragra = rule.getGraGra();
}
/** Return selected formulas as an Vector with Formula as an Element */
public Vector<Formula> getFormulas() {
return this.resultFormulas;
}
public boolean isCancelled() {
return this.isCancelled;
}
// constrainBuild() method
private void constrainBuild(Container container, Component component,
int grid_x, int grid_y, int grid_width, int grid_height, int fill,
int anchor, double weight_x, double weight_y, int top, int left,
int bottom, int right) {
GridBagConstraints c = new GridBagConstraints();
c.gridx = grid_x;
c.gridy = grid_y;
c.gridwidth = grid_width;
c.gridheight = grid_height;
c.fill = fill;
c.anchor = anchor;
c.weightx = weight_x;
c.weighty = weight_y;
c.insets = new Insets(top, left, bottom, right);
((GridBagLayout) container.getLayout()).setConstraints(component, c);
container.add(component);
}
}