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