001: /*
002: * CompilationCommands.java
003: *
004: * Copyright (C) 2003-2004 Peter Graves
005: * $Id: CompilationCommands.java,v 1.8 2004/06/09 23:45:04 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.AWTEvent;
025: import java.awt.event.MouseEvent;
026:
027: public final class CompilationCommands implements Constants {
028: private static CompilationBuffer lastCompilationBuffer;
029:
030: public static CompilationBuffer getCompilationBuffer() {
031: return lastCompilationBuffer;
032: }
033:
034: public static void compile() {
035: if (!checkPlatform("Compile"))
036: return;
037: final Editor editor = Editor.currentEditor();
038: CompileDialog d = new CompileDialog(editor);
039: editor.centerDialog(d);
040: d.show();
041: editor.repaintNow();
042: final String command = d.getCommand();
043: if (command != null && command.length() > 0)
044: compile(command, editor);
045: }
046:
047: public static void compile(String args) {
048: if (!checkPlatform("Compile"))
049: return;
050: if (args != null && args.length() > 0) {
051: History history = new History("compile.command");
052: history.append(args);
053: history.save();
054: compile(args, Editor.currentEditor());
055: }
056: }
057:
058: public static void recompile() {
059: if (!checkPlatform("Recompile"))
060: return;
061: final History history = new History("compile.command");
062: final String command = history.getPrevious();
063: if (command != null && command.length() > 0)
064: compile(command, Editor.currentEditor());
065: else
066: compile();
067: }
068:
069: private static boolean checkPlatform(String command) {
070: if (Platform.isPlatformWindows()) {
071: if (Platform.isPlatformWindows5())
072: ; // OK (Windows 2000, Windows XP)
073: else if (Platform.isPlatformWindowsNT4())
074: ; // OK (NT 4)
075: else {
076: MessageDialog
077: .showMessageDialog(
078: "This feature requires Windows NT 4, Windows 2000 or Windows XP.",
079: command);
080: return false;
081: }
082: }
083: return true;
084: }
085:
086: private static void compile(final String command,
087: final Editor editor) {
088: IdleThread.killFollowContextTask();
089: Editor.getTagFileManager().setEnabled(false);
090: saveCompilableBuffers(editor);
091:
092: CompilationBuffer cb = null;
093: boolean visible = false;
094:
095: if (lastCompilationBuffer != null) {
096: // Re-use existing compilation buffer.
097: cb = lastCompilationBuffer;
098: if (!Editor.getBufferList().contains(cb))
099: cb.relink();
100: cb.empty();
101: cb.setCommand(command);
102: cb.setParentBuffer(editor.getBuffer());
103: cb.setCurrentDirectory(editor.getCurrentDirectory());
104: // Is it visible?
105: EditorIterator it = new EditorIterator();
106: while (it.hasNext()) {
107: Editor ed = it.nextEditor();
108: if (ed.getBuffer() == cb) {
109: ed.updateLocation();
110: ed.repaintNow();
111: visible = true;
112: }
113: }
114: } else {
115: cb = new CompilationBuffer(command, editor
116: .getCurrentDirectory());
117: cb.setParentBuffer(editor.getBuffer());
118: lastCompilationBuffer = cb;
119: }
120:
121: // Call initialize() before starting the thread so we can put the
122: // expanded command in the location bar when the compilation buffer is
123: // activated.
124: cb.initialize();
125: // Don't keep a reference to the parent buffer indefinitely!
126: cb.setParentBuffer(null);
127: new Thread(cb).start();
128: if (!visible) {
129: Editor otherEditor = editor.getOtherEditor();
130: if (otherEditor != null) {
131: cb.setUnsplitOnClose(otherEditor.getBuffer()
132: .unsplitOnClose());
133: otherEditor.makeNext(cb);
134: } else
135: cb.setUnsplitOnClose(true);
136: editor.displayInOtherWindow(cb);
137: }
138: }
139:
140: public static void this Error() {
141: final Editor editor = Editor.currentEditor();
142: // If this method is invoked via a mouse event mapping, move dot to
143: // location of mouse click first.
144: AWTEvent e = editor.getDispatcher().getLastEvent();
145: if (e instanceof MouseEvent)
146: editor.mouseMoveDotToPoint((MouseEvent) e);
147: CompilationError error = CompilationError
148: .parseLineAsErrorMessage(editor.getDotLine());
149: if (error != null) {
150: final Buffer buffer = editor.getBuffer();
151: if (buffer instanceof CompilationErrorBuffer)
152: ((CompilationErrorBuffer) buffer)
153: .setCurrentError(error);
154: String errorFileName = error.getFileName();
155: int errorLineNumber = error.getLineNumber();
156: if (errorFileName != null && errorLineNumber != 0) {
157: Buffer buf = getSourceBuffer(buffer
158: .getCurrentDirectory(), errorFileName);
159: if (buf == null)
160: return;
161: Editor otherEditor = editor.getOtherEditor();
162: if (otherEditor != null)
163: otherEditor.makeNext(buf);
164: Editor ed = editor.activateInOtherWindow(buf);
165: int lineNumber = errorLineNumber - 1;
166: if (lineNumber < 0)
167: lineNumber = 0;
168: Position pos = buf.findOriginal(lineNumber, error
169: .getOffset());
170: ed.moveDotTo(pos);
171: ed.setUpdateFlag(REFRAME);
172: ed.updateDisplay();
173: Sidebar sidebar = editor.getSidebar();
174: if (sidebar != null)
175: sidebar.setUpdateFlag(SIDEBAR_BUFFER_LIST_ALL);
176: }
177: }
178: }
179:
180: public static void nextError() {
181: nextOrPreviousError(true);
182: }
183:
184: public static void previousError() {
185: nextOrPreviousError(false);
186: }
187:
188: private static void nextOrPreviousError(boolean next) {
189: final Editor editor = Editor.currentEditor();
190: CompilationErrorBuffer errorBuffer;
191: if (editor.getModeId() == XML_MODE)
192: errorBuffer = XmlMode.getErrorBuffer();
193: else
194: errorBuffer = lastCompilationBuffer;
195: if (errorBuffer == null)
196: return;
197: if (!Editor.getBufferList().contains(errorBuffer)) {
198: errorBuffer.relink();
199: Sidebar
200: .setUpdateFlagInAllFrames(SIDEBAR_BUFFER_LIST_CHANGED);
201: }
202: CompilationError error = next ? errorBuffer.nextError()
203: : errorBuffer.previousError();
204: if (error == null) {
205: editor.status("No more errors");
206: return;
207: }
208: boolean useOtherWindow = false;
209: // Find editor displaying error buffer (if any).
210: Editor ed = null;
211: for (EditorIterator it = new EditorIterator(); it.hasNext();) {
212: ed = it.nextEditor();
213: if (ed.getBuffer() == errorBuffer)
214: break;
215: }
216: if (ed.getBuffer() != errorBuffer) {
217: // The compilation buffer is not currently displayed.
218: ed = editor.displayInOtherWindow(errorBuffer);
219: } else if (ed == editor) {
220: // This command was invoked from the window displaying the
221: // compilation buffer.
222: useOtherWindow = true;
223: }
224: // Move caret to relevant line of error buffer.
225: Line errorLine = error.getErrorLine();
226: if (errorLine != null) {
227: Debug.assertTrue(ed.getBuffer() == errorBuffer);
228: ed.addUndo(SimpleEdit.MOVE);
229: ed.update(ed.getDotLine());
230: ed.setDot(errorLine, 0);
231: ed.update(ed.getDotLine());
232: ed.moveCaretToDotCol();
233: ed.getDisplay().setUpdateFlag(REFRAME);
234: ed.updateDisplay();
235: }
236: String errorFileName = error.getFileName();
237: int errorLineNumber = error.getLineNumber();
238: if (errorFileName != null && errorLineNumber != 0) {
239: // Find or create buffer for source file containing the error.
240: Buffer buf = getSourceBuffer(errorBuffer
241: .getCurrentDirectory(), errorFileName);
242: if (buf == null)
243: return;
244: Debug.assertTrue(ed.getBuffer() == errorBuffer);
245: if (useOtherWindow) {
246: Debug.assertTrue(ed == editor);
247: Editor otherEditor = editor.getOtherEditor();
248: if (otherEditor != null)
249: otherEditor.makeNext(buf);
250: ed = editor.activateInOtherWindow(buf);
251: } else {
252: ed = editor;
253: ed.makeNext(buf);
254: ed.activate(buf);
255: }
256: int lineNumber = errorLineNumber - 1;
257: if (lineNumber < 0)
258: lineNumber = 0;
259: Position pos = buf.findOriginal(lineNumber, error
260: .getOffset());
261: ed.moveDotTo(pos);
262: ed.setUpdateFlag(REFRAME);
263: ed.updateDisplay();
264: Sidebar sidebar = editor.getSidebar();
265: if (sidebar != null)
266: sidebar.setUpdateFlag(SIDEBAR_BUFFER_LIST_ALL);
267: } else
268: editor.status("No more errors");
269: }
270:
271: private static void saveCompilableBuffers(Editor editor) {
272: editor.setWaitCursor();
273: int numModified = 0;
274: int numErrors = 0;
275: for (BufferIterator it = new BufferIterator(); it.hasNext();) {
276: Buffer buf = it.nextBuffer();
277: if (!buf.isModified())
278: continue;
279: if (buf.isUntitled())
280: continue;
281: final int modeId = buf.getModeId();
282: if (modeId == SEND_MAIL_MODE)
283: continue;
284: if (modeId == CHECKIN_MODE)
285: continue;
286: if (buf.getFile() != null && buf.getFile().isLocal()) {
287: editor.status("Saving modified buffers...");
288: ++numModified;
289: if (buf
290: .getBooleanProperty(Property.REMOVE_TRAILING_WHITESPACE))
291: buf.removeTrailingWhitespace();
292: if (!buf.save())
293: ++numErrors;
294: }
295: }
296: if (numModified == 0)
297: ;
298: else if (numErrors == 0)
299: editor.status("Saving modified buffers...done");
300: else {
301: // User will already have seen detailed error information from
302: // Buffer.save().
303: editor.status("Unable to save all compilable buffers");
304: }
305: editor.setDefaultCursor();
306: }
307:
308: private static Buffer getSourceBuffer(File currentDirectory,
309: String errorFileName) {
310: File file = File.getInstance(currentDirectory, errorFileName);
311: if (!file.isFile()) {
312: // Strip path prefix.
313: file = File.getInstance(errorFileName);
314: String name = file.getName();
315: // Look in current directory.
316: file = File.getInstance(currentDirectory, name);
317: if (!file.isFile())
318: return null;
319: }
320: return Editor.getBuffer(file);
321: }
322:
323: public static void showMessage() {
324: final Editor editor = Editor.currentEditor();
325: CompilationErrorBuffer errorBuffer;
326: if (editor.getModeId() == XML_MODE)
327: errorBuffer = XmlMode.getErrorBuffer();
328: else
329: errorBuffer = lastCompilationBuffer;
330: if (errorBuffer != null) {
331: CompilationError error = errorBuffer.getCurrentError();
332: if (error != null) {
333: String message = error.getMessage();
334: if (message != null) {
335: int lineNumber = error.getLineNumber();
336: int columnNumber = -1;
337: int offset = error.getOffset();
338: if (offset >= 0)
339: columnNumber = offset + 1;
340: String title = "Line " + lineNumber;
341: if (columnNumber > 0)
342: title += " Col " + columnNumber;
343: if (message.length() > 65)
344: message = Utilities.wrap(message, 65, 8);
345: MessageDialog.showMessageDialog(editor, message,
346: title);
347: }
348: }
349: }
350: }
351: }
|