gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.swing / org.gvsig.scripting.swing.impl / src / main / java / org / gvsig / scripting / swing / impl / syntaxhighlight / JRSyntaxTextArea.java @ 478
History | View | Annotate | Download (9.87 KB)
1 |
package org.gvsig.scripting.swing.impl.syntaxhighlight; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.awt.event.KeyEvent; |
7 |
import java.awt.event.KeyListener; |
8 |
import java.net.URL; |
9 |
import java.util.HashMap; |
10 |
import java.util.Map; |
11 |
import javax.swing.ImageIcon; |
12 |
import javax.swing.JCheckBox; |
13 |
import javax.swing.JComponent; |
14 |
import javax.swing.JOptionPane; |
15 |
import javax.swing.JPanel; |
16 |
|
17 |
import javax.swing.JScrollPane; |
18 |
import javax.swing.KeyStroke; |
19 |
import javax.swing.UIManager; |
20 |
import javax.swing.event.CaretEvent; |
21 |
import javax.swing.event.CaretListener; |
22 |
import javax.swing.text.JTextComponent; |
23 |
import org.fife.rsta.ui.CollapsibleSectionPanel; |
24 |
import org.fife.rsta.ui.search.FindToolBar; |
25 |
import org.fife.rsta.ui.search.ReplaceToolBar; |
26 |
import org.fife.rsta.ui.search.SearchEvent; |
27 |
import org.fife.rsta.ui.search.SearchListener; |
28 |
|
29 |
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; |
30 |
import org.fife.ui.rsyntaxtextarea.SyntaxConstants; |
31 |
import org.fife.ui.rtextarea.RTextScrollPane; |
32 |
import org.fife.ui.rtextarea.SearchContext; |
33 |
import org.fife.ui.rtextarea.SearchEngine; |
34 |
import org.fife.ui.rtextarea.SearchResult; |
35 |
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent; |
36 |
|
37 |
public class JRSyntaxTextArea implements SyntaxtHighlightTextComponent, SearchListener { |
38 |
|
39 |
public static class MyCheckBox extends JCheckBox { |
40 |
|
41 |
private static final String uiClassID = "ToggleButtonUI"; |
42 |
|
43 |
@Override
|
44 |
public String getUIClassID() { |
45 |
return uiClassID;
|
46 |
} |
47 |
|
48 |
} |
49 |
|
50 |
private static class MyFindToolBar extends FindToolBar { |
51 |
|
52 |
public MyFindToolBar(SearchListener listener) {
|
53 |
super(listener);
|
54 |
} |
55 |
|
56 |
@Override
|
57 |
protected JCheckBox createCB(String key) { |
58 |
JCheckBox cb = new MyCheckBox(); |
59 |
cb.setToolTipText(searchMsg.getString(key)); |
60 |
cb.addActionListener(listener); |
61 |
cb.addMouseListener(listener); |
62 |
cb.setIcon(this.getIcon(key));
|
63 |
return cb;
|
64 |
} |
65 |
|
66 |
private ImageIcon getIcon(String key) { |
67 |
String res = "images/search-" + key.toLowerCase() + ".png"; |
68 |
URL url = this.getClass().getResource(res); |
69 |
if (url == null) { |
70 |
return new ImageIcon(); |
71 |
} |
72 |
return new ImageIcon(url); |
73 |
} |
74 |
|
75 |
} |
76 |
|
77 |
private static class MyReplaceToolBar extends ReplaceToolBar { |
78 |
|
79 |
public MyReplaceToolBar(SearchListener listener) {
|
80 |
super(listener);
|
81 |
} |
82 |
|
83 |
@Override
|
84 |
protected JCheckBox createCB(String key) { |
85 |
JCheckBox cb = new MyCheckBox(); |
86 |
cb.setToolTipText(searchMsg.getString(key)); |
87 |
cb.addActionListener(listener); |
88 |
cb.addMouseListener(listener); |
89 |
cb.setIcon(this.getIcon(key));
|
90 |
return cb;
|
91 |
} |
92 |
|
93 |
private ImageIcon getIcon(String key) { |
94 |
String res = "images/search-" + key.toLowerCase() + ".png"; |
95 |
URL url = this.getClass().getResource(res); |
96 |
if (url == null) { |
97 |
return new ImageIcon(); |
98 |
} |
99 |
return new ImageIcon(url); |
100 |
} |
101 |
|
102 |
} |
103 |
|
104 |
private final FindToolBar findToolBar; |
105 |
private final ReplaceToolBar replaceToolBar; |
106 |
private final CollapsibleSectionPanel csp; |
107 |
|
108 |
public class DefaultUpdateCaretPositionActionEvent extends ActionEvent implements UpdateCaretPositionActionEvent { |
109 |
|
110 |
/**
|
111 |
*
|
112 |
*/
|
113 |
private static final long serialVersionUID = 8238486105726094074L; |
114 |
int line = -1; |
115 |
int column = -1; |
116 |
|
117 |
public DefaultUpdateCaretPositionActionEvent(Object source, int id, |
118 |
String command, int line, int column) { |
119 |
super(source, id, command);
|
120 |
this.line = line;
|
121 |
this.column = column;
|
122 |
} |
123 |
|
124 |
public int getLine() { |
125 |
return this.line + 1; |
126 |
} |
127 |
|
128 |
public int getColumn() { |
129 |
return this.column; |
130 |
} |
131 |
|
132 |
public boolean hasLineAndColumn() { |
133 |
return this.line >= 0 && this.column >= 0; |
134 |
} |
135 |
|
136 |
} |
137 |
|
138 |
protected ActionListener updateCaretPosition = null; |
139 |
protected RSyntaxTextArea textArea = null; |
140 |
private final RTextScrollPane scrollPanel; |
141 |
private JPanel panel; |
142 |
private Map<String, String> contentTypeAlias; |
143 |
|
144 |
public JRSyntaxTextArea() {
|
145 |
this.contentTypeAlias = new HashMap<>(); |
146 |
this.contentTypeAlias.put("text/r", SyntaxConstants.SYNTAX_STYLE_C); |
147 |
this.contentTypeAlias.put("text/ecmascript", SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT); |
148 |
|
149 |
this.textArea = new RSyntaxTextArea(20, 60); |
150 |
this.init();
|
151 |
scrollPanel = new RTextScrollPane(this.textArea); |
152 |
panel = new JPanel(); |
153 |
panel.setLayout(new BorderLayout()); |
154 |
panel.add(scrollPanel, BorderLayout.CENTER);
|
155 |
|
156 |
findToolBar = new MyFindToolBar(this); |
157 |
replaceToolBar = new MyReplaceToolBar(this); |
158 |
replaceToolBar.setSearchContext(findToolBar.getSearchContext()); |
159 |
csp = new CollapsibleSectionPanel();
|
160 |
csp.add(scrollPanel); |
161 |
panel.add(csp, BorderLayout.CENTER);
|
162 |
int ctrl = (KeyEvent.CTRL_MASK | KeyEvent.ALT_MASK); |
163 |
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_F, ctrl); |
164 |
csp.addBottomComponent(ks, findToolBar); |
165 |
ks = KeyStroke.getKeyStroke(KeyEvent.VK_H, ctrl); |
166 |
csp.addBottomComponent(ks, replaceToolBar); |
167 |
|
168 |
textArea.addKeyListener(new KeyListener() { |
169 |
|
170 |
@Override
|
171 |
public void keyTyped(KeyEvent ke) { |
172 |
} |
173 |
|
174 |
@Override
|
175 |
public void keyPressed(KeyEvent ke) { |
176 |
if (ke.getModifiers() == (KeyEvent.CTRL_MASK | KeyEvent.ALT_MASK)) { |
177 |
if (ke.getKeyCode() == KeyEvent.VK_H) { |
178 |
csp.showBottomComponent(replaceToolBar); |
179 |
} else if (ke.getKeyCode() == KeyEvent.VK_F) { |
180 |
csp.showBottomComponent(findToolBar); |
181 |
} |
182 |
} |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public void keyReleased(KeyEvent ke) { |
187 |
} |
188 |
}); |
189 |
|
190 |
} |
191 |
|
192 |
protected void init() { |
193 |
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PYTHON); |
194 |
textArea.setCodeFoldingEnabled(true);
|
195 |
textArea.setClearWhitespaceLinesEnabled(true);
|
196 |
textArea.setAutoIndentEnabled(true);
|
197 |
textArea.setCloseCurlyBraces(true);
|
198 |
textArea.setWhitespaceVisible(true);
|
199 |
textArea.setAnimateBracketMatching(true);
|
200 |
textArea.setBracketMatchingEnabled(true);
|
201 |
textArea.setAutoIndentEnabled(true);
|
202 |
textArea.setTabsEmulated(true);
|
203 |
textArea.setTabSize(2);
|
204 |
textArea.setAntiAliasingEnabled(true);
|
205 |
|
206 |
textArea.addCaretListener(new CaretListener() { |
207 |
public void caretUpdate(CaretEvent e) { |
208 |
if (updateCaretPosition == null) { |
209 |
return;
|
210 |
} |
211 |
updateCaretPosition.actionPerformed( |
212 |
new DefaultUpdateCaretPositionActionEvent(textArea, 1, "position", textArea.getCaretLineNumber(), textArea.getCaretOffsetFromLineStart()) |
213 |
); |
214 |
} |
215 |
|
216 |
}); |
217 |
} |
218 |
|
219 |
private String normalizeContentType(String s) { |
220 |
s = s.toLowerCase(); |
221 |
if (this.contentTypeAlias.containsKey(s)) { |
222 |
s = this.contentTypeAlias.get(s);
|
223 |
} |
224 |
return s;
|
225 |
} |
226 |
|
227 |
public void setContentType(String contentType) { |
228 |
this.textArea.setSyntaxEditingStyle(this.normalizeContentType(contentType)); |
229 |
} |
230 |
|
231 |
public JScrollPane getJScrollPane() { |
232 |
return this.scrollPanel; |
233 |
} |
234 |
|
235 |
public JTextComponent getJTextComponent() { |
236 |
return this.textArea; |
237 |
} |
238 |
|
239 |
@Override
|
240 |
public JComponent asJComponent() { |
241 |
return this.panel; |
242 |
} |
243 |
|
244 |
public String getContentType() { |
245 |
return this.textArea.getSyntaxEditingStyle(); |
246 |
} |
247 |
|
248 |
public void addUpdateCaretPositionActionListener(ActionListener updateCaretPosition) { |
249 |
this.updateCaretPosition = updateCaretPosition;
|
250 |
} |
251 |
|
252 |
public void setText(String text) { |
253 |
this.textArea.setText(text);
|
254 |
} |
255 |
|
256 |
public String getText() { |
257 |
return this.textArea.getText(); |
258 |
} |
259 |
|
260 |
public void addKeyListener(KeyListener keyListener) { |
261 |
this.textArea.addKeyListener(keyListener);
|
262 |
} |
263 |
|
264 |
@Override
|
265 |
public void searchEvent(SearchEvent e) { |
266 |
|
267 |
SearchEvent.Type type = e.getType(); |
268 |
SearchContext context = e.getSearchContext(); |
269 |
SearchResult result = null; |
270 |
|
271 |
switch (type) {
|
272 |
default: // Prevent FindBugs warning later |
273 |
case MARK_ALL:
|
274 |
result = SearchEngine.markAll(textArea, context); |
275 |
break;
|
276 |
case FIND:
|
277 |
result = SearchEngine.find(textArea, context); |
278 |
if (!result.wasFound()) {
|
279 |
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
|
280 |
} |
281 |
break;
|
282 |
case REPLACE:
|
283 |
result = SearchEngine.replace(textArea, context); |
284 |
if (!result.wasFound()) {
|
285 |
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
|
286 |
} |
287 |
break;
|
288 |
case REPLACE_ALL:
|
289 |
result = SearchEngine.replaceAll(textArea, context); |
290 |
JOptionPane.showMessageDialog(null, result.getCount() |
291 |
+ " occurrences replaced.");
|
292 |
break;
|
293 |
} |
294 |
String text = null; |
295 |
if (result.wasFound()) {
|
296 |
text = "Text found; occurrences marked: " + result.getMarkedCount();
|
297 |
} else if (type == SearchEvent.Type.MARK_ALL) { |
298 |
if (result.getMarkedCount() > 0) { |
299 |
text = "Occurrences marked: " + result.getMarkedCount();
|
300 |
} else {
|
301 |
text = "";
|
302 |
} |
303 |
} else {
|
304 |
text = "Text not found";
|
305 |
} |
306 |
//setMessage(text);
|
307 |
} |
308 |
|
309 |
@Override
|
310 |
public String getSelectedText() { |
311 |
return textArea.getSelectedText();
|
312 |
} |
313 |
|
314 |
} |