1 2 25 26 package org.aspectj.ajde.ui; 27 28 import java.io.*; 29 import javax.swing.*; 30 import java.awt.*; 31 import java.util.*; 32 import org.aspectj.asm.*; 33 import org.aspectj.ajde.*; 34 import org.aspectj.ajde.ui.swing.*; 35 36 42 public class EditorManager { 43 44 private EditorAdapter editor = null; 45 private BasicEditor basicEditor = null; 46 private ArrayList editorListeners = new ArrayList(); 47 private Vector editors = new Vector(); 48 private JPanel editor_panel = null; 49 private Box editors_box = Box.createVerticalBox(); 50 51 public EditorManager(EditorAdapter ajdeEditor) { 52 if (ajdeEditor instanceof BasicEditor) { 53 this.basicEditor = (BasicEditor)ajdeEditor; 54 editors.add(basicEditor); 55 editors_box.add(basicEditor.getPanel()); 56 editor_panel = new JPanel(new BorderLayout()); 57 editor_panel.add(editors_box, BorderLayout.CENTER); 58 } else { 59 editors.add(ajdeEditor); 60 this.editor = ajdeEditor; 61 } 62 } 63 64 public void addListener(EditorListener editorListener) { 65 editorListeners.add(editorListener); 66 } 67 68 public void removeListener(EditorListener editorListener) { 69 editorListeners.remove(editorListener); 70 } 71 72 public void notifyCurrentFileChanged(String filePath) { 73 for (Iterator it = editorListeners.iterator(); it.hasNext(); ) { 74 ((EditorListener)it.next()).currentFileChanged(filePath); 75 } 76 } 77 78 public void addViewForSourceLine(final String filePath, final int lineNumber) { 79 if (basicEditor == null) return; 80 editors_box.remove(basicEditor.getPanel()); 81 final BasicEditor newEditor = new BasicEditor(); 82 editors.add(newEditor); 83 84 Runnable update = new Runnable () { 85 public void run() { 86 editors_box.add(newEditor.getPanel()); 87 newEditor.showSourceLine(filePath, lineNumber, true); 88 } 90 }; 91 92 if (SwingUtilities.isEventDispatchThread()) { 93 update.run(); 94 } else { 95 try { 96 SwingUtilities.invokeAndWait(update); 97 } catch (Exception e) { 98 Ajde.getDefault().getErrorHandler().handleError("Could not add view for source line.", e); 99 } 100 } 101 } 102 103 public String getCurrFile() { 104 if (basicEditor != null) { 105 return basicEditor.getCurrFile(); 106 } else { 107 return editor.getCurrFile(); 108 } 109 } 110 111 112 public void showSourceLine(SourceLocation sourceLocation, boolean highlight) { 113 if (sourceLocation != null) { 114 showSourceLine( 115 sourceLocation.getSourceFilePath(), 116 sourceLocation.getLineNumber(), 117 highlight); 118 } 119 } 120 121 124 public void showSourceLine(String filePath, int lineNumber, boolean highlight) { 125 if (editors.size() > 1) { 126 editors_box.removeAll(); 127 editors_box.add(basicEditor.getPanel()); 128 editors.removeAllElements(); 130 editors.add(basicEditor); 131 } 132 133 if (basicEditor != null) { 134 basicEditor.showSourceLine(filePath, lineNumber, highlight); 135 } else { 136 editor.showSourceLine(filePath, lineNumber, highlight); 137 } 138 } 139 140 public void pasteToCaretPos(String text) { 141 if (basicEditor != null) { 142 basicEditor.pasteToCaretPos(text); 143 } else { 144 editor.pasteToCaretPos(text); 145 } 146 } 147 148 public void showSourcelineAnnotation(String filePath, int lineNumber, java.util.List items) { 149 editor.showSourcelineAnnotation(filePath, lineNumber, items); 150 } 151 152 public void saveContents() { 153 try { 154 for (Iterator it = editors.iterator(); it.hasNext(); ) { 155 ((EditorAdapter)it.next()).saveContents(); 156 } 157 } catch (IOException ioe) { 158 Ajde.getDefault().getErrorHandler().handleError("Editor could not save the current file.", ioe); 159 } 160 } 161 162 public JPanel getEditorPanel() { 163 if (editor_panel != null) { 164 return editor_panel; 165 } else { 166 return basicEditor.getPanel(); 167 } 168 } 169 } 170 171 172 | Popular Tags |