aboutsummaryrefslogtreecommitdiffstats
path: root/ajde/src/main/java/org/aspectj/ajde/Ajde.java
blob: 3a75bbb0af6ac565d58c86ff88c2136870c1be44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* *******************************************************************
 * Copyright (c) 1999-2001 Xerox Corporation,
 *               2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v 2.0
 * which accompanies this distribution and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
 *
 * Contributors:
 *     Xerox/PARC     initial implementation
 *     Helen Hawkins  Converted to new interface (bug 148190)
 *******************************************************************/

package org.aspectj.ajde;

import java.awt.Frame;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.swing.JOptionPane;

import org.aspectj.ajde.core.AjCompiler;
import org.aspectj.ajde.core.IBuildProgressMonitor;
import org.aspectj.ajde.core.ICompilerConfiguration;
import org.aspectj.ajde.internal.BuildConfigListener;
import org.aspectj.ajde.internal.BuildConfigManager;
import org.aspectj.ajde.internal.LstBuildConfigManager;
import org.aspectj.ajde.ui.FileStructureView;
import org.aspectj.ajde.ui.StructureSearchManager;
import org.aspectj.ajde.ui.StructureViewManager;
import org.aspectj.ajde.ui.swing.BrowserViewManager;
import org.aspectj.ajde.ui.swing.OptionsFrame;
import org.aspectj.ajde.ui.swing.StructureViewPanel;
import org.aspectj.ajde.ui.swing.SwingTreeViewNodeFactory;
import org.aspectj.ajde.ui.swing.TreeViewBuildConfigEditor;
import org.aspectj.asm.AsmManager;
import org.aspectj.bridge.IMessage;
import org.aspectj.bridge.Message;
import org.aspectj.util.LangUtil;
import org.aspectj.util.Reflection;

/**
 * Singleton class used to initialize the Ajde ui as well as the properties required to run the compiler. Users must call
 * "Ajde.init(...)" before doing anything else. There are getter methods for the various properties that are set in the
 * initialization.
 *
 * This also defines the factory for getting new AjCompiler instances.
 *
 * @author Mik Kersten
 * @author Andy Clement
 */
public class Ajde {

	protected static final Ajde INSTANCE = new Ajde();
	private BrowserViewManager viewManager = null;

	private IdeUIAdapter ideUIAdapter = null;
	private TreeViewBuildConfigEditor buildConfigEditor = null;
	private IconRegistry iconRegistry;
	private IRuntimeProperties runtimeProperties;
	private boolean initialized = false;
	private AsmManager asm;
	private OptionsFrame optionsFrame = null;
	private Frame rootFrame = null;
	private StructureViewPanel fileStructurePanel = null;

	private EditorAdapter editorAdapter;
	private StructureViewManager structureViewManager;
	private StructureSearchManager structureSearchManager;
	private final BuildConfigManager configurationManager;

	// all to do with building....
	private ICompilerConfiguration compilerConfig;
	private IUIBuildMessageHandler uiBuildMsgHandler;
	private IBuildProgressMonitor buildProgressMonitor;
	private AjCompiler compiler;

	public AsmManager getModel() {
		return asm;
	}

	/**
	 * This class can only be constructured by itself (as a singleton) or by sub-classes.
	 */
	protected Ajde() {
		configurationManager = new LstBuildConfigManager();
	}

	/**
	 * Initializes the ajde ui and sets up the compiler
	 */
	public void init(ICompilerConfiguration compilerConfig, IUIBuildMessageHandler uiBuildMessageHandler,
			IBuildProgressMonitor monitor, EditorAdapter editorAdapter, IdeUIAdapter ideUIAdapter, IconRegistry iconRegistry,
			Frame rootFrame, IRuntimeProperties runtimeProperties, boolean useFileView) {
		try {

			INSTANCE.compilerConfig = compilerConfig;
			INSTANCE.uiBuildMsgHandler = uiBuildMessageHandler;
			INSTANCE.buildProgressMonitor = monitor;
			INSTANCE.asm = AsmManager.createNewStructureModel(Collections.<File,String>emptyMap());

			INSTANCE.iconRegistry = iconRegistry;
			INSTANCE.ideUIAdapter = ideUIAdapter;
			INSTANCE.buildConfigEditor = new TreeViewBuildConfigEditor();
			INSTANCE.rootFrame = rootFrame;
			INSTANCE.runtimeProperties = runtimeProperties;

			INSTANCE.configurationManager.addListener(INSTANCE.STRUCTURE_UPDATE_CONFIG_LISTENER);
			INSTANCE.ideUIAdapter = ideUIAdapter;
			INSTANCE.editorAdapter = editorAdapter;
			INSTANCE.structureSearchManager = new StructureSearchManager();
			INSTANCE.structureViewManager = new StructureViewManager(new SwingTreeViewNodeFactory(iconRegistry));

			if (useFileView) {
				FileStructureView structureView = structureViewManager.createViewForSourceFile(editorAdapter.getCurrFile(),
						structureViewManager.getDefaultViewProperties());
				structureViewManager.setDefaultFileView(structureView);
				fileStructurePanel = new StructureViewPanel(structureView);
			}

			viewManager = new BrowserViewManager();
			optionsFrame = new OptionsFrame(iconRegistry);

			initialized = true;
		} catch (Throwable t) {
			Message error = new Message("AJDE UI failed to initialize", IMessage.ABORT, t, null);
			uiBuildMsgHandler.handleMessage(error);
		}
	}

	public void showOptionsFrame() {
		int x = (rootFrame.getWidth() / 2) + rootFrame.getX() - optionsFrame.getWidth() / 2;
		int y = (rootFrame.getHeight() / 2) + rootFrame.getY() - optionsFrame.getHeight() / 2;
		optionsFrame.setLocation(x, y);
		optionsFrame.setVisible(true);
	}

	/**
	 * @return true if init(..) has been run, false otherwise
	 */
	public boolean isInitialized() {
		return initialized;
	}

	private final BuildConfigListener STRUCTURE_UPDATE_CONFIG_LISTENER = new BuildConfigListener() {
		public void currConfigChanged(String configFilePath) {
			if (configFilePath != null) {
				Ajde.getDefault().asm.readStructureModel(configFilePath);
			}
		}

		public void configsListUpdated(List configsList) {
		}
	};

	/**
	 * Utility to run the project main class from the project properties in the same VM using a class loader populated with the
	 * classpath and output path or jar. Errors are logged to the ErrorHandler.
	 *
	 * @return Thread running with process, or null if unable to start
	 */
	public Thread runInSameVM() {
		final RunProperties props = new RunProperties(compilerConfig, runtimeProperties, uiBuildMsgHandler, rootFrame);
		if (!props.valid) {
			return null; // error already handled
		}
		Runnable runner = new Runnable() {
			public void run() {
				try {
					Reflection.runMainInSameVM(props.classpath, props.mainClass, props.args);
				} catch (Throwable e) {
					Message msg = new Message("Error running " + props.mainClass, IMessage.ERROR, e, null);
					uiBuildMsgHandler.handleMessage(msg);
				}
			}
		};
		Thread result = new Thread(runner, props.mainClass);
		result.start();
		return result;
	}

	/**
	 * Utility to run the project main class from the project properties in a new VM. Errors are logged to the ErrorHandler.
	 *
	 * @return LangUtil.ProcessController running with process, or null if unable to start
	 */
	public LangUtil.ProcessController runInNewVM() {
		final RunProperties props = new RunProperties(compilerConfig, runtimeProperties, uiBuildMsgHandler, rootFrame);
		if (!props.valid) {
			return null; // error already handled
		}
		// setup to run asynchronously, pipe streams through, and report errors
		final StringBuffer command = new StringBuffer();
		LangUtil.ProcessController controller = new LangUtil.ProcessController() {
			public void doCompleting(Throwable thrown, int result) {
				LangUtil.ProcessController.Thrown any = getThrown();
				if (!any.thrown && (null == thrown) && (0 == result)) {
					return; // no errors
				}
				// handle errors
				String context = props.mainClass + " command \"" + command + "\"";
				if (null != thrown) {
					String m = "Exception running " + context;
					uiBuildMsgHandler.handleMessage(new Message(m, IMessage.ERROR, thrown, null));
				} else if (0 != result) {
					String m = "Result of running " + context;
					uiBuildMsgHandler.handleMessage(new Message(m, IMessage.ERROR, null, null));
				}
				if (null != any.fromInPipe) {
					String m = "Error processing input pipe for " + context;
					uiBuildMsgHandler.handleMessage(new Message(m, IMessage.ERROR, thrown, null));
				}
				if (null != any.fromOutPipe) {
					String m = "Error processing output pipe for " + context;
					uiBuildMsgHandler.handleMessage(new Message(m, IMessage.ERROR, thrown, null));
				}
				if (null != any.fromErrPipe) {
					String m = "Error processing error pipe for " + context;
					uiBuildMsgHandler.handleMessage(new Message(m, IMessage.ERROR, thrown, null));
				}
			}
		};

		controller = LangUtil.makeProcess(controller, props.classpath, props.mainClass, props.args);

		command.append(Arrays.asList(controller.getCommand()).toString());

		// now run the process
		controller.start();
		return controller;
	}

	/** struct class to interpret project properties */
	private static class RunProperties {
		final String mainClass;
		final String classpath;
		final String[] args;
		final boolean valid;
		private final Frame rootFrame;

		RunProperties(ICompilerConfiguration compilerConfig, IRuntimeProperties runtimeProperties, IUIBuildMessageHandler handler,
				Frame rootFrame) {
			// XXX really run arbitrary handler in constructor? hmm.
			LangUtil.throwIaxIfNull(runtimeProperties, "runtime properties");
			LangUtil.throwIaxIfNull(compilerConfig, "compiler configuration");
			LangUtil.throwIaxIfNull(handler, "handler");
			LangUtil.throwIaxIfNull(rootFrame, "rootFrame");
			String mainClass = null;
			String classpath = null;
			String[] args = null;
			boolean valid = false;
			this.rootFrame = rootFrame;

			mainClass = runtimeProperties.getClassToExecute();
			if (LangUtil.isEmpty(mainClass)) {
				showWarningMessage("No main class specified");
			} else {
				StringBuilder sb = new StringBuilder();
				List<File> outputDirs = compilerConfig.getOutputLocationManager().getAllOutputLocations();
				for (File dir : outputDirs) {
					sb.append(dir.getAbsolutePath() + File.pathSeparator);
				}
				classpath = LangUtil.makeClasspath(null, compilerConfig.getClasspath(), sb.toString(), compilerConfig.getOutJar());
				if (LangUtil.isEmpty(classpath)) {
					showWarningMessage("No classpath specified");
				} else {
					args = LangUtil.split(runtimeProperties.getExecutionArgs());
					valid = true;
				}
			}
			this.mainClass = mainClass;
			this.classpath = classpath;
			this.args = args;
			this.valid = valid;
		}

		private void showWarningMessage(String message) {
			JOptionPane.showMessageDialog(rootFrame, message, "Warning", JOptionPane.WARNING_MESSAGE);
		}

	}

	/**
	 * Set the build off in the same thread
	 *
	 * @param configFile
	 * @param buildFresh - true if want to do a full build, false otherwise
	 */
	public void runBuildInSameThread(String configFile, boolean buildFresh) {
		AjCompiler c = getCompilerForConfigFile(configFile);
		if (c == null)
			return;
		if (buildFresh) {
			c.buildFresh();
		} else {
			c.build();
		}
	}

	/**
	 * Set the build off in a different thread. Would need to set the build off in a different thread if using a swing application
	 * to display the build progress.
	 *
	 * @param configFile
	 * @param buildFresh - true if want to do a full build, false otherwise
	 */
	public void runBuildInDifferentThread(String configFile, boolean buildFresh) {
		AjCompiler c = getCompilerForConfigFile(configFile);
		if (c == null)
			return;
		CompilerThread compilerThread = new CompilerThread(c, buildFresh);
		compilerThread.start();
	}

	static class CompilerThread extends Thread {

		private final AjCompiler compiler;
		private final boolean buildFresh;

		public CompilerThread(AjCompiler compiler, boolean buildFresh) {
			this.compiler = compiler;
			this.buildFresh = buildFresh;
		}

		public void run() {
			if (buildFresh) {
				compiler.buildFresh();
			} else {
				compiler.build();
			}
		}
	}

	// ---------- getter methods for the ui --------------

	/**
	 * @return the singleton instance
	 */
	public static Ajde getDefault() {
		return INSTANCE;
	}

	/**
	 * @return the BrowserViewManager
	 */
	public BrowserViewManager getViewManager() {
		return viewManager;
	}

	/**
	 * @return the main frame
	 */
	public Frame getRootFrame() {
		return rootFrame;
	}

	/**
	 * @return the parent frame for the options panel
	 */
	public OptionsFrame getOptionsFrame() {
		return optionsFrame;
	}

	/**
	 * @return the IdeUIAdapter
	 */
	public IdeUIAdapter getIdeUIAdapter() {
		return ideUIAdapter;
	}

	/**
	 * @return the EditorAdapter
	 */
	public EditorAdapter getEditorAdapter() {
		return editorAdapter;
	}

	/**
	 * @return the TreeViewBuildConfigEditor
	 */
	public TreeViewBuildConfigEditor getBuildConfigEditor() {
		return buildConfigEditor;
	}

	/**
	 * @return the StructureViewPanel
	 */
	public StructureViewPanel getFileStructurePanel() {
		return fileStructurePanel;
	}

	/**
	 * @return the IconRegistry
	 */
	public IconRegistry getIconRegistry() {
		return iconRegistry;
	}

	/**
	 * @return the StructureViewManager
	 */
	public StructureViewManager getStructureViewManager() {
		return structureViewManager;
	}

	/**
	 * @return the StructureSearchManager
	 */
	public StructureSearchManager getStructureSearchManager() {
		return structureSearchManager;
	}

	/**
	 * @return the BuildConfigManager
	 */
	public BuildConfigManager getBuildConfigManager() {
		return configurationManager;
	}

	// -------------- getter methods for the compiler -------------

	/**
	 * @return the ICompilerConfiguration
	 */
	public ICompilerConfiguration getCompilerConfig() {
		return compilerConfig;
	}

	/**
	 * @return the IUIBuildMessageHandler
	 */
	public IUIBuildMessageHandler getMessageHandler() {
		return uiBuildMsgHandler;
	}

	/**
	 * @return the IBuildProgressMonitor
	 */
	public IBuildProgressMonitor getBuildProgressMonitor() {
		return buildProgressMonitor;
	}

	/**
	 * If the provided configFile is the same as the id for the last compiler then returns that, otherwise clears the state for the
	 * saved compiler and creates a new one for the provided configFile
	 *
	 * @param configFile
	 * @return the AjCompiler with the id of the given configFile
	 */
	public AjCompiler getCompilerForConfigFile(String configFile) {
		if (configFile == null) {
			return null;
		}
		if ((compiler == null || !compiler.getId().equals(configFile))) {
			if (compiler != null) {
				// have to remove the incremental state of the previous
				// compiler - this will remove it from the
				// IncrementalStateManager's
				// list
				compiler.clearLastState();
			}
			getMessageHandler().reset();
			compiler = new AjCompiler(configFile, getCompilerConfig(), getBuildProgressMonitor(), getMessageHandler());
		}
		return compiler;
	}

	public AsmManager getModelForConfigFile(String configFile) {
		return compiler.getModel();
		// if ((compiler == null || !compiler.getId().equals(configFile))) {
		// if (compiler != null) {
		// // have to remove the incremental state of the previous
		// // compiler - this will remove it from the
		// // IncrementalStateManager's
		// // list
		// compiler.clearLastState();
		// }
		// getMessageHandler().reset();
		// compiler = new AjCompiler(configFile, getCompilerConfig(), getBuildProgressMonitor(), getMessageHandler());
		// }

	}
}