File Source: ActionConditionDisplayPanel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionDisplayPanel__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.ui.IconManager;
28 import com.dmdirc.addons.ui_swing.components.ImageButton;
29 import com.dmdirc.addons.ui_swing.components.ImageToggleButton;
30 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
31 import com.dmdirc.util.ListenerList;
32
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35
36 import java.beans.PropertyChangeEvent;
37 import java.beans.PropertyChangeListener;
38 import javax.swing.JPanel;
39 import javax.swing.JToggleButton;
40
41 import net.miginfocom.swing.MigLayout;
42
43 /**
44 * Action condition display panel.
45 */
46 public class ActionConditionDisplayPanel extends JPanel implements ActionListener,
47 PropertyChangeListener {
48
49 /**
50 * A version number for this class. It should be changed whenever the class
51 * structure is changed (or anything else that would prevent serialized
52 * objects being unserialized with the new class).
53 */
54 private static final long serialVersionUID = 1;
55 /** Info label. */
56 private TextLabel label;
57 /** Edit button. */
58 private JToggleButton editButton;
59 /** Delete button. */
60 private ImageButton deleteButton;
61 /** Edit panel. */
62 private ActionConditionEditorPanel editPanel;
63 /** Listeners. */
64 private ListenerList listeners;
65 /** Action condition. */
66 private ActionCondition condition;
67 /** Action trigger. */
68 private ActionType trigger;
69
70 /**
71 * Instantiates the panel.
72 *
73 * @param condition Action condition
74 * @param trigger Action trigger
75 */
76 public ActionConditionDisplayPanel(final ActionCondition condition,
77 final ActionType trigger) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionConditionDisplayPanel(ActionCondition, ActionType)
*
* Preconditions:
* condition != null
*
* Presumptions:
* com.dmdirc.actions.ActionCondition:getTarget(...)@91 != null
*
* Postconditions:
* this.condition == &new ActionCondition(ActionConditionDisplayPanel#1)
* new ActionConditionEditorPanel(initComponents#4).condition == &new ActionCondition(ActionConditionDisplayPanel#1)
* this.deleteButton == &new ImageButton(initComponents#3)
* this.editButton == &new ImageToggleButton(initComponents#2)
* this.editPanel == &new ActionConditionEditorPanel(initComponents#4)
* this.label == &new TextLabel(initComponents#1)
* this.listeners == &new ListenerList(initComponents#5)
* this.trigger == trigger
* init'ed(this.trigger)
* new ActionConditionEditorPanel(initComponents#4).trigger == this.trigger
* ...
*
* Test Vectors:
* com.dmdirc.actions.ActionCondition:getArg(...)@91: {-231..-2, 0..232-1}, {-1}
* com.dmdirc.actions.ActionCondition:getComparison(...)@91: Inverse{null}, Addr_Set{null}
* com.dmdirc.actions.ActionCondition:getComponent(...)@91: Inverse{null}, Addr_Set{null}
* java.lang.String:isEmpty(...)@91: {0}, {1}
*/
78 super();
79
80 this.trigger = trigger;
81 this.condition = new ActionCondition(condition.getArg(),
82 condition.getComponent(), condition.getComparison(),
83 condition.getTarget());
84
85 initComponents();
86 addListeners();
87 layoutComponents();
88 validate();
89 layoutComponents();
90
91 if (condition.getArg() == -1 && condition.getComponent() == null &&
92 condition.getComparison() == null && condition.getTarget().isEmpty()) {
93 editPanel.setVisible(true);
94 editButton.setSelected(true);
95 }
96 }
97
98 /**
99 * Sets the action trigger.
100 *
101 * @param trigger new trigger
102 */
103 void setTrigger(final ActionType trigger) {
/*
P/P * Method: void setTrigger(ActionType)
*
* Preconditions:
* this.editButton != null
* this.editPanel != null
* this.editPanel.arguments != null
* this.editPanel.comparisons != null
* this.editPanel.components != null
* this.editPanel.target != null
* this.label != null
* (soft) this.condition != null
* (soft) this.editPanel.condition != null
* (soft) init'ed(this.label.sas)
*
* Postconditions:
* this.editPanel.trigger == trigger
* init'ed(this.editPanel.trigger)
* this.trigger == this.editPanel.trigger
*/
104 this.trigger = trigger;
105 editPanel.setTrigger(trigger);
106
107 editPanel.setVisible(trigger == null);
108 editButton.setSelected(trigger == null);
109
110 label.setText(updateSentence());
111 }
112
113 /** Initialises the components. */
114 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Preconditions:
* init'ed(this.trigger)
* (soft) this.condition != null
*
* Presumptions:
* com.dmdirc.ui.IconManager:getIconManager(...)@116 != null
* com.dmdirc.ui.IconManager:getIconManager(...)@119 != null
*
* Postconditions:
* this.deleteButton == &new ImageButton(initComponents#3)
* this.editButton == &new ImageToggleButton(initComponents#2)
* this.editPanel == &new ActionConditionEditorPanel(initComponents#4)
* this.label == &new TextLabel(initComponents#1)
* this.listeners == &new ListenerList(initComponents#5)
* new ActionConditionEditorPanel$1(initComponents#7) num objects == 1
* new ActionConditionEditorPanel(initComponents#4) num objects == 1
* new ImageButton(initComponents#3) num objects == 1
* new ImageToggleButton(initComponents#2) num objects == 1
* new JComboBox(initComponents#1) num objects == 1
* ...
*/
115 label = new TextLabel("", false);
116 editButton = new ImageToggleButton("edit", IconManager.getIconManager().
117 getIcon("edit-inactive"),
118 IconManager.getIconManager().getIcon("edit"));
119 deleteButton = new ImageButton("delete", IconManager.getIconManager().
120 getIcon("close-inactive"), IconManager.getIconManager().
121 getIcon("close-inactive"),
122 IconManager.getIconManager().getIcon("close-active"));
123
124 editPanel = new ActionConditionEditorPanel(condition, trigger);
125 listeners = new ListenerList();
126
127 editPanel.setVisible(trigger == null);
128 editButton.setSelected(trigger == null);
129 }
130
131 /** Adds the listeners. */
132 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.deleteButton != null
* this.editButton != null
* this.editPanel != null
*/
133 editButton.addActionListener(this);
134 deleteButton.addActionListener(this);
135 editPanel.addPropertyChangeListener("edit", this);
136 editPanel.addPropertyChangeListener("validationResult", this);
137 }
138
139 /** Lays out the components. */
140 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.deleteButton)
* init'ed(this.editButton)
* init'ed(this.editPanel)
* init'ed(this.label)
*/
141 setLayout(new MigLayout("ins 0, fillx, hidemode 3, pack, wmax 90%"));
142 add(label, "grow, push, wmax 85%");
143 add(editButton, "right");
144 add(deleteButton, "right, wrap");
145 add(editPanel, "alignx right");
146 }
147
148 /**
149 * {@inheritDoc}
150 *
151 * @param e Action event
152 */
153 @Override
154 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* init'ed(this.deleteButton)
* (soft) this.editButton != null
* (soft) this.editPanel != null
* (soft) this.listeners != null
*
* Presumptions:
* java.awt.event.ActionEvent:getSource(...)@155 != null
* java.awt.event.ActionEvent:getSource(...)@157 != null
* javax.swing.JToggleButton:getModel(...)@158 != null
*
* Test Vectors:
* java.lang.Object:equals(...)@155: {0}, {1}
* java.lang.Object:equals(...)@157: {0}, {1}
*/
155 if (e.getSource().equals(deleteButton)) {
156 fireConditionRemoved(this);
157 } else if (e.getSource().equals(editButton)) {
158 editPanel.setVisible(editButton.getModel().isSelected());
159 }
160 }
161
162 /**
163 * Adds an ActionConditionRemovalListener to the listener list.
164 *
165 * @param listener Listener to add
166 */
167 public void addConditionListener(final ActionConditionRemovalListener listener) {
/*
P/P * Method: void addConditionListener(ActionConditionRemovalListener)
*
* Preconditions:
* (soft) this.listeners != null
*
* Test Vectors:
* listener: Inverse{null}, Addr_Set{null}
*/
168 if (listener == null) {
169 return;
170 }
171
172 listeners.add(ActionConditionRemovalListener.class, listener);
173 }
174
175 /**
176 * Removes an ActionConditionRemovalListener from the listener list.
177 *
178 * @param listener Listener to remove
179 */
180 public void removeConditionListener(final ActionConditionRemovalListener listener) {
/*
P/P * Method: void removeConditionListener(ActionConditionRemovalListener)
*
* Preconditions:
* this.listeners != null
*/
181 listeners.remove(ActionConditionRemovalListener.class, listener);
182 }
183
184 /**
185 * Fired when the an action condition is removed.
186 *
187 * @param condition Removed condition
188 */
189 protected void fireConditionRemoved(final ActionConditionDisplayPanel condition) {
/*
P/P * Method: void fireConditionRemoved(ActionConditionDisplayPanel)
*
* Preconditions:
* this.listeners != null
*
* Presumptions:
* com.dmdirc.util.ListenerList:get(...)@190 != null
* java.util.Iterator:next(...)@190 != null
* listener.conditions@190 != null
* listener.validations@190 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@190: {0}, {1}
*/
190 for (ActionConditionRemovalListener listener : listeners.get(ActionConditionRemovalListener.class)) {
191 listener.conditionRemoved(condition);
192 }
193 }
194
195 /** {@inheritDoc} */
196 @Override
197 public void setEnabled(final boolean enabled) {
/*
P/P * Method: void setEnabled(bool)
*
* Preconditions:
* this.deleteButton != null
* this.editButton != null
* this.editPanel != null
* this.editPanel.arguments != null
* this.editPanel.comparisons != null
* this.editPanel.components != null
* this.editPanel.target != null
*/
198 editPanel.setEnabled(enabled);
199 editButton.setEnabled(enabled);
200 deleteButton.setEnabled(enabled);
201 }
202
203 /**
204 * Updates the condition sentence.
205 *
206 * @return Updated sentence
207 */
208 private String updateSentence() {
/*
P/P * Method: String updateSentence()
*
* Preconditions:
* init'ed(this.trigger)
* (soft) this.condition != null
*
* Presumptions:
* com.dmdirc.actions.ActionCondition:getArg(...)@215 >= 0
* com.dmdirc.actions.ActionCondition:getArg(...)@215 < com.dmdirc.actions.interfaces.ActionMetaType:getArgNames(...).length@215
* com.dmdirc.actions.ActionCondition:getComparison(...)@229 != null
* com.dmdirc.actions.ActionCondition:getComponent(...)@222 != null
* com.dmdirc.actions.interfaces.ActionMetaType:getArgNames(...).length@215 >= 1
* ...
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value in Addr_Set{&java.lang.StringBuilder:toString(...),&java.lang.StringBuilder:toString(...),&java.lang.StringBuilder:toString(...),&java.lang.StringBuilder:toString(...),&java.lang.StringBuilder:toString(...),&"..."}
*
* Test Vectors:
* this.trigger: Inverse{null}, Addr_Set{null}
* com.dmdirc.actions.ActionCondition:getArg(...)@214: {-1}, {-231..-2, 0..232-1}
* com.dmdirc.actions.ActionCondition:getComparison(...)@228: Addr_Set{null}, Inverse{null}
* com.dmdirc.actions.ActionCondition:getComponent(...)@221: Addr_Set{null}, Inverse{null}
* com.dmdirc.actions.ActionCondition:getTarget(...)@235: Addr_Set{null}, Inverse{null}
*/
209 if (trigger == null) {
210 return "...";
211 } else {
212 final StringBuilder sb = new StringBuilder();
213 sb.append("The ");
214 if (condition.getArg() != -1) {
215 sb.append(trigger.getType().getArgNames()[condition.getArg()]);
216 } else {
217 sb.append(" ...");
218 return sb.toString();
219 }
220 sb.append("'s ");
221 if (condition.getComponent() != null) {
222 sb.append(condition.getComponent().getName());
223 } else {
224 sb.append(" ...");
225 return sb.toString();
226 }
227 sb.append(" ");
228 if (condition.getComparison() != null) {
229 sb.append(condition.getComparison().getName());
230 } else {
231 sb.append(" ...");
232 return sb.toString();
233 }
234 sb.append(" '");
235 if (condition.getTarget() != null) {
236 sb.append(condition.getTarget());
237 } else {
238 sb.append(" ...");
239 return sb.toString();
240 }
241 sb.append("'");
242 return sb.toString();
243 }
244 }
245
246 /**
247 * Returns the action condition represented by this panel.
248 *
249 * @return Action condition
250 */
251 public ActionCondition getCondition() {
/*
P/P * Method: ActionCondition getCondition()
*
* Preconditions:
* init'ed(this.condition)
*
* Postconditions:
* return_value == this.condition
* init'ed(return_value)
*/
252 return condition;
253 }
254
255 /** {@inheritDoc} */
256 @Override
257 public void propertyChange(final PropertyChangeEvent evt) {
/*
P/P * Method: void propertyChange(PropertyChangeEvent)
*
* Preconditions:
* evt != null
* (soft) this.condition != null
* (soft) this.label != null
* (soft) init'ed(this.label.sas)
* (soft) init'ed(this.trigger)
*
* Test Vectors:
* java.lang.String:equals(...)@258: {0}, {1}
*/
258 if ("edit".equals(evt.getPropertyName())) {
259 label.setText(updateSentence());
260 } else {
261 firePropertyChange("validationResult", evt.getOldValue(),
262 evt.getNewValue());
263 }
264 }
265
266 /**
267 * Checks if this editor panel has errored.
268 *
269 * @return true iif the content it valid
270 */
271 public boolean checkError() {
/*
P/P * Method: bool checkError()
*
* Preconditions:
* this.editPanel != null
* this.editPanel.target != null
*
* Postconditions:
* init'ed(return_value)
*/
272 return editPanel.checkError();
273 }
274 }
SofCheck Inspector Build Version : 2.17854
| ActionConditionDisplayPanel.java |
2009-Jun-25 01:54:24 |
| ActionConditionDisplayPanel.class |
2009-Sep-02 17:04:15 |