C:\Java WorkShop\Lexx\src\lexx\plugins\ant\BuildProject.java

1    package lexx.plugins.ant; 
2     
3    import java.io.File; 
4    import java.io.IOException; 
5    import java.io.OutputStream; 
6     
7    import java.awt.BorderLayout; 
8    import java.awt.Color; 
9    import java.awt.Cursor; 
10   import java.awt.Point; 
11   import java.awt.event.ActionEvent; 
12   import java.awt.event.ActionListener; 
13   import java.awt.event.KeyEvent; 
14   import java.awt.event.KeyListener; 
15   import java.awt.event.MouseAdapter; 
16   import java.awt.event.MouseEvent; 
17   import java.awt.event.MouseMotionListener; 
18   import javax.swing.JButton; 
19   import javax.swing.JLabel; 
20   import javax.swing.JOptionPane; 
21   import javax.swing.JPanel; 
22   import javax.swing.JScrollPane; 
23   import javax.swing.JTextPane; 
24   import javax.swing.text.AttributeSet; 
25   import javax.swing.text.BadLocationException; 
26   import javax.swing.text.Style; 
27   import javax.swing.text.StyleConstants; 
28    
29   import org.apache.log4j.Logger; 
30   import lexx.gui.EditorFrame; 
31    
32   /** 
33    * <p>Compiles, runs and produces javadoc</p> 
34    * <p>Using Apacha Ant to compile, runs/produce javadoc</p> 
35    * <p>Copyright (c) 2002-2003</p> 
36    * @since 20/03/2003 
37    * @author Mohammed Imran 
38    * @version 1.0 
39    */ 
40   public final class BuildProject extends JPanel implements lexx.gui.Closeable 
41   { 
42     private static final Logger log = Logger.getLogger(BuildProject.class); 
43    
44     private static final String currentClass = System.getProperty("java.class.path") + ";" + 
45                                                System.getProperty("java.ext.dirs") + ";" + 
46                                                System.getProperty("sun.boot.class.path") + 
47                                                ";" + System.getProperty("user.dir") + 
48                                                "/Lexx.jar;" + 
49                                                lexx.RunApplet.codebase + ";Lexx.jar"; 
50    
51     private static final String sunTools = lexx.utils.Config.getJDKPath() + 
52                                            "/lib/tools.jar;" + 
53                                            lexx.utils.Config.getJDKPath() + 
54                                            "/lib/dt.jar" + ";" + currentClass; 
55     Process process; 
56    
57     /** 
58      * Generates build file for Apache Ant 
59      */ 
60     private static void buildFile(String projectName, String outputFile, 
61                                   String arguments) 
62     { 
63       String projectPath = lexx.utils.Config.getProjectPath(); 
64       String header = "<project name=\"" + projectName + 
65                       "\" default=\"dist\" basedir=\"" + projectPath + "\">\n"; 
66       header += 
67           "<description>\n        Automated build file\n    </description>\n"; 
68       header += "<!-- set global properties for this build -->\n  <property name=\"src\" location=\"src\"/>\n  <property name=\"build\" location=\"build\"/>\n  <property name=\"dist\"  location=\"dist\"/>\n"; 
69       String classpath = "<classpath>\n    <pathelement path=\"${classpath}\"/>\n    <fileset dir=\"lib\">\n        <include name=\"**/*.jar\"/>\n      <include name=\"**/*.class\"/>\n      </fileset>\n  </classpath>\n"; 
70       String target = "  <target name=\"init\">\n    <!-- Create the time stamp -->\n    <tstamp/>\n    <!-- Create the build directory structure used by compile -->\n    <mkdir dir=\"${build}\"/>\n  </target>\n  <target name=\"compile\" depends=\"init\"\n        description=\"compile the source \" >\n    <!-- Compile the java code from ${src} into ${build} -->\n    <javac srcdir=\"${src}\" destdir=\"${build}\" listfiles=\"true\"      deprecation=\"on\"      debug=\"on\">" + 
71                       classpath + "   </javac>\n" 
72                       + "<copy todir=\"${build}\"> \n        <fileset dir=\"src\"> \n          <exclude name=\"**/*.java\"/> \n        </fileset> \n      </copy> \n" + 
73                       "</target>\n\n  <target name=\"dist\" depends=\"compile\"\n        description=\"generate the distribution\" >\n    <!-- Create the distribution directory -->\n    <mkdir dir=\"${dist}/lib\"/>\n\n    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->\n    <jar jarfile=\"${dist}/lib/MyProject-${DSTAMP}.jar\" basedir=\"${build}\"/>\n  </target>\n\n  <target name=\"clean\"\n        description=\"clean up\" >\n    <!-- Delete the ${build} and ${dist} directory trees -->\n    <delete dir=\"${build}\"/>\n    <delete dir=\"${dist}\"/>\n  </target>\n" 
74                       + "<target name=\"createdoc\">\n <javadoc packagenames=\"*\" sourcepath=\"src\" destdir=\"docs\" author=\"true\" version=\"true\" use=\"true\">\n" + 
75                       classpath + "\n</javadoc>\n</target>\n"; 
76       String footer = "</project>\n"; 
77       String args = ""; 
78    
79       if(arguments != null) 
80       { 
81         args = "<arg value=\"" + arguments + "\"/>"; 
82    
83       } 
84       String buildFile = header + target + args + footer; 
85    
86       if(log.isDebugEnabled()) 
87       { 
88         log.info("buildFile(" + projectName + ", " + outputFile + ", " + 
89                  arguments + ")\n\n" + buildFile); 
90    
91       } 
92       if(outputFile == null) 
93       { 
94         outputFile = lexx.utils.Config.getProjectPath() + "/build.xml"; 
95    
96       } 
97       lexx.utils.FileUtil.overwriteFile(outputFile, buildFile); 
98     } 
99    
100    private String classPath = null; 
101   
102    public static String getLib(String projectPath) 
103    { 
104      String lib = projectPath + "/lib;" + projectPath + "/build;"; 
105      File[] files = new File(projectPath + "/lib").listFiles(); 
106   
107      if(files == null) 
108      { 
109        return ""; 
110      } 
111   
112      for(int i = 0; i < files.length; i++) 
113      { 
114        if(files[i].getName().endsWith("jar")) 
115        { 
116          lib += files[i].toString() + ";"; 
117        } 
118      } 
119      return lib.substring(0, lib.length() - 1); //remove last ; 
120    } 
121   
122    private String run = null; 
123    private boolean shouldDebug; 
124    protected int exitValue; 
125   
126    /** 
127     * Runs the users work 
128     * @param arguments arguments to add to java 
129     * @param runClassName the name of the class you wish to run 
130     * @param clPath any addition class path 
131     * @param debug this should be false as debugging is still under construction 
132     */ 
133    public final void runFile(String outputFile, String arguments, 
134                              String runClassName, String clPath, boolean debug) 
135    { 
136      stop.setEnabled(false); 
137      //this.compileAndWait(outputFile); 
138      stop.setEnabled(true); 
139      shouldDebug = debug; 
140   
141      run = runClassName; 
142      if(arguments != null) 
143      { 
144        run += " " + arguments; 
145   
146      } 
147      this.check(); 
148   
149      status.setText("Running: " + runClassName); 
150   
151      String p = lexx.utils.Config.getProjectPath(); 
152      if(outputFile != null) 
153      { 
154        p = new File(outputFile).getPath(); 
155   
156      } 
157      if(clPath == null) 
158      { 
159        clPath = ""; 
160   
161      } 
162      clPath += ";"; 
163   
164      classPath = clPath + getLib(p) + ";" + sunTools; 
165      while(classPath.endsWith(";")) 
166      { 
167        classPath = classPath.substring(0, classPath.length() - 1); 
168      } 
169   
170      String d = lexx.utils.Config.getJavaRunTimePath() + "java"; 
171      if(shouldDebug) 
172      { 
173        d = lexx.utils.Config.getJavaRunTimePath() + "jdb"; 
174   
175      } 
176      String r = d + " -classpath \"" + classPath + "\" " + run; 
177   
178      new ProcessHandler(r, this) 
179      { 
180        public String handleNormalMessage(String text) 
181        { 
182          try 
183          { 
184            if(text.trim().startsWith("[")) 
185            { 
186              String tmp = ""; 
187              boolean ignore = false; 
188              for(int i = 0; i < text.length(); i++) 
189              { 
190                if(text.charAt(i) == '[') 
191                { 
192                  ignore = true; 
193   
194                } 
195                if(ignore) 
196                { 
197                  if(text.charAt(i) == ']') 
198                  { 
199                    ignore = false; 
200                  } 
201                } 
202                else 
203                { 
204                  tmp += text.charAt(i); 
205                } 
206              } 
207              pane.getDocument().insertString(pane.getDocument().getLength(), tmp, 
208                                              pane.getParagraphAttributes()); 
209            } 
210            else 
211            { 
212              pane.getDocument().insertString(pane.getDocument().getLength(), 
213                                              text, pane.getParagraphAttributes()); 
214   
215            } 
216            pane.setCaretPosition(pane.getDocument().getLength()); 
217            return ""; 
218          } 
219          catch(BadLocationException ex) 
220          { 
221            log.fatal(ex, ex); 
222          } 
223          return ""; 
224        } 
225   
226        public String handleErrorMessage(String error) 
227        { 
228          int originalPos = pane.getDocument().getLength(); 
229          try 
230          { 
231            String tmp = error; 
232            boolean foundError = false; 
233            for(int x = 0; x < tmp.length(); x++) 
234            { 
235              if(x + 1 < tmp.length() && tmp.charAt(x) == '(') 
236              { 
237                foundError = true; 
238                x++; 
239                String before = tmp.substring(0, x); 
240                pane.getDocument().insertString(pane.getDocument().getLength(), 
241                                                before, errorSet); 
242   
243                String hyperlink = ""; 
244                for(int i = before.length(); i < tmp.length(); i++) 
245                { 
246                  if(tmp.charAt(i) == ')') 
247                  { 
248                    pane.getDocument().insertString(pane.getDocument().getLength(), 
249                                                    hyperlink, hyperLinkSet); 
250                    pane.getDocument().insertString(pane.getDocument().getLength(), 
251                                                    tmp.substring(i, tmp.length()), 
252                                                    errorSet); 
253                    break; 
254                  } 
255                  hyperlink += tmp.charAt(i); 
256                } 
257                break; 
258              } 
259            } 
260   
261            if(!foundError) 
262            { 
263              pane.getDocument().insertString(originalPos, error, errorSet); 
264   
265            } 
266            pane.setCaretPosition(pane.getDocument().getLength()); 
267            return ""; 
268          } 
269          catch(Exception ex) 
270          { 
271            try 
272            { 
273              if(originalPos != pane.getDocument().getLength()) 
274              { 
275                pane.getDocument().remove(originalPos, 
276                                          pane.getDocument().getLength() - 
277                                          originalPos); 
278              } 
279              pane.getDocument().insertString(pane.getDocument(). 
280                                              getLength(), error, errorSet); 
281            } 
282            catch(BadLocationException ex1) 
283            { 
284              log.fatal(ex1, ex1); 
285            } 
286          } 
287   
288          return ""; 
289        } 
290      }.start(); 
291    } 
292   
293    public final void waitUntilFinished() 
294    { 
295      while(process != null) 
296      { 
297        try 
298        { 
299          Thread.sleep(400); 
300        } 
301        catch(InterruptedException ex) 
302        {} 
303      } 
304    } 
305   
306    public final void waitUntilStartAndFinished() 
307    { 
308      if(!inUse) 
309      { 
310        while(!inUse) 
311        { 
312          try 
313          { 
314            Thread.sleep(100); 
315          } 
316          catch(InterruptedException ex) 
317          {} 
318        } 
319      } 
320   
321      while(inUse) 
322      { 
323        try 
324        { 
325          Thread.sleep(400); 
326        } 
327        catch(InterruptedException ex) 
328        {} 
329      } 
330    } 
331   
332    public final void checkStyle() 
333    { 
334      status.setText("Checking style of your source code"); 
335   
336      try 
337      { 
338        pane.getDocument().remove(0, pane.getDocument().getLength()); 
339        pane.getDocument().insertString(pane.getDocument().getLength(), 
340                                        "Checking style - Scanning Files\n", 
341                                        finishedSet); 
342      } 
343      catch(BadLocationException ex) 
344      {} 
345   
346      this.runFile(null, 
347                   " -r \"" + lexx.utils.Config.getSourceCodePath() + 
348                   File.pathSeparator + "\"", 
349                   "com.puppycrawl.tools.checkstyle.Main", 
350                   System.getProperty("java.class.path"), false); 
351    } 
352   
353    public BuildProject(ShowJava showJava) 
354    { 
355      this.buildFile("lexx", null, null); 
356      show = showJava; 
357      path = lexx.utils.Config.getProjectPath(); 
358      pane = new JTextPane(); 
359      Style error = pane.addStyle("error", pane.getLogicalStyle()); 
360      StyleConstants.setForeground(error, Color.RED); 
361      Style warning = pane.addStyle("warning", pane.getLogicalStyle()); 
362      StyleConstants.setForeground(warning, Color.ORANGE.darker()); 
363      Style ok = pane.addStyle("ok", pane.getLogicalStyle()); 
364      StyleConstants.setForeground(ok, Color.GREEN.darker().darker()); 
365      Style finished = pane.addStyle("finished", pane.getLogicalStyle()); 
366      StyleConstants.setForeground(finished, Color.MAGENTA); 
367      Style hyperlink = pane.addStyle("hyperlink", pane.getLogicalStyle()); 
368      StyleConstants.setForeground(hyperlink, Color.BLUE); 
369      StyleConstants.setUnderline(hyperlink, true); 
370   
371      errorSet = error.copyAttributes(); 
372      okSet = ok.copyAttributes(); 
373      finishedSet = finished.copyAttributes(); 
374      hyperLinkSet = hyperlink.copyAttributes(); 
375      warningSet = warning.copyAttributes(); 
376   
377      this.setLayout(new BorderLayout()); 
378      this.add(new JScrollPane(pane), BorderLayout.CENTER); 
379      JPanel bottomPanel = new JPanel(); 
380      bottomPanel.add(stop); 
381      bottomPanel.add(status); 
382      JPanel tmp = new JPanel(new BorderLayout()); 
383      tmp.add(bottomPanel, BorderLayout.WEST); 
384      this.add(tmp, BorderLayout.SOUTH); 
385      stop.setEnabled(false); 
386      stop.setText("Stop"); 
387   
388      stop.addActionListener(new ActionListener() 
389      { 
390        public void actionPerformed(ActionEvent e) 
391        { 
392          if(process != null) 
393          { 
394            process.destroy(); 
395            try 
396            { 
397              pane.getDocument().insertString(pane.getDocument().getLength(), 
398                                              "\n\n\nStopped by user\n", errorSet); 
399            } 
400            catch(BadLocationException ex) 
401            {} 
402            stop.setEnabled(false); 
403            exitValue = -1; 
404            unFreeze(); 
405          } 
406        } 
407      }); 
408   
409      pane.addMouseMotionListener(new MouseMotionListener() 
410      { 
411        public void mouseDragged(MouseEvent e) 
412        {} 
413   
414        public void mouseMoved(MouseEvent e) 
415        { 
416          if(isHyperLink(e)) 
417          { 
418            pane.setCursor(new Cursor(Cursor.HAND_CURSOR)); 
419          } 
420          else 
421          { 
422            pane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 
423          } 
424        } 
425   
426        public boolean isHyperLink(MouseEvent e) 
427        { 
428          Point pt = new Point(e.getX(), e.getY()); 
429          int pos = pane.viewToModel(pt); 
430          if(pos >= 0) 
431          { 
432            try 
433            { 
434              int start = pane.getStyledDocument().getCharacterElement(pos). 
435                          getStartOffset(); 
436              int length = pane.getStyledDocument().getCharacterElement(pos). 
437                           getEndOffset() - start; 
438              String text = pane.getStyledDocument().getText(start, length); 
439   
440              //checking if text if valid 
441              boolean isDigit = false; 
442              for(int i = text.length() - 1; i >= 0; i--) 
443              { 
444                if(!isDigit && !Character.isDigit(text.charAt(i))) 
445                { 
446                  return false; 
447                } 
448   
449                if(Character.isDigit(text.charAt(i))) 
450                { 
451                  isDigit = true; 
452                } 
453                else 
454                { 
455                  if(i - ".java:".length() >= 0 && 
456                     ".java:".equals(text.substring(i - ".java:".length() + 1, 
457                                                    i + 1))) 
458                  { //found hyper now need to link with 
459                    String name = text.substring(0, i); 
460                    int lineNum = Integer.parseInt(text.substring(i + 1, 
461                        text.length())); 
462                    return true; 
463                  } 
464                  else 
465                  { 
466                    return false; 
467                  } 
468                } 
469              } 
470            } 
471            catch(NumberFormatException ex) 
472            { 
473              log.fatal(ex, ex); 
474            } 
475            catch(BadLocationException ex) 
476            { 
477              log.fatal(ex, ex); 
478            } 
479          } 
480          return false; 
481        } 
482      }); 
483   
484      pane.addMouseListener(new MouseAdapter() 
485      { 
486        public void mouseClicked(MouseEvent e) 
487        { 
488          Point pt = new Point(e.getX(), e.getY()); 
489          int pos = pane.viewToModel(pt); 
490          if(pos >= 0) 
491          { 
492            try 
493            { 
494              int start = pane.getStyledDocument().getCharacterElement(pos). 
495                          getStartOffset(); 
496              int length = pane.getStyledDocument().getCharacterElement(pos). 
497                           getEndOffset() - start; 
498              String text = pane.getStyledDocument().getText(start, length); 
499   
500              //checking if text if valid 
501              boolean isDigit = false; 
502              for(int i = text.length() - 1; i >= 0; i--) 
503              { 
504                if(!isDigit && !Character.isDigit(text.charAt(i))) 
505                { 
506                  return; 
507                } 
508   
509                if(Character.isDigit(text.charAt(i))) 
510                { 
511                  isDigit = true; 
512                } 
513                else 
514                { 
515                  if(i - ".java:".length() >= 0 && 
516                     ".java:".equals(text.substring(i - ".java:".length() + 1, 
517                                                    i + 1))) 
518                  { //found hyper now need to link with 
519                    String name = text.substring(0, i); 
520                    int lineNum = Integer.parseInt(text.substring(i + 1, 
521                        text.length())); 
522                    show.gotoLine(name, lineNum); 
523                  } 
524                  else 
525                  { 
526                    return; 
527                  } 
528                } 
529              } 
530            } 
531            catch(NumberFormatException ex) 
532            { 
533              log.fatal(ex, ex); 
534            } 
535            catch(BadLocationException ex) 
536            { 
537              log.fatal(ex, ex); 
538            } 
539          } 
540        } 
541      }); 
542      pane.setEditable(false); 
543   
544      pane.addKeyListener(new KeyListener() 
545      { 
546        public void keyTyped(KeyEvent e) 
547        { 
548          if(process != null) 
549          { 
550            OutputStream out = process.getOutputStream(); 
551            try 
552            { 
553   
554              out.write( ("" + e.getKeyChar()).getBytes()); 
555            } 
556            catch(IOException ex) 
557            {} 
558          } 
559        } 
560   
561        public void keyPressed(KeyEvent e) 
562        {} 
563   
564        public void keyReleased(KeyEvent e) 
565        {} 
566   
567      }); 
568    } 
569   
570    private ShowJava show = null; 
571    private JTextPane pane = null; 
572    final JLabel status = new JLabel(); 
573    final JButton stop = new JButton(lexx.images.Icons.getImageIcon( 
574        "suspend.gif")); 
575   
576    private boolean inUse = false; 
577    private String buildFileName = null; 
578    private AttributeSet errorSet = null; 
579    private AttributeSet okSet = null; 
580    private AttributeSet finishedSet = null; 
581    private AttributeSet hyperLinkSet = null; 
582    private AttributeSet warningSet = null; 
583    private String path = null; 
584   
585    private synchronized void check() 
586    { 
587      while(inUse) 
588      { 
589        try 
590        { 
591          Thread.sleep(250); 
592        } 
593        catch(InterruptedException ex) 
594        {} 
595      } 
596      inUse = true; 
597    } 
598   
599    final void unFreeze() 
600    { 
601      inUse = false; 
602    } 
603   
604    public final void clean(String fileName) 
605    { 
606      this.check(); 
607      status.setText("Deleting old files"); 
608      if(fileName == null || "".equals(fileName)) 
609      { 
610        fileName = lexx.utils.Config.getProjectPath() + "/build.xml"; 
611      } 
612      buildFileName = fileName; 
613   
614      classPath = lexx.utils.Config.getProjectPath() + "/build;" + 
615                  getLib(lexx.utils.Config.getProjectPath()) + ";" + currentClass + 
616                  ";" + 
617                  sunTools; 
618      while(classPath.endsWith(";")) 
619      { 
620        classPath = classPath.substring(0, classPath.length() - 1); 
621   
622      } 
623      String r = lexx.utils.Config.getJavaRunTimePath() + "java -classpath \"" + 
624                 System.getProperty("java.class.path") + ";" + classPath + 
625                 "\" org.apache.tools.ant.Main -buildfile \"" + buildFileName + 
626                 "\" clean"; 
627   
628      new ProcessHandler(r, this) 
629      { 
630        public String handleErrorMessage(String message) 
631        { 
632          try 
633          { 
634            pane.getDocument().insertString(pane.getDocument().getLength(), 
635                                            message, errorSet); 
636            pane.setCaretPosition(pane.getDocument().getLength()); 
637            message = ""; 
638          } 
639          catch(BadLocationException ex) 
640          { 
641            ex.printStackTrace(); 
642          } 
643   
644          return message; 
645        } 
646   
647        public String handleNormalMessage(String message) 
648        { 
649          try 
650          { 
651            pane.getDocument().insertString(pane.getDocument().getLength(), 
652                                            message, 
653                                            pane.getParagraphAttributes()); 
654            pane.setCaretPosition(pane.getDocument().getLength()); 
655            return ""; 
656          } 
657          catch(BadLocationException ex) 
658          { 
659            ex.printStackTrace(); 
660          } 
661   
662          return message; 
663        } 
664      }.start(); 
665    } 
666   
667    private String antTask = null; 
668   
669    public final void javadoc(String fileName) 
670    { 
671      this.runTask(fileName, "Building javadoc", "createdoc"); 
672    } 
673   
674    private void runTask(String fileName, String statusInfo, String task) 
675    { 
676      this.check(); 
677      status.setText(statusInfo); 
678      antTask = task; 
679   
680      try 
681      { 
682        pane.getDocument().remove(0, pane.getDocument().getLength()); 
683        pane.getDocument().insertString(pane.getDocument().getLength(), 
684                                        "Scanning Files\n", finishedSet); 
685      } 
686      catch(BadLocationException ex) 
687      {} 
688   
689      if(fileName == null || "".equals(fileName)) 
690      { 
691        fileName = lexx.utils.Config.getProjectPath() + "/build.xml"; 
692   
693      } 
694      buildFileName = fileName; 
695      classPath = lexx.utils.Config.getProjectPath() + "/build;" + 
696                  getLib(lexx.utils.Config.getProjectPath()) + ";" + sunTools + 
697                  ";" + 
698                  currentClass; 
699      while(classPath.endsWith(";")) 
700      { 
701        classPath = classPath.substring(0, classPath.length() - 1); 
702   
703      } 
704   
705      String r = lexx.utils.Config.getJavaRunTimePath() + "java -classpath \"" + 
706                 System.getProperty("java.class.path") + classPath + 
707                 "\" org.apache.tools.ant.Main -buildfile \"" + buildFileName + 
708                 "\" " + 
709                 antTask; 
710      new ProcessHandler(r, this) 
711      { 
712        public String handleErrorMessage(String message) 
713        { 
714          try 
715          { 
716            pane.getDocument().insertString(pane.getDocument().getLength(), 
717                                            message, errorSet); 
718            pane.setCaretPosition(pane.getDocument().getLength()); 
719            message = ""; 
720          } 
721          catch(BadLocationException ex) 
722          { 
723            ex.printStackTrace(); 
724          } 
725   
726          return message; 
727        } 
728   
729        public String handleNormalMessage(String message) 
730        { 
731          try 
732          { 
733            pane.getDocument().insertString(pane.getDocument().getLength(), 
734                                            message, 
735                                            pane.getParagraphAttributes()); 
736            pane.setCaretPosition(pane.getDocument().getLength()); 
737            return ""; 
738          } 
739          catch(BadLocationException ex) 
740          { 
741            ex.printStackTrace(); 
742          } 
743   
744          return message; 
745        } 
746      }.start(); 
747    } 
748   
749    public final void compile(String fileName) 
750    { 
751      this.check(); 
752      status.setText("Compiling..."); 
753   
754      try 
755      { 
756        pane.getDocument().remove(0, pane.getDocument().getLength()); 
757        pane.getDocument().insertString(pane.getDocument().getLength(), 
758                                        "Scanning Files\n", finishedSet); 
759      } 
760      catch(BadLocationException ex) 
761      {} 
762   
763      if(fileName == null || "".equals(fileName)) 
764      { 
765        fileName = lexx.utils.Config.getProjectPath() + "/build.xml"; 
766   
767      } 
768      buildFileName = fileName; 
769   
770      this.buildFile("Lexx", null, null); 
771      classPath = lexx.utils.Config.getProjectPath() + "/build;" + currentClass + 
772                  ";" + 
773                  sunTools; 
774      while(classPath.endsWith(";")) 
775      { 
776        classPath = classPath.substring(0, classPath.length() - 1); 
777   
778      } 
779      String r = lexx.utils.Config.getJavaRunTimePath() + "java -classpath \"" + 
780                 System.getProperty("java.class.path") + classPath + 
781                 "\" org.apache.tools.ant.Main -buildfile \"" + buildFileName + 
782                 "\" compile"; 
783   
784      new ProcessHandler(r, this) 
785      { 
786        public String handleNormalMessage(String text) 
787        { 
788          try 
789          { 
790            String tmp = text.trim(); 
791   
792            if(tmp.startsWith("BUILD") || tmp.startsWith("Total time:") || 
793               tmp.startsWith("[javac]")) 
794            { 
795              if(tmp.startsWith("[javac]")) 
796              { 
797                tmp = "  " + tmp.substring(7, tmp.length()).trim() + "\n"; 
798   
799                if(tmp.trim().startsWith("Compiling ")) 
800                { 
801                  String num = ""; 
802                  String t = tmp.trim(); 
803                  for(int i = "Compiling ".length(); i < t.length(); i++) 
804                  { 
805                    if(Character.isWhitespace(t.charAt(i))) 
806                    { 
807                      break; 
808                    } 
809                    if(Character.isDigit(t.charAt(i))) 
810                    { 
811                      num += t.charAt(i); 
812                    } 
813                  } 
814                } 
815   
816                pane.getDocument().insertString(pane.getDocument().getLength(), 
817                                                tmp, okSet); 
818              } 
819              else 
820              { 
821                pane.getDocument().insertString(pane.getDocument().getLength(), 
822                                                tmp + "\n", finishedSet); 
823              } 
824            } 
825            else 
826            { 
827              pane.getDocument().insertString(pane.getDocument().getLength(), 
828                                              tmp + "\n", finishedSet); 
829            } 
830            text = ""; 
831            pane.setCaretPosition(pane.getDocument().getLength()); 
832          } 
833          catch(BadLocationException ex) 
834          {} 
835          return text; 
836        } 
837   
838        public String handleErrorMessage(String error) 
839        { 
840          try 
841          { 
842            String tmp = error.trim(); 
843            if(tmp.startsWith("[javac]")) 
844            { 
845              String spaces = "      "; 
846              tmp = spaces + tmp.substring(7, tmp.length()).trim() + "\n"; 
847              boolean foundLink = false; 
848   
849              if(lexx.utils.StringSearch.contains(path, error)) 
850              { 
851                foundLink = true; 
852              } 
853   
854              if(foundLink) 
855              { 
856                String before = null; 
857                int originalPos = pane.getDocument().getLength(); 
858                try 
859                { 
860                  before = tmp.substring(0, 
861                                         spaces.length() + path.length() + 
862                                         "/src".length() + 1); 
863                  AttributeSet set = errorSet; 
864   
865                  if(lexx.utils.StringSearch.contains("has been deprecated", tmp)) 
866                  { 
867                    set = warningSet; 
868   
869                  } 
870                  pane.getDocument().insertString(pane.getDocument().getLength(), 
871                                                  before, set); 
872   
873                  boolean foundSemi = false; 
874                  String hyperlink = ""; 
875                  for(int i = before.length(); i < tmp.length(); i++) 
876                  { 
877                    if(tmp.charAt(i) == ':') 
878                    { 
879                      if(foundSemi) 
880                      { 
881                        pane.getDocument().insertString(pane.getDocument(). 
882                            getLength(), hyperlink, hyperLinkSet); 
883                        pane.getDocument().insertString(pane.getDocument(). 
884                            getLength(), tmp.substring(i, tmp.length()), set); 
885                        break; 
886                      } 
887                      else 
888                      { 
889                        foundSemi = true; 
890                      } 
891                    } 
892                    hyperlink += tmp.charAt(i); 
893                  } 
894                } 
895                catch(Exception ex) 
896                { 
897                  ex.printStackTrace(); 
898                  try 
899                  { 
900                    if(originalPos != pane.getDocument().getLength()) 
901                    { 
902                      pane.getDocument().remove(originalPos, 
903                                                pane.getDocument().getLength() - 
904                                                originalPos); 
905                    } 
906                    pane.getDocument().insertString(pane.getDocument(). 
907                                                    getLength(), tmp, errorSet); 
908                    pane.setCaretPosition(pane.getDocument().getLength()); 
909                    return ""; 
910                  } 
911                  catch(BadLocationException ex1) 
912                  {} 
913                } 
914   
915              } 
916              else 
917              { 
918                if(lexx.utils.StringSearch.contains('^', tmp)) 
919                { 
920                  tmp += '\n'; 
921                } 
922                pane.getDocument().insertString(pane.getDocument().getLength(), 
923                                                tmp, errorSet); 
924              } 
925            } 
926            else 
927            { 
928              /** CHECKING IF THIS IS REQUIRED */ 
929              pane.getDocument().insertString(pane.getDocument().getLength(), tmp, 
930                                              errorSet); 
931            } 
932          } 
933          catch(Exception ex) 
934          {} 
935          pane.setCaretPosition(pane.getDocument().getLength()); 
936          return ""; 
937        } 
938   
939        public void finished() 
940        { 
941          if(getExitValue() == 0) 
942          { 
943            new Thread() 
944            { 
945              public void run() 
946              { 
947                lexx.utils.Config.updatePackagesClasses(); 
948              } 
949            }.start(); 
950          } 
951   
952          //updating server - if compiled sucessfully 
953          if(lexx.utils.Config.getClient() != null && getExitValue() == 0) 
954          { 
955            try 
956            { 
957              pane.getDocument().insertString(pane.getDocument().getLength(), 
958                  "\n\nUpdating files to server, please wait....", okSet); 
959              pane.setCaretPosition(pane.getDocument().getLength()); 
960            } 
961            catch(BadLocationException ex) 
962            {} 
963   
964            if(lexx.utils.Config.getFrameForDialog()instanceof EditorFrame) 
965            { 
966              EditorFrame e = (EditorFrame) lexx.utils.Config.getFrameForDialog(); 
967              e.sendOpenFilesToServer(); 
968            } 
969          } 
970        } 
971      }.start(); 
972    } 
973   
974    public final int compileAndWait(String fileName) 
975    { 
976      this.compile(fileName); 
977      this.waitUntilStartAndFinished(); 
978      return exitValue; 
979    } 
980   
981    public final boolean close() 
982    { 
983      if(inUse) 
984      { 
985        lexx.utils.Config.beep(); 
986        int ans = JOptionPane.showConfirmDialog(this, 
987            "Do you wish to keep following process active"); 
988        if(ans == JOptionPane.YES_OPTION || ans == JOptionPane.CANCEL_OPTION) 
989        { 
990          return false; 
991        } 
992        else 
993        { 
994          process.destroy(); 
995          return true; 
996        } 
997      } 
998      else 
999      { 
1000       return true; 
1001     } 
1002   } 
1003 }