1 2 25 26 package org.aspectj.ajde.ui.swing; 27 28 import java.awt.*; 29 import javax.swing.*; 30 import javax.swing.text.*; 31 import java.util.*; 32 import java.io.*; 33 import java.net.URL ; 34 import org.aspectj.asm.*; 35 import org.aspectj.ajde.*; 36 37 43 public class BasicEditor implements EditorAdapter { 44 45 private String NO_FILE = "<no file selected>"; 46 private String filePath = NO_FILE; 47 private JPanel editor_panel = new JPanel(); 48 49 private int currHighlightStart = 0; 51 private int currHighlightEnd = 0; 52 53 private BorderLayout borderLayout1 = new BorderLayout(); 54 private JScrollPane jScrollPane1 = new JScrollPane(); 55 private JEditorPane editorPane = new JEditorPane(); 56 57 public BasicEditor() { 58 try { 59 editorPane.setEditable(true); 60 editorPane.setContentType("text/plain"); 61 editorPane.setFont(new Font("Monospaced", 0, 11)); 62 editor_panel.add(editorPane); 63 jbInit(); 64 } 65 catch(Exception e) { 66 Ajde.getDefault().getErrorHandler().handleError("Could not initialize GUI.", e); 67 } 68 } 69 70 public String getCurrFile() { 71 return filePath; 72 } 73 74 public void showSourceLine(SourceLocation sourceLocation, boolean highlight) { 75 showSourceLine(sourceLocation.getSourceFilePath(), sourceLocation.getLineNumber(), highlight); 76 } 77 78 public void showSourceLine(int lineNumber, boolean highlight) { 79 showSourceLine(filePath, lineNumber, highlight); 80 } 81 82 public void pasteToCaretPos(String text) { 83 if (currHighlightEnd < 1) return; 84 String contents = editorPane.getText(); 85 String pasted = contents.substring(0, currHighlightEnd) + 86 text + contents.substring(currHighlightEnd, contents.length()); 87 editorPane.setText(pasted); 88 } 89 90 public void showSourceLine(String filePath, int lineNumber, boolean highlight) { 91 93 String oldPath = this.filePath; 94 this.filePath = filePath; 95 99 101 currHighlightStart = 0; 102 currHighlightEnd = 0; 103 editorPane.setText(readFile(filePath, lineNumber)); 104 try { 105 editorPane.getHighlighter().addHighlight(currHighlightStart, currHighlightEnd, DefaultHighlighter.DefaultPainter); 106 editorPane.setCaretPosition(currHighlightStart); 107 } catch (BadLocationException ble) { 108 Ajde.getDefault().getErrorHandler().handleError("Could not highlight location.", ble); 109 } 110 Ajde.getDefault().getEditorManager().notifyCurrentFileChanged(filePath); 111 } 112 113 116 public void showSourcelineAnnotation(String filePath, int lineNumber, java.util.List items) { } 117 118 public void addEditorViewForSourceLine(String filePath, int lineNumber) { 119 120 } 121 122 public void saveContents() throws IOException { 123 if (filePath != NO_FILE && filePath != "" && editorPane.getText() != "") { 124 BufferedWriter writer = new BufferedWriter(new FileWriter(filePath)); 125 writer.write(editorPane.getText()); 126 writer.flush(); 127 } 128 } 129 130 public JPanel getPanel() { 131 return editor_panel; 132 } 133 134 public void showSourceForFile(String filePath) { } 135 136 public void showSourceForLine(int lineNumber, boolean highlight) { } 137 138 public void showSourceForSourceLine(String filePath, int lineNumber, boolean highlight) { } 139 140 public String getCurrSourceFilePath() { return null; } 141 142 public void setBreakpointRequest(String filePath, int lineNumber, boolean isDeferred) { } 143 144 public void clearBreakpointRequest(String filePath, int lineNumber) { } 145 146 private String readFile(String filePath, int lineNumber) { 147 try { 148 URL url = ClassLoader.getSystemResource(filePath); 149 File file = new File(filePath); 150 if (!file.exists()) { 151 return "ERROR: file \"" + filePath + "\" does not exist."; 152 } 153 BufferedReader reader = new BufferedReader(new FileReader(file)); 154 StringBuffer contents = new StringBuffer (); 155 String line = reader.readLine(); 156 int numChars = 0; 157 int numLines = 0; 158 while (line != null) { 159 numLines++; 160 if (numLines < lineNumber) { 161 currHighlightStart += line.length()+1; 162 } 163 if (numLines == lineNumber) { 164 currHighlightEnd = currHighlightStart + line.length(); 165 } 166 contents.append(line); 167 contents.append('\n'); 168 line = reader.readLine(); 169 } 170 return contents.toString(); 171 } catch (IOException ioe) { 172 return "ERROR: could not read file \"" + filePath + "\", make sure that you have mounted /project/aop on X:\\"; 173 } 174 } 175 176 private void jbInit() throws Exception { 177 editor_panel.setFont(new java.awt.Font ("DialogInput", 1, 12)); 178 editor_panel.setLayout(borderLayout1); 179 editor_panel.add(jScrollPane1, BorderLayout.CENTER); 180 jScrollPane1.getViewport().add(editorPane, null); 181 } 182 } 183 | Popular Tags |