Source code: org/aspectj/tools/ajde/jbuilder/JBuilderManager.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):
24 */
25
26 package org.aspectj.tools.ajde.jbuilder;
27
28 import java.io.*;
29 import java.util.*;
30 import java.awt.*;
31 import java.awt.event.*;
32 import javax.swing.*;
33 import com.borland.primetime.ide.*;
34 import com.borland.primetime.actions.*;
35 import com.borland.primetime.node.*;
36 import com.borland.jbuilder.node.*;
37 import com.borland.primetime.editor.*;
38 import com.borland.primetime.ide.*;
39 import com.borland.primetime.vfs.*;
40 import com.borland.jbuilder.runtime.*;
41 import com.borland.primetime.runtime.*;
42 import com.borland.primetime.viewer.*;
43 import com.borland.primetime.properties.*;
44 import org.aspectj.ajde.*;
45 import org.aspectj.ajde.ui.*;
46 import org.aspectj.ajde.ui.swing.*;
47 import org.aspectj.ajde.internal.*;
48 import org.aspectj.asm.*;
49
50 /**
51 * @author Mik Kersten
52 */
53 public class JBuilderManager {
54
55 protected static JBuilderManager INSTANCE = new JBuilderManager();
56
57 private boolean navigationSeekPerformed = false;
58
59 private static final JBuilderIconRegistry ICON_REGISTRY = new JBuilderIconRegistry();
60 private JBuilderPreferencesAdapter preferencesAdapter;
61
62 /**
63 * Must be static for plug-in initialization reasons.
64 */
65 public static IconRegistry getJBuilderIconRegistry() {
66 return ICON_REGISTRY;
67 }
68
69 private final StructureModelListener MODEL_LISTENER = new StructureModelListener() {
70 public void modelUpdated(StructureModel model) {
71 clearCurrentGutter();
72 updateInlineAnnotations();
73 }
74 };
75
76 protected JBuilderManager() {
77 this.INSTANCE = this;
78 }
79
80 public JBuilderManager getJBDefault() {
81 return INSTANCE;
82 }
83
84 private boolean isInitialized = false;
85 private boolean isEnabled = false;
86
87 public static final String AJRT_LIB_NAME = "AspectJ Runtime";
88 private JDialog compileDialog = null;
89 private JPanel structureViewPanel = null;
90 private Component jBuilderStructureComponent = null;
91 private JBuilderEditorAdapter editorAdapter;
92
93 // TODO: move to TopManager
94 JBuilderPropertiesAdapter projectProperties = null;
95
96 public void init() {
97 if (isInitialized) return;
98
99 editorAdapter = new JBuilderEditorAdapter();
100 JBuilderTaskListManager compilerMessages = new JBuilderTaskListManager();
101 BuildProgressMonitor buildProgressMonitor = new DefaultBuildProgressMonitor(getRootFrame());
102 ErrorHandler errorHandler = new AjdeErrorHandler();
103 IdeUIAdapter uiAdapter = new JBuilderUIAdapter();
104 preferencesAdapter = new JBuilderPreferencesAdapter();
105 projectProperties = new JBuilderPropertiesAdapter(preferencesAdapter);
106
107 AjdeUIManager.getDefault().init(
108 editorAdapter,
109 compilerMessages,
110 projectProperties,
111 preferencesAdapter,
112 uiAdapter,
113 ICON_REGISTRY,
114 getRootFrame(),
115 true);
116
117 AjdeUIManager.getDefault().getOptionsFrame().addOptionsPanel(new JBuilderOptionsPanel());
118 Ajde.getDefault().getStructureModelManager().addListener(MODEL_LISTENER);
119 isInitialized = true;
120 Ajde.getDefault().getBuildManager().addListener(Listeners.COMPILER_LISTENER);
121 structureViewPanel = AjdeUIManager.getDefault().getFileStructurePanel();
122 //Ajde.getDefault().enableLogging(System.out);
123 verifyRuntimeLibraryExists();
124 }
125
126 public JBuilderPropertiesAdapter getProjectProperties() {
127 return projectProperties;
128 }
129
130 public void saveAll() {
131 BrowserFile.ACTION_NodeSaveAll.actionPerformed(Browser.getActiveBrowser());
132 }
133
134 public void showMessages() {
135 // ignored
136 }
137
138 public void hideMessages() {
139 // ignored
140 }
141
142 public void removeConfigFile(String configFile) {
143
144 }
145
146 public Frame getRootFrame() {
147 return Browser.getActiveBrowser();
148 }
149
150 public void updateStructureView() {
151 Node activeNode = Browser.getActiveBrowser().getActiveNode();
152 // if (activeNode != null) {
153 // AjdeUIManager.getDefault().getViewManager().updateView();
154 // }
155 jBuilderStructureComponent = Browser.getActiveBrowser().getStructureView().getComponent(0);
156 Browser.getActiveBrowser().getStructureView().setStructureComponent(structureViewPanel);
157 }
158
159 public void setStructureView(JPanel viewPanel) {
160 structureViewPanel = viewPanel;
161 updateStructureView();
162 }
163
164 public void resetStructureView() {
165 Browser.getActiveBrowser().setStructurePaneVisible(true);
166 }
167
168 public void updateCompileActions() {
169 Install.ajcCompileActionsGroup.removeAll();
170 java.util.List buildConfigs = Ajde.getDefault().getProjectProperties().getBuildConfigFiles();
171 int defaultIndex = 0;
172 if (buildConfigs.size() == 0) {
173 buildConfigs.add(Ajde.getDefault().getProjectProperties().getDefaultBuildConfigFile());
174 }
175 for (int i = 0; i < buildConfigs.size(); i++) {
176 final String config = (String)buildConfigs.get(i);
177 final int currIndex = i;
178 if (config.equals(Ajde.getDefault().getConfigurationManager().getActiveConfigFile())) {
179 defaultIndex = i;
180 }
181
182 Install.ajcCompileActionsGroup.add(
183 new BrowserAction(config, 'c', "Compile configuration: " + config + " using ajc...", BrowserIcons.ICON_FILETEXT) {
184 public void actionPerformed(Browser browser) {
185 JBuilderManager.INSTANCE.saveAll();
186 Ajde.getDefault().getConfigurationManager().setActiveConfigFile(config);
187 JBuilderManager.INSTANCE.getProjectProperties().setLastActiveBuildConfigFile(config);
188 Ajde.getDefault().getBuildManager().build();
189 Install.ajcCompileActionsGroup.setDefaultAction(currIndex);
190 }
191 });
192 }
193 Install.ajcCompileActionsGroup.setDefaultAction(defaultIndex);
194 Install.compilePopupMenu = new ActionPopupMenu(null, Install.ajcCompileActionsGroup);
195 }
196
197 /**
198 * @todo fix and make this generic
199 */
200 public void updateInlineAnnotations() {
201 if (Browser.getActiveBrowser().getActiveNode() == null) return;
202 try {
203 String filePath = Browser.getActiveBrowser().getActiveNode().getLongDisplayName();
204
205 Map annotationsMap = StructureModelManager.INSTANCE.getInlineAnnotations(
206 filePath,
207 projectProperties.getCodeInlineAnnotations(),
208 projectProperties.getMemberAndTypeInlineAnnotations()
209 );
210
211 if (annotationsMap == null) return;
212 Set keys = annotationsMap.keySet();
213 for (Iterator it = keys.iterator(); it.hasNext(); ) {
214 Integer lineNumber = (Integer)it.next();
215 java.util.List annotations = (java.util.List)annotationsMap.get(lineNumber);
216 Ajde.getDefault().getEditorManager().showSourcelineAnnotation(filePath, lineNumber.intValue(), annotations);
217 }
218 } catch (Exception e) {
219 Ajde.getDefault().getErrorHandler().handleError("Could not update inline annotations.", e);
220 }
221 }
222
223 public void clearCurrentGutter() {
224 Gutter gutter = getCurrGutter();
225 if (gutter == null) return;
226
227 for (int l = 1; l < gutter.getEditor().getLineCount(); l++) {
228 LineMark[] marks = gutter.getMarks(l);
229 if (marks != null) {
230 for (int i = 0; i < marks.length; i++) {
231 if (marks[i] instanceof AjdeGutterMark) {
232 gutter.removeMark(marks[i]);
233 }
234 }
235 }
236 }
237 }
238
239 public Gutter getCurrGutter() {
240 try {
241 TextNodeViewer viewer = (TextNodeViewer)Browser.getActiveBrowser().getViewerOfType(
242 Browser.getActiveBrowser().getActiveNode(),
243 Class.forName("com.borland.primetime.viewer.TextNodeViewer"));
244 return ((TextViewerComponent)viewer.getViewerComponent()).getMainView().getGutter();
245 } catch (ClassNotFoundException cnfe) {
246 return null;
247 } catch (NullPointerException npe) {
248 return null;
249 }
250 }
251
252 public String getDumpFilePath() {
253 return projectProperties.getRootProjectDir() + '/' +
254 projectProperties.getProjectName().substring(0, projectProperties.getProjectName().lastIndexOf('.')) + ".ser";
255 }
256
257 public boolean getNavigationSeekPerformed() {
258 return navigationSeekPerformed;
259 }
260
261 public void setNavigationSeekPerformed(boolean navigationSeekHasBenePerformed) {
262 this.navigationSeekPerformed = navigationSeekHasBenePerformed;
263 }
264
265 public boolean isEnabled() {
266 return isEnabled;
267 }
268
269 public void setEnabled(boolean isEnabled) {
270 this.isEnabled = isEnabled;
271 }
272
273 String getRuntimeClasspath() {
274 try {
275 return new File(PropertyManager.getInstallRootUrl().getFile() + "/lib/ext/aspectjrt.jar").getCanonicalPath();
276 } catch (IOException ioe) {
277 return "";
278 }
279 }
280
281 void verifyRuntimeLibraryExists() {
282 File libFile = new File(
283 PropertyManager.getSettingsRootUrl().getFile()
284 + File.separator
285 + AJRT_LIB_NAME
286 + ".library");
287 if (!libFile.exists()) {
288 final String LIB_FILE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
289 + "<library>\n"
290 + " <!--JBuilder Library Definition File-->\n"
291 + " <fullname>" + AJRT_LIB_NAME + "</fullname>\n"
292 + " <class>\n"
293 + " <path>[" + getRuntimeClasspath() + "]</path>\n"
294 + " </class>\n"
295 + "</library>\n";
296 try {
297 FileOutputStream fos = new FileOutputStream(libFile);
298 fos.write(LIB_FILE.getBytes());
299 fos.close();
300 } catch (IOException ioe) {
301 Ajde.getDefault().getErrorHandler().handleWarning("Could not add AspectJ Runtime library. Please add it manually.");
302 }
303 }
304 verifyProjectRuntimeLibrary();
305 }
306
307 void verifyProjectRuntimeLibrary() {
308 String libraries = Browser.getActiveBrowser().getActiveProject().getProperty(
309 "sys",
310 "Libraries",
311 "");
312
313 if (libraries.indexOf(JBuilderManager.AJRT_LIB_NAME) == -1) {
314 libraries = libraries + ";" + JBuilderManager.AJRT_LIB_NAME;
315 }
316
317 Browser.getActiveBrowser().getActiveProject().setProperty(
318 "sys",
319 "Libraries",
320 libraries
321 );
322
323 // Browser.getActiveBrowser().getActiveProject().setProperty(
324 // "runtime.0",
325 // "BuildTargetOnRun",
326 // "<None>"
327 // );
328 // Browser.getActiveBrowser().getActiveProject().setProperty(
329 // "debug.0",
330 // "BuildTargetOnDebug",
331 // "<None>"
332 // );
333 saveAll();
334 }
335
336 public EditorAdapter getEditorAdapter() {
337 return editorAdapter;
338 }
339
340 }
341
342 // private void swallowDebugger(JPanel panel) {
343 // MessageView messageView = Browser.getActiveBrowser().getMessageView();
344 //// MessageCategory[] categories = messageView.getTabs();
345 //// for (int i = 0; i < categories.length; i++) {
346 //// if (categories[i].getTitle().equals("ajdb")) {
347 //// try {
348 //// messageView.removeTab(categories[i]);
349 //// } catch (Exception e) {}
350 //// }
351 //// }
352 //// MessageCategory messageCategory = new MessageCategory("launching");
353 // messageView.add(panel);
354 // messageView.setVisible(true);
355 //
356 // }
357
358 // public void debug() {
359 // String mainClass = projectProperties.getClassToRun();
360 // if (mainClass.equals("")) {
361 // ErrorHandler.handleWarning("Please set a class to run.", Browser.getActiveBrowser());
362 // Browser.getActiveBrowser().ACTION_EditProjectProperties.actionPerformed(Browser.getActiveBrowser());
363 // JBuilderManager.INSTANCE.saveAll();
364 // return;
365 // }
366 // try {
367 // System.err.println(">> classToRun: " + mainClass);
368 // String[] args = { "-verbose", "-workingdir", projectProperties.getWorkingDir() };
369 // System.err.println(">>>" + projectProperties.getWorkingDir());
370 // args = new String[]{};
371 // org.aspectj.debugger.ide.IDEDebugger.launch
372 // (mainClass,
373 // projectProperties.getProjectSourcePath(),
374 // projectProperties.getClasspath(),
375 // projectProperties.getWorkingDir(),
376 // org.aspectj.debugger.base.Modes.JBUILDER4,
377 // args,
378 // new org.aspectj.ajde.debugger.AJDESourceShower
379 // (new EditorAdapter()),
380 // DebuggerAdapter.singletonInstance);
381 // //org.aspectj.debugger.ide.IDEDebugger.director.setVisible(false);
382 // //swallowDebugger((JPanel)org.aspectj.debugger.ide.IDEDebugger.director.getContentPane());
383 // } catch (Exception e) {
384 // ErrorHandler.handleError("Could not debug project.", e, TopManager.IDE_MANAGER.getRootFrame());
385 // }
386 // //Browser.getActiveBrowser().getMessageView().set
387 // }