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.

BasicEditor.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * Helen Hawkins Converted to new interface (bug 148190)
  13. * ******************************************************************/
  14. package org.aspectj.tools.ajbrowser.ui;
  15. import java.awt.BorderLayout;
  16. import java.awt.Font;
  17. import java.io.BufferedReader;
  18. import java.io.BufferedWriter;
  19. import java.io.File;
  20. import java.io.FileReader;
  21. import java.io.FileWriter;
  22. import java.io.IOException;
  23. import javax.swing.JEditorPane;
  24. import javax.swing.JPanel;
  25. import javax.swing.JScrollPane;
  26. import javax.swing.text.BadLocationException;
  27. import javax.swing.text.DefaultHighlighter;
  28. import org.aspectj.ajde.Ajde;
  29. import org.aspectj.ajde.EditorAdapter;
  30. import org.aspectj.bridge.ISourceLocation;
  31. import org.aspectj.tools.ajbrowser.BrowserManager;
  32. import org.aspectj.tools.ajbrowser.core.BrowserErrorHandler;
  33. /**
  34. * Bare-bones editor implementation used when the framework is being used
  35. * standalone.
  36. *
  37. * @author Mik Kersten
  38. */
  39. public class BasicEditor implements EditorAdapter {
  40. private String NO_FILE = "<no file selected>";
  41. private String filePath = NO_FILE;
  42. private JPanel editor_panel = new JPanel();
  43. // @todo get rid of these
  44. private int currHighlightStart = 0;
  45. private int currHighlightEnd = 0;
  46. private BorderLayout borderLayout1 = new BorderLayout();
  47. private JScrollPane jScrollPane1 = new JScrollPane();
  48. private JEditorPane editorPane = new JEditorPane();
  49. public BasicEditor() {
  50. try {
  51. editorPane.setEditable(true);
  52. editorPane.setContentType("text/plain");
  53. editorPane.setFont(new Font("Monospaced", 0, 11));
  54. editor_panel.add(editorPane);
  55. jbInit();
  56. }
  57. catch(Exception e) {
  58. BrowserErrorHandler.handleError("Could not initialize GUI.", e);
  59. }
  60. }
  61. public String getCurrFile() {
  62. return filePath;
  63. }
  64. public void showSourceLine(ISourceLocation sourceLocation, boolean highlight) {
  65. try {
  66. showSourceLine(sourceLocation.getSourceFile().getAbsolutePath(), sourceLocation.getLine(), highlight);
  67. } catch (NullPointerException npe) {
  68. Ajde.getDefault().getIdeUIAdapter().displayStatusInformation(" no corresponding source line to seek to");
  69. }
  70. }
  71. public void showSourceLine(int lineNumber, boolean highlight) {
  72. showSourceLine(filePath, lineNumber, highlight);
  73. }
  74. public void pasteToCaretPos(String text) {
  75. if (currHighlightEnd < 1) return;
  76. String contents = editorPane.getText();
  77. String pasted = contents.substring(0, currHighlightEnd) +
  78. text + contents.substring(currHighlightEnd, contents.length());
  79. editorPane.setText(pasted);
  80. }
  81. public void showSourceLine(String filePath, int lineNumber, boolean highlight) {
  82. //AjdeUIManager.getDefault().getIdeUIAdapter().resetEditor();
  83. this.filePath = filePath;
  84. // if (oldPath != filePath && !Ajde.INSTANCE.BROWSER_MANAGER.isGlobalMode()) {
  85. // Ajde.INSTANCE.BROWSER_MANAGER.updateView();
  86. // }
  87. // Ajde.IDE_MANAGER.setEditorStatusText(filePath);
  88. currHighlightStart = 0;
  89. currHighlightEnd = 0;
  90. editorPane.setText(readFile(filePath, lineNumber));
  91. try {
  92. editorPane.getHighlighter().addHighlight(currHighlightStart, currHighlightEnd, DefaultHighlighter.DefaultPainter);
  93. editorPane.setCaretPosition(currHighlightStart);
  94. } catch (BadLocationException ble) {
  95. BrowserErrorHandler.handleError("Could not highlight location.", ble);
  96. }
  97. BrowserManager.getDefault().getEditorManager().notifyCurrentFileChanged(filePath);
  98. }
  99. /**
  100. * Not implemented.
  101. */
  102. public void showSourcelineAnnotation(String filePath, int lineNumber, java.util.List items) { }
  103. public void addEditorViewForSourceLine(String filePath, int lineNumber) {
  104. }
  105. public void saveContents() throws IOException {
  106. if (!filePath.equals(NO_FILE) && !filePath.equals("") && !editorPane.getText().equals("")) {
  107. BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
  108. writer.write(editorPane.getText());
  109. writer.close();
  110. }
  111. }
  112. public JPanel getPanel() {
  113. return editor_panel;
  114. }
  115. public void showSourceForFile(String filePath) { }
  116. public void showSourceForLine(int lineNumber, boolean highlight) { }
  117. public void showSourceForSourceLine(String filePath, int lineNumber, boolean highlight) { }
  118. public String getCurrSourceFilePath() { return null; }
  119. public void setBreakpointRequest(String filePath, int lineNumber, boolean isDeferred) { }
  120. public void clearBreakpointRequest(String filePath, int lineNumber) { }
  121. private String readFile(String filePath, int lineNumber) {
  122. try {
  123. // URL url = ClassLoader.getSystemResource(filePath);
  124. File file = new File(filePath);
  125. if (!file.exists()) {
  126. return "ERROR: file \"" + filePath + "\" does not exist.";
  127. }
  128. BufferedReader reader = new BufferedReader(new FileReader(file));
  129. StringBuffer contents = new StringBuffer();
  130. String line = reader.readLine();
  131. int numLines = 0;
  132. while (line != null) {
  133. numLines++;
  134. if (numLines < lineNumber) {
  135. currHighlightStart += line.length()+1;
  136. }
  137. if (numLines == lineNumber) {
  138. currHighlightEnd = currHighlightStart + line.length();
  139. }
  140. contents.append(line);
  141. contents.append('\n');
  142. line = reader.readLine();
  143. }
  144. reader.close();
  145. return contents.toString();
  146. } catch (IOException ioe) {
  147. return "ERROR: could not read file \"" + filePath + "\", make sure that you have mounted /project/aop on X:\\";
  148. }
  149. }
  150. private void jbInit() throws Exception {
  151. editor_panel.setFont(new java.awt.Font("DialogInput", 1, 12));
  152. editor_panel.setLayout(borderLayout1);
  153. editor_panel.add(jScrollPane1, BorderLayout.CENTER);
  154. jScrollPane1.getViewport().add(editorPane, null);
  155. }
  156. }