KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > actions > ActionUtils


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
20
21 package org.netbeans.core.windows.actions;
22
23
24 import java.awt.Container JavaDoc;
25 import java.awt.event.*;
26 import java.beans.*;
27 import java.io.IOException JavaDoc;
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 /**
41  * Utility class for creating contextual actions for window system
42  * and window action handlers.
43  *
44  * @author Peter Zavadsky
45  */

46 public abstract class ActionUtils {
47     
48     private static HashMap<Object JavaDoc, Object JavaDoc> sharedAccelerators = new HashMap<Object JavaDoc, Object JavaDoc>();
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); // Separator
65
actions.add(new SaveDocumentAction(tc));
66             actions.add(new CloneDocumentAction(tc));
67             actions.add(null); // Separator
68
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             // #82053: don't include maximize action for floating (separate) views
74
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     /**** PENDING remove during merge, TabbedListener removed, instead drive directly */
90     private static Container JavaDoc slidingContext;
91     
92     public static void setSlidingContext (Container JavaDoc slidingContext) {
93         ActionUtils.slidingContext = slidingContext;
94     }
95     /******** end of PENDING **********/
96     
97     /** Auto-hide toggle action */
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         /** Chnage boolean state and delegate event to winsys through
121          * SlideController (implemented by SlideBar component)
122          */

123         public void actionPerformed(ActionEvent e) {
124             // update state and menu item
125
state = !state;
126             getMenuItem().setSelected(state);
127             // send event to winsys
128
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 JavaDoc)getValue(Action.NAME), state);
138                 //#45940 - hardwiring the shortcut UI since the actual shortcut processignb is also
139
// hardwired in AbstractTabViewDisplayerUI class.
140
// later this should be probably made customizable?
141
// -> how to get rid of the parameters passed to the action here then?
142
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, InputEvent.CTRL_DOWN_MASK));
143                 menuItem.addActionListener(this);
144             }
145             return menuItem;
146         }
147         
148     } // End of class AutoHideWindowAction
149

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             // share key accelerator with org.openide.actions.SaveAction
158
saveAction = (Action)SaveAction.get(SaveAction.class);
159             putValue(Action.ACCELERATOR_KEY, saveAction.getValue(Action.ACCELERATOR_KEY));
160             // fix of 40954 - weak listener instead of hard one
161
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         /** Keep accelerator key in sync with org.openide.actions.SaveAction */
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     } // End of class SaveDocumentAction.
178

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     } // End of class CloneDocumentAction.
192

193     // Utility methods >>
194
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 JavaDoc ioe) {
238                 Exceptions.printStackTrace(ioe);
239             }
240         }
241     }
242     
243     private static SaveCookie getSaveCookie(TopComponent tc) {
244         Lookup lookup = tc.getLookup();
245         Object JavaDoc 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 JavaDoc key, Object JavaDoc value) {
262         sharedAccelerators.put(key, value);
263     }
264     
265     static Object JavaDoc getSharedAccelerator (Object JavaDoc key) {
266         return sharedAccelerators.get(key);
267     }
268
269     // Utility methods <<
270
}
271
272
Popular Tags