KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.core.windows.actions;
21
22
23 import org.netbeans.core.windows.Constants;
24 import org.netbeans.core.windows.ModeImpl;
25 import org.netbeans.core.windows.WindowManagerImpl;
26 import org.openide.awt.JMenuPlus;
27 import org.openide.util.NbBundle;
28 import org.openide.util.WeakListeners;
29 import org.openide.util.actions.Presenter;
30 import org.openide.windows.TopComponent;
31 import org.openide.windows.WindowManager;
32
33 import javax.swing.*;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.beans.PropertyChangeEvent JavaDoc;
36 import java.beans.PropertyChangeListener JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Set JavaDoc;
39
40
41 /**
42  * Action which allows to change layout of windows.
43  *
44  * @author Peter Zavadsky
45  */

46 public class AttachWindowAction extends AbstractAction
47 implements Presenter.Menu {
48
49
50     // For this class only.
51
private static final String JavaDoc PROP_ACTION_CHANGED = "actionChanged"; // NOI18N
52

53     private final PropertyChangeListener JavaDoc propListener;
54     
55     
56     public AttachWindowAction() {
57         propListener = new PropertyChangeListener JavaDoc() {
58             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
59                 String JavaDoc propName = evt.getPropertyName();
60                 if(TopComponent.Registry.PROP_ACTIVATED.equals(propName)
61                  || WindowManager.PROP_MODES.equals(propName)
62                  || WindowManagerImpl.PROP_EDITOR_AREA_STATE.equals(propName)) {
63                     updateState();
64                 }
65             }
66         };
67         TopComponent.Registry registry = TopComponent.getRegistry();
68         registry.addPropertyChangeListener(WeakListeners.propertyChange(propListener, registry));
69         
70         WindowManagerImpl.getInstance().addPropertyChangeListener(
71             WeakListeners.propertyChange(propListener, WindowManagerImpl.getInstance()));
72         
73         updateState();
74     }
75     
76     public void actionPerformed(ActionEvent JavaDoc evt) {}
77     
78     private void updateState() {
79         TopComponent active = TopComponent.getRegistry().getActivated();
80         Object JavaDoc param = active == null ? "" : active.getName(); // NOI18N
81
putValue(Action.NAME, NbBundle.getMessage(MaximizeWindowAction.class, "CTL_AttachWindowAction", param));
82
83         // XXX In separated state, the action should be present,
84
// when achieved that, remove this kind of code.
85
if(WindowManagerImpl.getInstance().getEditorAreaState() == Constants.EDITOR_AREA_SEPARATED) {
86             setEnabled(false);
87         } else {
88             setEnabled(active != null);
89         }
90
91         // So the presenter knows when to update itself.
92
firePropertyChange(PROP_ACTION_CHANGED, null, null);
93     }
94
95
96     /** Implements <code>Presenter.Menu</code>. */
97     public JMenuItem getMenuPresenter() {
98         JMenuItem mi = new LazyPopup(this);
99         org.openide.awt.Actions.connect(mi, this, false);
100         return mi;
101     }
102     
103     
104     /** Menu item which will create its items lazilly when the popup will becomming visible.
105      * Performance savings.*/

106     private static class LazyPopup extends JMenuPlus implements PropertyChangeListener JavaDoc {
107         
108         /** Indicates whether menu items were created. */
109         private boolean created = false;
110
111         
112         /** Constructor. */
113         public LazyPopup(Action action) {
114             setText((String JavaDoc)action.getValue(Action.NAME));
115             
116             action.addPropertyChangeListener(WeakListeners.propertyChange(this, action));
117         }
118
119         
120         /** Gets popup menu. Overrides superclass. Adds lazy menu items creation. */
121         public JPopupMenu getPopupMenu() {
122             if(!created) {
123                 createMenuItems();
124             }
125             
126             return super.getPopupMenu();
127         }
128
129         /** Creates items when actually needed. */
130         private void createMenuItems() {
131             created = true;
132             removeAll();
133
134             ModeImpl editorMode = (ModeImpl)WindowManagerImpl.getInstance().findMode("editor"); // NOI18N
135
if(editorMode != null) {
136                 add(new ModeMenu(editorMode, NbBundle.getMessage(AttachWindowAction.class, "CTL_Documents"), true));
137                 add(new JSeparator());
138             }
139
140             Set JavaDoc modeSet = WindowManagerImpl.getInstance().getModes();
141             
142             for(Iterator JavaDoc it = modeSet.iterator(); it.hasNext(); ) {
143                 ModeImpl mode = (ModeImpl)it.next();
144                 
145                 if("editor".equals(mode.getName())) { // NOI18N
146
continue;
147                 }
148
149                 if(!mode.getOpenedTopComponents().isEmpty()) {
150                     TopComponent tc = mode.getSelectedTopComponent();
151                     if(tc != null) {
152                         String JavaDoc name = tc.getName();
153                         add(new ModeMenu(mode, name == null ? "null" : name, false)); // NOI18N
154
}
155                 }
156             }
157         }
158
159         /** Implements <code>PropertyChangeListener</code>. */
160         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
161             if(Action.NAME.equals(evt.getPropertyName())) {
162                 setText((String JavaDoc)evt.getNewValue());
163             } else if(PROP_ACTION_CHANGED.equals(evt.getPropertyName())) { // NOI18N
164
created = false;
165             }
166         }
167     } // End of class LazyPopup.
168

169     
170     /** Sub menu for each mode. */
171     private static class ModeMenu extends JMenu {
172         public ModeMenu(ModeImpl mode, String JavaDoc name, boolean editor) {
173             super(name);
174
175             TopComponent active = TopComponent.getRegistry().getActivated();
176             if(mode.canContain(active)) {
177                 if(!editor) {
178                     add(new AttachAction(mode, null));
179                     add(new JSeparator());
180                 }
181
182                 add(new AttachAction(mode, Constants.TOP));
183                 add(new AttachAction(mode, Constants.LEFT));
184                 add(new AttachAction(mode, Constants.BOTTOM));
185                 add(new AttachAction(mode, Constants.RIGHT));
186             } else {
187                 setEnabled(false);
188             }
189         }
190     } // End of ModeMenu class.
191

192
193     /** Action which acutally performs the attaching of the component. */
194     private static class AttachAction extends AbstractAction {
195         private final ModeImpl mode;
196         private final String JavaDoc side;
197         
198         public AttachAction(ModeImpl mode, String JavaDoc side) {
199             this.mode = mode;
200             this.side = side;
201             
202             String JavaDoc key;
203             if(side == null) {
204                 key = "CTL_SideAsLastTab"; // NOI18N
205
TopComponent active = TopComponent.getRegistry().getActivated();
206                 if(mode.getOpenedTopComponents().contains(active)) {
207                     setEnabled(false);
208                 }
209             } else if(side == Constants.TOP) {
210                 key = "CTL_SideTop"; // NOI18N
211
} else if(side == Constants.LEFT) {
212                 key = "CTL_SideLeft"; // NOI18N
213
} else if(side == Constants.BOTTOM) {
214                 key = "CTL_SideBottom"; // NOI18N
215
} else if(side == Constants.RIGHT) {
216                 key = "CTL_SideRight"; // NOI18N
217
} else {
218                 return;
219             }
220             
221             putValue(Action.NAME, NbBundle.getMessage(AttachWindowAction.class, key));
222         }
223         
224         public void actionPerformed(ActionEvent JavaDoc evt) {
225             WindowManagerImpl wm = WindowManagerImpl.getInstance();
226             ModeImpl activeMode = wm.getActiveMode();
227             if(activeMode == null) {
228                 return;
229             }
230
231             TopComponent selected = activeMode.getSelectedTopComponent();
232             if(selected == null || !mode.canContain(selected)) {
233                 return;
234             }
235
236             if(side == null) {
237                 mode.dockInto(selected);
238                 selected.open();
239                 selected.requestActive();
240             } else {
241                 wm.attachTopComponentToSide(selected, mode, side);
242             }
243         }
244     } // End of class AttachAction.
245

246 }
247
248
Popular Tags