1 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 ; 28 import java.beans.PropertyChangeEvent ; 29 import java.beans.PropertyChangeListener ; 30 import java.util.Arrays ; 31 import java.util.Hashtable ; 32 import java.util.List ; 33 import javax.swing.JMenu ; 34 import javax.swing.JMenuItem ; 35 import javax.swing.JRadioButtonMenuItem ; 36 import org.openide.awt.Mnemonics; 37 38 39 44 public class WorkspaceSwitchAction extends CallableSystemAction { 45 public String 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 getMenuPresenter() { 54 JMenu 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 menu2Workspace = new Hashtable (10); 67 68 final Hashtable workspace2Menu = new Hashtable (10); 70 71 final Hashtable workspace2Listener = new Hashtable (10); 73 74 final Workspace[] currentDeskRef = new Workspace[1]; 76 currentDeskRef[0] = pool.getCurrentWorkspace(); 77 78 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 JRadioButtonMenuItem curItem = (JRadioButtonMenuItem ) workspace2Menu.get(currentDeskRef[0]); 87 88 if (curItem != null) { 89 curItem.setSelected(true); 90 } 91 92 pool.addPropertyChangeListener( 94 getWorkspacePoolListener(workspace2Menu, menu2Workspace, workspace2Listener, currentDeskRef, menu) 95 ); 96 97 return menu; 98 } 99 100 101 public void performAction() { 102 assert false; 103 } 104 105 106 private java.awt.event.ActionListener createActionListener( 107 final JRadioButtonMenuItem menuItem, final Workspace[] currentDeskRef, final Hashtable menu2Workspace, 108 final Hashtable workspace2Menu 109 ) { 110 return new java.awt.event.ActionListener () { 111 public void actionPerformed(java.awt.event.ActionEvent 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 ) workspace2Menu.get(desk)).setSelected(true); 123 124 if (desk == currentDeskRef[0]) { 125 return; 126 } 127 128 if (currentDeskRef[0] != null) { 130 ((JRadioButtonMenuItem ) workspace2Menu.get(currentDeskRef[0])).setSelected(false); 131 } 132 133 currentDeskRef[0] = desk; 134 desk.activate(); 135 } 136 }; 137 } 138 139 140 private PropertyChangeListener getWorkspacePoolListener( 141 final Hashtable workspace2Menu, final Hashtable menu2Workspace, final Hashtable workspace2Listener, 142 final Workspace[] currentDeskRef, final JMenu menu 143 ) { 144 PropertyChangeListener pcl1 = new PropertyChangeListener () { 145 public void propertyChange(PropertyChangeEvent 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 menu2 = ((JRadioButtonMenuItem ) workspace2Menu.get(currentDeskRef[0])); 154 155 if (menu2 != null) { 156 menu2.setSelected(false); 157 } 158 159 currentDeskRef[0] = newDesk; 160 menu2 = ((JRadioButtonMenuItem ) 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 175 List newList = Arrays.asList(newWorkspaces); 176 List oldList = Arrays.asList(oldWorkspaces); 177 178 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 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 204 void attachWorkspace( 205 Workspace workspace, Workspace[] currentDeskRef, Hashtable workspace2Menu, Hashtable menu2Workspace, 206 Hashtable workspace2Listener, JMenu menu 207 ) { 208 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem (); 210 Mnemonics.setLocalizedText(menuItem, workspace.getDisplayName()); 211 HelpCtx.setHelpIDString(menuItem, WorkspaceSwitchAction.class.getName()); 212 213 ActionListener 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 223 void detachWorkspace( 224 Workspace workspace, Hashtable workspace2Menu, Hashtable menu2Workspace, Hashtable workspace2Listener, 225 JMenu menu 226 ) { 227 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem ) workspace2Menu.get(workspace); 228 workspace2Menu.remove(workspace); 229 menu2Workspace.remove(workspace2Listener.get(workspace)); 230 workspace2Listener.remove(workspace); 231 menu.remove(menuItem); 232 } 233 234 235 236 private PropertyChangeListener createNameListener(final JRadioButtonMenuItem item) { 238 return new PropertyChangeListener () { 239 public void propertyChange(PropertyChangeEvent ev) { 240 if (ev.getPropertyName().equals("name")) { 241 item.setText((String ) ev.getNewValue()); 242 } 243 } 244 }; 245 } 246 } 247 | Popular Tags |