Revision 468 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

View differences:

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

  
3
import java.awt.BorderLayout;
3 4
import java.awt.event.ActionEvent;
4 5
import java.awt.event.ActionListener;
6
import java.awt.event.KeyEvent;
5 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;
6 13

  
7 14
import javax.swing.JScrollPane;
15
import javax.swing.KeyStroke;
16
import javax.swing.UIManager;
8 17
import javax.swing.event.CaretEvent;
9 18
import javax.swing.event.CaretListener;
10 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;
11 25

  
12 26
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
13 27
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
14 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;
15 32
import org.gvsig.scripting.swing.api.SyntaxtHighlightTextComponent;
16 33

  
17
public class JRSyntaxTextArea implements SyntaxtHighlightTextComponent {
34
public class JRSyntaxTextArea implements SyntaxtHighlightTextComponent, SearchListener {
35
    private final FindToolBar findToolBar;
36
    private final ReplaceToolBar replaceToolBar;
37
    private final CollapsibleSectionPanel csp;
18 38

  
19
	public class DefaultUpdateCaretPositionActionEvent extends ActionEvent implements UpdateCaretPositionActionEvent {
39
    public class DefaultUpdateCaretPositionActionEvent extends ActionEvent implements UpdateCaretPositionActionEvent {
20 40

  
21
		/**
22
		 * 
23
		 */
24
		private static final long serialVersionUID = 8238486105726094074L;
25
		int line = -1;
26
		int column = -1;
27
		
28
		public DefaultUpdateCaretPositionActionEvent(Object source, int id,
29
				String command, int line, int column) {
30
			super(source, id, command);
31
			this.line = line;
32
			this.column = column;
33
		}
41
        /**
42
         *
43
         */
44
        private static final long serialVersionUID = 8238486105726094074L;
45
        int line = -1;
46
        int column = -1;
34 47

  
35
		public int getLine() {
36
			return this.line+1;
37
		}
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
        }
38 54

  
39
		public int getColumn() {
40
			return this.column;
41
		}
55
        public int getLine() {
56
            return this.line + 1;
57
        }
42 58

  
43
		public boolean hasLineAndColumn() {
44
			return this.line >=0 && this.column >= 0;
45
		}
46
		
47
	}
48
	
49
	protected ActionListener updateCaretPosition = null;
50
	protected RSyntaxTextArea textArea = null;
51
	
52
	public JRSyntaxTextArea() {
53
		this.textArea = new RSyntaxTextArea(20, 60);
54
		this.init();
55
	}
56
	
57
	protected void init() {
58
		textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PYTHON);
59
		textArea.setCodeFoldingEnabled(true);
60
		textArea.setClearWhitespaceLinesEnabled(true);
61
		textArea.setAutoIndentEnabled(true);
62
		textArea.setCloseCurlyBraces(true);
63
		textArea.setWhitespaceVisible(true);
64
		textArea.setAnimateBracketMatching(true);
65
		textArea.setBracketMatchingEnabled(true);
66
		textArea.setAutoIndentEnabled(true);
67
		textArea.setTabsEmulated(true);
68
		textArea.setTabSize(2);
69
		textArea.setAntiAliasingEnabled(true);
70
		
71
		textArea.addCaretListener(new CaretListener() {
72
			public void caretUpdate(CaretEvent e) {
73
				if( updateCaretPosition== null ) {
74
					return;
75
				}
76
				updateCaretPosition.actionPerformed(
77
						new DefaultUpdateCaretPositionActionEvent(textArea, 1, "position", textArea.getCaretLineNumber(), textArea.getCaretOffsetFromLineStart() ) 
78
				);
79
			}
80
			
81
		});
82
	}
83
	
84
	public void setContentType(String contentType) {
85
		this.textArea.setSyntaxEditingStyle(contentType);
86
	}
87
	
88
	public JScrollPane getJScrollPane() {
89
		return  new RTextScrollPane(this.textArea);
90
	}
59
        public int getColumn() {
60
            return this.column;
61
        }
91 62

  
92
	public JTextComponent getJTextComponent() {
93
		return this.textArea;
94
	}
63
        public boolean hasLineAndColumn() {
64
            return this.line >= 0 && this.column >= 0;
65
        }
95 66

  
96
	public String getContentType() {
97
		return this.textArea.getSyntaxEditingStyle();
98
	}
99
	
100
	public void addUpdateCaretPositionActionListener(ActionListener updateCaretPosition) {
101
		this.updateCaretPosition = updateCaretPosition;  
102
	}
67
    }
103 68

  
104
	public void setText(String text) {
105
		this.textArea.setText(text);
106
	}
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);
107 79

  
108
	public String getText() {
109
		return this.textArea.getText();
110
	}
111
	
112
	public void addKeyListener(KeyListener keyListener) {
113
		this.textArea.addKeyListener(keyListener);
114
	}
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

  
115 245
}

Also available in: Unified diff