File Source: ActionTriggersPanel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTriggersPanel__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.ActionManager;
26 import com.dmdirc.actions.interfaces.ActionType;
27 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
28 import com.dmdirc.addons.ui_swing.components.renderers.ActionTypeRenderer;
29
30 import java.awt.Dimension;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.beans.PropertyChangeEvent;
34 import java.beans.PropertyChangeListener;
35 import java.util.List;
36
37 import javax.swing.BorderFactory;
38 import javax.swing.JButton;
39 import javax.swing.JComboBox;
40 import javax.swing.JComponent;
41 import javax.swing.JPanel;
42 import javax.swing.JPopupMenu;
43 import javax.swing.JScrollPane;
44 import javax.swing.SwingUtilities;
45 import javax.swing.event.PopupMenuEvent;
46 import javax.swing.event.PopupMenuListener;
47
48 import net.miginfocom.swing.MigLayout;
49
50 /**
51 * Action triggers panel.
52 */
/*
P/P * Method: ActionTriggersListPanel access$100(ActionTriggersPanel)
*
* Preconditions:
* x0 != null
* init'ed(x0.triggerList)
*
* Postconditions:
* return_value == x0.triggerList
* init'ed(return_value)
*/
53 public class ActionTriggersPanel extends JPanel implements ActionListener,
54 ActionTriggerRemovalListener, PropertyChangeListener {
55
56 /**
57 * A version number for this class. It should be changed whenever the class
58 * structure is changed (or anything else that would prevent serialized
59 * objects being unserialized with the new class).
60 */
61 private static final long serialVersionUID = 1;
62 /** Trigger combo box. */
63 private JComboBox trigger;
64 /** Add button. */
65 private JButton add;
66 /** Triggers list. */
67 private ActionTriggersListPanel triggerList;
68
69 /** Instantiates the panel. */
70 public ActionTriggersPanel() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTriggersPanel()
*
* Postconditions:
* this.add == &new JButton(initComponents#5)
* this.trigger == &new JComboBox(initComponents#1)
* this.triggerList == &new ActionTriggersListPanel(initComponents#6)
* new ActionTriggersListPanel(initComponents#6) num objects == 1
* new ArrayList(ActionTriggersListPanel#2) num objects == 1
* new JButton(initComponents#5) num objects == 1
* new JComboBox(initComponents#1) num objects == 1
* new ListenerList(ActionTriggersListPanel#1) num objects == 1
* new ActionTriggersListPanel(initComponents#6).listeners == &new ListenerList(ActionTriggersListPanel#1)
* new ActionTriggersListPanel(initComponents#6).triggers == &new ArrayList(ActionTriggersListPanel#2)
*/
71 super();
72
73 initComponents();
74 addListeners();
75 layoutComponents();
76 }
77
78 /** Initialises the components. */
79 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Presumptions:
* com.dmdirc.actions.ActionManager:getTypeGroups(...)@82 != null
* init'ed(java.lang.Boolean.TRUE)
*
* Postconditions:
* this.add == &new JButton(initComponents#5)
* this.trigger == &new JComboBox(initComponents#1)
* this.triggerList == &new ActionTriggersListPanel(initComponents#6)
* new ActionTriggersListPanel(initComponents#6) num objects == 1
* new ArrayList(ActionTriggersListPanel#2) num objects == 1
* new JButton(initComponents#5) num objects == 1
* new JComboBox(initComponents#1) num objects == 1
* new ListenerList(ActionTriggersListPanel#1) num objects == 1
* this.triggerList.listeners == &new ListenerList(ActionTriggersListPanel#1)
* this.triggerList.triggers == &new ArrayList(ActionTriggersListPanel#2)
*/
80 setBorder(BorderFactory.createTitledBorder(getBorder(), "Triggers"));
81
82 trigger =
83 new JComboBox(new ActionTypeModel(getFontMetrics(getFont()),
84 ActionManager.getTypeGroups()));
85 //Only fire events on selection not on highlight
86 trigger.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
87 trigger.setRenderer(new ActionTypeRenderer());
88 trigger.setPrototypeDisplayValue("Testing");
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTriggersPanel$1(ActionTriggersPanel)
*/
89 trigger.addPopupMenuListener(new PopupMenuListener() {
90
91 @Override
92 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
/*
P/P * Method: void popupMenuWillBecomeVisible(PopupMenuEvent)
*
* Preconditions:
* e != null
* (soft) this.trigger != null
*
* Presumptions:
* (int) (java.awt.Dimension:getWidth(...)@101) in {-6_442_450_943..6_442_450_943}
* (int) (java.awt.Dimension:getWidth(...)@101) + javax.swing.JComboBox:getModel(...).maxWidth@101 in range
* (int) (java.awt.Dimension:getWidth(...)@101) + javax.swing.JComboBox:getModel(...).maxWidth@101 in {-231..232-1}
* javax.swing.JComboBox:getModel(...)@101 != null
* javax.swing.JComboBox:getModel(...)@105 != null
* ...
*
* Test Vectors:
* javax.swing.JScrollPane:instanceof(...)@100: {0}, {1}
*/
93 JComboBox box = (JComboBox) e.getSource();
94 Object comp = box.getUI().getAccessibleChild(box, 0);
95 if (!(comp instanceof JPopupMenu)) {
96 return;
97 }
98 JComponent scrollPane = (JComponent) ((JPopupMenu) comp).getComponent(0);
99 Dimension size = scrollPane.getPreferredSize();
100 if (scrollPane instanceof JScrollPane) {
101 size.width = ((ActionTypeModel) trigger.getModel()).
102 getMaxWidth() + (int) ((JScrollPane) scrollPane).
103 getVerticalScrollBar().getPreferredSize().getWidth();
104 } else {
105 size.width = ((ActionTypeModel) trigger.getModel()).getMaxWidth();
106 }
107 scrollPane.setPreferredSize(size);
108 scrollPane.setMaximumSize(size);
109 }
110
111 @Override
112 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
/*
P/P * Method: void popupMenuWillBecomeInvisible(PopupMenuEvent)
*/
113 }
114
115 @Override
116 public void popupMenuCanceled(PopupMenuEvent e) {
/*
P/P * Method: void popupMenuCanceled(PopupMenuEvent)
*/
117 }
118 });
119
120
121 add = new JButton("Add");
122 add.setEnabled(trigger.getSelectedIndex() != -1);
123
124 triggerList = new ActionTriggersListPanel();
125 }
126
127 /** Adds the listeners. */
128 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.add != null
* this.trigger != null
* this.triggerList != null
* (soft) this.triggerList.listeners != null
*/
129 add.addActionListener(this);
130 trigger.addActionListener(this);
131 triggerList.addTriggerListener(this);
132
133 triggerList.addPropertyChangeListener("triggerCount", this);
134 }
135
136 /** Lays out the components. */
137 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.add)
* init'ed(this.trigger)
* init'ed(this.triggerList)
*/
138 setLayout(new MigLayout("fill, pack"));
139
140 add(new TextLabel("This action will be triggered when any of these events occurs: "),
141 "growx, pushx, wrap, spanx");
142 add(triggerList, "grow, push, wrap, spanx");
143 add(trigger, "growx, pushx");
144 add(add, "right");
145 }
146
147 /**
148 * Returns the primary trigger for this panel.
149 *
150 * @return Primary trigger or null
151 */
152 public ActionType getPrimaryTrigger() {
/*
P/P * Method: ActionType getPrimaryTrigger()
*
* Preconditions:
* this.triggerList != null
* this.triggerList.triggers != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.util.List:size(...)@203: {-231..-1, 1..232-1}, {0}
*/
153 if (triggerList.getTriggerCount() == 0) {
154 return null;
155 }
156 return triggerList.getTrigger(0);
157 }
158
159 /**
160 * Returns the list of triggers.
161 *
162 * @return Trigger list
163 */
164 public ActionType[] getTriggers() {
/*
P/P * Method: ActionType[] getTriggers()
*
* Preconditions:
* this.triggerList != null
* this.triggerList.triggers != null
*
* Presumptions:
* java.util.List:size(...)@166 >= 0
*
* Postconditions:
* init'ed(return_value)
*/
165 final List<ActionType> triggers = triggerList.getTriggers();
166 return triggers.toArray(new ActionType[triggers.size()]);
167 }
168
169 /**
170 * Sets the triggers.
171 *
172 * @param triggers Sets the triggers.
173 */
174 void setTriggers(final ActionType[] triggers) {
/*
P/P * Method: void setTriggers(ActionType[])
*
* Preconditions:
* this.triggerList != null
* this.triggerList.triggers != null
* triggers != null
* triggers.length <= 232-1
* (soft) init'ed(triggers[...])
*/
175 triggerList.clearTriggers();
176
177 for (ActionType localTrigger : triggers) {
178 triggerList.addTrigger(localTrigger);
179 }
180
181 repopulateTriggers();
182 }
183
184 /**
185 * {@inheritDoc}
186 *
187 * @param e Action event
188 */
189 @Override
190 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* this.trigger != null
* (soft) this.add != null
* (soft) this.triggerList != null
*/
191 if (e.getSource() == trigger) {
192 add.setEnabled(trigger.getSelectedIndex() != -1);
193 } else {
194 triggerList.addTrigger((ActionType) trigger.getSelectedItem());
195 repopulateTriggers();
196 }
197 }
198
199 /** {@inheritDoc} */
200 @Override
201 public void triggerRemoved(final ActionType trigger) {
/*
P/P * Method: void triggerRemoved(ActionType)
*/
202 repopulateTriggers();
203 }
204
205 /**
206 * Repopulates the triggers in the panel.
207 */
208 private void repopulateTriggers() {
/*
P/P * Method: void repopulateTriggers()
*/
209 SwingUtilities.invokeLater(new Runnable() {
210
211 /** {@inheritDoc} */
212 @Override
213 public void run() {
/*
P/P * Method: void run()
*
* Preconditions:
* this.trigger != null
* this.triggerList != null
* this.triggerList.triggers != null
*
* Presumptions:
* com.dmdirc.actions.ActionManager:getCompatibleTypes(...)@221 != null
* com.dmdirc.actions.ActionManager:getTypeGroups(...)@217 != null
* javax.swing.JComboBox:getModel(...)@214 != null
* javax.swing.JComboBox:getModel(...)@217 != null
* javax.swing.JComboBox:getModel(...)@218 != null
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@221: {0}, {1}
* java.util.List:contains(...)@223: {1}, {0}
* java.util.List:size(...)@203: {-231..-1, 1..232-1}, {0}
*/
214 ((ActionTypeModel) trigger.getModel()).removeAllElements();
215
216 if (triggerList.getTriggerCount() == 0) {
217 ((ActionTypeModel) trigger.getModel()).setTypeGroup(ActionManager.getTypeGroups());
218 trigger.setEnabled((trigger.getModel().getSize() > 0));
219 return;
220 }
221 for (ActionType thisType : ActionManager.getCompatibleTypes(triggerList.getTrigger(0))) {
222 final List<ActionType> types = triggerList.getTriggers();
223 if (!types.contains(thisType)) {
224 ((ActionTypeModel) trigger.getModel()).addElement(thisType);
225 }
226 }
227 trigger.setEnabled(trigger.getModel().getSize() > 0);
228 }
229 });
230 }
231
232 /** {@inheritDoc} */
233 @Override
234 public void setEnabled(final boolean enabled) {
/*
P/P * Method: void setEnabled(bool)
*
* Preconditions:
* this.add != null
* this.trigger != null
* this.triggerList != null
*
* Presumptions:
* javax.swing.JComboBox:getModel(...)@238 != null
*
* Test Vectors:
* enabled: {0}, {1}
* javax.swing.ComboBoxModel:getSize(...)@238: {-231..0}, {1..232-1}
*/
235 triggerList.setEnabled(enabled);
236 if (enabled) {
237 add.setEnabled(trigger.getSelectedIndex() != -1);
238 if (trigger.getModel().getSize() > 0) {
239 trigger.setEnabled(enabled);
240 }
241 } else {
242 add.setEnabled(false);
243 trigger.setEnabled(false);
244 }
245 }
246
247 /** {@inheritDoc} */
248 @Override
249 public void propertyChange(final PropertyChangeEvent evt) {
/*
P/P * Method: void propertyChange(PropertyChangeEvent)
*
* Preconditions:
* evt != null
*
* Presumptions:
* java.beans.PropertyChangeEvent:getNewValue(...)@250 != null
* java.beans.PropertyChangeEvent:getOldValue(...)@250 != null
*/
250 firePropertyChange("validationResult", (Integer) evt.getOldValue() > 0,
251 (Integer) evt.getNewValue() > 0);
252 }
253
254 /** Validates the triggers. */
255 public void validateTriggers() {
/*
P/P * Method: void validateTriggers()
*
* Preconditions:
* this.triggerList != null
* this.triggerList.triggers != null
*/
256 triggerList.validateTriggers();
257 }
258 }
SofCheck Inspector Build Version : 2.17854
| ActionTriggersPanel.java |
2009-Jun-25 01:54:24 |
| ActionTriggersPanel.class |
2009-Sep-02 17:04:15 |
| ActionTriggersPanel$1.class |
2009-Sep-02 17:04:15 |
| ActionTriggersPanel$2.class |
2009-Sep-02 17:04:15 |