KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > ToggleViewAction


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 package org.netbeans.modules.tasklist.core;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import javax.swing.SwingUtilities JavaDoc;
25 import org.netbeans.modules.tasklist.core.*;
26 import org.openide.awt.Actions;
27
28 import org.openide.util.HelpCtx;
29 import org.openide.util.actions.BooleanStateAction;
30 import org.openide.util.actions.Presenter;
31 import org.openide.windows.Mode;
32 import org.openide.windows.TopComponent;
33 import org.openide.windows.WindowManager;
34
35 /**
36  * Shows/closes a view.
37  */

38 public abstract class ToggleViewAction extends BooleanStateAction implements
39 PropertyChangeListener JavaDoc, Presenter.Menu {
40
41     private static final long serialVersionUID = 1;
42
43     private boolean block;
44     private String JavaDoc mode = "output"; // NOI18N
45
private WeakReference JavaDoc activated;
46     
47     public javax.swing.JMenuItem JavaDoc getMenuPresenter() {
48         return new Actions.MenuItem(this, true);
49     }
50
51     public void setBooleanState(final boolean value) {
52         super.setBooleanState(value);
53         if (block)
54             return;
55         block = true;
56         
57         // XXX is it realy called from not AWT thread?
58
SwingUtilities.invokeLater(new Runnable JavaDoc() {
59             public void run() {
60                 toggleView(value);
61                 block = false;
62             }
63         });
64     }
65
66     /**
67      * Should return the view to be shown
68      *
69      * @return the view
70      */

71     protected abstract TopComponent getView();
72
73     /**
74      * Returns true if the view is visible (on the current workspace).
75      * This method could be overriden to don't create the view if it is not
76      * visible.
77      *
78      * @return true = the view is opened on the current workspace
79      */

80     protected boolean isViewOpened() {
81         return getView().isOpened();
82     }
83     
84     /**
85      * Closes/opens the view
86      *
87      * @param visible true = open the view
88      */

89     private void toggleView(boolean visible) {
90         if (visible == isViewOpened())
91             return;
92
93         TopComponent view = getView();
94         WindowManager wm = WindowManager.getDefault();
95         if (!visible) {
96             Mode mode = wm.findMode(view);
97             if (mode != null)
98                 this.mode = mode.getName();
99             view.close();
100             if (activated != null) {
101                 TopComponent act = (TopComponent) activated.get();
102                 if (act != null)
103                     act.requestActive();
104             }
105         } else {
106             TopComponent act = WindowManager.getDefault().
107                 getRegistry().getActivated();
108             if (act == null)
109                 activated = null;
110             else
111                 activated = new WeakReference JavaDoc(act);
112             Mode mode = wm.findMode(this.mode);
113             if (mode != null) {
114                 mode.dockInto(view);
115             }
116             view.open();
117             view.requestVisible();
118             view.requestActive();
119         }
120     }
121
122     protected void initialize() {
123         super.initialize();
124         WindowManager.getDefault().addPropertyChangeListener(this);
125         WindowManager.getDefault().getRegistry().addPropertyChangeListener(this);
126         putProperty(PROP_BOOLEAN_STATE, Boolean.valueOf(isViewOpened()));
127     }
128     
129     public HelpCtx getHelpCtx() {
130         return HelpCtx.DEFAULT_HELP;
131     }
132
133     public void propertyChange(PropertyChangeEvent JavaDoc e) {
134         String JavaDoc p = e.getPropertyName();
135         if (p.equals(TopComponent.Registry.PROP_OPENED)) {
136             super.setBooleanState(isViewOpened());
137         }
138     }
139 }
140
Popular Tags