KickJava   Java API By Example, From Geeks To Geeks.

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


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.windows.TopComponent;
27
28 import javax.swing.*;
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31
32
33 /**
34  * @author Peter Zavadsky
35  */

36 public class CloseWindowAction extends AbstractAction
37 implements PropertyChangeListener JavaDoc {
38
39     public CloseWindowAction() {
40         putValue(NAME, NbBundle.getMessage(CloseWindowAction.class, "CTL_CloseWindowAction"));
41         TopComponent.getRegistry().addPropertyChangeListener(
42             WeakListeners.propertyChange(this, TopComponent.getRegistry()));
43         updateEnabled();
44     }
45     
46     private TopComponent tc;
47     // dno't update enable state, is tied to one component only
48
public CloseWindowAction(TopComponent topcomp) {
49         tc = topcomp;
50         //Include the name in the label for the popup menu - it may be clicked over
51
//a component that is not selected
52
putValue(Action.NAME, NbBundle.getMessage(ActionUtils.class,
53         "LBL_CloseWindowAction")); //NOI18N
54
setEnabled(true);
55     }
56     
57     
58     /** Perform the action. Sets/unsets maximzed mode. */
59     public void actionPerformed(java.awt.event.ActionEvent JavaDoc ev) {
60         TopComponent topC = tc;
61         if (topC == null) {
62             // the updating instance will get the TC to close from winsys
63
topC = TopComponent.getRegistry().getActivated();
64         }
65         if(topC != null) {
66             ActionUtils.closeWindow(topC);
67         }
68     }
69
70     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
71         if(TopComponent.Registry.PROP_ACTIVATED.equals(evt.getPropertyName())) {
72             updateEnabled();
73         }
74     }
75     
76     private void updateEnabled() {
77         setEnabled(TopComponent.getRegistry().getActivated() != null);
78     }
79     
80     /** Overriden to share accelerator with
81      * org.netbeans.core.windows.actions.ActionUtils.CloseWindowAction
82      */

83     public void putValue(String JavaDoc key, Object JavaDoc newValue) {
84         if (Action.ACCELERATOR_KEY.equals(key)) {
85             ActionUtils.putSharedAccelerator("CloseWindow", newValue);
86         } else {
87             super.putValue(key, newValue);
88         }
89     }
90     
91     /** Overriden to share accelerator with
92      * org.netbeans.core.windows.actions.ActionUtils.CloseWindowAction
93      */

94     public Object JavaDoc getValue(String JavaDoc key) {
95         if (Action.ACCELERATOR_KEY.equals(key)) {
96             return ActionUtils.getSharedAccelerator("CloseWindow");
97         } else {
98             return super.getValue(key);
99         }
100     }
101     
102 }
103
104
Popular Tags