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.4KB

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