File Source: ActionEditorDialog.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionEditorDialog__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.Action;
26 import com.dmdirc.addons.ui_swing.components.StandardDialog;
27
28 import java.awt.Dimension;
29 import java.awt.Window;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32 import java.awt.event.WindowAdapter;
33 import java.awt.event.WindowEvent;
34 import java.beans.PropertyChangeEvent;
35 import java.beans.PropertyChangeListener;
36
37 import javax.swing.JButton;
38
39 import net.miginfocom.swing.MigLayout;
40
41 /**
42 * Action editor dialog.
43 */
44 public class ActionEditorDialog extends StandardDialog implements ActionListener,
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 /** Previously created instance of ActionEditorDialog. */
54 private static volatile ActionEditorDialog me;
55 /** Name panel. */
56 private ActionNamePanel name;
57 /** Triggers panel. */
58 private ActionTriggersPanel triggers;
59 /** Response panel. */
60 private ActionResponsePanel response;
61 /** Conditions panel. */
62 private ActionConditionsPanel conditions;
63 /** Substitutions panel. */
64 private ActionSubstitutionsPanel substitutions;
65 /** Show substitutions button. */
66 private JButton showSubstitutions;
67 /** Is the name valid? */
68 private boolean nameValid = false;
69 /** Are the triggers valid? */
70 private boolean triggersValid = false;
71 /** Are the conditions valid? */
72 private boolean conditionsValid = false;
73 /** Action to be edited. */
74 private Action action;
75 /** Action group. */
76 private String group;
77
78 /**
79 * Instantiates the panel.
80 *
81 * @param window Parent window
82 * @param group Action's group
83 */
84 private ActionEditorDialog(final Window window, final String group) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionEditorDialog(Window, String)
*
* Postconditions:
* this.action == null
* init'ed(this.conditions)
* init'ed(this.conditionsValid)
* this.group == group
* init'ed(this.group)
* init'ed(this.name)
* init'ed(this.nameValid)
* init'ed(this.response)
* init'ed(this.showSubstitutions)
* init'ed(this.substitutions)
* ...
*/
85 this(window, group, null);
86 }
87
88 /**
89 * Instantiates the panel.
90 *
91 * @param window Parent window
92 * @param action Action to be edited
93 * @param group Action's group
94 */
95 private ActionEditorDialog(final Window window, final String group,
96 final Action action) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionEditorDialog(Window, String, Action)
*
* Presumptions:
* init'ed(java.awt.Dialog$ModalityType.DOCUMENT_MODAL)
* this.conditions != null
* this.name != null
* this.showSubstitutions != null
* this.triggers != null
*
* Postconditions:
* this.action == action
* init'ed(this.action)
* init'ed(this.conditions)
* init'ed(this.conditionsValid)
* this.group == group
* init'ed(this.group)
* init'ed(this.name)
* init'ed(this.nameValid)
* init'ed(this.response)
* init'ed(this.showSubstitutions)
* ...
*/
97 super(window, ModalityType.DOCUMENT_MODAL);
98 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
99 setTitle("DMDirc: Action Editor");
100
101 this.group = group;
102 this.action = action;
103
104 initComponents();
105 addListeners();
106 doComponents();
107 layoutComponents();
108
109 setResizable(false);
110 }
111
112 /**
113 * Is the dialog open?
114 *
115 * @return is the dialog open
116 */
117 public static boolean isOpen() {
/*
P/P * Method: bool isOpen()
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* init'ed(return_value)
*/
118 synchronized (ActionEditorDialog.class) {
119 return me != null;
120 }
121 }
122
123 /**
124 * Creates the dialog if one doesn't exist, and displays it.
125 *
126 * @param window Parent window
127 * @param group Action's group
128 */
129 public static void showActionEditorDialog(final Window window,
130 final String group) {
/*
P/P * Method: void showActionEditorDialog(Window, String)
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* me == One-of{old me, &new ActionEditorDialog(getActionEditorDialog#1)}
* me != null
* new ActionEditorDialog(getActionEditorDialog#1) num objects <= 1
* new ActionEditorDialog(getActionEditorDialog#1).action == null
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).conditions)
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).conditionsValid)
* new ActionEditorDialog(getActionEditorDialog#1).group == group
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).group)
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).name)
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).nameValid)
* ...
*/
131 showActionEditorDialog(window, group, null);
132 }
133
134 /**
135 * Creates the dialog if one doesn't exist, and displays it.
136 *
137 * @param window Parent window
138 * @param group Action's group
139 * @param action Action to be edited
140 */
141 public static void showActionEditorDialog(final Window window,
142 final String group, final Action action) {
/*
P/P * Method: void showActionEditorDialog(Window, String, Action)
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* me == One-of{old me, &new ActionEditorDialog(getActionEditorDialog#1)}
* me != null
* new ActionEditorDialog(getActionEditorDialog#1) num objects <= 1
* new ActionEditorDialog(getActionEditorDialog#1).action == action
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).action)
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).conditions)
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).conditionsValid)
* new ActionEditorDialog(getActionEditorDialog#1).group == group
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).group)
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).name)
* ...
*/
143 getActionEditorDialog(window, group, action);
144
145 me.pack();
146 me.setLocationRelativeTo(window);
147 me.setVisible(true);
148 }
149
150 /**
151 * Returns the current instance of the ActionEditorDialog.
152 *
153 * @param window Parent window
154 * @param group Action's group
155 *
156 * @return The current ActionEditorDialog instance
157 */
158 public static ActionEditorDialog getActionEditorDialog(final Window window,
159 final String group) {
/*
P/P * Method: ActionEditorDialog getActionEditorDialog(Window, String)
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* me == One-of{old me, &new ActionEditorDialog(getActionEditorDialog#1*)}
* me != null
* return_value == me
* new ActionEditorDialog(getActionEditorDialog#1*) num objects <= 1
* new ActionEditorDialog(getActionEditorDialog#1*).action == null
* init'ed(new ActionEditorDialog(getActionEditorDialog#1*).conditions)
* init'ed(new ActionEditorDialog(getActionEditorDialog#1*).conditionsValid)
* new ActionEditorDialog(getActionEditorDialog#1*).group == group
* init'ed(new ActionEditorDialog(getActionEditorDialog#1*).group)
* init'ed(new ActionEditorDialog(getActionEditorDialog#1*).name)
* ...
*/
160 return getActionEditorDialog(window, group, null);
161 }
162
163 /**
164 * Returns the current instance of the ActionEditorDialog.
165 *
166 * @param window Parent window
167 * @param group Action's group
168 * @param action Action to be edited
169 *
170 * @return The current ActionEditorDialog instance
171 */
172 public static ActionEditorDialog getActionEditorDialog(final Window window,
173 final String group, final Action action) {
/*
P/P * Method: ActionEditorDialog getActionEditorDialog(Window, String, Action)
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* me == One-of{old me, &new ActionEditorDialog(getActionEditorDialog#1)}
* me != null
* return_value == One-of{old me, &new ActionEditorDialog(getActionEditorDialog#1)}
* return_value != null
* new ActionEditorDialog(getActionEditorDialog#1) num objects <= 1
* new ActionEditorDialog(getActionEditorDialog#1).action == action
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).action)
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).conditions)
* init'ed(new ActionEditorDialog(getActionEditorDialog#1).conditionsValid)
* new ActionEditorDialog(getActionEditorDialog#1).group == group
* ...
*/
174 synchronized (ActionEditorDialog.class) {
175 if (me == null) {
176 me = new ActionEditorDialog(window, group, action);
177 }
178 }
179
180 return me;
181 }
182
183 /** Sets components initial states and stuff. */
184 private void doComponents() {
/*
P/P * Method: void doComponents()
*
* Preconditions:
* init'ed(this.action)
* this.conditions != null
* this.conditions.list != null
* this.conditions.list.conditions != null
* this.conditions.tree != null
* this.conditions.tree.allButton != null
* this.conditions.tree.customButton != null
* this.conditions.tree.oneButton != null
* this.response != null
* this.response.formatter != null
* ...
*
* Presumptions:
* com.dmdirc.actions.Action:getConditions(...)@201 != null
* com.dmdirc.actions.Action:getResponse(...).length@198 <= 232-1
* com.dmdirc.actions.Action:getResponse(...)@198 != null
* com.dmdirc.actions.Action:getResponse(...)[...]@198 != null
* com.dmdirc.actions.Action:getTriggers(...).length@197 <= 232-1
* ...
*
* Postconditions:
* possibly_updated(this.conditions.list.treePanel.conditionCount)
* init'ed(this.conditions.list.trigger)
* init'ed(this.conditions.list.validates)
* possibly_updated(this.conditions.tree.conditionCount)
* this.conditions.tree.treeFactory == One-of{old this.conditions.tree.treeFactory, &new ConditionTreeFactory$ConjunctionFactory(sortTreeFactory#1), &new ConditionTreeFactory$DisjunctionFactory(sortTreeFactory#2), &new ConditionTreeFactory$CustomFactory(sortTreeFactory#3)}
* init'ed(this.conditions.tree.validates)
* possibly_updated(this.conditions.trigger)
* this.conditionsValid == 1
* init'ed(this.nameValid)
* init'ed(this.triggersValid)
* ...
*
* Test Vectors:
* this.action: Addr_Set{null}, Inverse{null}
*/
185 triggers.setEnabled(action != null);
186 response.setEnabled(action != null);
187 conditions.setEnabled(action != null);
188 substitutions.setVisible(false);
189
190 triggersValid = action != null;
191 conditionsValid = true;
192 nameValid = action != null;
193 getOkButton().setEnabled(action != null);
194
195 if (action != null) {
196 name.setActionName(action.getName());
197 triggers.setTriggers(action.getTriggers());
198 response.setResponse(action.getResponse());
199 response.setFormatter(action.getNewFormat());
200 conditions.setActionTrigger(action.getTriggers()[0]);
201 conditions.setConditions(action.getConditions());
202 conditions.setConditionTree(action.getRealConditionTree());
203 }
204 }
205
206 /** Initialises the components. */
207 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Presumptions:
* this.list != null
* this.tree != null
*
* Postconditions:
* this.conditions == &new ActionConditionsPanel(initComponents#6)
* this.name == &new ActionNamePanel(initComponents#3)
* this.response == &new ActionResponsePanel(initComponents#5)
* this.showSubstitutions == &new JButton(initComponents#8)
* this.substitutions == &new ActionSubstitutionsPanel(initComponents#7)
* this.triggers == &new ActionTriggersPanel(initComponents#4)
* new ActionConditionsPanel(initComponents#6) num objects == 1
* this.conditions.listValidates == 1
* this.conditions.treeValidates == 1
* new ActionNamePanel(initComponents#3) num objects == 1
* ...
*/
208 orderButtons(new JButton(), new JButton());
209 name = new ActionNamePanel("");
210 triggers = new ActionTriggersPanel();
211 response = new ActionResponsePanel();
212 conditions = new ActionConditionsPanel();
213 substitutions = new ActionSubstitutionsPanel();
214 showSubstitutions = new JButton("Show Substitutions");
215 }
216
217 /** Adds the listeners. */
218 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.conditions != null
* this.name != null
* this.showSubstitutions != null
* this.triggers != null
*
* Presumptions:
* com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionEditorDialog:getCancelButton(...)@221 != null
* com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionEditorDialog:getOkButton(...)@220 != null
*/
219 showSubstitutions.addActionListener(this);
220 getOkButton().addActionListener(this);
221 getCancelButton().addActionListener(this);
222
223 name.addPropertyChangeListener("validationResult", this);
224 triggers.addPropertyChangeListener("validationResult", this);
225 conditions.addPropertyChangeListener("validationResult", this);
226
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionEditorDialog$1(ActionEditorDialog)
*/
227 addWindowListener(new WindowAdapter() {
228
229 /** {@inheritDoc} */
230 @Override
231 public void windowClosing(final WindowEvent e) {
/*
P/P * Method: void windowClosing(WindowEvent)
*
* Presumptions:
* com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionEditorDialog:getCancelButton(...)@232 != null
*/
232 getCancelButton().doClick();
233 }
234 });
235 }
236
237 /** Lays out the components. */
238 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.conditions)
* init'ed(this.name)
* init'ed(this.response)
* init'ed(this.showSubstitutions)
* init'ed(this.substitutions)
* init'ed(this.triggers)
*/
239 setMinimumSize(new Dimension(800, 600));
240 setLayout(new MigLayout("fill, hidemode 3, wrap 2, pack, hmax 80sp, wmin 800, wmax 800"));
241
242 add(name, "grow, w 50%");
243 add(conditions, "spany 3, grow, pushx, w 50%");
244 add(triggers, "grow, w 50%");
245 add(response, "grow, pushy, w 50%");
246 add(substitutions, "spanx, grow, push");
247 add(showSubstitutions, "left, sgx button, split 3, spanx 2");
248 add(getLeftButton(), "right, sgx button, gapleft push");
249 add(getRightButton(), "right, sgx button");
250 }
251
252 /**
253 * @{inheritDoc}
254 *
255 * @param e Action event
256 */
257 @Override
258 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* (soft) init'ed(me)
* (soft) init'ed(this.action)
* (soft) this.conditions != null
* (soft) this.conditions.list != null
* (soft) this.conditions.list.conditions != null
* (soft) this.conditions.tree != null
* (soft) this.conditions.tree.treeFactory != null
* (soft) init'ed(this.group)
* (soft) this.name != null
* ...
*
* Presumptions:
* java.awt.event.ActionEvent:getSource(...)@259 != null
* java.awt.event.ActionEvent:getSource(...)@263 != null
* java.awt.event.ActionEvent:getSource(...)@266 != null
*
* Postconditions:
* me == One-of{old me, null}
* init'ed(me)
*
* Test Vectors:
* java.lang.Object:equals(...)@259: {0}, {1}
* java.lang.Object:equals(...)@263: {0}, {1}
* java.lang.Object:equals(...)@266: {0}, {1}
*/
259 if (e.getSource().equals(showSubstitutions)) {
260 substitutions.setVisible(!substitutions.isVisible());
261 showSubstitutions.setText(substitutions.isVisible() ? "Hide Substitutions"
262 : "Show Substitutions");
263 } else if (e.getSource().equals(getOkButton())) {
264 save();
265 dispose();
266 } else if (e.getSource().equals(getCancelButton())) {
267 dispose();
268 }
269 }
270
271 /** Saves the action being edited. */
272 private void save() {
/*
P/P * Method: void save()
*
* Preconditions:
* init'ed(this.action)
* this.conditions != null
* this.conditions.list != null
* this.conditions.list.conditions != null
* this.conditions.tree != null
* this.conditions.tree.treeFactory != null
* this.name != null
* this.name.name != null
* this.name.name.textField != null
* this.response != null
* ...
*
* Test Vectors:
* this.action: Inverse{null}, Addr_Set{null}
*/
273 name.getActionName();
274 triggers.getTriggers();
275 response.getResponse();
276 response.getFormatter();
277 conditions.getConditions();
278 conditions.getConditionTree();
279 if (action == null) {
280 new Action(group, name.getActionName(), triggers.getTriggers(),
281 response.getResponse(), conditions.getConditions(),
282 conditions.getConditionTree(), response.getFormatter());
283 } else {
284 action.setName(name.getActionName());
285 action.setConditionTree(conditions.getConditionTree());
286 action.setConditions(conditions.getConditions());
287 action.setNewFormat(response.getFormatter());
288 action.setResponse(response.getResponse());
289 action.setTriggers(triggers.getTriggers());
290 action.save();
291 }
292 }
293
294 /** @{inheritDoc} */
295 @Override
296 public void propertyChange(final PropertyChangeEvent evt) {
/*
P/P * Method: void propertyChange(PropertyChangeEvent)
*
* Preconditions:
* evt != null
* init'ed(this.name)
* (soft) init'ed(this.conditions.list.trigger)
* (soft) init'ed(this.conditionsValid)
* (soft) init'ed(this.nameValid)
* (soft) init'ed(this.triggersValid)
* (soft) this.conditions != null
* (soft) this.conditions.add != null
* (soft) this.conditions.list != null
* (soft) this.conditions.list.conditions != null
* ...
*
* Presumptions:
* com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionEditorDialog:getOkButton(...)@315 != null
* java.beans.PropertyChangeEvent:getNewValue(...)@298 != null
* java.beans.PropertyChangeEvent:getNewValue(...)@299 != null
* java.beans.PropertyChangeEvent:getNewValue(...)@300 != null
* java.beans.PropertyChangeEvent:getNewValue(...)@301 != null
* ...
*
* Postconditions:
* init'ed(this.conditions.list.trigger)
* possibly_updated(this.conditions.trigger)
* init'ed(this.conditionsValid)
* init'ed(this.nameValid)
* init'ed(this.triggersValid)
*
* Test Vectors:
* java.lang.Object:equals(...)@297: {0}, {1}
* java.lang.Object:equals(...)@302: {0}, {1}
* java.lang.Object:equals(...)@311: {0}, {1}
*/
297 if (evt.getSource().equals(name)) {
298 nameValid = (Boolean) evt.getNewValue();
299 triggers.setEnabled((Boolean) evt.getNewValue());
300 conditions.setEnabled((Boolean) evt.getNewValue());
301 response.setEnabled((Boolean) evt.getNewValue());
302 } else if (evt.getSource().equals(triggers)) {
303 triggersValid = (Boolean) evt.getNewValue();
304
305 response.setEnabled((Boolean) evt.getNewValue());
306 conditions.setEnabled((Boolean) evt.getNewValue());
307 substitutions.setEnabled((Boolean) evt.getNewValue());
308
309 substitutions.setType(triggers.getPrimaryTrigger());
310 conditions.setActionTrigger(triggers.getPrimaryTrigger());
311 } else if (evt.getSource().equals(conditions)) {
312 conditionsValid = (Boolean) evt.getNewValue();
313 }
314
315 getOkButton().setEnabled(triggersValid && conditionsValid && nameValid);
316 }
317
318 /** {@inheritDoc} */
319 @Override
320 public void dispose() {
/*
P/P * Method: void dispose()
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* me == null
*
* Test Vectors:
* me: Inverse{null}, Addr_Set{null}
*/
321 if (me == null) {
322 return;
323 }
324 synchronized (me) {
325 super.dispose();
326 me = null;
327 }
328 }
329 }
SofCheck Inspector Build Version : 2.17854
| ActionEditorDialog.java |
2009-Jun-25 01:54:24 |
| ActionEditorDialog.class |
2009-Sep-02 17:04:15 |
| ActionEditorDialog$1.class |
2009-Sep-02 17:04:15 |