File Source: ActionConditionsListPanel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsListPanel__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.actions.ActionCondition;
26 import com.dmdirc.actions.interfaces.ActionType;
27 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
28
29 import java.beans.PropertyChangeEvent;
30 import java.beans.PropertyChangeListener;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34
35 import java.util.Map;
36 import javax.swing.JLabel;
37 import javax.swing.JPanel;
38
39 import net.miginfocom.swing.MigLayout;
40
41 /**
42 * Action conditions list panel.
43 */
44 public class ActionConditionsListPanel extends JPanel implements ActionConditionRemovalListener,
45 PropertyChangeListener {
46
47 /**
48 * A version number for this class. It should be changed whenever the class
49 * structure is changed (or anything else that would prevent serialized
50 * objects being unserialized with the new class).
51 */
52 private static final long serialVersionUID = 1;
53 /** Action trigger. */
54 private ActionType trigger;
55 /** Conditions list. */
56 private List<ActionConditionDisplayPanel> conditions;
57 /** Condition tree panel. */
58 private ActionConditionsTreePanel treePanel;
59 /** Condition validation results. */
60 private Map<ActionConditionDisplayPanel, Boolean> validations;
61 /** validates. */
62 private boolean validates = true;
63
64 /**
65 * Instantiates the panel.
66 *
67 * @param treePanel Condition tree panel.
68 */
69 public ActionConditionsListPanel(final ActionConditionsTreePanel treePanel) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsListPanel(ActionConditionsTreePanel)
*
* Postconditions:
* this.conditions == &new ArrayList(ActionConditionsListPanel#2)
* this.treePanel == treePanel
* init'ed(this.treePanel)
* this.trigger == null
* this.validates == 1
* new ArrayList(ActionConditionsListPanel#2) num objects == 1
* new HashMap(ActionConditionsListPanel#1) num objects == 1
* this.validations == &new HashMap(ActionConditionsListPanel#1)
*/
70 this(null, new ArrayList<ActionConditionDisplayPanel>(), treePanel);
71 }
72
73 /**
74 * Instantiates the panel.
75 *
76 * @param trigger Action trigger
77 * @param treePanel Condition tree panel.
78 */
79 public ActionConditionsListPanel(final ActionType trigger,
80 final ActionConditionsTreePanel treePanel) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsListPanel(ActionType, ActionConditionsTreePanel)
*
* Postconditions:
* this.conditions == &new ArrayList(ActionConditionsListPanel#2)
* this.treePanel == treePanel
* init'ed(this.treePanel)
* this.trigger == trigger
* init'ed(this.trigger)
* this.validates == 1
* new ArrayList(ActionConditionsListPanel#2) num objects == 1
* new HashMap(ActionConditionsListPanel#1) num objects == 1
* this.validations == &new HashMap(ActionConditionsListPanel#1)
*/
81 this(trigger, new ArrayList<ActionConditionDisplayPanel>(), treePanel);
82 }
83
84 /**
85 * Instantiates the panel.
86 *
87 * @param trigger Action trigger
88 * @param conditions List of existing conditions;
89 * @param treePanel Condition tree panel.
90 */
91 public ActionConditionsListPanel(final ActionType trigger,
92 final List<ActionConditionDisplayPanel> conditions,
93 final ActionConditionsTreePanel treePanel) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionsListPanel(ActionType, List, ActionConditionsTreePanel)
*
* Postconditions:
* this.conditions == &new ArrayList(ActionConditionsListPanel#2)
* this.treePanel == treePanel
* init'ed(this.treePanel)
* this.trigger == trigger
* init'ed(this.trigger)
* this.validates == 1
* new ArrayList(ActionConditionsListPanel#2) num objects == 1
* new HashMap(ActionConditionsListPanel#1) num objects == 1
* this.validations == &new HashMap(ActionConditionsListPanel#1)
*/
94 super();
95
96 validations = new HashMap<ActionConditionDisplayPanel, Boolean>();
97
98 this.trigger = trigger;
99 this.conditions = new ArrayList<ActionConditionDisplayPanel>(conditions);
100 this.treePanel = treePanel;
101
102 initComponents();
103 addListeners();
104 layoutComponents();
105 }
106
107 /** Initialises the components. */
108 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Preconditions:
* init'ed(this.trigger)
* (soft) this.conditions != null
*
* Test Vectors:
* this.trigger: Inverse{null}, Addr_Set{null}
*/
109 setLayout(new MigLayout("fillx, wrap 2, pack"));
110
111 if (trigger == null) {
112 setEnabled(false);
113 }
114 }
115
116 /** Adds the listeners. */
117 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.conditions != null
*
* Presumptions:
* condition.listeners@118 != null
* java.util.Iterator:next(...)@118 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@118: {0}, {1}
*/
118 for (ActionConditionDisplayPanel condition : conditions) {
119 condition.addConditionListener(this);
120 }
121 }
122
123 /** Lays out the components. */
124 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.trigger)
* (soft) this.conditions != null
*
* Presumptions:
* com.dmdirc.actions.interfaces.ActionMetaType:getArgNames(...)@131 != null
* com.dmdirc.actions.interfaces.ActionType:getType(...)@131 != null
*
* Test Vectors:
* this.trigger: Inverse{null}, Addr_Set{null}
* com.dmdirc.actions.interfaces.ActionMetaType:getArgNames(...).length@131: {1..+Inf}, {0}
* java.util.Iterator:hasNext(...)@136: {1}, {0}
*/
125 setVisible(false);
126 removeAll();
127 int index = 0;
128 if (trigger == null) {
129 add(new TextLabel("You must add at least one trigger before you can add conditions."),
130 "alignx center, aligny top, grow, push, w 90%!");
131 } else if (trigger.getType().getArgNames().length == 0) {
132 add(new TextLabel("Trigger does not have any arguments."),
133 "alignx center, aligny top, grow, push, w 90%!");
134 } else {
135 synchronized (conditions) {
136 for (ActionConditionDisplayPanel condition : conditions) {
137 index++;
138 add(new JLabel(index + "."), "aligny top");
139 add(condition, "growx, pushx, aligny top");
140 }
141 }
142 if (index == 0) {
143 add(new JLabel("No conditions."),
144 "alignx center, aligny top, growx, pushx");
145 }
146 }
147 setVisible(true);
148
149 }
150
151 /**
152 * Adds an action condition to the list.
153 *
154 * @param condition Action condition
155 */
156 public void addCondition(final ActionCondition condition) {
/*
P/P * Method: void addCondition(ActionCondition)
*
* Preconditions:
* condition != null
* init'ed(this.validates)
* this.conditions != null
* this.treePanel != null
* this.treePanel.rule != null
* this.treePanel.rule.errorIcon != null
* this.treePanel.rule.textField != null
* this.treePanel.treeValidator != null
* init'ed(this.trigger)
* this.validations != null
* ...
*
* Postconditions:
* init'ed(this.treePanel.conditionCount)
* init'ed(this.validates)
*/
157 final ActionConditionDisplayPanel panel =
158 new ActionConditionDisplayPanel(condition, trigger);
159 panel.addConditionListener(this);
160 panel.addPropertyChangeListener("validationResult", this);
161 validations.put(panel, panel.checkError());
162 propertyChange(null);
163 synchronized (conditions) {
164 conditions.add(panel);
165 }
166 treePanel.setConditionCount(conditions.size());
167 layoutComponents();
168 }
169
170 /**
171 * Deletes an action condition from the list.
172 *
173 * @param condition Action condition
174 */
175 public void delCondition(final ActionCondition condition) {
/*
P/P * Method: void delCondition(ActionCondition)
*
* Preconditions:
* this.conditions != null
* this.treePanel != null
* this.treePanel.rule != null
* this.treePanel.rule.errorIcon != null
* this.treePanel.rule.textField != null
* this.treePanel.treeValidator != null
* (soft) init'ed(this.validates)
* (soft) this.treePanel.rule.validator != null
* (soft) init'ed(this.trigger)
* (soft) this.validations != null
*
* Presumptions:
* java.util.Iterator:next(...)@179 != null
* localCondition.condition@179 != null
*
* Postconditions:
* init'ed(this.treePanel.conditionCount)
* init'ed(this.validates)
*
* Test Vectors:
* com.dmdirc.actions.ActionCondition:equals(...)@180: {0}, {1}
* java.util.Iterator:hasNext(...)@179: {1}, {0}
*/
176 ActionConditionDisplayPanel removeCondition = null;
177
178 synchronized (conditions) {
179 for (ActionConditionDisplayPanel localCondition : conditions) {
180 if (localCondition.getCondition().equals(condition)) {
181 removeCondition = localCondition;
182 break;
183 }
184 }
185 }
186
187 treePanel.setConditionCount(conditions.size());
188
189 if (removeCondition != null) {
190 conditionRemoved(removeCondition);
191 }
192 }
193
194 /**
195 * Clear conditions.
196 */
197 public void clearConditions() {
/*
P/P * Method: void clearConditions()
*
* Preconditions:
* this.conditions != null
* (soft) init'ed(this.validates)
* (soft) this.treePanel != null
* (soft) this.treePanel.rule != null
* (soft) this.treePanel.rule.errorIcon != null
* (soft) this.treePanel.rule.textField != null
* (soft) this.treePanel.rule.validator != null
* (soft) this.treePanel.treeValidator != null
* (soft) init'ed(this.trigger)
* (soft) this.validations != null
*
* Presumptions:
* java.util.Iterator:next(...)@198 != null
*
* Postconditions:
* possibly_updated(this.treePanel.conditionCount)
* init'ed(this.validates)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@198: {0}, {1}
*/
198 for (ActionConditionDisplayPanel condition : conditions) {
199 delCondition(condition.getCondition());
200 }
201 }
202
203 /**
204 * Returns the condition list.
205 *
206 * @return condition list
207 */
208 public List<ActionCondition> getConditions() {
/*
P/P * Method: List getConditions()
*
* Preconditions:
* this.conditions != null
*
* Presumptions:
* java.util.Iterator:next(...)@213 != null
*
* Postconditions:
* return_value == &new ArrayList(getConditions#1)
* new ArrayList(getConditions#1) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@213: {1}, {0}
*/
209 final List<ActionCondition> conditionList =
210 new ArrayList<ActionCondition>();
211
212 synchronized (conditions) {
213 for (ActionConditionDisplayPanel condition : conditions) {
214 conditionList.add(condition.getCondition());
215 }
216 }
217
218 return conditionList;
219 }
220
221 /**
222 * Sets the action trigger for the panel.
223 *
224 * @param trigger Action trigger
225 */
226 public void setTrigger(final ActionType trigger) {
/*
P/P * Method: void setTrigger(ActionType)
*
* Preconditions:
* init'ed(this.trigger)
* this.conditions != null
*
* Presumptions:
* java.util.Iterator:next(...)@231 != null
* panel.condition@231 != null
* panel.editButton@231 != null
* panel.editPanel.arguments@231 != null
* panel.editPanel.comparisons@231 != null
* ...
*
* Postconditions:
* this.trigger == trigger
* init'ed(this.trigger)
*
* Test Vectors:
* this.trigger: Inverse{null}, Addr_Set{null}
* java.util.Iterator:hasNext(...)@231: {0}, {1}
*/
227 if (this.trigger == null) {
228 conditions.clear();
229 }
230
231 for (ActionConditionDisplayPanel panel : conditions) {
232 panel.setTrigger(trigger);
233 }
234 this.trigger = trigger;
235 setEnabled(trigger != null);
236 layoutComponents();
237 }
238
239 /** {@inheritDoc} */
240 @Override
241 public void conditionRemoved(final ActionConditionDisplayPanel condition) {
/*
P/P * Method: void conditionRemoved(ActionConditionDisplayPanel)
*
* Preconditions:
* init'ed(this.validates)
* this.conditions != null
* init'ed(this.trigger)
* this.validations != null
*
* Postconditions:
* init'ed(this.validates)
*/
242 synchronized (conditions) {
243 conditions.remove(condition);
244 validations.remove(condition);
245 }
246 propertyChange(null);
247 layoutComponents();
248 }
249
250 /** {@inheritDoc} */
251 @Override
252 public void setEnabled(final boolean enabled) {
/*
P/P * Method: void setEnabled(bool)
*
* Preconditions:
* this.conditions != null
*
* Presumptions:
* condition.deleteButton@253 != null
* condition.editButton@253 != null
* condition.editPanel.arguments@253 != null
* condition.editPanel.comparisons@253 != null
* condition.editPanel.components@253 != null
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@253: {0}, {1}
*/
253 for (ActionConditionDisplayPanel condition : conditions) {
254 condition.setEnabled(enabled);
255 }
256 }
257
258 /** {@inheritDoc} */
259 @Override
260 public void propertyChange(final PropertyChangeEvent evt) {
/*
P/P * Method: void propertyChange(PropertyChangeEvent)
*
* Preconditions:
* init'ed(this.validates)
* this.validations != null
*
* Presumptions:
* java.util.Iterator:next(...)@267 != null
* java.util.Map:values(...)@267 != null
*
* Postconditions:
* init'ed(this.validates)
*
* Test Vectors:
* evt: Addr_Set{null}, Inverse{null}
* java.lang.Boolean:booleanValue(...)@267: {1}, {0}
* java.util.Iterator:hasNext(...)@267: {0}, {1}
*/
261 if (evt != null) {
262 validations.put((ActionConditionDisplayPanel) evt.getSource(),
263 (Boolean) evt.getNewValue());
264 }
265
266 boolean pass = true;
267 for (boolean validation : validations.values()) {
268 if (!validation) {
269 pass = false;
270 break;
271 }
272 }
273
274 firePropertyChange("validationResult", validates, pass);
275 validates = pass;
276 }
277 }
SofCheck Inspector Build Version : 2.17854
| ActionConditionsListPanel.java |
2009-Jun-25 01:54:24 |
| ActionConditionsListPanel.class |
2009-Sep-02 17:04:15 |