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 / composer / editors / BaseTextEditor.java @ 1486
History | View | Annotate | Download (4.32 KB)
1 |
package org.gvsig.scripting.swing.impl.composer.editors; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.event.ActionListener; |
5 |
import java.awt.event.FocusListener; |
6 |
import java.beans.PropertyChangeListener; |
7 |
import javax.swing.JPanel; |
8 |
import javax.swing.event.DocumentEvent; |
9 |
import javax.swing.event.DocumentListener; |
10 |
import javax.swing.text.JTextComponent; |
11 |
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; |
12 |
import org.gvsig.scripting.swing.api.ScriptingSwingLocator; |
13 |
import org.gvsig.scripting.swing.api.ScriptingUIManager; |
14 |
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent; |
15 |
import org.gvsig.scripting.swing.impl.composer.ChangeListenerSupport; |
16 |
|
17 |
public class BaseTextEditor extends JPanel { |
18 |
|
19 |
private static final long serialVersionUID = -4217823183731699721L; |
20 |
|
21 |
private final ChangeListenerSupport changeListeners; |
22 |
final ScriptingUIManager uimanager;
|
23 |
private SyntaxtHighlightTextComponent editorText;
|
24 |
|
25 |
|
26 |
public BaseTextEditor() {
|
27 |
this.uimanager = ScriptingSwingLocator.getUIManager();
|
28 |
this.changeListeners = new ChangeListenerSupport(); |
29 |
this.initComponent();
|
30 |
} |
31 |
|
32 |
public ChangeListenerSupport getChangeListenerSupport() {
|
33 |
return this.changeListeners; |
34 |
} |
35 |
|
36 |
public void addUpdateCaretPositionActionListener(ActionListener actionlistener) { |
37 |
editorText.addUpdateCaretPositionActionListener(actionlistener); |
38 |
} |
39 |
|
40 |
public void addChangeListener(PropertyChangeListener listener) { |
41 |
this.changeListeners.addChangeListener(listener);
|
42 |
} |
43 |
|
44 |
protected void initComponent() { |
45 |
this.setLayout(new BorderLayout()); |
46 |
editorText = (SyntaxtHighlightTextComponent) uimanager.createSyntaxHighlightingPanel(); |
47 |
editorText.getJTextComponent().getDocument().addDocumentListener(new DocumentListener() { |
48 |
@Override
|
49 |
public void insertUpdate(DocumentEvent e) { |
50 |
changeListeners.fireChange("code", null); |
51 |
} |
52 |
|
53 |
@Override
|
54 |
public void removeUpdate(DocumentEvent e) { |
55 |
changeListeners.fireChange("code", null); |
56 |
} |
57 |
|
58 |
@Override
|
59 |
public void changedUpdate(DocumentEvent e) { |
60 |
changeListeners.fireChange("code", null); |
61 |
} |
62 |
}); |
63 |
this.add(editorText.asJComponent(), BorderLayout.CENTER); |
64 |
} |
65 |
|
66 |
public void setText(String mimeType, String text) { |
67 |
boolean enabled = this.changeListeners.setEnabled(false); |
68 |
try {
|
69 |
editorText.setContentType(mimeType); |
70 |
editorText.setText(text); |
71 |
editorText.discardAllEdits(); |
72 |
} finally {
|
73 |
this.changeListeners.setEnabled(enabled);
|
74 |
} |
75 |
} |
76 |
|
77 |
public String getText() { |
78 |
return this.editorText.getText(); |
79 |
} |
80 |
|
81 |
public JTextComponent getJTextComponent() { |
82 |
return this.editorText.getJTextComponent(); |
83 |
} |
84 |
|
85 |
public void selectLine(int line) { |
86 |
JTextComponent editor = this.getJTextComponent(); |
87 |
editor.requestFocusInWindow(); |
88 |
|
89 |
String code = editor.getText();
|
90 |
int lineCounter = 0; |
91 |
int initialSelection = 0; |
92 |
int finalSelection = 0; |
93 |
for (int j = 0; j < code.length(); j++) { |
94 |
if (code.charAt(j) == '\n') { |
95 |
lineCounter++; |
96 |
if (lineCounter == line - 1) { |
97 |
initialSelection = j; |
98 |
} |
99 |
if (lineCounter == line || (finalSelection == 0 && j == code.length() - 1)) { |
100 |
finalSelection = j; |
101 |
} |
102 |
} |
103 |
} |
104 |
|
105 |
editor.select(initialSelection, finalSelection); |
106 |
} |
107 |
|
108 |
public SyntaxtHighlightTextComponent getSyntaxtHighlightTextComponent() {
|
109 |
return this.editorText; |
110 |
} |
111 |
|
112 |
public void gotoline(int line) { |
113 |
getSyntaxtHighlightTextComponent().gotoline(line); |
114 |
} |
115 |
|
116 |
public int getLineCount() { |
117 |
JTextComponent textComponent = this.getJTextComponent(); |
118 |
if( textComponent instanceof RSyntaxTextArea ) { |
119 |
return ((RSyntaxTextArea)textComponent).getLineCount();
|
120 |
} |
121 |
return 0; |
122 |
} |
123 |
|
124 |
public void setEditable(boolean editable) { |
125 |
this.editorText.setEditable(editable);
|
126 |
} |
127 |
|
128 |
public boolean isEditable() { |
129 |
return this.editorText.isEditable(); |
130 |
} |
131 |
|
132 |
public void addFocusListener(FocusListener l) { |
133 |
this.editorText.addFocusListener(l);
|
134 |
} |
135 |
} |