KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.util.NbBundle;
25 import org.openide.util.WeakListeners;
26 import org.openide.util.Mutex;
27 import org.openide.windows.TopComponent;
28 import org.netbeans.core.windows.ModeImpl;
29 import org.netbeans.core.windows.WindowManagerImpl;
30 import org.netbeans.core.windows.Constants;
31
32 import javax.swing.*;
33 import java.beans.PropertyChangeEvent JavaDoc;
34 import java.beans.PropertyChangeListener JavaDoc;
35 import java.awt.event.KeyEvent JavaDoc;
36
37
38 /**
39  * @author Tim Boudreau
40  */

41 public class CloseAllButThisAction extends AbstractAction
42 implements PropertyChangeListener JavaDoc, Runnable JavaDoc {
43
44     public CloseAllButThisAction() {
45         putValue(NAME, NbBundle.getMessage(CloseAllButThisAction.class,
46             "CTL_CloseAllButThisAction")); //NOI18N
47

48         TopComponent.getRegistry().addPropertyChangeListener(
49             WeakListeners.propertyChange(this, TopComponent.getRegistry()));
50         updateEnabled();
51     }
52     
53     private TopComponent tc;
54     public CloseAllButThisAction(TopComponent topComp) {
55         tc = topComp;
56         //Include the name in the label for the popup menu - it may be clicked over
57
//a component that is not selected
58
putValue(Action.NAME, NbBundle.getMessage(ActionUtils.class,
59         "LBL_CloseAllButThisAction")); //NOI18N
60

61     }
62
63     /** Perform the action. Sets/unsets maximzed mode. */
64     public void actionPerformed(java.awt.event.ActionEvent JavaDoc ev) {
65         TopComponent topC = tc;
66         if (topC == null) {
67             // for the updating sate action instance..
68
topC = TopComponent.getRegistry().getActivated();
69         }
70         if(topC != null) {
71             ActionUtils.closeAllExcept(topC);
72         }
73     }
74
75     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
76         if(TopComponent.Registry.PROP_ACTIVATED.equals(evt.getPropertyName())) {
77             updateEnabled();
78         }
79     }
80     
81     private void updateEnabled() {
82         Mutex.EVENT.readAccess(this);
83     }
84     
85     /** Overriden to share accelerator with
86      * org.netbeans.core.windows.actions.ActionUtils.CloseWindowAction
87      */

88     public void putValue(String JavaDoc key, Object JavaDoc newValue) {
89         if (Action.ACCELERATOR_KEY.equals(key)) {
90             ActionUtils.putSharedAccelerator("CloseAllButThis", newValue); //NOI18N
91
} else {
92             super.putValue(key, newValue);
93         }
94     }
95     
96     /** Overriden to share accelerator with
97      * org.netbeans.core.windows.actions.ActionUtils.CloseWindowAction
98      */

99     public Object JavaDoc getValue(String JavaDoc key) {
100         if (Action.ACCELERATOR_KEY.equals(key)) {
101             return ActionUtils.getSharedAccelerator("CloseAllButThis"); //NOI18N
102
} else {
103             return super.getValue(key);
104         }
105     }
106
107     public void run() {
108         TopComponent tc = TopComponent.getRegistry().getActivated();
109         ModeImpl mode = (ModeImpl)WindowManagerImpl.getInstance().findMode(tc);
110         setEnabled(tc instanceof TopComponent.Cloneable
111             && mode != null && mode.getKind() == Constants.MODE_KIND_EDITOR);
112     }
113
114 }
115
116
Popular Tags