Source Code Cross Referenced for JEditPrettyPrinter.java in  » UML » jrefactory » org » acm » seguin » ide » jedit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » UML » jrefactory » org.acm.seguin.ide.jedit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.