001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.openide.actions;
042:
043: import org.openide.util.HelpCtx;
044: import org.openide.util.NbBundle;
045: import org.openide.util.actions.CallableSystemAction;
046: import org.openide.windows.WindowManager;
047: import org.openide.windows.Workspace;
048: import java.awt.event.ActionListener;
049: import java.beans.PropertyChangeEvent;
050: import java.beans.PropertyChangeListener;
051: import java.util.Arrays;
052: import java.util.Hashtable;
053: import java.util.List;
054: import javax.swing.JMenu;
055: import javax.swing.JMenuItem;
056: import javax.swing.JRadioButtonMenuItem;
057: import org.openide.awt.Mnemonics;
058:
059: /** Switch to a different workspace.
060: * @see Workspace#activate
061: * @author Ales Novak
062: * @deprecated No longer used since there are no more workspaces.
063: */
064: @Deprecated
065: @SuppressWarnings({"unchecked","deprecation"})
066: public class WorkspaceSwitchAction extends CallableSystemAction {
067: public String getName() {
068: return NbBundle.getBundle(WorkspaceSwitchAction.class)
069: .getString("WorkspacesItems");
070: }
071:
072: public HelpCtx getHelpCtx() {
073: return new HelpCtx(WorkspaceSwitchAction.class);
074: }
075:
076: public JMenuItem getMenuPresenter() {
077: // beware, we shouldn't cache menu intstance, because getMenuPresenter
078: // can be legally called several times and menu component cannot be
079: // contained in more than one component hierarchy
080: JMenu menu = new org.openide.awt.JMenuPlus();
081: Mnemonics.setLocalizedText(menu, getName());
082: menu.setHorizontalTextPosition(JMenu.RIGHT);
083: menu.setHorizontalAlignment(JMenu.LEFT);
084: menu.setIcon(getIcon());
085: HelpCtx.setHelpIDString(menu, WorkspaceSwitchAction.class
086: .getName());
087:
088: final WindowManager pool = WindowManager.getDefault();
089:
090: final Hashtable menu2Workspace = new Hashtable(10);
091:
092: // ^ maps listener on workspace
093: final Hashtable workspace2Menu = new Hashtable(10);
094:
095: // ^ maps workspace to menuitem
096: final Hashtable workspace2Listener = new Hashtable(10);
097:
098: // ^ maps workspace to action listener
099: final Workspace[] currentDeskRef = new Workspace[1];
100: currentDeskRef[0] = pool.getCurrentWorkspace();
101:
102: // attach all workspaces
103: Workspace[] workspaces = pool.getWorkspaces();
104:
105: for (int i = 0; i < workspaces.length; i++) {
106: attachWorkspace(workspaces[i], currentDeskRef,
107: workspace2Menu, menu2Workspace, workspace2Listener,
108: menu);
109: }
110:
111: // check on currently active workspace
112: JRadioButtonMenuItem curItem = (JRadioButtonMenuItem) workspace2Menu
113: .get(currentDeskRef[0]);
114:
115: if (curItem != null) {
116: curItem.setSelected(true);
117: }
118:
119: // listen to the changes in workspaces
120: pool.addPropertyChangeListener(getWorkspacePoolListener(
121: workspace2Menu, menu2Workspace, workspace2Listener,
122: currentDeskRef, menu));
123:
124: return menu;
125: }
126:
127: /** Not implemented. May only be used in a menu presenter, with the children performing the action. */
128: public void performAction() {
129: assert false;
130: }
131:
132: /** creates new actionlistener for given menuitem */
133: private java.awt.event.ActionListener createActionListener(
134: final JRadioButtonMenuItem menuItem,
135: final Workspace[] currentDeskRef,
136: final Hashtable menu2Workspace,
137: final Hashtable workspace2Menu) {
138: return new java.awt.event.ActionListener() {
139: public void actionPerformed(java.awt.event.ActionEvent evt) {
140: Workspace desk = (Workspace) menu2Workspace.get(this );
141:
142: if (desk == null) {
143: return;
144: }
145:
146: if (workspace2Menu.get(desk) == null) {
147: return;
148: }
149:
150: ((JRadioButtonMenuItem) workspace2Menu.get(desk))
151: .setSelected(true);
152:
153: if (desk == currentDeskRef[0]) {
154: return;
155: }
156:
157: // deactivate old if present
158: if (currentDeskRef[0] != null) {
159: ((JRadioButtonMenuItem) workspace2Menu
160: .get(currentDeskRef[0])).setSelected(false);
161: }
162:
163: currentDeskRef[0] = desk;
164: desk.activate();
165: }
166: };
167: }
168:
169: /** creates propertychangelistener that listens on current workspace */
170: private PropertyChangeListener getWorkspacePoolListener(
171: final Hashtable workspace2Menu,
172: final Hashtable menu2Workspace,
173: final Hashtable workspace2Listener,
174: final Workspace[] currentDeskRef, final JMenu menu) {
175: PropertyChangeListener pcl1 = new PropertyChangeListener() {
176: public void propertyChange(PropertyChangeEvent che) {
177: if (che.getPropertyName().equals(
178: WindowManager.PROP_CURRENT_WORKSPACE)) {
179: Workspace newDesk = (Workspace) che.getNewValue();
180:
181: if (currentDeskRef[0] == newDesk) {
182: return;
183: }
184:
185: JRadioButtonMenuItem menu2 = ((JRadioButtonMenuItem) workspace2Menu
186: .get(currentDeskRef[0]));
187:
188: if (menu2 != null) {
189: menu2.setSelected(false);
190: }
191:
192: currentDeskRef[0] = newDesk;
193: menu2 = ((JRadioButtonMenuItem) workspace2Menu
194: .get(newDesk));
195:
196: if (menu2 != null) {
197: menu2.setSelected(true);
198: }
199: } else if (che.getPropertyName().equals(
200: WindowManager.PROP_WORKSPACES)) {
201: Workspace[] newWorkspaces = (Workspace[]) che
202: .getNewValue();
203: Workspace[] oldWorkspaces = (Workspace[]) che
204: .getOldValue();
205:
206: /*for (int i = 0; i < oldWorkspaces.length; i++) {
207: System.out.println ("Old Value["+i+"]= "+oldWorkspaces[i].getName());
208: }
209: for (int i = 0; i < newWorkspaces.length; i++) {
210: System.out.println ("New Value["+i+"]= "+newWorkspaces[i].getName());
211: }*/
212: List newList = Arrays.asList(newWorkspaces);
213: List oldList = Arrays.asList(oldWorkspaces);
214:
215: // remove old
216: for (int i = 0; i < oldWorkspaces.length; i++) {
217: if (newList.indexOf(oldWorkspaces[i]) < 0) {
218: detachWorkspace(oldWorkspaces[i],
219: workspace2Menu, menu2Workspace,
220: workspace2Listener, menu);
221: }
222: }
223:
224: // attach new
225: for (int i = 0; i < newWorkspaces.length; i++) {
226: if (oldList.indexOf(newWorkspaces[i]) < 0) {
227: attachWorkspace(newWorkspaces[i],
228: currentDeskRef, workspace2Menu,
229: menu2Workspace, workspace2Listener,
230: menu);
231: }
232: }
233: }
234: }
235: };
236:
237: return pcl1;
238: }
239:
240: /** Initializes listeners atc to the given workspace */
241: void attachWorkspace(Workspace workspace,
242: Workspace[] currentDeskRef, Hashtable workspace2Menu,
243: Hashtable menu2Workspace, Hashtable workspace2Listener,
244: JMenu menu) {
245: // bugfix #6116 - change from getName() to getDisplayName()
246: JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem();
247: Mnemonics
248: .setLocalizedText(menuItem, workspace.getDisplayName());
249: HelpCtx.setHelpIDString(menuItem, WorkspaceSwitchAction.class
250: .getName());
251:
252: ActionListener listener = createActionListener(menuItem,
253: currentDeskRef, menu2Workspace, workspace2Menu);
254: menuItem.addActionListener(listener);
255: menu2Workspace.put(listener, workspace);
256: workspace2Listener.put(workspace, listener);
257: workspace2Menu.put(workspace, menuItem);
258: workspace
259: .addPropertyChangeListener(createNameListener(menuItem));
260: menu.add(menuItem);
261: }
262:
263: /** Frees all listeners etc from given workspace. */
264: void detachWorkspace(Workspace workspace, Hashtable workspace2Menu,
265: Hashtable menu2Workspace, Hashtable workspace2Listener,
266: JMenu menu) {
267: JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) workspace2Menu
268: .get(workspace);
269: workspace2Menu.remove(workspace);
270: menu2Workspace.remove(workspace2Listener.get(workspace));
271: workspace2Listener.remove(workspace);
272: menu.remove(menuItem);
273: }
274:
275: /** creates new PropertyChangeListener that listens for "name" property... */
276:
277: // NOI18N
278: private PropertyChangeListener createNameListener(
279: final JRadioButtonMenuItem item) {
280: return new PropertyChangeListener() {
281: public void propertyChange(PropertyChangeEvent ev) {
282: if (ev.getPropertyName().equals("name")) {
283: item.setText((String) ev.getNewValue());
284: }
285: }
286: };
287: }
288: }
|