You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditorManager.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.tools.ajbrowser.ui;
  14. import java.awt.BorderLayout;
  15. import java.awt.event.KeyEvent;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Vector;
  19. import javax.swing.Box;
  20. import javax.swing.JPanel;
  21. import javax.swing.SwingUtilities;
  22. import org.aspectj.ajde.EditorAdapter;
  23. import org.aspectj.ajde.EditorListener;
  24. import org.aspectj.bridge.ISourceLocation;
  25. import org.aspectj.tools.ajbrowser.core.BrowserErrorHandler;
  26. /**
  27. * Responsible for controlling the editor.
  28. *
  29. * @todo remove coupling to <CODE>BasicEditor</CODE>
  30. * @author Mik Kersten
  31. */
  32. public class EditorManager {
  33. /** @return true if input modifiers have shift down */
  34. public static boolean isShiftDown(int modifiers) {
  35. return (0 != (modifiers & KeyEvent.SHIFT_MASK));
  36. }
  37. private EditorAdapter editor = null;
  38. private BasicEditor basicEditor = null;
  39. private ArrayList<EditorListener> editorListeners = new ArrayList<>();
  40. private Vector<EditorAdapter> editors = new Vector<>();
  41. private JPanel editor_panel = null;
  42. private Box editors_box = Box.createVerticalBox();
  43. public EditorManager(EditorAdapter ajdeEditor) {
  44. if (ajdeEditor instanceof BasicEditor) {
  45. this.basicEditor = (BasicEditor) ajdeEditor;
  46. editors.add(basicEditor);
  47. editors_box.add(basicEditor.getPanel());
  48. editor_panel = new JPanel(new BorderLayout());
  49. editor_panel.add(editors_box, BorderLayout.CENTER);
  50. } else {
  51. editors.add(ajdeEditor);
  52. this.editor = ajdeEditor;
  53. }
  54. }
  55. public void addListener(EditorListener editorListener) {
  56. editorListeners.add(editorListener);
  57. }
  58. public void removeListener(EditorListener editorListener) {
  59. editorListeners.remove(editorListener);
  60. }
  61. public void notifyCurrentFileChanged(String filePath) {
  62. for (EditorListener editorListener : editorListeners) {
  63. editorListener.currentFileChanged(filePath);
  64. }
  65. }
  66. public void addViewForSourceLine(final String filePath, final int lineNumber) {
  67. if (basicEditor == null)
  68. return;
  69. editors_box.remove(basicEditor.getPanel());
  70. final BasicEditor newEditor = new BasicEditor();
  71. editors.add(newEditor);
  72. Runnable update = new Runnable() {
  73. public void run() {
  74. editors_box.add(newEditor.getPanel());
  75. newEditor.showSourceLine(filePath, lineNumber, true);
  76. // AjdeUIManager.getDefault().getIdeUIAdapter().resetGUI();
  77. }
  78. };
  79. if (SwingUtilities.isEventDispatchThread()) {
  80. update.run();
  81. } else {
  82. try {
  83. SwingUtilities.invokeAndWait(update);
  84. } catch (Exception e) {
  85. BrowserErrorHandler.handleError("Could not add view for source line.", e);
  86. }
  87. }
  88. }
  89. public String getCurrFile() {
  90. if (basicEditor != null) {
  91. return basicEditor.getCurrFile();
  92. } else {
  93. return editor.getCurrFile();
  94. }
  95. }
  96. public void showSourceLine(ISourceLocation sourceLocation, boolean highlight) {
  97. if (sourceLocation != null) {
  98. showSourceLine(sourceLocation.getSourceFile().getAbsolutePath(), sourceLocation.getLine(), highlight);
  99. }
  100. }
  101. /**
  102. * @todo remove "instanceof AjdeManager" hack
  103. */
  104. public void showSourceLine(String filePath, int lineNumber, boolean highlight) {
  105. if (editors.size() > 1) {
  106. editors_box.removeAll();
  107. editors_box.add(basicEditor.getPanel());
  108. // AjdeUIManager.getDefault().getIdeUIAdapter().resetGUI();
  109. editors.removeAllElements();
  110. editors.add(basicEditor);
  111. }
  112. if (basicEditor != null) {
  113. basicEditor.showSourceLine(filePath, lineNumber, highlight);
  114. } else {
  115. editor.showSourceLine(filePath, lineNumber, highlight);
  116. }
  117. }
  118. public void pasteToCaretPos(String text) {
  119. if (basicEditor != null) {
  120. basicEditor.pasteToCaretPos(text);
  121. } else {
  122. editor.pasteToCaretPos(text);
  123. }
  124. }
  125. public void showSourcelineAnnotation(String filePath, int lineNumber, java.util.List items) {
  126. editor.showSourcelineAnnotation(filePath, lineNumber, items);
  127. }
  128. public void saveContents() {
  129. try {
  130. for (EditorAdapter editorAdapter : editors) {
  131. editorAdapter.saveContents();
  132. }
  133. } catch (IOException ioe) {
  134. BrowserErrorHandler.handleError("Editor could not save the current file.", ioe);
  135. }
  136. }
  137. public JPanel getEditorPanel() {
  138. if (editor_panel != null) {
  139. return editor_panel;
  140. } else {
  141. return basicEditor.getPanel();
  142. }
  143. }
  144. }