KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > CompilerErrorPanel


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32 END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import edu.rice.cs.drjava.DrJava;
37 import edu.rice.cs.drjava.config.OptionConstants;
38 import edu.rice.cs.drjava.config.OptionEvent;
39 import edu.rice.cs.drjava.config.OptionListener;
40 import edu.rice.cs.drjava.model.SingleDisplayModel;
41 import edu.rice.cs.drjava.model.compiler.CompilerModel;
42 import edu.rice.cs.drjava.model.compiler.CompilerErrorModel;
43 import edu.rice.cs.drjava.model.compiler.CompilerInterface;
44 import edu.rice.cs.drjava.model.compiler.NoCompilerAvailable;
45 import edu.rice.cs.util.UnexpectedException;
46 import edu.rice.cs.util.text.SwingDocument;
47
48 import javax.swing.*;
49 import javax.swing.text.BadLocationException JavaDoc;
50 import javax.swing.text.Position JavaDoc;
51 import java.awt.*;
52 import java.awt.event.ItemEvent JavaDoc;
53 import java.awt.event.ItemListener JavaDoc;
54 import java.io.File JavaDoc;
55 import java.util.Vector JavaDoc;
56
57 /**
58  * The panel which houses the list of errors after an unsuccessful compilation.
59  * If the user clicks on the combobox, move the definitions cursor to the
60  * error in the source.
61  * If the cursor is moved onto a line with an error, select the appropriate
62  * error in the list but do not move the cursor.
63  *
64  * @version $Id: CompilerErrorPanel.java 4026 2006-10-31 15:50:16Z rcartwright $
65  */

66 public class CompilerErrorPanel extends ErrorPanel {
67   
68   /** Whether a compile has occurred since the last compiler change. */
69   private boolean _compileHasOccurred;
70   private CompilerErrorListPane _errorListPane;
71   private final JComboBox _compilerChoiceBox;
72   
73   /** The list of files from the last compilation unit that were not compiled because they were not source files. */
74   private File JavaDoc[] _excludedFiles = new File JavaDoc[0];
75   
76   /** Constructor.
77    * @param model SingleDisplayModel in which we are running
78    * @param frame MainFrame in which we are displayed
79    */

80   public CompilerErrorPanel(SingleDisplayModel model, MainFrame frame) {
81     super(model, frame, "Compiler Output", "Compiler");
82     _compileHasOccurred = false;
83     _numErrors = 0;
84     
85     _errorListPane = new CompilerErrorListPane();
86     setErrorListPane(_errorListPane);
87     
88     /******** Initialize the drop-down compiler menu ********/
89     // Limitation: Only compiler choices are those that were available
90
// at the time this box was created.
91
// Also: The UI will go out of sync with reality if the active compiler
92
// is later changed somewhere else. This is because there is no way
93
// to listen on the active compiler.
94
final CompilerModel compilerModel = getModel().getCompilerModel();
95     _compilerChoiceBox = new JComboBox(compilerModel.getAvailableCompilers());
96     _compilerChoiceBox.setEditable(false);
97     _compilerChoiceBox.setSelectedItem(compilerModel.getActiveCompiler());
98     _compilerChoiceBox.addItemListener(new ItemListener JavaDoc() {
99       public void itemStateChanged(ItemEvent JavaDoc e) {
100         CompilerInterface compiler = (CompilerInterface) _compilerChoiceBox.getSelectedItem();
101         if (compiler == null) compiler = NoCompilerAvailable.ONLY;
102         compilerModel.setActiveCompiler(compiler);
103         compilerModel.resetCompilerErrors();
104         _compileHasOccurred = false;
105         reset();
106       }
107     });
108     
109     customPanel.add(_compilerChoiceBox, BorderLayout.NORTH);
110     
111     DrJava.getConfig().addOptionListener(OptionConstants.JAVAC_LOCATION, new CompilerLocationOptionListener<File JavaDoc>());
112     DrJava.getConfig().addOptionListener(OptionConstants.EXTRA_COMPILERS, new CompilerLocationOptionListener<Vector JavaDoc<String JavaDoc>>());
113   }
114   
115   
116   /** The OptionListener for compiler LOCATIONs */
117   private class CompilerLocationOptionListener<T> implements OptionListener<T> {
118     
119     public void optionChanged(OptionEvent<T> oce) {
120       _compilerChoiceBox.removeAllItems();
121       CompilerInterface[] availCompilers = getModel().getCompilerModel().getAvailableCompilers();
122       for (int i=0; i<availCompilers.length; i++) {
123         _compilerChoiceBox.addItem(availCompilers[i]);
124       }
125     }
126   }
127   
128   /** Returns the CompilerErrorListPane that this panel manages. */
129   public CompilerErrorListPane getErrorListPane() { return _errorListPane; }
130   
131   /** Called when compilation begins. */
132   public void setCompilationInProgress() {
133     _errorListPane.setCompilationInProgress();
134   }
135   
136   public CompilerErrorModel getErrorModel() { return getModel().getCompilerModel().getCompilerErrorModel(); }
137   
138   /** Clean up when the tab is closed. */
139   @Override JavaDoc
140   protected void _close() {
141     super._close();
142     getModel().getCompilerModel().resetCompilerErrors();
143     reset();
144   }
145   
146   /** Reset the errors to the current error information immediately following compilation. */
147   public void reset(File JavaDoc[] excludedFiles) {
148     _excludedFiles = excludedFiles;
149     reset();
150   }
151   
152   /** Reset the errors to the current error information. */
153   public void reset() {
154     // _nextErrorButton.setEnabled(false);
155
// _prevErrorButton.setEnabled(false);
156
// Utilities.showDebug("Reset being called by CompilerErrorPanel");
157
_numErrors = getModel().getCompilerModel().getNumErrors();
158     
159     _errorListPane.updateListPane(true);
160     // _nextErrorButton.setEnabled(_errorListPane.hasNextError());
161
// _prevErrorButton.setEnabled(_errorListPane.hasPrevError());
162
}
163   
164   class CompilerErrorListPane extends ErrorPanel.ErrorListPane {
165     
166     protected void _updateWithErrors() throws BadLocationException JavaDoc {
167       SwingDocument doc = new SwingDocument();
168       if (_excludedFiles.length != 0) {
169         final StringBuilder JavaDoc msgBuffer =
170           new StringBuilder JavaDoc("Compilation completed. The following files were not compiled:\n");
171         for (File JavaDoc f: _excludedFiles) {
172           if (f != null) { msgBuffer.append(" ").append(f).append('\n'); } // do not print files from untitled docs
173
}
174         doc.append(msgBuffer.toString(), NORMAL_ATTRIBUTES);
175       }
176
177       String JavaDoc failureName = "error";
178       if (getErrorModel().hasOnlyWarnings()) failureName = "warning";
179
180       _updateWithErrors(failureName, "found", doc);
181     }
182     
183     /** Puts the error pane into "compilation in progress" state. */
184     public void setCompilationInProgress() {
185       _errorListPositions = new Position JavaDoc[0];
186       _compileHasOccurred = true;
187       
188       SwingDocument doc = new SwingDocument();
189        
190       try { doc.insertString(0, "Compilation in progress, please wait...", NORMAL_ATTRIBUTES); }
191       catch (BadLocationException JavaDoc ble) { throw new UnexpectedException(ble); }
192       
193       setDocument(doc);
194       selectNothing();
195     }
196     
197     /** Used to show that the last compile was successful.
198      * @param done ignored: we assume that this is only called after compilation is completed
199      */

200     protected void _updateNoErrors(boolean done) throws BadLocationException JavaDoc {
201       SwingDocument doc = new SwingDocument();
202       String JavaDoc message;
203       if (_compileHasOccurred) {
204         if (_excludedFiles.length == 0) message = "Compilation completed.";
205         else {
206           final StringBuilder JavaDoc msgBuffer =
207             new StringBuilder JavaDoc("Compilation completed. The following files were not compiled:\n");
208           for (File JavaDoc f: _excludedFiles) {
209             if (f!=null) { msgBuffer.append(" ").append(f).append('\n'); } // do not print files from untitled docs
210
}
211           message = msgBuffer.toString();
212         }
213       }
214       else if (getModel().getCompilerModel().getAvailableCompilers().length == 0)
215         message = "No compiler is available. Please specify one in\nthe Preferences dialog in the Edit menu.";
216       else if (getModel().getCompilerModel().getActiveCompiler() == NoCompilerAvailable.ONLY)
217         message = "No compiler available.";
218       else
219         message = getModel().getCompilerModel().getActiveCompiler().getName() + " compiler ready.";
220       
221       doc.insertString(0, message, NORMAL_ATTRIBUTES);
222       setDocument(doc);
223       _updateScrollButtons();
224       selectNothing();
225     }
226   }
227 }
228
Popular Tags