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 / syntaxhighlight / JEditorPaneSyntaxHighlighting.java @ 468

History | View | Annotate | Download (3.92 KB)

1
package org.gvsig.scripting.swing.impl.syntaxhighlight;
2

    
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5
import javax.swing.JComponent;
6

    
7
import javax.swing.JEditorPane;
8
import javax.swing.JScrollPane;
9
import javax.swing.event.CaretEvent;
10
import javax.swing.event.CaretListener;
11
import javax.swing.text.BadLocationException;
12
import javax.swing.text.Document;
13
import javax.swing.text.JTextComponent;
14
import javax.swing.text.StyledEditorKit;
15

    
16
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent;
17
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent.UpdateCaretPositionActionEvent;
18
import org.gvsig.scripting.swing.impl.syntaxhighlight.styles.JavaStyledDocument;
19
import org.gvsig.scripting.swing.impl.syntaxhighlight.styles.PythonStyledDocument;
20

    
21
public class JEditorPaneSyntaxHighlighting extends JEditorPane implements SyntaxtHighlightTextComponent {
22

    
23
        /**
24
         * 
25
         */
26
        private static final long serialVersionUID = -4451156833693922180L;
27

    
28
        protected ActionListener updateCaretPosition = null; 
29
        
30
        public JEditorPaneSyntaxHighlighting() {
31

    
32
                this.setEditorKitForContentType(
33
                                "text/java",
34
                                new StyledEditorKit()
35
                                {
36
                                        /**
37
                                         * 
38
                                         */
39
                                        private static final long serialVersionUID = 2002618010308466332L;
40

    
41
                                        public Document createDefaultDocument()
42
                                        {
43
                                                return new JavaStyledDocument();
44
                                        }
45
                                }
46
                );
47
                this.setEditorKitForContentType(
48
                                "text/javascript",
49
                                new StyledEditorKit()
50
                                {
51
                                        /**
52
                                         * 
53
                                         */
54
                                        private static final long serialVersionUID = 830100022798600914L;
55

    
56
                                        public Document createDefaultDocument()
57
                                        {
58
                                                return new JavaStyledDocument();
59
                                        }
60
                                }
61
                );
62
                this.setEditorKitForContentType(
63
                                "text/python",
64
                                new StyledEditorKit()
65
                                {
66
                                        /**
67
                                         * 
68
                                         */
69
                                        private static final long serialVersionUID = -2728112890159261335L;
70

    
71
                                        public Document createDefaultDocument()
72
                                        {
73
                                                return new PythonStyledDocument();
74
                                        }
75
                                }
76
                );
77

    
78
                final JEditorPaneSyntaxHighlighting source = this;
79
                this.addCaretListener(new CaretListener() {
80
                        public void caretUpdate(CaretEvent e) {
81
                                if( updateCaretPosition== null ) {
82
                                        return;
83
                                }
84
                                int lineNumber = 1;
85
                                int columnNumber = 0;
86
                                int currentPosition = getCaretPosition();
87
                                Document doc = getDocument();
88
                                
89
                                PythonStyledDocument pydoc = null;
90
                                if( doc instanceof PythonStyledDocument ) {
91
                                        pydoc  = (PythonStyledDocument) doc;
92
                                }
93
                                
94
                                try        {
95
                                        String strText = doc.getText(0, currentPosition);
96
                                        char arrText[] = strText.toCharArray();
97
                                        for(int i=0; i<strText.length(); i++) {
98
                                                if(arrText[i] == '\n') lineNumber++;
99
                                        }
100
                                        columnNumber = currentPosition - strText.lastIndexOf('\n');
101
                                }
102
                                catch(BadLocationException ble)        {
103
                                        System.err.println(ble);
104
                                }
105
                                if( pydoc != null ) {
106
                                        pydoc.setPosition(lineNumber, columnNumber);
107
                                }
108
                                updateCaretPosition.actionPerformed(
109
                                                new DefaultUpdateCaretPositionActionEvent(source, 1, "position", lineNumber, columnNumber));
110
                        }
111
                        
112
                });
113
                
114
        }
115
        
116
        public void addUpdateCaretPositionActionListener(ActionListener updateCaretPosition) {
117
                this.updateCaretPosition = updateCaretPosition;  
118
        }
119

    
120
        public JScrollPane getJScrollPane() {
121
                return  new JScrollPane(this);
122
        }
123

    
124
        public JTextComponent getJTextComponent() {
125
                return this;
126
        }
127

    
128
        
129
        @Override
130
        public JComponent asJComponent() {
131
            return this.getJScrollPane();
132
        }
133
        
134
        public class DefaultUpdateCaretPositionActionEvent extends ActionEvent implements UpdateCaretPositionActionEvent {
135

    
136
                int line = -1;
137
                int column = -1;
138
                
139
                public DefaultUpdateCaretPositionActionEvent(Object source, int id,
140
                                String command, int line, int column) {
141
                        super(source, id, command);
142
                        this.line = line;
143
                        this.column = column;
144
                }
145

    
146
                public int getLine() {
147
                        return this.line;
148
                }
149

    
150
                public int getColumn() {
151
                        return this.column;
152
                }
153

    
154
                public boolean hasLineAndColumn() {
155
                        return this.line >=0 && this.column >= 0;
156
                }
157
                
158
        }
159
}