KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ajbrowser > BrowserManager


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the IDE support for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25
26 package org.aspectj.tools.ajbrowser;
27
28 import java.util.*;
29 import java.awt.Frame JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JFrame JavaDoc;
32 import java.io.*;
33 import org.aspectj.ajde.*;
34 import org.aspectj.ajde.ui.*;
35 import org.aspectj.ajde.internal.*;
36 import org.aspectj.ajde.ui.internal.*;
37 import org.aspectj.ajde.ui.swing.*;
38
39 /**
40  * IDE manager for standalone AJDE application.
41  *
42  * @author Mik Kersten
43  */

44 public class BrowserManager {
45     
46     private static final BrowserManager INSTANCE = new BrowserManager();
47     private BrowserProperties browserProjectProperties;
48     
49     public static BrowserManager getDefault() {
50         return INSTANCE;
51     }
52     
53     private List configFiles = new ArrayList();
54     
55     public static final String JavaDoc TITLE = "AspectJ Browser";
56     
57     private static TopFrame topFrame = null;
58     
59     public void init(String JavaDoc[] configFilesArgs, boolean visible) {
60         try {
61             UserPreferencesAdapter preferencesAdapter = new UserPreferencesStore();
62             browserProjectProperties = new BrowserProperties(preferencesAdapter);
63             TaskListManager taskListManager = new CompilerMessagesPanel();
64             BasicEditor ajdeEditor = new BasicEditor();
65             BrowserUIAdapter browserUIAdapter = new BrowserUIAdapter();
66             topFrame = new TopFrame();
67             configFiles = getConfigFilesList(configFilesArgs);
68
69             AjdeUIManager.getDefault().init(
70                 ajdeEditor,
71                 taskListManager,
72                 browserProjectProperties,
73                 preferencesAdapter,
74                 browserUIAdapter,
75                 new IconRegistry(),
76                 topFrame,
77                 true);
78             
79             Ajde.getDefault().getBuildManager().addListener(BUILD_MESSAGES_LISTENER);
80             
81             MultiStructureViewPanel multiViewPanel = new MultiStructureViewPanel(
82                 AjdeUIManager.getDefault().getViewManager().getBrowserPanel(),
83                 AjdeUIManager.getDefault().getFileStructurePanel()
84             );
85             
86             topFrame.init(
87                 multiViewPanel,
88                 (CompilerMessagesPanel)taskListManager,
89                 Ajde.getDefault().getEditorManager().getEditorPanel()
90             );
91                 
92             if (visible) topFrame.setVisible(true);
93               
94             if (configFiles.size() == 0) {
95                 Ajde.getDefault().getErrorHandler().handleWarning(
96                     "No build configuration selected. "
97                         + "Select a \".lst\" build configuration file in order to compile and navigate structure.");
98             } else {
99                 //UiManager.getDefault().getViewManager().updateConfigsList();
100
}
101         
102             AjdeUIManager.getDefault().getOptionsFrame().addOptionsPanel(new BrowserOptionsPanel());
103         
104             //String lastOpenFilePath = browserProjectProperties.getLastOpenSourceFilePath();
105
//Ajde.getDefault().getEditorManager().showSourceLine(lastOpenFilePath, 1, false);
106
//Ajde.getDefault().getStructureViewManager().fireNavigationAction(lastOpenFilePath, 6);
107
//Ajde.getDefault().enableLogging(System.out);
108

109             if (configFilesArgs.length > 0 && configFilesArgs[0] != null) {
110                 Ajde.getDefault().getConfigurationManager().setActiveConfigFile(configFilesArgs[0]);
111             }
112         } catch (Throwable JavaDoc t) {
113             t.printStackTrace();
114             Ajde.getDefault().getErrorHandler().handleError(
115                 "AJDE failed to initialize.",
116                 t);
117         }
118     }
119
120     public void resetEditorFrame() {
121         topFrame.resetSourceEditorPanel();
122     }
123
124     public void resetEditor() {
125         BrowserManager.getDefault().getRootFrame().setSize(BrowserManager.getDefault().getRootFrame().getWidth()+1, BrowserManager.getDefault().getRootFrame().getHeight()+1);
126         BrowserManager.getDefault().getRootFrame().doLayout();
127         BrowserManager.getDefault().getRootFrame().repaint();
128     }
129
130     public void setStatusInformation(String JavaDoc text) {
131         topFrame.statusText_label.setText(text);
132     }
133
134     public void setEditorStatusText(String JavaDoc text) {
135         topFrame.setTitle(BrowserManager.TITLE + " - " + text);
136     }
137
138     public void run() {
139         Runner.run(Ajde.getDefault().getProjectProperties().getClassToExecute());
140     }
141
142     public void saveAll() {
143         Ajde.getDefault().getEditorManager().saveContents();
144     }
145
146     public void showMessages() {
147         topFrame.showMessagesPanel();
148     }
149
150     public void hideMessages() {
151         topFrame.hideMessagesPanel();
152     }
153
154     public JFrame JavaDoc getRootFrame() {
155         return topFrame;
156     }
157
158     public void openFile(String JavaDoc filePath) {
159         try {
160             if (filePath.endsWith(".lst")) {
161                 AjdeUIManager.getDefault().getBuildConfigEditor().openFile(filePath);
162                 topFrame.setEditorPanel(AjdeUIManager.getDefault().getBuildConfigEditor());
163             } else if (filePath.endsWith(".java") || filePath.endsWith(".aj")){
164                 Ajde.getDefault().getEditorManager().showSourceLine(filePath, 0, false);
165             } else {
166                 Ajde.getDefault().getErrorHandler().handleError("File: " + filePath
167                     + " could not be opened because the extension was not recoginzed.");
168             }
169         } catch (IOException ioe) {
170             Ajde.getDefault().getErrorHandler().handleError("Could not open file: " + filePath, ioe);
171         } catch (InvalidResourceException ire) {
172             Ajde.getDefault().getErrorHandler().handleError("Invalid file: " + filePath, ire);
173         }
174         
175         browserProjectProperties.setLastOpenSourceFilePath(filePath);
176     }
177
178     private List getConfigFilesList(String JavaDoc[] configFiles) {
179         List configs = new ArrayList();
180         for (int i = 0; i < configFiles.length; i++) {
181             if (configFiles[i].endsWith(BuildConfigManager.CONFIG_FILE_SUFFIX)) {
182                 configs.add(configFiles[i]);
183             }
184         }
185         return configs;
186     }
187
188     private static class Runner {
189         public static void run(String JavaDoc className) {
190             try {
191                 Process JavaDoc p = Runtime.getRuntime().exec("java "
192                     //+ "-classpath" + Ajde.getDefault().getProjectProperties().getClasspath() + " "
193
+ className);
194             } catch(IOException ioe) {
195                 Ajde.getDefault().getErrorHandler().handleError("Coud not run: " + className, ioe);
196             }
197         }
198   
199         public static void invoke(String JavaDoc className) {
200             try {
201                 if (className == null || className.length() == 0) {
202                     Ajde.getDefault().getErrorHandler().handleWarning("No main class specified, please select a class to run.");
203
204                 } else {
205                     Class JavaDoc[] argTypes = { String JavaDoc[].class };
206                     java.lang.reflect.Method JavaDoc method = Class.forName(className).getDeclaredMethod("main", argTypes);
207                     Object JavaDoc[] args = { new String JavaDoc[0] };
208                     method.invoke(null, args);
209                 }
210             } catch(ClassNotFoundException JavaDoc cnfe) {
211                 Ajde.getDefault().getErrorHandler().handleWarning("Main class not found: " + className +
212                 "\nMake sure that you have \".\" on your classpath.");
213             } catch(NoSuchMethodException JavaDoc nsme) {
214                 Ajde.getDefault().getErrorHandler().handleWarning("Class: " + className + " does not declare public static void main(String[])");
215             } catch(java.lang.reflect.InvocationTargetException JavaDoc ite) {
216                 Ajde.getDefault().getErrorHandler().handleWarning("Could not execute: " + className);
217             } catch(IllegalAccessException JavaDoc iae) {
218                 Ajde.getDefault().getErrorHandler().handleWarning("Class: " + className + " does not declare public main method");
219             }
220         }
221     }
222     
223     private final BuildListener BUILD_MESSAGES_LISTENER = new BuildListener() {
224         
225         public void compileStarted(String JavaDoc buildConfigFile) { }
226         
227         public void compileFinished(String JavaDoc buildConfigFile, int buildTime, boolean succeeded, boolean warnings) {
228             int timeInSeconds = buildTime/1000;
229             if (succeeded && !warnings) {
230                 hideMessages();
231             } else {
232                 showMessages();
233             }
234         }
235         
236         public void compileAborted(String JavaDoc buildConfigFile, String JavaDoc message) { }
237     };
238     
239     public List getConfigFiles() {
240         return configFiles;
241     }
242
243     public BrowserProperties getBrowserProjectProperties() {
244         return browserProjectProperties;
245     }
246 }
247
Popular Tags