Statistics
| Revision:

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
package org.gvsig.scripting.swing.impl.composer.editors;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.event.ActionListener;
5
import java.beans.PropertyChangeListener;
6
import javax.swing.JPanel;
7
import javax.swing.event.DocumentEvent;
8
import javax.swing.event.DocumentListener;
9
import javax.swing.text.JTextComponent;
10
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
11
import org.gvsig.scripting.swing.api.ScriptingSwingLocator;
12
import org.gvsig.scripting.swing.api.ScriptingUIManager;
13
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent;
14
import org.gvsig.scripting.swing.impl.composer.ChangeListenerSupport;
15

    
16
public class BaseTextEditor extends JPanel {
17

    
18
    private static final long serialVersionUID = -4217823183731699721L;
19

    
20
    private final ChangeListenerSupport changeListeners;
21
    final ScriptingUIManager uimanager;
22
    private SyntaxtHighlightTextComponent editorText;
23

    
24
    
25
    public BaseTextEditor() {
26
        this.uimanager = ScriptingSwingLocator.getUIManager();
27
        this.changeListeners = new ChangeListenerSupport();
28
        this.initComponent();
29
    }
30

    
31
    public ChangeListenerSupport getChangeListenerSupport() {
32
        return this.changeListeners;
33
    }
34
    
35
    public void addUpdateCaretPositionActionListener(ActionListener actionlistener) {
36
        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
        editorText.getJTextComponent().getDocument().addDocumentListener(new DocumentListener() {
47
            @Override
48
            public void insertUpdate(DocumentEvent e) {
49
                changeListeners.fireChange("code", null);
50
            }
51

    
52
            @Override
53
            public void removeUpdate(DocumentEvent e) {
54
                changeListeners.fireChange("code", null);
55
            }
56

    
57
            @Override
58
            public void changedUpdate(DocumentEvent e) {
59
                changeListeners.fireChange("code", null);
60
            }
61
        });
62
        this.add(editorText.asJComponent(), BorderLayout.CENTER);
63
    }
64

    
65
    public void setText(String mimeType, String text) {
66
        boolean enabled = this.changeListeners.setEnabled(false);
67
        try {
68
            editorText.setContentType(mimeType);
69
            editorText.setText(text);
70
            editorText.discardAllEdits();
71
        } finally {
72
            this.changeListeners.setEnabled(enabled);
73
        }
74
    }
75
    
76
    public String getText() {
77
        return this.editorText.getText();
78
    }
79

    
80
    public JTextComponent getJTextComponent() {
81
        return this.editorText.getJTextComponent();
82
    }
83

    
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
    
107
    public SyntaxtHighlightTextComponent getSyntaxtHighlightTextComponent() {
108
        return this.editorText;
109
    }
110

    
111
    public void gotoline(int line) {
112
        getSyntaxtHighlightTextComponent().gotoline(line);
113
    }
114

    
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
}