Source code: org/aspectj/tools/ajde/netbeans/AJStartBrowserAction.java
1
2 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 *
4 * This file is part of the IDE support for the AspectJ(tm)
5 * programming language; see http://aspectj.org
6 *
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.1 (the "License"); you may not use this file except in
9 * compliance with the License. You may obtain a copy of the License at
10 * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
11 *
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
16 *
17 * The Original Code is AspectJ.
18 *
19 * The Initial Developer of the Original Code is Xerox Corporation. Portions
20 * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
21 * All Rights Reserved.
22 *
23 * Contributor(s): Phil Sager (psager@mb.sympatico.ca)
24 */
25
26 package org.aspectj.tools.ajde.netbeans;
27
28 import java.awt.event.*;
29 import java.awt.Image;
30 import java.awt.Component;
31 import java.awt.BorderLayout;
32
33 import javax.swing.JPanel;
34 import javax.swing.JTabbedPane;
35 import javax.swing.ImageIcon;
36 import javax.swing.SwingUtilities;
37
38 import org.netbeans.core.actions.MemoryMeterAction;
39 import org.openide.TopManager;
40 import org.openide.windows.*;
41 import org.openide.explorer.*;
42 import org.openide.explorer.view.*;
43 import org.openide.filesystems.*;
44
45 import org.openide.awt.Toolbar;
46 import org.openide.awt.ToolbarPool;
47
48 import org.openide.util.HelpCtx;
49 import org.openide.util.NbBundle;
50 import org.openide.util.actions.SystemAction;
51 import org.openide.util.actions.*;
52 import org.openide.awt.*;
53
54 import org.aspectj.ajde.*;
55 import org.aspectj.ajde.ui.swing.AjdeUIManager;
56
57 /** Action to enable/disable the Aspectj browser in the Netbeans explorer.
58 *
59 * @author Phil Sager
60 */
61 public class AJStartBrowserAction extends BooleanStateAction {
62
63 private boolean installed = false;
64 private boolean firstInstall = true;
65 private Actions.ToolbarToggleButton presenter = null;
66
67 public StructureViewTab structureViewTab = null;
68
69 /**
70 * TODO: the toolbar buttons are not enabled the first time the AJ enable
71 * button is pressed. Use cookies to enable/disable them.
72 */
73 public void performAction() {
74 try {
75 Workspace editing = org.openide.TopManager.getDefault()
76 .getWindowManager()
77 .findWorkspace("Editing"); //NOI18N
78
79 if (editing != null) {
80 editing.activate();
81 }
82 CallableSystemAction BUILD = (CallableSystemAction)SystemAction.get(AJBuildAction.class);
83 CallableSystemAction SELECT = (CallableSystemAction)SystemAction.get(AJSelectConfigurationAction.class);
84 //CallableSystemAction RUN = (CallableSystemAction)SystemAction.get(AJRunAction.class);
85 CallableSystemAction OPTIONS = (CallableSystemAction)SystemAction.get(AJOptionsAction.class);
86
87 //BUILD.set
88 MemoryMeterAction action;
89
90 if (!installed) {
91 BUILD.setEnabled(true);
92 SELECT.setEnabled(true);
93 OPTIONS.setEnabled(true);
94
95 NbManager.init();
96 structureViewTab = new StructureViewTab(NbManager.INSTANCE.getViewPanel());
97
98 org.openide.TopManager.getDefault()
99 .getWindowManager()
100 .getMainWindow()
101 .addWindowListener(
102 new java.awt.event.WindowAdapter() {
103 public void windowDeiconified(WindowEvent e) {
104 }
105 });
106 Runnable update = new Runnable() {
107 public void run() {
108 toggleStructureView(true);
109 structureViewTab.setVisible(true);
110 }
111 };
112 if (SwingUtilities.isEventDispatchThread()) {
113 update.run();
114 } else {
115 try {
116 SwingUtilities.invokeAndWait(update);
117 } catch (Exception e) {}
118 }
119 installed = true;
120 //AjdeUIManager.getDefault().getViewManager().updateView();
121 } else {
122 BUILD.setEnabled(false);
123 SELECT.setEnabled(false);
124 OPTIONS.setEnabled(false);
125
126 toggleStructureView(false);
127 installed = false;
128 }
129 if (firstInstall) {
130 org.openide.TopManager.getDefault().getRepository()
131 .addRepositoryListener(new RepositoryListener() {
132 public void fileSystemAdded(RepositoryEvent ev) {
133 //AjdeUIManager.getDefault().getViewManager().updateConfigsList();
134 }
135 public void fileSystemRemoved(RepositoryEvent ev) {
136 //AjdeUIManager.getDefault().getViewManager().updateConfigsList();
137 }
138 public void fileSystemPoolReordered(RepositoryReorderedEvent ev) {
139 // ignored
140 }
141 });
142 firstInstall = false;
143 }
144 } catch (Throwable t) {
145 Ajde.getDefault().getErrorHandler().handleError("Could not start AJDE.", t);
146 }
147 }
148
149 /** Get the current state.
150 * @return <code>true</code> if on
151 */
152 public boolean getBooleanState() {
153 return installed;
154 }
155
156 /** Set the current state.
157 * Fires a change event, which should be used to affect other components when
158 * its state is toggled.
159 * @param value <code>true</code> to turn on, <code>false</code> to turn off
160 */
161 public void setBooleanState(boolean value) {
162 performAction();
163 //if (!installed && value) {
164 //
165 //}
166 }
167
168 public java.awt.Component getToolbarPresenter() {
169 if (presenter == null) {
170 presenter = new Actions.ToolbarToggleButton (this);
171 }
172 return presenter;
173 }
174
175 public String getName() {
176 return NbBundle.getMessage(AJStartBrowserAction.class, "LBL_StartBrowserAction");
177 }
178
179 protected String iconResource() {
180 return "/org/aspectj/ajde/resources/actions/startAjde.gif";
181 }
182
183 public HelpCtx getHelpCtx() {
184 return HelpCtx.DEFAULT_HELP;
185 // If you will provide context help then use:
186 // return new HelpCtx (StartBrowserAction.class);
187 }
188
189 /**
190 * Display/Hide the Aspectj Browser window in the Explorer. This method docks
191 * the AJ Browser window into the main Explorer tabbed pane window, allowing the
192 * user to change Explorer views without disabling AJ...pjs
193 *
194 * @param show boolean value. true == show, which opens the AJ Browser window.
195 */
196 private void toggleStructureView(boolean show) {
197 try {
198 Mode ajExplore = getExplorerMode();
199 ajExplore.dockInto(structureViewTab);
200
201 if (show) {
202 structureViewTab.open();
203 structureViewTab.setName("Aspectj");
204
205 } else {
206 structureViewTab.close();
207
208 }
209
210 } catch (Exception e) {
211 Ajde.getDefault().getErrorHandler().handleError("Could not toggle structure view.", e);
212 }
213 }
214
215 /**
216 * Finds the Mode for the main Explorer window. This method uses pre-defined
217 * default mode name. These modes are defined in the core, however, they are
218 * not dependable, as they are not part of the APIs....pjs
219 * TODO: throw exception on mode == null which means there is no Explorer panel.
220 */
221 private static Mode getExplorerMode(){
222 Mode mode = null;
223 Workspace[] workspaces = org.openide.TopManager.getDefault()
224 .getWindowManager()
225 .getWorkspaces();
226
227 for (int i = 0; i < workspaces.length; i++){
228 if(workspaces[i].getName().equals("Editing")){
229 mode = workspaces[i].findMode("explorer");
230 return mode;
231 }
232 }
233 return mode; // null...
234 }
235
236 /** Perform extra initialization of this action's singleton.
237 * PLEASE do not use constructors for this purpose! */
238 protected void initialize () {
239 super.setEnabled(true);
240 }
241
242
243 static class StructureViewComponent extends TopComponent {
244 public StructureViewComponent(JPanel panel) {
245 this.setName("AspectJ Browser");
246 this.setLayout(new BorderLayout());
247 this.add(panel, BorderLayout.CENTER);
248 this.add(panel);
249 }
250
251 public Image getIcon() {
252 return null; //((ImageIcon)ForteIcons.INSTANCE.getStartAjdeIcon()).getImage();
253 }
254
255 public SystemAction[] getSystemActions() {
256 return new SystemAction[0];
257 }
258 }
259
260 static class StructureViewTab extends ExplorerPanel {
261 public StructureViewTab(JPanel panel) {
262 this.setLayout(new BorderLayout());
263 this.add(panel, BorderLayout.CENTER);
264 }
265
266
267 public Image getIcon() {
268 return ((ImageIcon)NbIconRegistry.INSTANCE.getStartAjdeIcon()).getImage();
269 }
270 }
271 }