KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.event.ActionEvent JavaDoc;
23 import javax.swing.AbstractAction JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import org.netbeans.core.windows.ModeImpl;
26 import org.netbeans.core.windows.WindowManagerImpl;
27 import org.openide.util.HelpCtx;
28 import org.openide.util.NbBundle;
29 import org.openide.util.actions.CallableSystemAction;
30 import org.openide.windows.TopComponent;
31 import org.openide.windows.WindowManager;
32
33 /**
34  * Action perform undock or dock, either of given or active top component.
35  * Undock means that TopCompoment is moved to new, separate floating window,
36  * Dock means move into main window area.
37  *
38  */

39 public final class UndockWindowAction extends AbstractAction JavaDoc {
40
41     private final TopComponent tc;
42
43     /**
44      * Creates instance of action to Undock/Dock of currently active top
45      * component in the system. For use in main menu.
46      */

47     public UndockWindowAction () {
48         this.tc = null;
49     }
50
51     /**
52      * Undock/Dock of given TopComponent.
53      * For use in the context menus.
54      */

55     public UndockWindowAction (TopComponent tc) {
56         this.tc = tc;
57     }
58     
59     public void actionPerformed (ActionEvent JavaDoc e) {
60         // contextTC shound never be null thanks to isEnabled impl
61
WindowManagerImpl wmi = WindowManagerImpl.getInstance();
62         TopComponent contextTC = getTC2WorkWith();
63         boolean isDocked = wmi.isDocked(contextTC);
64         int kind = ((ModeImpl)wmi.findMode(contextTC)).getKind();
65
66         if (isDocked) {
67             wmi.userUndockedTopComponent(contextTC, kind);
68         } else {
69             wmi.userDockedTopComponent(contextTC, kind);
70         }
71     }
72     
73     /** Overriden to share accelerator between instances of this action.
74      */

75     public void putValue(String JavaDoc key, Object JavaDoc newValue) {
76         if (Action.ACCELERATOR_KEY.equals(key)) {
77             ActionUtils.putSharedAccelerator("UndockWindowAction", newValue); //NOI18N
78
} else {
79             super.putValue(key, newValue);
80         }
81     }
82
83     /** Overriden to share accelerator between instances of this action.
84      */

85     public Object JavaDoc getValue(String JavaDoc key) {
86         if (Action.ACCELERATOR_KEY.equals(key)) {
87             return ActionUtils.getSharedAccelerator("UndockWindowAction"); //NOI18N
88
} else {
89             return super.getValue(key);
90         }
91     }
92
93     public boolean isEnabled() {
94         updateName();
95         return getTC2WorkWith() != null;
96     }
97
98     private void updateName() {
99         TopComponent contextTC = getTC2WorkWith();
100         boolean isDocked = contextTC != null ? WindowManagerImpl.getInstance().isDocked(contextTC) : true;
101         putValue(Action.NAME,
102                 NbBundle.getMessage(UndockWindowAction.class,
103                 isDocked ? "CTL_UndockWindowAction" : "CTL_UndockWindowAction_Dock"));
104     }
105
106     private TopComponent getTC2WorkWith () {
107         if (tc != null) {
108             return tc;
109         }
110         return WindowManager.getDefault().getRegistry().getActivated();
111     }
112
113 }
114
Popular Tags