001: package tide.editor.linemessages;
002:
003: import snow.html.HTMLUtils;
004: import java.awt.Color;
005: import snow.utils.storage.StorageVector;
006: import tide.sources.TypeLocator;
007: import tide.sources.FileItem;
008: import java.util.*;
009: import snow.utils.StringUtils;
010: import java.awt.event.ActionEvent;
011: import javax.swing.AbstractAction;
012: import tide.editor.MainEditorFrame;
013:
014: public final class CompilationMessage extends LineMessage {
015: private String mess, shortMess, category = "";
016: private int column = -1;
017: private boolean error;
018:
019: private CompilationMessage(boolean error, String javaName,
020: int line, int column, String mess, long created) {
021: super (javaName, line, created);
022: this .error = error;
023: this .column = column;
024: this .mess = mess;
025: this .shortMess = mess.replace('\n', ' ').trim();
026:
027: if (!error) {
028: // warning
029: if (shortMess.startsWith("warning: [")) {
030: int pos = shortMess.indexOf(']', 10);
031: if (pos > 0) {
032: category = shortMess.substring(10, pos);
033: shortMess = shortMess.substring(pos + 2);
034: }
035: } else if (shortMess.startsWith("warning:")) {
036: shortMess = shortMess.substring(9).trim();
037: }
038: }
039:
040: if (shortMess.endsWith("^")) {
041: shortMess = StringUtils.firstLine(shortMess); // [Feb2008]: nicer.
042: //shortMess = shortMess.substring(0,shortMess.length()-1).trim();
043: }
044: }
045:
046: @Override
047: public List<AbstractAction> lookForAvailableActions() {
048: List<AbstractAction> actions = new ArrayList<AbstractAction>();
049:
050: if (shortMess.startsWith("cannot find symbol")) {
051: String start = "symbol : class ";
052: //int pos = shortMess.indexOf(start);
053: String clName = StringUtils
054: .extractFromFirstToNext_Excluded(shortMess, start,
055: " "); // OK: line end have been replaced with spaces
056: if (clName != null) {
057: //System.out.println("clName="+clName);
058: final FileItem fi = TypeLocator.searchTypeForName(
059: clName, false, true); // respect case, but accept "ending with...". Todo: exact end
060: // TODO: like the editor popup !!
061: if (fi != null) {
062: AbstractAction al = new AbstractAction(
063: "Add missing import for "
064: + fi.getJavaName()) {
065: public void actionPerformed(ActionEvent ae) {
066: MainEditorFrame.instance.editorPanel
067: .addMissingImport(fi.getJavaName()
068: + ";");
069: }
070: };
071: actions.add(al);
072: }
073: }
074: }
075: return actions;
076: }
077:
078: public static void createAndAdd(String javaName, int line,
079: final String mess_) {
080: String mess = mess_.trim();
081:
082: boolean error = !mess.contains("warning: ");
083:
084: // remove before ":<line>:"
085: int pos = mess.indexOf(':');
086: if (pos > 0) {
087: mess = mess.substring(pos + 1);
088: pos = mess.indexOf(':');
089: if (pos > 0) {
090: mess = mess.substring(pos + 1);
091: }
092: }
093:
094: // todo: detect column
095: pos = mess.lastIndexOf("\n");
096: int column = -1;
097: if (pos > 0) {
098: String lastLine = mess.substring(pos);
099: column = lastLine.indexOf('^');
100: if (column >= 0) {
101: // remove the last line
102: //no, let its information be... mess = mess.substring(0, pos);
103: // TODO: underline or show error position.
104: }
105:
106: }
107:
108: CompilationMessage cm = new CompilationMessage(error, javaName,
109: line, column, mess, System.currentTimeMillis());
110: LineMessagesManager.getInstance().add(cm);
111:
112: }
113:
114: // false for warnings
115: public boolean isError() {
116: return error;
117: }
118:
119: @Override
120: public int getColumn() {
121: return column;
122: }
123:
124: @Override
125: public int getShiftX() {
126: return 3;
127: }
128:
129: @Override
130: public String getLetter() {
131: return error ? "E" : "W";
132: }
133:
134: @Override
135: public Color getColor() {
136: return error ? Color.red : Color.orange;
137: }
138:
139: @Override
140: public String getMessage() {
141: return mess;
142: }
143:
144: @Override
145: public String getMessageForTableColumn() {
146: return shortMess;
147: }
148:
149: // todo: highlight last line at column position
150: @Override
151: public String toStringHTML() {
152: return HTMLUtils.createCODEHTMLFromText(mess);
153: }
154:
155: @Override
156: public String getMessageOriginator() {
157: return "Compiler";
158: }
159:
160: @Override
161: public String getCategory() {
162: return error ? "Error" : (category.length() == 0 ? "Warning"
163: : category);
164: }
165:
166: @Override
167: public StorageVector getStorageRepresentation() {
168: StorageVector sv = new StorageVector();
169: sv.add(1);
170: sv.add("CompilationMessage");
171: sv.add(javaName);
172: sv.add(mess);
173: sv.add(line);
174: sv.add(column);
175: sv.add(error);
176: sv.add(created);
177: return sv;
178: }
179:
180: public static CompilationMessage createFromStorageVector(
181: StorageVector sv) {
182: long created = sv.size() > 7 ? (Long) sv.get(7) : System
183: .currentTimeMillis();
184: return new CompilationMessage((Boolean) sv.get(6), (String) sv
185: .get(2), (Integer) sv.get(4), (Integer) sv.get(5),
186: (String) sv.get(3), created);
187: }
188:
189: }
|