Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/aspectj/tools/ajde/netbeans/NbManager.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
24   */
25  
26  package org.aspectj.tools.ajde.netbeans;
27  
28  import java.util.Enumeration;
29  import java.io.*;
30  import java.util.*;
31  import org.aspectj.tools.ide.*;
32  import org.openide.*;
33  import org.openide.util.HelpCtx;
34  import org.openide.util.actions.CallableSystemAction;
35  import org.openide.windows.*;
36  import org.openide.loaders.DataFolder;
37  import org.openide.modules.ModuleInstall;
38  import org.openide.util.actions.SystemAction;
39  import org.openide.explorer.*;
40  import org.openide.execution.*;
41  import org.openide.explorer.view.*;
42  import org.openide.nodes.*;
43  import org.openide.text.*;
44  import org.openide.filesystems.*;
45  import org.openide.loaders.*;
46  import org.openide.cookies.*;
47  import org.netbeans.core.*;
48  import org.openide.src.*;
49  import java.awt.Frame;
50  import javax.swing.*;
51  import javax.swing.text.StyledDocument;
52  import org.aspectj.ajde.*;
53  import org.aspectj.ajde.ui.internal.*;
54  import org.aspectj.ajde.ui.swing.*;
55  import org.aspectj.ajde.ui.*;
56  import org.aspectj.ajde.internal.*;
57  
58  /**
59   *  a singleton class that acts as the main bridge between AJDE and
60   *  netbeans.
61   *  
62   */
63  public class NbManager {
64  
65      public static NbManager INSTANCE = null;
66      private static final String NETBEANS_COMPILE_FLAGS = "-XtargetNearSource";
67      private NbProjectProperties projectProperties = null;
68    private UserPreferencesAdapter preferencesAdapter = null;
69    private StructureViewPanel multiViewPanel;
70  
71      public static void init() {
72          if (INSTANCE == null) {// pjs...
73              NbManager.INSTANCE = new NbManager();
74          }
75      }
76  
77      /**
78       *  changed to private. Mar.10,02 -pjs...
79       */
80      protected NbManager() {
81        preferencesAdapter = new UserPreferencesStore();
82        projectProperties = new NbProjectProperties(preferencesAdapter);
83          NbEditorAdapter editorAdapter = new NbEditorAdapter();
84          NbCompilerMessages compilerMessages = new NbCompilerMessages();
85          IdeUIAdapter uiAdapter = new NbUIAdapter();
86          
87          AjdeUIManager.getDefault().init(
88            editorAdapter, 
89            compilerMessages, 
90              projectProperties, 
91              preferencesAdapter, 
92              uiAdapter,
93              new NbIconRegistry(),
94              getRootFrame(),
95              false);
96  
97      multiViewPanel = AjdeUIManager.getDefault().getViewManager().getBrowserPanel();
98  
99      String options = AjdeUIManager.getDefault().getBuildOptions().getNonStandardOptions();
100     if (options == null || options.indexOf(NETBEANS_COMPILE_FLAGS) == -1) {
101       AjdeUIManager.getDefault().getBuildOptions().setNonStandardOptions(
102         options + " " + NETBEANS_COMPILE_FLAGS
103       );  
104     }  
105     Ajde.getDefault().getConfigurationManager().setActiveConfigFile(BuildConfigManager.DEFAULT_CONFIG_LABEL);
106     
107     //System.err.println(">> " + AjdeUIManager.getDefault().getBuildOptions().getNonStandardOptions());
108         //AjdeUIManager.getDefault().getViewManager().setGlobalMode(true);
109         //AjdeUIManager.getDefault().getViewManager().updateConfigsList();
110     }
111     
112     /**
113      * Not implemented.
114      */
115     public void setEditorStatusText(String text) { }
116 
117     /**
118      * Not implemented.
119      */
120     public void editConfigFile(String configFile) { }
121     
122 
123     public String getDumpFilePath() {
124         return null;
125     }
126 
127     public IconRegistry getIcons() {
128         return NbIconRegistry.INSTANCE;
129     }
130 
131     /**
132      * @todo: fix this ridiculous way of finding class and executor.
133      */
134     public void run() { 
135         try {
136             String mainClass = projectProperties.getClassToExecute();
137 //            JOptionPane.showInputDialog("Please type the fully qualified name of the class to be executed: ");
138             if (mainClass == null || mainClass.length() == 0) {
139                 Ajde.getDefault().getErrorHandler().handleWarning("No main class specified, please select a class to run.");
140                 AjdeUIManager.getDefault().getOptionsFrame().showPanel(BuildOptionsPanel.getDefault());
141             } else { 
142                 String classpath = projectProperties.getOutputPath() + File.pathSeparator + projectProperties.getClasspath();
143                 Enumeration e = ProcessExecutor.executors();
144                 boolean succeeded = false;
145                 while (e.hasMoreElements()) {
146                     Object o = e.nextElement();
147                     if (o instanceof ProcessExecutor &&  // sooooo dumb, ah well
148                         o.getClass().toString().equals("class org.netbeans.modules.java.JavaProcessExecutor")) {
149                         ((ProcessExecutor)o).setClassPath(new NbClassPath(classpath));
150                         ((ProcessExecutor)o).execute(new ExecInfo(mainClass));
151                         succeeded = true;
152                     }
153                 }
154             }
155         } catch (Exception e) {
156             Ajde.getDefault().getErrorHandler().handleError("Main class not found.", e);
157         }
158     }
159 
160     public void build() {
161         if (projectProperties.getOutputPath() != null) {
162             Ajde.getDefault().getBuildManager().build();
163         } else {
164             Ajde.getDefault().getErrorHandler().handleWarning("No output path defined, please select an output path.");
165             AjdeUIManager.getDefault().getOptionsFrame().showPanel(BuildOptionsPanel.getDefault());
166         }
167     }
168   
169     /**
170      * Disabled.
171      */
172     public void debug() {
173 //
174 //        setStatusText("ajdb starting...");
175 //
176 //        PrintStream out = Systemm.out;
177 //        PrintStream err = Systemm.err;
178 //        try {
179 //            DebuggerAdapter.install();
180 //            //TODO: This can stay empty
181 //            String classToRun = projectProperties.getClassToRun();
182 //            String sourcePath = ((ForteProjectProperties)projectProperties).getDebuggerSourcePath();
183 //            String classPath =  ((ForteProjectProperties)projectProperties).getDebuggerClasspath();
184 //            String workingdir = projectProperties.getWorkingDir();
185 //
186 //            String[] args = { "-workingdir", workingdir, "-classpath", classPath };
187 //            org.aspectj.debugger.ide.IDEInterface ideInterface = //new org.aspectj.debugger.ide.IDEInterfaceImpl();
188 //                new DebuggerAdapter(); //.singletonInstance;
189 //            org.aspectj.debugger.ide.SourceShower shower =
190 //                ((DebuggerAdapter)ideInterface).getSourceShower();
191 //
192 //          org.aspectj.debugger.ide.IDEDebugger.launch
193 //              (
194 //               classToRun, //jBuilderManager.getProjectProperties().getClassToRun(),
195 //               sourcePath, //jBuilderManager.getProjectProperties().getProjectSourcePath(),
196 //               classPath, //jBuilderManager.getProjectProperties().getClasspath(),
197 //               workingdir,
198 //               org.aspectj.debugger.base.Modes.FORTE, //org.aspectj.debugger.base.Modes.JBUILDER4,
199 //               args,
200 //               shower,
201 //               ideInterface); //org.aspectj.tools.ajde.jbuilder4.debugger.JBuilderIDEInterface.singletonInstance);
202 //
203 //        } catch (Exception e) {
204 //            ErrorHandler.handleError("error running ajdb");
205 //        }
206 //        setStatusText("");
207     }
208 
209     public List getBuildOptions() {
210         List options = new ArrayList();
211         options.add("-g");
212         String classpath = projectProperties.getClasspath();
213         if (classpath != "") {
214             options.add("-classpath");
215             options.add(classpath);
216         }
217         String outpath = projectProperties.getOutputPath();
218         if (outpath != "") {
219             options.add("-d");
220             options.add(outpath);
221         }
222         return options;
223     }
224 
225     public Frame getRootFrame() {
226         return TopManager.getDefault().getWindowManager().getMainWindow();
227 //        return null;
228     }
229 
230     public void saveAll() {
231         org.openide.TopManager.getDefault().saveAll();
232     }
233 
234     public void mountConfigFile(String configFile) {
235 
236     }
237 
238     public void removeConfigFile(String configFile) {
239 
240     }
241 
242     public void showMessages() {
243         // ignore
244     }
245 
246     public void hideMessages() {
247         // ignore
248     }
249 
250     
251     private static String findClassNameToRun() {
252         return "";
253 /*
254         String newClassToRun = "";
255         Enumeration enum = org.openide.TopManager.getDefault().getRepository().getFileSystems();
256         while (enum.hasMoreElements()) {
257             FileSystem fileSystem = (FileSystem)enum.nextElement();
258             if (!(fileSystem instanceof ExLocalFileSystem)) continue;
259             ExLocalFileSystem exLocalFileSystem = (ExLocalFileSystem)fileSystem;
260             File rootFile = exLocalFileSystem.getRootDirectory();
261             List dirs = new Vector();
262             List files = new Vector();
263             dirs.add(rootFile);
264             Vector classElements = new Vector();
265             while (dirs.size() > 0) {
266                 File dir = (File)dirs.remove(0);
267                 File[] fileArray = dir.listFiles(new FileFilter() {
268                         public boolean accept(File f) {
269                             return f.isDirectory() || f.getName().endsWith(".java");
270                         }
271                     });
272                 for (int i = 0; i < fileArray.length; i++) {
273                     File file = fileArray[i];
274                     if (file.isDirectory()) {
275                         dirs.add(file);
276                     } else {
277                         DataObject dataObject =
278                             DebuggerAdapter.singletonInstance.dataObject(fileObject(rootFile.getAbsolutePath(), file.getAbsolutePath()));
279 
280                         if (dataObject == null) {
281                             continue;
282                         }
283                         SourceElement source =
284                             ((SourceCookie)dataObject.getCookie(SourceCookie.class)).getSource();
285                         if (source == null) {
286                             continue;
287                         }
288                         ClassElement[] classes = null;
289                         try {
290                             classes = source.getClasses();
291                         } catch (Throwable t) {
292                             classes = null;
293                         }
294                         if (classes == null) {
295                             continue;
296                         }
297                         for (int j = 0; j < classes.length; j++) {
298                             ClassElement ce = classes[j];
299                             if (ce.hasMainMethod()) {
300                                 classElements.add(DebuggerAdapter.className(ce));
301                                 break;
302                             }
303                         }
304                     }
305                 }
306             }
307             SortedSet sortedElements = new TreeSet(classElements);
308             if (sortedElements.size() == 0) {
309 
310             } else if (sortedElements.size() == 1) {
311                 return sortedElements.first()+"";
312             } else {
313                 Object[] classes = new Object[sortedElements.size()];
314                 int i = 0;
315                 Iterator sortedIter = sortedElements.iterator();
316                 while (sortedIter.hasNext()) {
317                     classes[i++] = sortedIter.next();
318                 }
319                 Object o = JOptionPane.showInputDialog
320                     (null,
321                      "Please select a class to run",
322                      "Select class",
323                      JOptionPane.QUESTION_MESSAGE,
324                      null,
325                      classes,
326                      classes[0]);
327                 if (o != null) {
328                     return null;
329                 }
330             }
331         }
332         return newClassToRun;
333 */ 
334     }
335 
336     private static FileObject fileObject(String baseDir, String fullSourceName) {
337         baseDir = baseDir.replace('\\', '/');
338         fullSourceName = fullSourceName.replace('\\', '/');
339         String relativePath = fullSourceName.substring(baseDir.length()+1, fullSourceName.length());//.replace('/', '.');
340 
341         int ilastSlash = relativePath.lastIndexOf('/');
342         int ilastDot = relativePath.lastIndexOf('.');
343 
344         if (ilastDot == -1 || ilastDot == relativePath.length()-1) {
345             return null;
346         }
347 
348         String packageName = ilastSlash != -1 ?
349             relativePath.substring(0, ilastSlash).replace('/', '.') :
350             "";
351         String fileName = relativePath.substring(ilastSlash+1, ilastDot);
352         String extension = relativePath.substring(ilastDot+1);
353 
354         if (!"java".equals(extension)) {
355             return null;
356         }
357         return org.openide.TopManager.getDefault().getRepository().find(packageName, fileName, extension);
358     }
359     
360   public StructureViewPanel getViewPanel() {
361     return multiViewPanel;
362   }
363 
364 }