001: package org.acm.seguin.ide.jedit;
002:
003: import java.io.File;
004: import java.io.StringWriter;
005: import java.io.Writer;
006: import java.util.Enumeration;
007: import java.util.Iterator;
008: import java.util.Vector;
009:
010: import errorlist.DefaultErrorSource;
011: import net.sourceforge.jrefactory.ast.SimpleNode;
012: import net.sourceforge.jrefactory.factory.BufferParserFactory;
013: import net.sourceforge.jrefactory.parser.ParseException;
014: import net.sourceforge.jrefactory.parser.Token;
015:
016: import org.acm.seguin.awt.ExceptionPrinter;
017: import org.acm.seguin.ide.common.options.PropertiesFile;
018: import org.acm.seguin.pretty.PrettyPrintFile;
019: import org.acm.seguin.project.Project;
020: import org.gjt.sp.jedit.*;
021: import org.gjt.sp.jedit.io.VFSManager;
022: import org.gjt.sp.jedit.textarea.JEditTextArea;
023: import org.gjt.sp.util.Log;
024:
025: // note: JEditPrettyPrinter is _not_ derived from PrettyPrintFromIDE
026: // (like it should be), because then we cannot handle the parse
027: // exceptions ourselves.
028: /**
029: * Description of the Class
030: *
031: * @author Mike Atkinson (Mike)
032: * @since 1.0
033: * @created 14 July 2003
034: */
035: public class JEditPrettyPrinter extends PrettyPrintFile implements
036: Runnable {
037: protected JavaStyleExceptionPrinter exceptionPrinter = new JavaStyleExceptionPrinter();
038: protected Buffer buffer;
039: protected Writer writer;
040: protected JavaStylePlugin jsPlugin;
041:
042: protected View view;
043: protected static Throwable exception = null;
044:
045: /**
046: * Constructor for the JEditPrettyPrinter object
047: *
048: * @param jsPlugin Description of Parameter
049: * @param view Description of Parameter
050: * @param buffer Description of Parameter
051: * @since v 1.0
052: */
053: public JEditPrettyPrinter(JavaStylePlugin jsPlugin, View view,
054: Buffer buffer) {
055: super ();
056: super .setAsk(false);
057: //Log.log(Log.DEBUG, this, "JEditPrettyPrinter()");
058: this .jsPlugin = jsPlugin;
059: this .view = view;
060: this .buffer = buffer;
061: this .writer = new StringWriter();
062: JavaStylePlugin.getErrorSource().clear();
063: ExceptionPrinter.register(exceptionPrinter);
064: }
065:
066: /**
067: * Main processing method for the JEditPrettyPrinter object
068: *
069: * @since v 1.0
070: */
071: public void run() {
072: Log.log(Log.DEBUG, this , "formatting the buffer...");
073:
074: int caretPos = 0;
075: JEditTextArea textarea = null;
076:
077: try {
078: if (view != null) {
079: view.showWaitCursor();
080: textarea = view.getTextArea();
081: caretPos = textarea.getCaretPosition();
082: }
083:
084: setProjectData(view, buffer);
085: setSettings();
086:
087: // set settings from jEdit
088: // get text string
089: String before = buffer.getText(0, buffer.getLength());
090: setInputString(before);
091:
092: // remember and remove all markers:
093: Vector markers = (Vector) buffer.getMarkers().clone();
094:
095: apply(null);
096:
097: // JRefactory reformat (may cause exception to be set).
098: String contents = getOutputBuffer();
099:
100: // get the new contents back
101: if (exception != null) {
102: exception.printStackTrace();
103: // the JRefactory reformatting caused an exception, so handle it.
104: if (exception instanceof ParseException) {
105: Token currentToken = ((ParseException) exception).currentToken;
106: JavaStylePlugin.getErrorSource().clear();
107: JavaStylePlugin
108: .getErrorSource()
109: .addError(
110: DefaultErrorSource.ERROR,
111: buffer.getPath(),
112: Math
113: .max(
114: 0,
115: currentToken.next.beginLine - 1),
116: Math
117: .max(
118: 0,
119: currentToken.next.beginColumn - 1),
120: Math
121: .max(
122: 0,
123: currentToken.next.endColumn),
124: exception.getMessage());
125: } else {
126: exception.printStackTrace();
127: // no idea what caused this exception
128: Log.log(Log.ERROR, this , exception);
129: }
130: exception = null;
131:
132: } else if (contents == null || contents.length() == 0) {
133: // we did not get an exception but JRefactory still did not produce valid contents
134: GUIUtilities.error(view, "javastyle.error.other", null);
135: } else if (!contents.equals(before)) {
136: // The JRefactory reformatting caused a change in the text so perform the edit.
137: try {
138: buffer.beginCompoundEdit();
139: if (markers.size() > 0) {
140: buffer.removeAllMarkers();
141: }
142:
143: buffer.remove(0, buffer.getLength());
144: buffer.insert(0, contents);
145:
146: // restore markers:
147: Enumeration enumx = markers.elements();
148:
149: while (enumx.hasMoreElements()) {
150: Marker marker = (Marker) enumx.nextElement();
151:
152: buffer.addMarker(marker.getShortcut(), marker
153: .getPosition());
154: }
155: // restore caret position
156: if (textarea != null) {
157: int bufLen = textarea.getBufferLength();
158:
159: textarea
160: .setCaretPosition(caretPos > bufLen ? bufLen
161: : caretPos);
162: textarea.scrollToCaret(true);
163: }
164: } catch (Exception ex) {
165: ex.printStackTrace();
166: Log.log(Log.ERROR, this , ex);
167: } finally {
168: buffer.endCompoundEdit();
169: }
170: }
171: } catch (Throwable ex) {
172: ex.printStackTrace();
173: Log.log(Log.ERROR, this , ex);
174: GUIUtilities.error(view, "javastyle.error.parse",
175: new Object[] { ex.toString() });
176: } finally {
177: if (view != null) {
178: view.hideWaitCursor();
179: }
180: }
181: }
182:
183: /**
184: * Sets the InputString attribute of the JEditPrettyPrinter object
185: *
186: * @param input The new InputString value
187: * @since v 1.0
188: */
189: protected void setInputString(String input) {
190: if (input == null) {
191: return;
192: }
193: setParserFactory(new BufferParserFactory(input));
194: }
195:
196: /**
197: * Gets the OutputBuffer attribute of the JEditPrettyPrinter object
198: *
199: * @return The OutputBuffer value
200: * @since v 1.0
201: */
202: protected String getOutputBuffer() {
203: return writer.toString();
204: }
205:
206: /**
207: * Gets the OutputStream attribute of the JEditPrettyPrinter object
208: *
209: * @param file Description of Parameter
210: * @return The Writer value
211: * @since v 1.0
212: */
213: protected Writer getWriter(File file) {
214: return writer;
215: }
216:
217: /**
218: * After we have applied the pretty printing
219: *
220: * @param inputFile Description of Parameter
221: * @param root Description of Parameter
222: * @since v 1.0
223: */
224: protected void postApply(File inputFile, SimpleNode root) {
225: PropertiesFile props = jsPlugin.getProperties("pretty",
226: jsPlugin.getProjectName(view, buffer));
227: if (props.getBoolean("checkOnSave", false)) {
228: jsPlugin.instanceCheck(view, buffer, true);
229: }
230: }
231:
232: /**
233: * Sets the Settings attribute of the JEditPrettyPrinter object
234: *
235: * @since v 1.0
236: */
237: protected void setSettings() {
238: // determine new settings:
239: boolean noTabs = buffer.getMode().getBooleanProperty("noTabs");
240: Object indentSizeProp = buffer.getMode().getProperty(
241: "indentSize");
242: String indentSize = "4";
243:
244: if (indentSizeProp != null) {
245: indentSize = indentSizeProp.toString();
246: }
247: JavaStylePlugin.setProperty("indent.char", noTabs ? "space"
248: : "tab");
249: JavaStylePlugin
250: .setProperty("indent", noTabs ? indentSize : "1");
251: JavaStylePlugin.setProperty("end.line", "NL");
252:
253: // save settings:
254: jsPlugin.saveProperties();
255: }
256:
257: /**
258: * Sets the projectData attribute of the JEditPrettyPrinter object
259: *
260: * @param view The new ProjectData value
261: * @param buffer The new ProjectData value
262: * @since v 1.0
263: */
264: protected static void setProjectData(View view, Buffer buffer) {
265: try {
266: String path = buffer.getPath();
267: Class clazz = Class.forName("projectviewer.ProjectViewer");
268: //projectviewer.ProjectViewer viewer = projectviewer.ProjectViewer.getViewer(view);
269: projectviewer.ProjectManager manager = projectviewer.ProjectManager
270: .getInstance();
271: //List projs = new ArrayList();
272:
273: for (Iterator i = manager.getProjects(); i.hasNext();) {
274: projectviewer.vpt.VPTProject proj = (projectviewer.vpt.VPTProject) i
275: .next();
276:
277: if (proj.isProjectFile(path)) {
278: Project project = Project
279: .getProject(proj.getName());
280:
281: if (project == null) {
282: project = org.acm.seguin.project.Project
283: .createProject(proj.getName());
284: }
285:
286: org.acm.seguin.project.Project
287: .setCurrentProject(project);
288: break;
289: }
290: }
291: } catch (ClassNotFoundException e) {
292: }
293: }
294:
295: /**
296: * Prints to stdout and includes a dialog box
297: *
298: * @author Chris Seguin
299: * @since 2.6.33
300: * @created December 6, 2001
301: */
302: protected class JavaStyleExceptionPrinter extends ExceptionPrinter {
303: /**
304: * Prints exceptions
305: *
306: * @param exc Description of Parameter
307: * @param interactive Description of Parameter
308: * @since 2.6.33
309: */
310: public void printException(Throwable exc, boolean interactive) {
311: exception = exc;
312: }
313: }
314:
315: }
|