KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > CompilationCommands


1 /*
2  * CompilationCommands.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: CompilationCommands.java,v 1.8 2004/06/09 23:45:04 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.AWTEvent JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26
27 public final class CompilationCommands implements Constants
28 {
29     private static CompilationBuffer lastCompilationBuffer;
30
31     public static CompilationBuffer getCompilationBuffer()
32     {
33         return lastCompilationBuffer;
34     }
35
36     public static void compile()
37     {
38         if (!checkPlatform("Compile"))
39             return;
40         final Editor editor = Editor.currentEditor();
41         CompileDialog d = new CompileDialog(editor);
42         editor.centerDialog(d);
43         d.show();
44         editor.repaintNow();
45         final String JavaDoc command = d.getCommand();
46         if (command != null && command.length() > 0)
47             compile(command, editor);
48     }
49
50     public static void compile(String JavaDoc args)
51     {
52         if (!checkPlatform("Compile"))
53             return;
54         if (args != null && args.length() > 0) {
55             History history = new History("compile.command");
56             history.append(args);
57             history.save();
58             compile(args, Editor.currentEditor());
59         }
60     }
61
62     public static void recompile()
63     {
64         if (!checkPlatform("Recompile"))
65             return;
66         final History history = new History("compile.command");
67         final String JavaDoc command = history.getPrevious();
68         if (command != null && command.length() > 0)
69             compile(command, Editor.currentEditor());
70         else
71             compile();
72     }
73
74     private static boolean checkPlatform(String JavaDoc command)
75     {
76         if (Platform.isPlatformWindows()) {
77             if (Platform.isPlatformWindows5())
78                 ; // OK (Windows 2000, Windows XP)
79
else if (Platform.isPlatformWindowsNT4())
80                 ; // OK (NT 4)
81
else {
82                 MessageDialog.showMessageDialog(
83                     "This feature requires Windows NT 4, Windows 2000 or Windows XP.",
84                     command);
85                 return false;
86             }
87         }
88         return true;
89     }
90
91     private static void compile(final String JavaDoc command, final Editor editor)
92     {
93         IdleThread.killFollowContextTask();
94         Editor.getTagFileManager().setEnabled(false);
95         saveCompilableBuffers(editor);
96
97         CompilationBuffer cb = null;
98         boolean visible = false;
99
100         if (lastCompilationBuffer != null) {
101             // Re-use existing compilation buffer.
102
cb = lastCompilationBuffer;
103             if (!Editor.getBufferList().contains(cb))
104                 cb.relink();
105             cb.empty();
106             cb.setCommand(command);
107             cb.setParentBuffer(editor.getBuffer());
108             cb.setCurrentDirectory(editor.getCurrentDirectory());
109             // Is it visible?
110
EditorIterator it = new EditorIterator();
111             while (it.hasNext()) {
112                 Editor ed = it.nextEditor();
113                 if (ed.getBuffer() == cb) {
114                     ed.updateLocation();
115                     ed.repaintNow();
116                     visible = true;
117                 }
118             }
119         } else {
120             cb = new CompilationBuffer(command, editor.getCurrentDirectory());
121             cb.setParentBuffer(editor.getBuffer());
122             lastCompilationBuffer = cb;
123         }
124
125         // Call initialize() before starting the thread so we can put the
126
// expanded command in the location bar when the compilation buffer is
127
// activated.
128
cb.initialize();
129         // Don't keep a reference to the parent buffer indefinitely!
130
cb.setParentBuffer(null);
131         new Thread JavaDoc(cb).start();
132         if (!visible) {
133             Editor otherEditor = editor.getOtherEditor();
134             if (otherEditor != null) {
135                 cb.setUnsplitOnClose(otherEditor.getBuffer().unsplitOnClose());
136                 otherEditor.makeNext(cb);
137             } else
138                 cb.setUnsplitOnClose(true);
139             editor.displayInOtherWindow(cb);
140         }
141     }
142
143     public static void thisError()
144     {
145         final Editor editor = Editor.currentEditor();
146         // If this method is invoked via a mouse event mapping, move dot to
147
// location of mouse click first.
148
AWTEvent JavaDoc e = editor.getDispatcher().getLastEvent();
149         if (e instanceof MouseEvent JavaDoc)
150             editor.mouseMoveDotToPoint((MouseEvent JavaDoc)e);
151         CompilationError error =
152             CompilationError.parseLineAsErrorMessage(editor.getDotLine());
153         if (error != null) {
154             final Buffer buffer = editor.getBuffer();
155             if (buffer instanceof CompilationErrorBuffer)
156                 ((CompilationErrorBuffer)buffer).setCurrentError(error);
157             String JavaDoc errorFileName = error.getFileName();
158             int errorLineNumber = error.getLineNumber();
159             if (errorFileName != null && errorLineNumber != 0) {
160                 Buffer buf =
161                     getSourceBuffer(buffer.getCurrentDirectory(),
162                         errorFileName);
163                 if (buf == null)
164                     return;
165                 Editor otherEditor = editor.getOtherEditor();
166                 if (otherEditor != null)
167                     otherEditor.makeNext(buf);
168                 Editor ed = editor.activateInOtherWindow(buf);
169                 int lineNumber = errorLineNumber - 1;
170                 if (lineNumber < 0)
171                     lineNumber = 0;
172                 Position pos = buf.findOriginal(lineNumber, error.getOffset());
173                 ed.moveDotTo(pos);
174                 ed.setUpdateFlag(REFRAME);
175                 ed.updateDisplay();
176                 Sidebar sidebar = editor.getSidebar();
177                 if (sidebar != null)
178                     sidebar.setUpdateFlag(SIDEBAR_BUFFER_LIST_ALL);
179             }
180         }
181     }
182
183     public static void nextError()
184     {
185         nextOrPreviousError(true);
186     }
187
188     public static void previousError()
189     {
190         nextOrPreviousError(false);
191     }
192
193     private static void nextOrPreviousError(boolean next)
194     {
195         final Editor editor = Editor.currentEditor();
196         CompilationErrorBuffer errorBuffer;
197         if (editor.getModeId() == XML_MODE)
198             errorBuffer = XmlMode.getErrorBuffer();
199         else
200             errorBuffer = lastCompilationBuffer;
201         if (errorBuffer == null)
202             return;
203         if (!Editor.getBufferList().contains(errorBuffer)) {
204             errorBuffer.relink();
205             Sidebar.setUpdateFlagInAllFrames(SIDEBAR_BUFFER_LIST_CHANGED);
206         }
207         CompilationError error =
208             next ? errorBuffer.nextError() : errorBuffer.previousError();
209         if (error == null) {
210             editor.status("No more errors");
211             return;
212         }
213         boolean useOtherWindow = false;
214         // Find editor displaying error buffer (if any).
215
Editor ed = null;
216         for (EditorIterator it = new EditorIterator(); it.hasNext();) {
217             ed = it.nextEditor();
218             if (ed.getBuffer() == errorBuffer)
219                 break;
220         }
221         if (ed.getBuffer() != errorBuffer) {
222             // The compilation buffer is not currently displayed.
223
ed = editor.displayInOtherWindow(errorBuffer);
224         } else if (ed == editor) {
225             // This command was invoked from the window displaying the
226
// compilation buffer.
227
useOtherWindow = true;
228         }
229         // Move caret to relevant line of error buffer.
230
Line errorLine = error.getErrorLine();
231         if (errorLine != null) {
232             Debug.assertTrue(ed.getBuffer() == errorBuffer);
233             ed.addUndo(SimpleEdit.MOVE);
234             ed.update(ed.getDotLine());
235             ed.setDot(errorLine, 0);
236             ed.update(ed.getDotLine());
237             ed.moveCaretToDotCol();
238             ed.getDisplay().setUpdateFlag(REFRAME);
239             ed.updateDisplay();
240         }
241         String JavaDoc errorFileName = error.getFileName();
242         int errorLineNumber = error.getLineNumber();
243         if (errorFileName != null && errorLineNumber != 0) {
244             // Find or create buffer for source file containing the error.
245
Buffer buf =
246                 getSourceBuffer(errorBuffer.getCurrentDirectory(),
247                     errorFileName);
248             if (buf == null)
249                 return;
250             Debug.assertTrue(ed.getBuffer() == errorBuffer);
251             if (useOtherWindow) {
252                 Debug.assertTrue(ed == editor);
253                 Editor otherEditor = editor.getOtherEditor();
254                 if (otherEditor != null)
255                     otherEditor.makeNext(buf);
256                 ed = editor.activateInOtherWindow(buf);
257             } else {
258                 ed = editor;
259                 ed.makeNext(buf);
260                 ed.activate(buf);
261             }
262             int lineNumber = errorLineNumber - 1;
263             if (lineNumber < 0)
264                 lineNumber = 0;
265             Position pos = buf.findOriginal(lineNumber, error.getOffset());
266             ed.moveDotTo(pos);
267             ed.setUpdateFlag(REFRAME);
268             ed.updateDisplay();
269             Sidebar sidebar = editor.getSidebar();
270             if (sidebar != null)
271                 sidebar.setUpdateFlag(SIDEBAR_BUFFER_LIST_ALL);
272         } else
273             editor.status("No more errors");
274     }
275
276     private static void saveCompilableBuffers(Editor editor)
277     {
278         editor.setWaitCursor();
279         int numModified = 0;
280         int numErrors = 0;
281         for (BufferIterator it = new BufferIterator(); it.hasNext();) {
282             Buffer buf = it.nextBuffer();
283             if (!buf.isModified())
284                 continue;
285             if (buf.isUntitled())
286                 continue;
287             final int modeId = buf.getModeId();
288             if (modeId == SEND_MAIL_MODE)
289                 continue;
290             if (modeId == CHECKIN_MODE)
291                 continue;
292             if (buf.getFile() != null && buf.getFile().isLocal()) {
293                 editor.status("Saving modified buffers...");
294                 ++numModified;
295                 if (buf.getBooleanProperty(Property.REMOVE_TRAILING_WHITESPACE))
296                     buf.removeTrailingWhitespace();
297                 if (!buf.save())
298                     ++numErrors;
299             }
300         }
301         if (numModified == 0)
302             ;
303         else if (numErrors == 0)
304             editor.status("Saving modified buffers...done");
305         else {
306             // User will already have seen detailed error information from
307
// Buffer.save().
308
editor.status("Unable to save all compilable buffers");
309         }
310         editor.setDefaultCursor();
311     }
312
313     private static Buffer getSourceBuffer(File currentDirectory,
314         String JavaDoc errorFileName)
315     {
316         File file = File.getInstance(currentDirectory, errorFileName);
317         if (!file.isFile()) {
318             // Strip path prefix.
319
file = File.getInstance(errorFileName);
320             String JavaDoc name = file.getName();
321             // Look in current directory.
322
file = File.getInstance(currentDirectory, name);
323             if (!file.isFile())
324                 return null;
325         }
326         return Editor.getBuffer(file);
327     }
328
329     public static void showMessage()
330     {
331         final Editor editor = Editor.currentEditor();
332         CompilationErrorBuffer errorBuffer;
333         if (editor.getModeId() == XML_MODE)
334             errorBuffer = XmlMode.getErrorBuffer();
335         else
336             errorBuffer = lastCompilationBuffer;
337         if (errorBuffer != null) {
338             CompilationError error = errorBuffer.getCurrentError();
339             if (error != null) {
340                 String JavaDoc message = error.getMessage();
341                 if (message != null) {
342                     int lineNumber = error.getLineNumber();
343                     int columnNumber = -1;
344                     int offset = error.getOffset();
345                     if (offset >= 0)
346                         columnNumber = offset + 1;
347                     String JavaDoc title = "Line " + lineNumber;
348                     if (columnNumber > 0)
349                         title += " Col " + columnNumber;
350                     if (message.length() > 65)
351                         message = Utilities.wrap(message, 65, 8);
352                     MessageDialog.showMessageDialog(editor, message, title);
353                 }
354             }
355         }
356     }
357 }
358
Popular Tags