KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > WorkspaceSwitchAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.actions;
20
21 import org.openide.awt.Actions;
22 import org.openide.util.HelpCtx;
23 import org.openide.util.NbBundle;
24 import org.openide.util.actions.CallableSystemAction;
25 import org.openide.windows.WindowManager;
26 import org.openide.windows.Workspace;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.List JavaDoc;
33 import javax.swing.JMenu JavaDoc;
34 import javax.swing.JMenuItem JavaDoc;
35 import javax.swing.JRadioButtonMenuItem JavaDoc;
36 import org.openide.awt.Mnemonics;
37
38
39 /** Switch to a different workspace.
40 * @see Workspace#activate
41 * @author Ales Novak
42  * @deprecated No longer used since there are no more workspaces.
43 */

44 public class WorkspaceSwitchAction extends CallableSystemAction {
45     public String JavaDoc getName() {
46         return NbBundle.getBundle(WorkspaceSwitchAction.class).getString("WorkspacesItems");
47     }
48
49     public HelpCtx getHelpCtx() {
50         return new HelpCtx(WorkspaceSwitchAction.class);
51     }
52
53     public JMenuItem JavaDoc getMenuPresenter() {
54         // beware, we shouldn't cache menu intstance, because getMenuPresenter
55
// can be legally called several times and menu component cannot be
56
// contained in more than one component hierarchy
57
JMenu JavaDoc menu = new org.openide.awt.JMenuPlus();
58         Mnemonics.setLocalizedText(menu, getName());
59         menu.setHorizontalTextPosition(JMenu.RIGHT);
60         menu.setHorizontalAlignment(JMenu.LEFT);
61         menu.setIcon(getIcon());
62         HelpCtx.setHelpIDString(menu, WorkspaceSwitchAction.class.getName());
63
64         final WindowManager pool = WindowManager.getDefault();
65
66         final Hashtable JavaDoc menu2Workspace = new Hashtable JavaDoc(10);
67
68         // ^ maps listener on workspace
69
final Hashtable JavaDoc workspace2Menu = new Hashtable JavaDoc(10);
70
71         // ^ maps workspace to menuitem
72
final Hashtable JavaDoc workspace2Listener = new Hashtable JavaDoc(10);
73
74         // ^ maps workspace to action listener
75
final Workspace[] currentDeskRef = new Workspace[1];
76         currentDeskRef[0] = pool.getCurrentWorkspace();
77
78         // attach all workspaces
79
Workspace[] workspaces = pool.getWorkspaces();
80
81         for (int i = 0; i < workspaces.length; i++) {
82             attachWorkspace(workspaces[i], currentDeskRef, workspace2Menu, menu2Workspace, workspace2Listener, menu);
83         }
84
85         // check on currently active workspace
86
JRadioButtonMenuItem JavaDoc curItem = (JRadioButtonMenuItem JavaDoc) workspace2Menu.get(currentDeskRef[0]);
87
88         if (curItem != null) {
89             curItem.setSelected(true);
90         }
91
92         // listen to the changes in workspaces
93
pool.addPropertyChangeListener(
94             getWorkspacePoolListener(workspace2Menu, menu2Workspace, workspace2Listener, currentDeskRef, menu)
95         );
96
97         return menu;
98     }
99
100     /** Not implemented. May only be used in a menu presenter, with the children performing the action. */
101     public void performAction() {
102         assert false;
103     }
104
105     /** creates new actionlistener for given menuitem */
106     private java.awt.event.ActionListener JavaDoc createActionListener(
107         final JRadioButtonMenuItem JavaDoc menuItem, final Workspace[] currentDeskRef, final Hashtable JavaDoc menu2Workspace,
108         final Hashtable JavaDoc workspace2Menu
109     ) {
110         return new java.awt.event.ActionListener JavaDoc() {
111                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
112                     Workspace desk = (Workspace) menu2Workspace.get(this);
113
114                     if (desk == null) {
115                         return;
116                     }
117
118                     if (workspace2Menu.get(desk) == null) {
119                         return;
120                     }
121
122                     ((JRadioButtonMenuItem JavaDoc) workspace2Menu.get(desk)).setSelected(true);
123
124                     if (desk == currentDeskRef[0]) {
125                         return;
126                     }
127
128                     // deactivate old if present
129
if (currentDeskRef[0] != null) {
130                         ((JRadioButtonMenuItem JavaDoc) workspace2Menu.get(currentDeskRef[0])).setSelected(false);
131                     }
132
133                     currentDeskRef[0] = desk;
134                     desk.activate();
135                 }
136             };
137     }
138
139     /** creates propertychangelistener that listens on current workspace */
140     private PropertyChangeListener JavaDoc getWorkspacePoolListener(
141         final Hashtable JavaDoc workspace2Menu, final Hashtable JavaDoc menu2Workspace, final Hashtable JavaDoc workspace2Listener,
142         final Workspace[] currentDeskRef, final JMenu JavaDoc menu
143     ) {
144         PropertyChangeListener JavaDoc pcl1 = new PropertyChangeListener JavaDoc() {
145                 public void propertyChange(PropertyChangeEvent JavaDoc che) {
146                     if (che.getPropertyName().equals(WindowManager.PROP_CURRENT_WORKSPACE)) {
147                         Workspace newDesk = (Workspace) che.getNewValue();
148
149                         if (currentDeskRef[0] == newDesk) {
150                             return;
151                         }
152
153                         JRadioButtonMenuItem JavaDoc menu2 = ((JRadioButtonMenuItem JavaDoc) workspace2Menu.get(currentDeskRef[0]));
154
155                         if (menu2 != null) {
156                             menu2.setSelected(false);
157                         }
158
159                         currentDeskRef[0] = newDesk;
160                         menu2 = ((JRadioButtonMenuItem JavaDoc) workspace2Menu.get(newDesk));
161
162                         if (menu2 != null) {
163                             menu2.setSelected(true);
164                         }
165                     } else if (che.getPropertyName().equals(WindowManager.PROP_WORKSPACES)) {
166                         Workspace[] newWorkspaces = (Workspace[]) che.getNewValue();
167                         Workspace[] oldWorkspaces = (Workspace[]) che.getOldValue();
168
169                         /*for (int i = 0; i < oldWorkspaces.length; i++) {
170                           System.out.println ("Old Value["+i+"]= "+oldWorkspaces[i].getName());
171                         }
172                         for (int i = 0; i < newWorkspaces.length; i++) {
173                           System.out.println ("New Value["+i+"]= "+newWorkspaces[i].getName());
174                         }*/

175                         List JavaDoc newList = Arrays.asList(newWorkspaces);
176                         List JavaDoc oldList = Arrays.asList(oldWorkspaces);
177
178                         // remove old
179
for (int i = 0; i < oldWorkspaces.length; i++) {
180                             if (newList.indexOf(oldWorkspaces[i]) < 0) {
181                                 detachWorkspace(
182                                     oldWorkspaces[i], workspace2Menu, menu2Workspace, workspace2Listener, menu
183                                 );
184                             }
185                         }
186
187                         // attach new
188
for (int i = 0; i < newWorkspaces.length; i++) {
189                             if (oldList.indexOf(newWorkspaces[i]) < 0) {
190                                 attachWorkspace(
191                                     newWorkspaces[i], currentDeskRef, workspace2Menu, menu2Workspace, workspace2Listener,
192                                     menu
193                                 );
194                             }
195                         }
196                     }
197                 }
198             };
199
200         return pcl1;
201     }
202
203     /** Initializes listeners atc to the given workspace */
204     void attachWorkspace(
205         Workspace workspace, Workspace[] currentDeskRef, Hashtable JavaDoc workspace2Menu, Hashtable JavaDoc menu2Workspace,
206         Hashtable JavaDoc workspace2Listener, JMenu JavaDoc menu
207     ) {
208         // bugfix #6116 - change from getName() to getDisplayName()
209
JRadioButtonMenuItem JavaDoc menuItem = new JRadioButtonMenuItem JavaDoc();
210         Mnemonics.setLocalizedText(menuItem, workspace.getDisplayName());
211         HelpCtx.setHelpIDString(menuItem, WorkspaceSwitchAction.class.getName());
212
213         ActionListener JavaDoc listener = createActionListener(menuItem, currentDeskRef, menu2Workspace, workspace2Menu);
214         menuItem.addActionListener(listener);
215         menu2Workspace.put(listener, workspace);
216         workspace2Listener.put(workspace, listener);
217         workspace2Menu.put(workspace, menuItem);
218         workspace.addPropertyChangeListener(createNameListener(menuItem));
219         menu.add(menuItem);
220     }
221
222     /** Frees all listeners etc from given workspace. */
223     void detachWorkspace(
224         Workspace workspace, Hashtable JavaDoc workspace2Menu, Hashtable JavaDoc menu2Workspace, Hashtable JavaDoc workspace2Listener,
225         JMenu JavaDoc menu
226     ) {
227         JRadioButtonMenuItem JavaDoc menuItem = (JRadioButtonMenuItem JavaDoc) workspace2Menu.get(workspace);
228         workspace2Menu.remove(workspace);
229         menu2Workspace.remove(workspace2Listener.get(workspace));
230         workspace2Listener.remove(workspace);
231         menu.remove(menuItem);
232     }
233
234     /** creates new PropertyChangeListener that listens for "name" property... */
235
236     // NOI18N
237
private PropertyChangeListener JavaDoc createNameListener(final JRadioButtonMenuItem JavaDoc item) {
238         return new PropertyChangeListener JavaDoc() {
239                 public void propertyChange(PropertyChangeEvent JavaDoc ev) {
240                     if (ev.getPropertyName().equals("name")) {
241                         item.setText((String JavaDoc) ev.getNewValue());
242                     }
243                 }
244             };
245     }
246 }
247
Popular Tags