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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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.List;
  19. import java.util.Vector;
  20. import javax.swing.Box;
  21. import javax.swing.JPanel;
  22. import javax.swing.SwingUtilities;
  23. import org.aspectj.ajde.EditorAdapter;
  24. import org.aspectj.ajde.EditorListener;
  25. import org.aspectj.bridge.ISourceLocation;
  26. import org.aspectj.tools.ajbrowser.core.BrowserErrorHandler;
  27. /**
  28. * Responsible for controlling the editor.
  29. *
  30. * <p><b>TODO:</b> remove coupling to <CODE>BasicEditor</CODE></p>
  31. * @author Mik Kersten
  32. */
  33. public class EditorManager {
  34. /** @return true if input modifiers have shift down */
  35. public static boolean isShiftDown(int modifiers) {
  36. return (0 != (modifiers & KeyEvent.SHIFT_MASK));
  37. }
  38. private EditorAdapter editor = null;
  39. private BasicEditor basicEditor = null;
  40. private List<EditorListener> editorListeners = new ArrayList<>();
  41. private Vector<EditorAdapter> editors = new Vector<>();
  42. private JPanel editor_panel = null;
  43. private Box editors_box = Box.createVerticalBox();
  44. public EditorManager(EditorAdapter ajdeEditor) {
  45. if (ajdeEditor instanceof BasicEditor) {
  46. this.basicEditor = (BasicEditor) ajdeEditor;
  47. editors.add(basicEditor);
  48. editors_box.add(basicEditor.getPanel());
  49. editor_panel = new JPanel(new BorderLayout());
  50. editor_panel.add(editors_box, BorderLayout.CENTER);
  51. } else {
  52. editors.add(ajdeEditor);
  53. this.editor = ajdeEditor;
  54. }
  55. }
  56. public void addListener(EditorListener editorListener) {
  57. editorListeners.add(editorListener);
  58. }
  59. public void removeListener(EditorListener editorListener) {
  60. editorListeners.remove(editorListener);
  61. }
  62. public void notifyCurrentFileChanged(String filePath) {
  63. for (EditorListener editorListener : editorListeners) {
  64. editorListener.currentFileChanged(filePath);
  65. }
  66. }
  67. public void addViewForSourceLine(final String filePath, final int lineNumber) {
  68. if (basicEditor == null)
  69. return;
  70. editors_box.remove(basicEditor.getPanel());
  71. final BasicEditor newEditor = new BasicEditor();
  72. editors.add(newEditor);
  73. Runnable update = new Runnable() {
  74. public void run() {
  75. editors_box.add(newEditor.getPanel());
  76. newEditor.showSourceLine(filePath, lineNumber, true);
  77. // AjdeUIManager.getDefault().getIdeUIAdapter().resetGUI();
  78. }
  79. };
  80. if (SwingUtilities.isEventDispatchThread()) {
  81. update.run();
  82. } else {
  83. try {
  84. SwingUtilities.invokeAndWait(update);
  85. } catch (Exception e) {
  86. BrowserErrorHandler.handleError("Could not add view for source line.", e);
  87. }
  88. }
  89. }
  90. public String getCurrFile() {
  91. if (basicEditor != null) {
  92. return basicEditor.getCurrFile();
  93. } else {
  94. return editor.getCurrFile();
  95. }
  96. }
  97. public void showSourceLine(ISourceLocation sourceLocation, boolean highlight) {
  98. if (sourceLocation != null) {
  99. showSourceLine(sourceLocation.getSourceFile().getAbsolutePath(), sourceLocation.getLine(), highlight);
  100. }
  101. }
  102. /**
  103. * <b>TODO</b>: remove "instanceof AjdeManager" hack
  104. */
  105. public void showSourceLine(String filePath, int lineNumber, boolean highlight) {
  106. if (editors.size() > 1) {
  107. editors_box.removeAll();
  108. editors_box.add(basicEditor.getPanel());
  109. // AjdeUIManager.getDefault().getIdeUIAdapter().resetGUI();
  110. editors.removeAllElements();
  111. editors.add(basicEditor);
  112. }
  113. if (basicEditor != null) {
  114. basicEditor.showSourceLine(filePath, lineNumber, highlight);
  115. } else {
  116. editor.showSourceLine(filePath, lineNumber, highlight);
  117. }
  118. }
  119. public void pasteToCaretPos(String text) {
  120. if (basicEditor != null) {
  121. basicEditor.pasteToCaretPos(text);
  122. } else {
  123. editor.pasteToCaretPos(text);
  124. }
  125. }
  126. public void showSourcelineAnnotation(String filePath, int lineNumber, java.util.List items) {
  127. editor.showSourcelineAnnotation(filePath, lineNumber, items);
  128. }
  129. public void saveContents() {
  130. try {
  131. for (EditorAdapter editorAdapter : editors) {
  132. editorAdapter.saveContents();
  133. }
  134. } catch (IOException ioe) {
  135. BrowserErrorHandler.handleError("Editor could not save the current file.", ioe);
  136. }
  137. }
  138. public JPanel getEditorPanel() {
  139. if (editor_panel != null) {
  140. return editor_panel;
  141. } else {
  142. return basicEditor.getPanel();
  143. }
  144. }
  145. }