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 @ 468
History | View | Annotate | Download (7.98 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.util.HashMap; |
9 |
import java.util.Map; |
10 |
import javax.swing.JComponent; |
11 |
import javax.swing.JOptionPane; |
12 |
import javax.swing.JPanel; |
13 |
|
14 |
import javax.swing.JScrollPane; |
15 |
import javax.swing.KeyStroke; |
16 |
import javax.swing.UIManager; |
17 |
import javax.swing.event.CaretEvent; |
18 |
import javax.swing.event.CaretListener; |
19 |
import javax.swing.text.JTextComponent; |
20 |
import org.fife.rsta.ui.CollapsibleSectionPanel; |
21 |
import org.fife.rsta.ui.search.FindToolBar; |
22 |
import org.fife.rsta.ui.search.ReplaceToolBar; |
23 |
import org.fife.rsta.ui.search.SearchEvent; |
24 |
import org.fife.rsta.ui.search.SearchListener; |
25 |
|
26 |
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; |
27 |
import org.fife.ui.rsyntaxtextarea.SyntaxConstants; |
28 |
import org.fife.ui.rtextarea.RTextScrollPane; |
29 |
import org.fife.ui.rtextarea.SearchContext; |
30 |
import org.fife.ui.rtextarea.SearchEngine; |
31 |
import org.fife.ui.rtextarea.SearchResult; |
32 |
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent; |
33 |
|
34 |
public class JRSyntaxTextArea implements SyntaxtHighlightTextComponent, SearchListener { |
35 |
private final FindToolBar findToolBar; |
36 |
private final ReplaceToolBar replaceToolBar; |
37 |
private final CollapsibleSectionPanel csp; |
38 |
|
39 |
public class DefaultUpdateCaretPositionActionEvent extends ActionEvent implements UpdateCaretPositionActionEvent { |
40 |
|
41 |
/**
|
42 |
*
|
43 |
*/
|
44 |
private static final long serialVersionUID = 8238486105726094074L; |
45 |
int line = -1; |
46 |
int column = -1; |
47 |
|
48 |
public DefaultUpdateCaretPositionActionEvent(Object source, int id, |
49 |
String command, int line, int column) { |
50 |
super(source, id, command);
|
51 |
this.line = line;
|
52 |
this.column = column;
|
53 |
} |
54 |
|
55 |
public int getLine() { |
56 |
return this.line + 1; |
57 |
} |
58 |
|
59 |
public int getColumn() { |
60 |
return this.column; |
61 |
} |
62 |
|
63 |
public boolean hasLineAndColumn() { |
64 |
return this.line >= 0 && this.column >= 0; |
65 |
} |
66 |
|
67 |
} |
68 |
|
69 |
protected ActionListener updateCaretPosition = null; |
70 |
protected RSyntaxTextArea textArea = null; |
71 |
private final RTextScrollPane scrollPanel; |
72 |
private JPanel panel; |
73 |
private Map<String,String> contentTypeAlias; |
74 |
|
75 |
public JRSyntaxTextArea() {
|
76 |
this.contentTypeAlias = new HashMap<>(); |
77 |
this.contentTypeAlias.put("text/r",SyntaxConstants.SYNTAX_STYLE_C); |
78 |
this.contentTypeAlias.put("text/ecmascript", SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT); |
79 |
|
80 |
this.textArea = new RSyntaxTextArea(20, 60); |
81 |
this.init();
|
82 |
scrollPanel = new RTextScrollPane(this.textArea); |
83 |
panel = new JPanel(); |
84 |
panel.setLayout(new BorderLayout()); |
85 |
panel.add(scrollPanel, BorderLayout.CENTER);
|
86 |
|
87 |
findToolBar = new FindToolBar(this); |
88 |
replaceToolBar = new ReplaceToolBar(this); |
89 |
replaceToolBar.setSearchContext(findToolBar.getSearchContext()); |
90 |
csp = new CollapsibleSectionPanel();
|
91 |
csp.add(scrollPanel); |
92 |
panel.add(csp, BorderLayout.CENTER);
|
93 |
int ctrl =(KeyEvent.CTRL_MASK|KeyEvent.ALT_MASK); |
94 |
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_F, ctrl); |
95 |
csp.addBottomComponent(ks, findToolBar); |
96 |
ks = KeyStroke.getKeyStroke(KeyEvent.VK_H, ctrl); |
97 |
csp.addBottomComponent(ks, replaceToolBar); |
98 |
|
99 |
textArea.addKeyListener(new KeyListener() { |
100 |
|
101 |
@Override
|
102 |
public void keyTyped(KeyEvent ke) { |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public void keyPressed(KeyEvent ke) { |
107 |
if( ke.getModifiers() == (KeyEvent.CTRL_MASK|KeyEvent.ALT_MASK) ) { |
108 |
if( ke.getKeyCode() == KeyEvent.VK_H) { |
109 |
csp.showBottomComponent(replaceToolBar); |
110 |
} else if( ke.getKeyCode() == KeyEvent.VK_F) { |
111 |
csp.showBottomComponent(findToolBar); |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public void keyReleased(KeyEvent ke) { |
118 |
} |
119 |
}); |
120 |
|
121 |
} |
122 |
|
123 |
protected void init() { |
124 |
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PYTHON); |
125 |
textArea.setCodeFoldingEnabled(true);
|
126 |
textArea.setClearWhitespaceLinesEnabled(true);
|
127 |
textArea.setAutoIndentEnabled(true);
|
128 |
textArea.setCloseCurlyBraces(true);
|
129 |
textArea.setWhitespaceVisible(true);
|
130 |
textArea.setAnimateBracketMatching(true);
|
131 |
textArea.setBracketMatchingEnabled(true);
|
132 |
textArea.setAutoIndentEnabled(true);
|
133 |
textArea.setTabsEmulated(true);
|
134 |
textArea.setTabSize(2);
|
135 |
textArea.setAntiAliasingEnabled(true);
|
136 |
|
137 |
textArea.addCaretListener(new CaretListener() { |
138 |
public void caretUpdate(CaretEvent e) { |
139 |
if (updateCaretPosition == null) { |
140 |
return;
|
141 |
} |
142 |
updateCaretPosition.actionPerformed( |
143 |
new DefaultUpdateCaretPositionActionEvent(textArea, 1, "position", textArea.getCaretLineNumber(), textArea.getCaretOffsetFromLineStart()) |
144 |
); |
145 |
} |
146 |
|
147 |
}); |
148 |
} |
149 |
|
150 |
private String normalizeContentType(String s) { |
151 |
s = s.toLowerCase(); |
152 |
if( this.contentTypeAlias.containsKey(s) ) { |
153 |
s = this.contentTypeAlias.get(s);
|
154 |
} |
155 |
return s;
|
156 |
} |
157 |
|
158 |
public void setContentType(String contentType) { |
159 |
this.textArea.setSyntaxEditingStyle(this.normalizeContentType(contentType)); |
160 |
} |
161 |
|
162 |
public JScrollPane getJScrollPane() { |
163 |
return this.scrollPanel; |
164 |
} |
165 |
|
166 |
public JTextComponent getJTextComponent() { |
167 |
return this.textArea; |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public JComponent asJComponent() { |
172 |
return this.panel; |
173 |
} |
174 |
|
175 |
public String getContentType() { |
176 |
return this.textArea.getSyntaxEditingStyle(); |
177 |
} |
178 |
|
179 |
public void addUpdateCaretPositionActionListener(ActionListener updateCaretPosition) { |
180 |
this.updateCaretPosition = updateCaretPosition;
|
181 |
} |
182 |
|
183 |
public void setText(String text) { |
184 |
this.textArea.setText(text);
|
185 |
} |
186 |
|
187 |
public String getText() { |
188 |
return this.textArea.getText(); |
189 |
} |
190 |
|
191 |
public void addKeyListener(KeyListener keyListener) { |
192 |
this.textArea.addKeyListener(keyListener);
|
193 |
} |
194 |
|
195 |
@Override
|
196 |
public void searchEvent(SearchEvent e) { |
197 |
|
198 |
SearchEvent.Type type = e.getType(); |
199 |
SearchContext context = e.getSearchContext(); |
200 |
SearchResult result = null; |
201 |
|
202 |
switch (type) {
|
203 |
default: // Prevent FindBugs warning later |
204 |
case MARK_ALL:
|
205 |
result = SearchEngine.markAll(textArea, context); |
206 |
break;
|
207 |
case FIND:
|
208 |
result = SearchEngine.find(textArea, context); |
209 |
if (!result.wasFound()) {
|
210 |
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
|
211 |
} |
212 |
break;
|
213 |
case REPLACE:
|
214 |
result = SearchEngine.replace(textArea, context); |
215 |
if (!result.wasFound()) {
|
216 |
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
|
217 |
} |
218 |
break;
|
219 |
case REPLACE_ALL:
|
220 |
result = SearchEngine.replaceAll(textArea, context); |
221 |
JOptionPane.showMessageDialog(null, result.getCount() |
222 |
+ " occurrences replaced.");
|
223 |
break;
|
224 |
} |
225 |
String text = null; |
226 |
if (result.wasFound()) {
|
227 |
text = "Text found; occurrences marked: " + result.getMarkedCount();
|
228 |
} else if (type == SearchEvent.Type.MARK_ALL) { |
229 |
if (result.getMarkedCount() > 0) { |
230 |
text = "Occurrences marked: " + result.getMarkedCount();
|
231 |
} else {
|
232 |
text = "";
|
233 |
} |
234 |
} else {
|
235 |
text = "Text not found";
|
236 |
} |
237 |
//setMessage(text);
|
238 |
} |
239 |
|
240 |
@Override
|
241 |
public String getSelectedText() { |
242 |
return textArea.getSelectedText();
|
243 |
} |
244 |
|
245 |
} |