File Source: ActionConditionsTreePanel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsTreePanel__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.ui_swing.dialogs.actioneditor;
24
25 import com.dmdirc.config.prefs.validator.ConditionRuleValidator;
26 import com.dmdirc.actions.ConditionTree;
27 import com.dmdirc.actions.ConditionTreeFactory;
28 import com.dmdirc.actions.ConditionTreeFactory.ConditionTreeFactoryType;
29 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
30 import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
31
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34
35 import java.beans.PropertyChangeEvent;
36 import java.beans.PropertyChangeListener;
37 import javax.swing.ButtonGroup;
38 import javax.swing.JPanel;
39 import javax.swing.JRadioButton;
40 import javax.swing.event.DocumentEvent;
41 import javax.swing.event.DocumentListener;
42
43 import net.miginfocom.swing.MigLayout;
44
45 /**
46 * Action conditions tree panel.
47 */
48 public class ActionConditionsTreePanel extends JPanel implements ActionListener,
49 PropertyChangeListener, DocumentListener {
50
51 /**
52 * A version number for this class. It should be changed whenever the class
53 * structure is changed (or anything else that would prevent serialized
54 * objects being unserialized with the new class).
55 */
56 private static final long serialVersionUID = 1;
57 /** Button group. */
58 private ButtonGroup group;
59 /** All triggers button. */
60 private JRadioButton allButton;
61 /** One trigger button. */
62 private JRadioButton oneButton;
63 /** Custom rule button. */
64 private JRadioButton customButton;
65 /** Custom rule field. */
66 private ValidatingJTextField rule;
67 /** Condition tree factory. */
68 private ConditionTreeFactory treeFactory;
69 /** Condition count. */
70 private int conditionCount = 0;
71 /** Condition rule validator. */
72 private ConditionRuleValidator treeValidator;
73 /** validates. */
74 private boolean validates = true;
75
76 /**
77 * Instantiates the panel.
78 */
79 public ActionConditionsTreePanel() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsTreePanel()
*
* Preconditions:
* (soft) init'ed(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsTreePanel$1__static_init.new int[](ActionConditionsTreePanel$1__static_init#1)[...])
*
* Presumptions:
* com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType:values(...).length >= 1
*
* Postconditions:
* this.allButton == &new JRadioButton(initComponents#2)
* init'ed(this.conditionCount)
* this.customButton == &new JRadioButton(initComponents#4)
* this.group == &new ButtonGroup(initComponents#1)
* this.oneButton == &new JRadioButton(initComponents#3)
* this.rule == &new ValidatingJTextField(initComponents#6)
* this.treeFactory == One-of{&new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1), &new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2), &new ConditionTreeFactory$CustomFactory(sortTreeFactory#3)}
* this.treeFactory in Addr_Set{&new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1),&new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2),&new ConditionTreeFactory$CustomFactory(sortTreeFactory#3)}
* this.treeValidator == &new ConditionRuleValidator(initComponents#5)
* new ValidatingJTextField(initComponents#6).validator == &new ConditionRuleValidator(initComponents#5)
* ...
*/
80 super();
81
82 initComponents();
83 addListeners();
84 layoutComponents();
85
86 selectTreeButton();
87 }
88
89 /** Initialises the components. */
90 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Preconditions:
* init'ed(this.conditionCount)
*
* Postconditions:
* this.allButton == &new JRadioButton(initComponents#2)
* this.customButton == &new JRadioButton(initComponents#4)
* this.group == &new ButtonGroup(initComponents#1)
* this.oneButton == &new JRadioButton(initComponents#3)
* this.rule == &new ValidatingJTextField(initComponents#6)
* this.treeValidator == &new ConditionRuleValidator(initComponents#5)
* this.rule.validator == &new ConditionRuleValidator(initComponents#5)
* new ButtonGroup(initComponents#1) num objects == 1
* new ConditionRuleValidator(initComponents#5) num objects == 1
* new JLabel(ValidatingJTextField#1) num objects == 1
* ...
*/
91 group = new ButtonGroup();
92 allButton = new JRadioButton("All of the conditions are true");
93 oneButton = new JRadioButton("At least one of the conditions is true");
94 customButton = new JRadioButton("The conditions match a custom rule");
95 treeValidator = new ConditionRuleValidator(conditionCount);
96
97 rule = new ValidatingJTextField(treeValidator);
98
99 group.add(allButton);
100 group.add(oneButton);
101 group.add(customButton);
102 }
103
104 /** Adds the listeners. */
105 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.allButton != null
* this.customButton != null
* this.oneButton != null
* this.rule != null
* this.rule.textField != null
*
* Presumptions:
* javax.swing.JTextField:getDocument(...)@332 != null
*/
106 allButton.addActionListener(this);
107 oneButton.addActionListener(this);
108 customButton.addActionListener(this);
109 rule.addPropertyChangeListener("validationResult", this);
110 rule.getDocument().addDocumentListener(this);
111 }
112
113 /** Lays out the components. */
114 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.allButton)
* init'ed(this.customButton)
* init'ed(this.oneButton)
* init'ed(this.rule)
*/
115 setLayout(new MigLayout("fill, wrap 1, pack, hidemode 3"));
116 add(new TextLabel("Only execute this action if..."), "growx, pushx");
117 add(allButton, "growx, pushx");
118 add(oneButton, "growx, pushx");
119 add(customButton, "growx, pushx");
120 add(rule, "growx, pushx");
121 }
122
123 /**
124 * Selects the appropriate radio button for the tree.
125 */
126 private void selectTreeButton() {
/*
P/P * Method: void selectTreeButton()
*
* Preconditions:
* init'ed(this.treeFactory)
* this.group != null
* this.rule != null
* this.rule.errorIcon != null
* this.rule.textField != null
* (soft) init'ed(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsTreePanel$1__static_init.new int[](ActionConditionsTreePanel$1__static_init#1)[...])
* (soft) init'ed(this.validates)
* (soft) this.allButton != null
* (soft) init'ed(this.conditionCount)
* (soft) this.customButton != null
* ...
*
* Presumptions:
* com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType.CONJUNCTION != null
* com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType:values(...).length >= 1
* com.dmdirc.actions.ConditionTreeFactory_ConditionTreeFactoryType:ordinal(...)@135 < com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType:values(...).length
* com.dmdirc.actions.ConditionTreeFactory:getConditionTree(...)@143 != null
* com.dmdirc.actions.ConditionTreeFactory:getType(...)@132 != null
* ...
*
* Postconditions:
* this.treeFactory == One-of{&new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1), &new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2), &new ConditionTreeFactory$CustomFactory(sortTreeFactory#3)}
* this.treeFactory in Addr_Set{&new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1),&new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2),&new ConditionTreeFactory$CustomFactory(sortTreeFactory#3)}
* init'ed(this.validates)
* new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1) num objects <= 1
* new ConditionTreeFactory$CustomFactory(sortTreeFactory#3) num objects <= 1
* new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2) num objects <= 1
*
* Test Vectors:
* com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsTreePanel$1__static_init.new int[](ActionConditionsTreePanel$1__static_init#1)[...]: {1}, {2}, {-231..0, 3..232-1}
* this.treeFactory: Inverse{null}, Addr_Set{null}
*/
127 group.clearSelection();
128 final ConditionTreeFactoryType type;
129 if (treeFactory == null) {
130 type = ConditionTreeFactoryType.CONJUNCTION;
131 } else {
132 type = treeFactory.getType();
133 }
134
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsTreePanel$1__static_init
*
* Presumptions:
* com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType.CUSTOM != null
* com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType.DISJUNCTION != null
* com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType:values(...).length@135 >= 1
* com.dmdirc.actions.ConditionTreeFactory_ConditionTreeFactoryType:ordinal(...)@135 >= 0
* com.dmdirc.actions.ConditionTreeFactory_ConditionTreeFactoryType:ordinal(...)@135 < com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType:values(...).length@135
* ...
*
* Postconditions:
* new int[](ActionConditionsTreePanel$1__static_init#1) num objects == 1
*/
135 switch (type) {
136 case DISJUNCTION:
137 oneButton.setSelected(true);
138 rule.setText("");
139 rule.setEnabled(false);
140 break;
141 case CUSTOM:
142 customButton.setSelected(true);
143 rule.setText(treeFactory.getConditionTree(conditionCount).
144 toString());
145 rule.setEnabled(true);
146 break;
147 default:
148 allButton.setSelected(true);
149 rule.setText("");
150 rule.setEnabled(false);
151 break;
152 }
153
154 sortTreeFactory();
155 }
156
157 /** Sorts the tree factory out. */
158 private void sortTreeFactory() {
/*
P/P * Method: void sortTreeFactory()
*
* Preconditions:
* this.allButton != null
* this.group != null
* (soft) init'ed(this.validates)
* (soft) this.oneButton != null
* (soft) this.rule != null
* (soft) this.rule.errorIcon != null
* (soft) this.rule.textField != null
* (soft) this.rule.validator != null
*
* Presumptions:
* javax.swing.ButtonGroup:getSelection(...)@159 != null
* javax.swing.ButtonGroup:getSelection(...)@162 != null
*
* Postconditions:
* this.treeFactory in Addr_Set{&new ConditionTreeFactory$CustomFactory(sortTreeFactory#3),&new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2),&new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1)}
* init'ed(this.validates)
* new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1) num objects <= 1
* new ConditionTreeFactory$CustomFactory(sortTreeFactory#3) num objects <= 1
* new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2) num objects <= 1
*
* Test Vectors:
* java.lang.Object:equals(...)@159: {0}, {1}
* java.lang.Object:equals(...)@162: {0}, {1}
*/
159 if (group.getSelection().equals(allButton.getModel())) {
160 treeFactory = new ConditionTreeFactory.ConjunctionFactory();
161 firePropertyChange("validationResult", validates, true);
162 } else if (group.getSelection().equals(oneButton.getModel())) {
163 treeFactory = new ConditionTreeFactory.DisjunctionFactory();
164 firePropertyChange("validationResult", validates, true);
165 validates = true;
166 } else {
167 treeFactory =
168 new ConditionTreeFactory.CustomFactory(ConditionTree.parseString(rule.getText()));
169 rule.checkError();
170 }
171
172 }
173
174 /**
175 * {@inheritDoc}
176 *
177 * @param e Action event
178 */
179 @Override
180 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* this.allButton != null
* init'ed(this.customButton)
* this.group != null
* this.rule != null
* this.rule.errorIcon != null
* this.rule.textField != null
* (soft) init'ed(this.validates)
* (soft) this.oneButton != null
* (soft) this.rule.validator != null
*
* Presumptions:
* java.awt.event.ActionEvent:getSource(...)@181 != null
*
* Postconditions:
* this.treeFactory == One-of{&new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1), &new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2), &new ConditionTreeFactory$CustomFactory(sortTreeFactory#3)}
* this.treeFactory in Addr_Set{&new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1),&new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2),&new ConditionTreeFactory$CustomFactory(sortTreeFactory#3)}
* init'ed(this.validates)
* new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1) num objects <= 1
* new ConditionTreeFactory$CustomFactory(sortTreeFactory#3) num objects <= 1
* new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2) num objects <= 1
*/
181 rule.setEnabled(e.getSource().equals(customButton));
182 sortTreeFactory();
183 }
184
185 /** {@inheritDoc} */
186 @Override
187 public void setEnabled(final boolean enabled) {
/*
P/P * Method: void setEnabled(bool)
*
* Preconditions:
* this.allButton != null
* this.customButton != null
* this.oneButton != null
*/
188 allButton.setEnabled(enabled);
189 oneButton.setEnabled(enabled);
190 customButton.setEnabled(enabled);
191 }
192
193 /**
194 * Returns the selected rule type.
195 *
196 * @param conditionCount Condition count
197 *
198 * @return Selected rule type
199 */
200 public ConditionTreeFactoryType getRuleType(final int conditionCount) {
/*
P/P * Method: ConditionTreeFactory$ConditionTreeFactoryType getRuleType(int)
*
* Preconditions:
* (soft) this.treeFactory != null
*
* Presumptions:
* init'ed(com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType.CONJUNCTION)
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* conditionCount: {-231..1}, {2..232-1}
*/
201 if (conditionCount >= 2) {
202 return treeFactory.getType();
203 } else {
204 return ConditionTreeFactoryType.CONJUNCTION;
205 }
206 }
207
208 /**
209 * Returns the current custom rule.
210 *
211 * @param conditionCount number of conditions
212 *
213 * @return Custom rule
214 */
215 public ConditionTree getRule(final int conditionCount) {
/*
P/P * Method: ConditionTree getRule(int)
*
* Preconditions:
* this.treeFactory != null
*
* Postconditions:
* init'ed(return_value)
*/
216 treeFactory.getConditionTree(conditionCount);
217 return treeFactory.getConditionTree(conditionCount);
218 }
219
220 /**
221 * Sets the tree rule.
222 *
223 * @param conditionCount condition count
224 * @param tree new condition tree
225 */
226 public void setRule(final int conditionCount, final ConditionTree tree) {
/*
P/P * Method: void setRule(int, ConditionTree)
*
* Preconditions:
* (soft) init'ed(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsTreePanel$1__static_init.new int[](ActionConditionsTreePanel$1__static_init#1)[...])
* (soft) init'ed(this.validates)
* (soft) this.allButton != null
* (soft) this.customButton != null
* (soft) this.group != null
* (soft) this.oneButton != null
* (soft) this.rule != null
* (soft) this.rule.errorIcon != null
* (soft) this.rule.textField != null
* (soft) this.rule.validator != null
*
* Presumptions:
* com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType:values(...).length >= 1
*
* Postconditions:
* this.conditionCount == One-of{old this.conditionCount, conditionCount}
* this.treeFactory == One-of{old this.treeFactory, &new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1), &new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2), &new ConditionTreeFactory$CustomFactory(sortTreeFactory#3)}
* init'ed(this.validates)
* new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1) num objects <= 1
* new ConditionTreeFactory$CustomFactory(sortTreeFactory#3) num objects <= 1
* new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2) num objects <= 1
*
* Test Vectors:
* conditionCount: {-231..1}, {2..232-1}
* tree: Addr_Set{null}, Inverse{null}
*/
227 if (tree != null && conditionCount >= 2) {
228 this.conditionCount = conditionCount;
229 treeFactory = ConditionTreeFactory.getFactory(tree, conditionCount);
230 selectTreeButton();
231 }
232 }
233
234 /**
235 * Sets the new condition count.
236 *
237 * @param conditionCount new condition count.
238 */
239 public void setConditionCount(final int conditionCount) {
/*
P/P * Method: void setConditionCount(int)
*
* Preconditions:
* this.rule != null
* this.rule.errorIcon != null
* this.rule.textField != null
* this.treeValidator != null
* (soft) this.rule.validator != null
*
* Postconditions:
* this.conditionCount == conditionCount
* init'ed(this.conditionCount)
*/
240 this.conditionCount = conditionCount;
241 treeValidator.setArgs(conditionCount);
242 rule.checkError();
243 }
244
245 /** {@inheritDoc} */
246 @Override
247 public void propertyChange(final PropertyChangeEvent evt) {
/*
P/P * Method: void propertyChange(PropertyChangeEvent)
*
* Preconditions:
* evt != null
* init'ed(this.validates)
*
* Presumptions:
* java.beans.PropertyChangeEvent:getNewValue(...)@249 != null
*
* Postconditions:
* init'ed(this.validates)
*/
248 firePropertyChange("validationResult", validates, evt.getNewValue());
249 validates = (Boolean) evt.getNewValue();
250 }
251
252 /** Validates the conditions. */
253 public void validateConditions() {
/*
P/P * Method: void validateConditions()
*
* Preconditions:
* init'ed(this.treeFactory)
* this.group != null
* this.rule != null
* this.rule.errorIcon != null
* this.rule.textField != null
* (soft) init'ed(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsTreePanel$1__static_init.new int[](ActionConditionsTreePanel$1__static_init#1)[...])
* (soft) init'ed(this.validates)
* (soft) this.allButton != null
* (soft) init'ed(this.conditionCount)
* (soft) this.customButton != null
* ...
*
* Presumptions:
* com.dmdirc.actions.ConditionTreeFactory$ConditionTreeFactoryType:values(...).length >= 1
*
* Postconditions:
* this.treeFactory == One-of{&new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1), &new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2), &new ConditionTreeFactory$CustomFactory(sortTreeFactory#3)}
* this.treeFactory in Addr_Set{&new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1),&new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2),&new ConditionTreeFactory$CustomFactory(sortTreeFactory#3)}
* init'ed(this.validates)
* new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1) num objects <= 1
* new ConditionTreeFactory$CustomFactory(sortTreeFactory#3) num objects <= 1
* new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2) num objects <= 1
*/
254 selectTreeButton();
255 }
256
257 /** {@inheritDoc} */
258 @Override
259 public void insertUpdate(final DocumentEvent e) {
/*
P/P * Method: void insertUpdate(DocumentEvent)
*
* Preconditions:
* this.rule != null
* this.rule.textField != null
*
* Postconditions:
* this.treeFactory == &new ConditionTreeFactory$CustomFactory(insertUpdate#1)
* new ConditionTreeFactory$CustomFactory(insertUpdate#1) num objects == 1
*/
260 treeFactory =
261 new ConditionTreeFactory.CustomFactory(ConditionTree.parseString(rule.getText()));
262 }
263
264 /** {@inheritDoc} */
265 @Override
266 public void removeUpdate(final DocumentEvent e) {
/*
P/P * Method: void removeUpdate(DocumentEvent)
*
* Preconditions:
* this.rule != null
* this.rule.textField != null
*
* Postconditions:
* this.treeFactory == &new ConditionTreeFactory$CustomFactory(removeUpdate#1)
* new ConditionTreeFactory$CustomFactory(removeUpdate#1) num objects == 1
*/
267 treeFactory =
268 new ConditionTreeFactory.CustomFactory(ConditionTree.parseString(rule.getText()));
269 }
270
271 /** {@inheritDoc} */
272 @Override
273 public void changedUpdate(final DocumentEvent e) {
274 //Ignore
/*
P/P * Method: void changedUpdate(DocumentEvent)
*/
275 }
276 }
SofCheck Inspector Build Version : 2.17854
| ActionConditionsTreePanel.java |
2009-Jun-25 01:54:24 |
| ActionConditionsTreePanel.class |
2009-Sep-02 17:04:15 |
| ActionConditionsTreePanel$1.class |
2009-Sep-02 17:04:15 |