KickJava   Java API By Example, From Geeks To Geeks.

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


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.Actions;
27 import org.openide.util.NbBundle;
28 import org.openide.util.WeakListeners;
29 import org.openide.windows.TopComponent;
30
31 import javax.swing.*;
32 import java.awt.*;
33 import java.beans.PropertyChangeEvent JavaDoc;
34 import java.beans.PropertyChangeListener JavaDoc;
35
36
37 /** An action that can toggle maximized window system mode for specific window.
38  *
39  * @author Peter Zavadsky
40  */

41 public class MaximizeWindowAction extends AbstractAction {
42
43     private final PropertyChangeListener JavaDoc propListener;
44     private TopComponent topComponent;
45     private boolean isPopup;
46     
47     public MaximizeWindowAction() {
48         propListener = new PropertyChangeListener JavaDoc() {
49             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
50                 String JavaDoc propName = evt.getPropertyName();
51                 if(WindowManagerImpl.PROP_MAXIMIZED_MODE.equals(propName)
52                 || WindowManagerImpl.PROP_EDITOR_AREA_STATE.equals(evt.getPropertyName())
53                 || WindowManagerImpl.PROP_ACTIVE_MODE.equals(evt.getPropertyName())) {
54                     updateState();
55                 }
56                 // #64876: correctly initialize after startup
57
if (TopComponent.Registry.PROP_ACTIVATED.equals(propName)) {
58                     updateState();
59                 }
60             }
61         };
62         TopComponent.Registry registry = TopComponent.getRegistry();
63         registry.addPropertyChangeListener(WeakListeners.propertyChange(propListener, registry));
64         WindowManagerImpl wm = WindowManagerImpl.getInstance();
65         wm.addPropertyChangeListener(WeakListeners.propertyChange(propListener, wm));
66         
67         updateState();
68     }
69     /**
70      * Alternate constructor to maximize given specific TopComponent.
71      * For use in the context menu and maximization on demand,
72      * invoked from ActionUtils and TabbedHandler.
73      *
74      * see #38801 for details
75      */

76     public MaximizeWindowAction (TopComponent tc) {
77         topComponent = tc;
78         propListener = null;
79         isPopup = true;
80         updateState();
81     }
82     
83     /** Perform the action. Sets/unsets maximzed mode. */
84     public void actionPerformed (java.awt.event.ActionEvent JavaDoc ev) {
85         WindowManagerImpl wm = WindowManagerImpl.getInstance();
86         TopComponent curTC = getTCToWorkWith();
87         
88         if(wm.isDocked(curTC)) {
89             // inside main window
90
ModeImpl mode = (ModeImpl)wm.findMode(curTC);
91             String JavaDoc tcID = wm.findTopComponentID( curTC );
92             
93             if( mode.getKind() == Constants.MODE_KIND_SLIDING ) {
94                 //maximize/restore slided-in window
95
wm.userToggledTopComponentSlideInMaximize( tcID );
96             } else if( null != mode ) {
97                 ModeImpl previousMax = wm.getCurrentMaximizedMode();
98                 if( null != previousMax ) {
99                     if( previousMax.getKind() == Constants.MODE_KIND_EDITOR && mode.getKind() == Constants.MODE_KIND_VIEW ) {
100                         wm.switchMaximizedMode( mode );
101                     } else {
102                         wm.switchMaximizedMode( null );
103                     }
104                 } else {
105                     wm.switchMaximizedMode( mode );
106                 }
107             } else {
108                 wm.switchMaximizedMode( null );
109             }
110         } else {
111             // separate windows
112
ModeImpl curMax = (ModeImpl)wm.findMode(curTC);
113             if (curMax != null) {
114                 if(curMax.getFrameState() == Frame.NORMAL) {
115                     curMax.setFrameState(Frame.MAXIMIZED_BOTH);
116                 } else {
117                     curMax.setFrameState(Frame.NORMAL);
118                 }
119             }
120         }
121         
122         updateState();
123     }
124
125     /** Updates state of this action, may be called from non-AWT thread.
126      * #44825 - Shortcuts folder can call our constructor from non-AWT thread.
127      */

128     private void updateState() {
129         if (SwingUtilities.isEventDispatchThread()) {
130             doUpdateState();
131         } else {
132             SwingUtilities.invokeLater(new Runnable JavaDoc() {
133                 public void run() {
134                     doUpdateState();
135                 }
136             });
137         }
138     }
139     
140     /** Updates state and text of this action.
141      */

142     private void doUpdateState() {
143         WindowManagerImpl wm = WindowManagerImpl.getInstance();
144         TopComponent active = getTCToWorkWith();
145         Object JavaDoc param = active == null ? "" : active.getName(); // NOI18N
146
boolean maximize;
147         ModeImpl activeMode = (ModeImpl)wm.findMode(active);
148         if (activeMode == null) {
149             return;
150         }
151
152         if (wm.isDocked(active)) {
153             maximize = wm.getCurrentMaximizedMode() != activeMode;
154         } else {
155             maximize = activeMode.getFrameState() == Frame.NORMAL;
156         }
157         
158         if (activeMode != null && activeMode.getKind() == Constants.MODE_KIND_SLIDING) {
159             maximize = null != active && !wm.isTopComponentMaximizedWhenSlidedIn( wm.findTopComponentID( active ) );
160         }
161
162         String JavaDoc label;
163         if(maximize) {
164             label = NbBundle.getMessage(MaximizeWindowAction.class, "CTL_MaximizeWindowAction", param);
165         } else {
166             label = NbBundle.getMessage(MaximizeWindowAction.class, "CTL_UnmaximizeWindowAction", param);
167         }
168         putValue(Action.NAME, (isPopup ? Actions.cutAmpersand(label) : label));
169         
170         setEnabled(activeMode != null /*&& activeMode.getKind() != Constants.MODE_KIND_SLIDING*/);
171     }
172     
173     private TopComponent getTCToWorkWith () {
174         if (topComponent != null) {
175             return topComponent;
176         }
177         return TopComponent.getRegistry().getActivated();
178     }
179     
180     /** Overriden to share accelerator between instances of this action.
181      */

182     public void putValue(String JavaDoc key, Object JavaDoc newValue) {
183         if (Action.ACCELERATOR_KEY.equals(key)) {
184             ActionUtils.putSharedAccelerator("MaximizeWindow", newValue);
185         } else {
186             super.putValue(key, newValue);
187         }
188     }
189
190     /** Overriden to share accelerator between instances of this action.
191      */

192     public Object JavaDoc getValue(String JavaDoc key) {
193         if (Action.ACCELERATOR_KEY.equals(key)) {
194             return ActionUtils.getSharedAccelerator("MaximizeWindow");
195         } else {
196             return super.getValue(key);
197         }
198     }
199     
200 }
201
202
Popular Tags