File Source: WindowMenuFrameManager.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.framemanager.windowmenu.WindowMenuFrameManager__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 package com.dmdirc.addons.ui_swing.framemanager.windowmenu;
23
24 import com.dmdirc.FrameContainer;
25 import com.dmdirc.FrameContainerComparator;
26 import com.dmdirc.config.IdentityManager;
27 import com.dmdirc.interfaces.SelectionListener;
28 import com.dmdirc.ui.IconManager;
29 import com.dmdirc.ui.WindowManager;
30 import com.dmdirc.ui.interfaces.FrameManager;
31 import com.dmdirc.ui.interfaces.Window;
32 import com.dmdirc.addons.ui_swing.UIUtilities;
33
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.util.Map;
37 import java.util.TreeMap;
38
39 import java.util.concurrent.atomic.AtomicBoolean;
40 import javax.swing.JComponent;
41 import javax.swing.JMenu;
42 import javax.swing.JMenuItem;
43 import javax.swing.JPopupMenu;
44 import javax.swing.JSeparator;
45 import javax.swing.event.MenuEvent;
46 import javax.swing.event.MenuListener;
47
48 /**
49 * Manages the window menu window list.
50 */
/*
P/P * Method: void access$200(WindowMenuFrameManager)
*
* Preconditions:
* x0 != null
* x0.closeMenuItem != null
* x0.enabledMenuItems != null
* x0.minimiseMenuItem != null
* x0.separator != null
* x0.toggleStateMenuItem != null
*/
51 public final class WindowMenuFrameManager extends JMenu implements FrameManager,
52 ActionListener, SelectionListener, MenuListener {
53
54 /**
55 * A version number for this class. It should be changed whenever the class
56 * structure is changed (or anything else that would prevent serialized
57 * objects being unserialized with the new class).
58 */
59 private static final long serialVersionUID = 1;
60 /** Menu item list. */
61 private final Map<FrameContainer, FrameContainerMenuItem> menuItemMap;
62 /** Comparator. */
63 private final FrameContainerComparator comparator =
64 new FrameContainerComparator();
65 /** Non frame container menu count. */
66 private final int itemCount;
67 /** Menu items for toggling, closing and minimising. */
68 private final JMenuItem toggleStateMenuItem, closeMenuItem, minimiseMenuItem;
69 /** Seperator. */
70 private final JSeparator separator;
71 /** Active window. */
72 private Window activeWindow;
73 /** Enabled menu items? */
74 private final AtomicBoolean enabledMenuItems = new AtomicBoolean(false);
75
76 /**
77 * Creates a new instance of WindowMenuFrameManager.
78 */
79 public WindowMenuFrameManager() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.framemanager.windowmenu.WindowMenuFrameManager()
*
* Presumptions:
* com.dmdirc.ui.IconManager:getIconManager(...)@106 != null
* com.dmdirc.ui.IconManager:getIconManager(...)@90 != null
* com.dmdirc.ui.IconManager:getIconManager(...)@98 != null
*
* Postconditions:
* init'ed(this.activeWindow)
* this.closeMenuItem == &new JMenuItem(WindowMenuFrameManager#6)
* this.comparator == &new FrameContainerComparator(WindowMenuFrameManager#1)
* this.enabledMenuItems == &new AtomicBoolean(WindowMenuFrameManager#2)
* init'ed(this.itemCount)
* this.menuItemMap == &new TreeMap(WindowMenuFrameManager#3)
* this.minimiseMenuItem == &new JMenuItem(WindowMenuFrameManager#4)
* this.separator == &new JPopupMenu$Separator(WindowMenuFrameManager#7)
* this.toggleStateMenuItem == &new JMenuItem(WindowMenuFrameManager#5)
* new AtomicBoolean(WindowMenuFrameManager#2) num objects == 1
* ...
*/
80 super();
81
82 menuItemMap =
83 new TreeMap<FrameContainer, FrameContainerMenuItem>(comparator);
84
85 setText("Window");
86 setMnemonic('w');
87 WindowManager.addFrameManager(this);
88 addMenuListener(this);
89
90 minimiseMenuItem = new JMenuItem(IconManager.getIconManager().getIcon(
91 "minimise"));
92 minimiseMenuItem.setMnemonic('n');
93 minimiseMenuItem.setText("Minimise");
94 minimiseMenuItem.setActionCommand("Minimise");
95 minimiseMenuItem.addActionListener(this);
96 add(minimiseMenuItem);
97
98 toggleStateMenuItem = new JMenuItem(IconManager.getIconManager().getIcon(
99 "maximise"));
100 toggleStateMenuItem.setMnemonic('m');
101 toggleStateMenuItem.setText("Maximise");
102 toggleStateMenuItem.setActionCommand("ToggleState");
103 toggleStateMenuItem.addActionListener(this);
104 add(toggleStateMenuItem);
105
106 closeMenuItem = new JMenuItem(IconManager.getIconManager().getIcon(
107 "close"));
108 closeMenuItem.setMnemonic('c');
109 closeMenuItem.setText("Close");
110 closeMenuItem.setActionCommand("Close");
111 closeMenuItem.addActionListener(this);
112 add(closeMenuItem);
113
114 separator = new JPopupMenu.Separator();
115 add(separator);
116
117 itemCount = getMenuComponentCount();
118 checkToggleState();
119 }
120
121 /**
122 * Checks the number of components in the menu and enables menus items
123 * appropriately.
124 */
125 private void checkMenuItems() {
/*
P/P * Method: void checkMenuItems()
*
* Preconditions:
* this.closeMenuItem != null
* this.enabledMenuItems != null
* this.minimiseMenuItem != null
* this.separator != null
* this.toggleStateMenuItem != null
*/
126 enabledMenuItems.set((getMenuComponentCount() > itemCount));
127 separator.setVisible(enabledMenuItems.get());
128 closeMenuItem.setEnabled(enabledMenuItems.get());
129 toggleStateMenuItem.setEnabled(enabledMenuItems.get());
130 minimiseMenuItem.setEnabled(enabledMenuItems.get());
131 }
132
133 /** {@inheritDoc} */
134 @Override
135 public void setParent(final JComponent parent) {
136 //Ignore
/*
P/P * Method: void setParent(JComponent)
*/
137 }
138
139 /** {@inheritDoc} */
140 @Override
141 public boolean canPositionVertically() {
/*
P/P * Method: bool canPositionVertically()
*
* Postconditions:
* return_value == 1
*/
142 return true;
143 }
144
145 /** {@inheritDoc} */
146 @Override
147 public boolean canPositionHorizontally() {
/*
P/P * Method: bool canPositionHorizontally()
*
* Postconditions:
* return_value == 1
*/
148 return true;
149 }
150
151 /** {@inheritDoc} */
152 @Override
153 public void addWindow(final FrameContainer window) {
/*
P/P * Method: void addWindow(FrameContainer)
*/
154 addFrameContainer(window);
155 }
156
157 /** {@inheritDoc} */
158 @Override
159 public void delWindow(final FrameContainer window) {
/*
P/P * Method: void delWindow(FrameContainer)
*/
160 removeFramecontainer(window);
161 }
162
163 /** {@inheritDoc} */
164 @Override
165 public void addWindow(final FrameContainer parent,
166 final FrameContainer window) {
/*
P/P * Method: void addWindow(FrameContainer, FrameContainer)
*/
167 addFrameContainer(window);
168 }
169
170 /** {@inheritDoc} */
171 @Override
172 public void delWindow(final FrameContainer parent,
173 final FrameContainer window) {
/*
P/P * Method: void delWindow(FrameContainer, FrameContainer)
*/
174 removeFramecontainer(window);
175 }
176
177 /**
178 * Adds a frame container to the list.
179 *
180 * @param window Window to add to the list
181 */
182 private void addFrameContainer(final FrameContainer window) {
/*
P/P * Method: void addFrameContainer(FrameContainer)
*/
183 UIUtilities.invokeLater(new Runnable() {
184
185 /** {@inheritDoc} */
186 @Override
187 public void run() {
/*
P/P * Method: void run()
*
* Preconditions:
* this.closeMenuItem != null
* this.enabledMenuItems != null
* this.menuItemMap != null
* this.minimiseMenuItem != null
* this.separator != null
* this.toggleStateMenuItem != null
* this.val$window != null
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* (soft) this.comparator != null
*
* Postconditions:
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(new ArrayList(getSources#1) num objects)
* init'ed(new ConfigManager(getGlobalConfig#1) num objects)
* possibly_updated(new ConfigManager(getGlobalConfig#1).channel)
* possibly_updated(new ConfigManager(getGlobalConfig#1).file)
* possibly_updated(new ConfigManager(getGlobalConfig#1).ircd)
* possibly_updated(new ConfigManager(getGlobalConfig#1).listeners)
* possibly_updated(new ConfigManager(getGlobalConfig#1).network)
* possibly_updated(new ConfigManager(getGlobalConfig#1).server)
* ...
*
* Test Vectors:
* com.dmdirc.addons.ui_swing.framemanager.windowmenu.WindowMenuFrameManager:isShowing(...)@191: {0}, {1}
*/
188 final FrameContainerMenuItem mi =
189 new FrameContainerMenuItem(window);
190 synchronized (menuItemMap) {
191 if (isShowing()) {
192 setSelected(false);
193 setPopupMenuVisible(false);
194 }
195 menuItemMap.put(window, mi);
196 window.addSelectionListener(WindowMenuFrameManager.this);
197 add(mi, getIndex(window));
198 checkMenuItems();
199 }
200 }
201 });
202 }
203
204 /**
205 * Removes a frame container from the list.
206 *
207 * @param window Window to remove from list
208 */
209 private void removeFramecontainer(final FrameContainer window) {
/*
P/P * Method: void removeFramecontainer(FrameContainer)
*/
210 UIUtilities.invokeLater(new Runnable() {
211
212 /** {@inheritDoc} */
213 @Override
214 public void run() {
/*
P/P * Method: void run()
*
* Preconditions:
* this.closeMenuItem != null
* this.enabledMenuItems != null
* this.menuItemMap != null
* this.minimiseMenuItem != null
* this.separator != null
* this.toggleStateMenuItem != null
* (soft) this.val$window != null
*
* Test Vectors:
* com.dmdirc.addons.ui_swing.framemanager.windowmenu.WindowMenuFrameManager:isShowing(...)@216: {0}, {1}
* java.util.Map:get(...)@220: Addr_Set{null}, Inverse{null}
*/
215 synchronized (menuItemMap) {
216 if (isShowing()) {
217 setSelected(false);
218 setPopupMenuVisible(false);
219 }
220 final FrameContainerMenuItem mi = menuItemMap.get(window);
221 if (mi != null) {
222 remove(mi);
223 menuItemMap.remove(window);
224 window.removeSelectionListener(
225 WindowMenuFrameManager.this);
226 }
227 checkMenuItems();
228 }
229 }
230 });
231 }
232
233 /**
234 * {@inheritDoc}
235 *
236 * @param e Action event
237 */
238 @Override
239 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* this.enabledMenuItems != null
* (soft) e != null
* (soft) this.activeWindow != null
*
* Presumptions:
* java.awt.event.ActionEvent:getActionCommand(...)@241 != null
* java.awt.event.ActionEvent:getActionCommand(...)@243 != null
* java.awt.event.ActionEvent:getActionCommand(...)@245 != null
*
* Test Vectors:
* java.lang.String:equals(...)@241: {0}, {1}
* java.lang.String:equals(...)@243: {0}, {1}
* java.lang.String:equals(...)@245: {0}, {1}
* java.util.concurrent.atomic.AtomicBoolean:get(...)@240: {0}, {1}
*/
240 if (enabledMenuItems.get()) {
241 if (e.getActionCommand().equals("ToggleState")) {
242 activeWindow.toggleMaximise();
243 } else if (e.getActionCommand().equals("Minimise")) {
244 activeWindow.minimise();
245 } else if (e.getActionCommand().equals("Close")) {
246 activeWindow.close();
247 }
248 }
249 }
250
251 /** {@inheritDoc} */
252 @Override
253 public void selectionChanged(final Window window) {
/*
P/P * Method: void selectionChanged(Window)
*
* Preconditions:
* (soft) window != null
*
* Presumptions:
* java.util.Iterator:next(...)@262 != null
* java.util.Map:values(...)@262 != null
* menuItem.frame@262 != null
*
* Postconditions:
* this.activeWindow == window
* this.activeWindow != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@262: {0}, {1}
*/
254 activeWindow = window;
255 final Map<FrameContainer, FrameContainerMenuItem> newMap =
256 new TreeMap<FrameContainer, FrameContainerMenuItem>(
257 comparator);
258 synchronized (menuItemMap) {
259 newMap.putAll(menuItemMap);
260 }
261
262 for (FrameContainerMenuItem menuItem : newMap.values()) {
263 menuItem.selectionChanged(window);
264 }
265 }
266
267 /**
268 * Checks and sets the state of the toggle menu item.
269 */
270 private void checkToggleState() {
/*
P/P * Method: void checkToggleState()
*
* Preconditions:
* init'ed(this.activeWindow)
* this.closeMenuItem != null
* this.enabledMenuItems != null
* this.minimiseMenuItem != null
* this.separator != null
* this.toggleStateMenuItem != null
*
* Presumptions:
* com.dmdirc.ui.IconManager:getIconManager(...)@279 != null
* com.dmdirc.ui.IconManager:getIconManager(...)@284 != null
*
* Test Vectors:
* this.activeWindow: Addr_Set{null}, Inverse{null}
* com.dmdirc.ui.interfaces.Window:isMaximum(...)@277: {0}, {1}
*/
271 checkMenuItems();
272 if (activeWindow != null) {
273 toggleStateMenuItem.setEnabled(true);
274 closeMenuItem.setEnabled(true);
275 minimiseMenuItem.setEnabled(true);
276
277 if (activeWindow.isMaximum()) {
278 toggleStateMenuItem.setText("Restore");
279 toggleStateMenuItem.setIcon(IconManager.getIconManager().getIcon(
280 "restore"));
281 toggleStateMenuItem.setMnemonic('r');
282 } else {
283 toggleStateMenuItem.setText("Maximise");
284 toggleStateMenuItem.setIcon(IconManager.getIconManager().getIcon(
285 "maximise"));
286 toggleStateMenuItem.setMnemonic('m');
287 }
288 }
289 }
290
291 /**
292 * Compares the new child with the existing children or parent to decide
293 * where it needs to be inserted.
294 *
295 * @param newChild new node to be inserted.
296 *
297 * @return index where new node is to be inserted.
298 */
299 private int getIndex(final FrameContainer newChild) {
/*
P/P * Method: int getIndex(FrameContainer)
*
* Preconditions:
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) newChild != null
* (soft) init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* (soft) this.comparator != null
*
* Presumptions:
* com.dmdirc.addons.ui_swing.framemanager.windowmenu.WindowMenuFrameManager:getMenuComponent(...).frame@304 != null
* com.dmdirc.addons.ui_swing.framemanager.windowmenu.WindowMenuFrameManager:getMenuComponent(...)@304 != null
*
* Postconditions:
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* java.lang.StringBuilder:toString(...)._tainted == 0
* init'ed(return_value)
* init'ed(new ArrayList(getSources#1) num objects)
* init'ed(new ConfigManager(getGlobalConfig#1) num objects)
* init'ed(new ConfigManager(getGlobalConfig#1).channel)
* init'ed(new ConfigManager(getGlobalConfig#1).file)
* init'ed(new ConfigManager(getGlobalConfig#1).ircd)
* init'ed(new ConfigManager(getGlobalConfig#1).listeners)
* init'ed(new ConfigManager(getGlobalConfig#1).network)
* ...
*
* Test Vectors:
* java.lang.Boolean:parseBoolean(...)@159: {0}, {1}
* java.lang.String:compareToIgnoreCase(...)@308: {0..232-1}, {-231..-1}
*/
300 for (int i = itemCount; i < getMenuComponentCount(); i++) {
301 if (!(getMenuComponent(i) instanceof FrameContainerMenuItem)) {
302 continue;
303 }
304 final FrameContainer child =
305 ((FrameContainerMenuItem) getMenuComponent(i)).getFrame();
306 if (sortBefore(newChild, child)) {
307 return i;
308 } else if (!sortAfter(newChild, child) &&
309 IdentityManager.getGlobalConfig().getOptionBool("treeview",
310 "sortwindows") && newChild.toString().compareToIgnoreCase(
311 child.toString()) < 0) {
312 return i;
313 }
314 }
315
316 return getMenuComponentCount();
317 }
318
319 /**
320 * Compares the types of the specified nodes' objects to see if the new
321 * node should be sorted before the other.
322 *
323 * @param newChild The new child to be tested
324 * @param child The existing child that it's being tested against
325 *
326 * @return True iff newChild should be sorted before child
327 */
328 private boolean sortBefore(final FrameContainer newChild,
329 final FrameContainer child) {
330
/*
P/P * Method: bool sortBefore(FrameContainer, FrameContainer)
*
* Preconditions:
* this.comparator != null
*
* Postconditions:
* init'ed(return_value)
*/
331 return comparator.compare(newChild, child) <= -1;
332 }
333
334 /**
335 * Compares the types of the specified nodes' objects to see if the new
336 * node should be sorted after the other.
337 *
338 * @param newChild The new child to be tested
339 * @param child The existing child that it's being tested against
340 *
341 * @return True iff newChild should be sorted before child
342 */
343 private boolean sortAfter(final FrameContainer newChild,
344 final FrameContainer child) {
345
/*
P/P * Method: bool sortAfter(FrameContainer, FrameContainer)
*
* Preconditions:
* this.comparator != null
*
* Postconditions:
* init'ed(return_value)
*/
346 return comparator.compare(newChild, child) >= 1;
347 }
348
349 /** {@inheritDoc} */
350 @Override
351 public void menuSelected(final MenuEvent e) {
/*
P/P * Method: void menuSelected(MenuEvent)
*
* Preconditions:
* init'ed(this.activeWindow)
* this.closeMenuItem != null
* this.enabledMenuItems != null
* this.minimiseMenuItem != null
* this.separator != null
* this.toggleStateMenuItem != null
*/
352 checkToggleState();
353 }
354
355 /** {@inheritDoc} */
356 @Override
357 public void menuDeselected(final MenuEvent e) {
358 //Ignore
/*
P/P * Method: void menuDeselected(MenuEvent)
*/
359 }
360
361 /** {@inheritDoc} */
362 @Override
363 public void menuCanceled(final MenuEvent e) {
364 //Ignore
/*
P/P * Method: void menuCanceled(MenuEvent)
*/
365 }
366 }
SofCheck Inspector Build Version : 2.17854
| WindowMenuFrameManager.java |
2009-Jun-25 01:54:24 |
| WindowMenuFrameManager.class |
2009-Sep-02 17:04:16 |
| WindowMenuFrameManager$1.class |
2009-Sep-02 17:04:16 |
| WindowMenuFrameManager$2.class |
2009-Sep-02 17:04:16 |