1 19 20 21 package org.netbeans.core.windows.actions; 22 23 24 import java.awt.Container ; 25 import java.awt.event.*; 26 import java.beans.*; 27 import java.io.IOException ; 28 import java.util.*; 29 import javax.swing.*; 30 import org.netbeans.core.windows.*; 31 import org.netbeans.core.windows.view.ui.slides.SlideController; 32 import org.openide.actions.SaveAction; 33 import org.openide.cookies.SaveCookie; 34 import org.openide.util.*; 35 import org.openide.util.actions.Presenter; 36 import org.openide.windows.Mode; 37 import org.openide.windows.TopComponent; 38 39 40 46 public abstract class ActionUtils { 47 48 private static HashMap<Object , Object > sharedAccelerators = new HashMap<Object , Object >(); 49 50 private ActionUtils() {} 51 52 public static Action[] createDefaultPopupActions(TopComponent tc) { 53 ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(tc); 54 int kind = mode != null ? mode.getKind() : Constants.MODE_KIND_EDITOR; 55 56 List<Action> actions = new ArrayList<Action>(); 57 if(kind == Constants.MODE_KIND_EDITOR) { 58 actions.add(new CloseAllDocumentsAction(false)); 59 CloseAllButThisAction allBut = new CloseAllButThisAction(tc); 60 if (mode != null && mode.getOpenedTopComponents().size() == 1) { 61 allBut.setEnabled(false); 62 } 63 actions.add(allBut); 64 actions.add(null); actions.add(new SaveDocumentAction(tc)); 66 actions.add(new CloneDocumentAction(tc)); 67 actions.add(null); actions.add(new CloseWindowAction(tc)); 69 actions.add(new MaximizeWindowAction(tc)); 70 actions.add(new UndockWindowAction(tc)); 71 } else if (kind == Constants.MODE_KIND_VIEW) { 72 actions.add(new CloseWindowAction(tc)); 73 if (mode.getState() == Constants.MODE_STATE_JOINED) { 75 actions.add(new MaximizeWindowAction(tc)); 76 } 77 actions.add(new UndockWindowAction(tc)); 78 } else if (kind == Constants.MODE_KIND_SLIDING) { 79 actions.add(new CloseWindowAction(tc)); 80 if (mode.getState() == Constants.MODE_STATE_JOINED) { 81 actions.add(new MaximizeWindowAction(tc)); 82 } 83 actions.add(new UndockWindowAction(tc)); 84 } 85 86 return actions.toArray(new Action[actions.size()]); 87 } 88 89 90 private static Container slidingContext; 91 92 public static void setSlidingContext (Container slidingContext) { 93 ActionUtils.slidingContext = slidingContext; 94 } 95 96 97 98 public static final class AutoHideWindowAction extends AbstractAction implements Presenter.Popup { 99 100 private final SlideController slideController; 101 102 private final int tabIndex; 103 104 private boolean state; 105 106 private JCheckBoxMenuItem menuItem; 107 108 public AutoHideWindowAction(SlideController slideController, int tabIndex, boolean initialState) { 109 super(); 110 this.slideController = slideController; 111 this.tabIndex = tabIndex; 112 this.state = initialState; 113 putValue(Action.NAME, NbBundle.getMessage(ActionUtils.class, "LBL_AutoHideWindowAction")); 114 } 115 116 public HelpCtx getHelpCtx() { 117 return null; 118 } 119 120 123 public void actionPerformed(ActionEvent e) { 124 state = !state; 126 getMenuItem().setSelected(state); 127 slideController.userToggledAutoHide(tabIndex, state); 129 } 130 131 public JMenuItem getPopupPresenter() { 132 return getMenuItem(); 133 } 134 135 private JCheckBoxMenuItem getMenuItem() { 136 if (menuItem == null) { 137 menuItem = new JCheckBoxMenuItem((String )getValue(Action.NAME), state); 138 menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, InputEvent.CTRL_DOWN_MASK)); 143 menuItem.addActionListener(this); 144 } 145 return menuItem; 146 } 147 148 } 150 private static class SaveDocumentAction extends AbstractAction implements PropertyChangeListener { 151 private final TopComponent tc; 152 private Action saveAction; 153 154 public SaveDocumentAction(TopComponent tc) { 155 this.tc = tc; 156 putValue(Action.NAME, NbBundle.getMessage(ActionUtils.class, "LBL_SaveDocumentAction")); 157 saveAction = (Action)SaveAction.get(SaveAction.class); 159 putValue(Action.ACCELERATOR_KEY, saveAction.getValue(Action.ACCELERATOR_KEY)); 160 PropertyChangeListener weakL = WeakListeners.propertyChange(this, saveAction); 162 saveAction.addPropertyChangeListener(weakL); 163 setEnabled(getSaveCookie(tc) != null); 164 } 165 166 public void actionPerformed(ActionEvent evt) { 167 saveDocument(tc); 168 } 169 170 171 public void propertyChange(PropertyChangeEvent evt) { 172 if (Action.ACCELERATOR_KEY.equals(evt.getPropertyName())) { 173 putValue(Action.ACCELERATOR_KEY, saveAction.getValue(Action.ACCELERATOR_KEY)); 174 } 175 } 176 177 } 179 private static class CloneDocumentAction extends AbstractAction { 180 private final TopComponent tc; 181 public CloneDocumentAction(TopComponent tc) { 182 this.tc = tc; 183 putValue(Action.NAME, NbBundle.getMessage(ActionUtils.class, "LBL_CloneDocumentAction")); 184 setEnabled(tc instanceof TopComponent.Cloneable); 185 } 186 187 public void actionPerformed(ActionEvent evt) { 188 cloneWindow(tc); 189 } 190 191 } 193 public static void closeAllDocuments() { 195 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 196 Set<TopComponent> tcs = new HashSet<TopComponent>(); 197 for(Mode mode: wm.getModes()) { 198 ModeImpl modeImpl = (ModeImpl)mode; 199 if(modeImpl.getKind() == Constants.MODE_KIND_EDITOR) { 200 tcs.addAll(modeImpl.getOpenedTopComponents()); 201 } 202 } 203 204 for(TopComponent tc: tcs) { 205 tc.close(); 206 } 207 } 208 209 public static void closeAllExcept (TopComponent c) { 210 WindowManagerImpl wm = WindowManagerImpl.getInstance(); 211 Set<TopComponent> tcs = new HashSet<TopComponent>(); 212 for(Mode mode: wm.getModes()) { 213 ModeImpl modeImpl = (ModeImpl)mode; 214 if(modeImpl.getKind() == Constants.MODE_KIND_EDITOR) { 215 tcs.addAll(modeImpl.getOpenedTopComponents()); 216 } 217 } 218 219 for(TopComponent tc: tcs) { 220 if (tc != c) { 221 tc.close(); 222 } 223 } 224 } 225 226 227 228 static void closeWindow(TopComponent tc) { 229 tc.close(); 230 } 231 232 private static void saveDocument(TopComponent tc) { 233 SaveCookie sc = getSaveCookie(tc); 234 if(sc != null) { 235 try { 236 sc.save(); 237 } catch(IOException ioe) { 238 Exceptions.printStackTrace(ioe); 239 } 240 } 241 } 242 243 private static SaveCookie getSaveCookie(TopComponent tc) { 244 Lookup lookup = tc.getLookup(); 245 Object obj = lookup.lookup(SaveCookie.class); 246 if(obj instanceof SaveCookie) { 247 return (SaveCookie)obj; 248 } 249 250 return null; 251 } 252 253 static void cloneWindow(TopComponent tc) { 254 if(tc instanceof TopComponent.Cloneable) { 255 TopComponent clone = ((TopComponent.Cloneable)tc).cloneComponent(); 256 clone.open(); 257 clone.requestActive(); 258 } 259 } 260 261 static void putSharedAccelerator (Object key, Object value) { 262 sharedAccelerators.put(key, value); 263 } 264 265 static Object getSharedAccelerator (Object key) { 266 return sharedAccelerators.get(key); 267 } 268 269 } 271 272 | Popular Tags |