KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > ajde > ui > swing > BasicEditor


1
2 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3  *
4  * This file is part of the IDE support for the AspectJ(tm)
5  * programming language; see http://aspectj.org
6  *
7  * The contents of this file are subject to the Mozilla Public License
8  * Version 1.1 (the "License"); you may not use this file except in
9  * compliance with the License. You may obtain a copy of the License at
10  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is AspectJ.
18  *
19  * The Initial Developer of the Original Code is Xerox Corporation. Portions
20  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
21  * All Rights Reserved.
22  *
23  * Contributor(s):
24  */

25
26 package org.aspectj.ajde.ui.swing;
27
28 import java.awt.*;
29 import javax.swing.*;
30 import javax.swing.text.*;
31 import java.util.*;
32 import java.io.*;
33 import java.net.URL JavaDoc;
34 import org.aspectj.asm.*;
35 import org.aspectj.ajde.*;
36
37 /**
38  * Bare-bones editor implementation used when the framework is being used
39  * standalone.
40  *
41  * @author Mik Kersten
42  */

43 public class BasicEditor implements EditorAdapter {
44
45     private String JavaDoc NO_FILE = "<no file selected>";
46     private String JavaDoc filePath = NO_FILE;
47     private JPanel editor_panel = new JPanel();
48
49     // @todo get rid of these
50
private int currHighlightStart = 0;
51     private int currHighlightEnd = 0;
52
53     private BorderLayout borderLayout1 = new BorderLayout();
54     private JScrollPane jScrollPane1 = new JScrollPane();
55     private JEditorPane editorPane = new JEditorPane();
56
57     public BasicEditor() {
58         try {
59             editorPane.setEditable(true);
60             editorPane.setContentType("text/plain");
61             editorPane.setFont(new Font("Monospaced", 0, 11));
62             editor_panel.add(editorPane);
63             jbInit();
64         }
65         catch(Exception JavaDoc e) {
66             Ajde.getDefault().getErrorHandler().handleError("Could not initialize GUI.", e);
67         }
68     }
69
70     public String JavaDoc getCurrFile() {
71         return filePath;
72     }
73
74     public void showSourceLine(SourceLocation sourceLocation, boolean highlight) {
75         showSourceLine(sourceLocation.getSourceFilePath(), sourceLocation.getLineNumber(), highlight);
76     }
77
78     public void showSourceLine(int lineNumber, boolean highlight) {
79         showSourceLine(filePath, lineNumber, highlight);
80     }
81
82     public void pasteToCaretPos(String JavaDoc text) {
83         if (currHighlightEnd < 1) return;
84         String JavaDoc contents = editorPane.getText();
85         String JavaDoc pasted = contents.substring(0, currHighlightEnd) +
86             text + contents.substring(currHighlightEnd, contents.length());
87         editorPane.setText(pasted);
88     }
89
90     public void showSourceLine(String JavaDoc filePath, int lineNumber, boolean highlight) {
91         //AjdeUIManager.getDefault().getIdeUIAdapter().resetEditor();
92

93         String JavaDoc oldPath = this.filePath;
94         this.filePath = filePath;
95 // if (oldPath != filePath && !Ajde.INSTANCE.BROWSER_MANAGER.isGlobalMode()) {
96
// Ajde.INSTANCE.BROWSER_MANAGER.updateView();
97
// }
98

99 // Ajde.IDE_MANAGER.setEditorStatusText(filePath);
100

101         currHighlightStart = 0;
102         currHighlightEnd = 0;
103         editorPane.setText(readFile(filePath, lineNumber));
104         try {
105             editorPane.getHighlighter().addHighlight(currHighlightStart, currHighlightEnd, DefaultHighlighter.DefaultPainter);
106             editorPane.setCaretPosition(currHighlightStart);
107         } catch (BadLocationException ble) {
108             Ajde.getDefault().getErrorHandler().handleError("Could not highlight location.", ble);
109         }
110         Ajde.getDefault().getEditorManager().notifyCurrentFileChanged(filePath);
111     }
112
113     /**
114      * Not implemented.
115      */

116     public void showSourcelineAnnotation(String JavaDoc filePath, int lineNumber, java.util.List JavaDoc items) { }
117
118     public void addEditorViewForSourceLine(String JavaDoc filePath, int lineNumber) {
119         
120     }
121
122     public void saveContents() throws IOException {
123         if (filePath != NO_FILE && filePath != "" && editorPane.getText() != "") {
124             BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
125             writer.write(editorPane.getText());
126             writer.flush();
127         }
128     }
129
130     public JPanel getPanel() {
131         return editor_panel;
132     }
133
134     public void showSourceForFile(String JavaDoc filePath) { }
135
136     public void showSourceForLine(int lineNumber, boolean highlight) { }
137
138     public void showSourceForSourceLine(String JavaDoc filePath, int lineNumber, boolean highlight) { }
139
140     public String JavaDoc getCurrSourceFilePath() { return null; }
141
142     public void setBreakpointRequest(String JavaDoc filePath, int lineNumber, boolean isDeferred) { }
143
144     public void clearBreakpointRequest(String JavaDoc filePath, int lineNumber) { }
145
146     private String JavaDoc readFile(String JavaDoc filePath, int lineNumber) {
147         try {
148             URL JavaDoc url = ClassLoader.getSystemResource(filePath);
149             File file = new File(filePath);
150             if (!file.exists()) {
151                 return "ERROR: file \"" + filePath + "\" does not exist.";
152             }
153             BufferedReader reader = new BufferedReader(new FileReader(file));
154             StringBuffer JavaDoc contents = new StringBuffer JavaDoc();
155             String JavaDoc line = reader.readLine();
156             int numChars = 0;
157             int numLines = 0;
158             while (line != null) {
159                 numLines++;
160                 if (numLines < lineNumber) {
161                     currHighlightStart += line.length()+1;
162                 }
163                 if (numLines == lineNumber) {
164                     currHighlightEnd = currHighlightStart + line.length();
165                 }
166                 contents.append(line);
167                 contents.append('\n');
168                 line = reader.readLine();
169             }
170             return contents.toString();
171         } catch (IOException ioe) {
172             return "ERROR: could not read file \"" + filePath + "\", make sure that you have mounted /project/aop on X:\\";
173         }
174     }
175
176     private void jbInit() throws Exception JavaDoc {
177         editor_panel.setFont(new java.awt.Font JavaDoc("DialogInput", 1, 12));
178         editor_panel.setLayout(borderLayout1);
179         editor_panel.add(jScrollPane1, BorderLayout.CENTER);
180         jScrollPane1.getViewport().add(editorPane, null);
181     }
182 }
183
Popular Tags