gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.util / org.gvsig.tools.util.impl / src / main / java / org / gvsig / texteditor / AbstractJTextEditor.java @ 2285
History | View | Annotate | Download (7.3 KB)
1 |
package org.gvsig.texteditor; |
---|---|
2 |
|
3 |
import java.awt.Container; |
4 |
import java.awt.Point; |
5 |
import java.awt.Rectangle; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.event.ActionListener; |
8 |
import javax.swing.JComponent; |
9 |
import javax.swing.JPanel; |
10 |
import javax.swing.JViewport; |
11 |
import javax.swing.SwingUtilities; |
12 |
import javax.swing.event.CaretEvent; |
13 |
import javax.swing.event.ChangeEvent; |
14 |
import javax.swing.event.ChangeListener; |
15 |
import javax.swing.event.DocumentEvent; |
16 |
import javax.swing.event.DocumentListener; |
17 |
import javax.swing.text.BadLocationException; |
18 |
import javax.swing.text.Document; |
19 |
import javax.swing.text.Element; |
20 |
import javax.swing.text.JTextComponent; |
21 |
import org.gvsig.tools.swing.api.ChangeListenerHelper; |
22 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
23 |
import org.slf4j.Logger; |
24 |
import org.slf4j.LoggerFactory; |
25 |
|
26 |
/**
|
27 |
*
|
28 |
* @author jjdelcerro
|
29 |
*/
|
30 |
public abstract class AbstractJTextEditor extends JPanel implements JTextEditor { |
31 |
protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultJTextEditor.class); |
32 |
|
33 |
protected static class UpdateCaretPositionActionEventImpl |
34 |
extends ActionEvent |
35 |
implements UpdateCaretPositionActionEvent
|
36 |
{ |
37 |
|
38 |
private final int line; |
39 |
private final int column; |
40 |
private final boolean hasLineAndCol; |
41 |
|
42 |
public UpdateCaretPositionActionEventImpl(Object comp, int line, int column, boolean hasLineAndCol) { |
43 |
super(comp, 1, "UpdateCaretPosition"); |
44 |
this.line = line;
|
45 |
this.column = column;
|
46 |
this.hasLineAndCol = hasLineAndCol;
|
47 |
} |
48 |
|
49 |
@Override
|
50 |
public int getLine() { |
51 |
return this.line; |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public int getColumn() { |
56 |
return this.column; |
57 |
} |
58 |
|
59 |
@Override
|
60 |
public boolean hasLineAndColumn() { |
61 |
return this.hasLineAndCol; |
62 |
} |
63 |
|
64 |
} |
65 |
|
66 |
protected ChangeListenerHelper changeListeners;
|
67 |
protected ActionListener updateCaretPositionListener; |
68 |
|
69 |
public AbstractJTextEditor() {
|
70 |
this.changeListeners = ToolsSwingLocator.getToolsSwingManager().createChangeListenerHelper();
|
71 |
|
72 |
final JTextComponent textComponent = this.getJTextComponent(); |
73 |
final Document textDocument = textComponent.getDocument(); |
74 |
textDocument.addDocumentListener(new DocumentListener() { |
75 |
@Override
|
76 |
public void insertUpdate(DocumentEvent e) { |
77 |
changeListeners.fireEvent(new ChangeEvent(AbstractJTextEditor.this)); |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public void removeUpdate(DocumentEvent e) { |
82 |
changeListeners.fireEvent(new ChangeEvent(AbstractJTextEditor.this)); |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public void changedUpdate(DocumentEvent e) { |
87 |
changeListeners.fireEvent(new ChangeEvent(AbstractJTextEditor.this)); |
88 |
} |
89 |
}); |
90 |
|
91 |
textComponent.addCaretListener((CaretEvent e) -> {
|
92 |
if (updateCaretPositionListener == null) { |
93 |
return;
|
94 |
} |
95 |
int lineNumber = 1; |
96 |
int columnNumber = 0; |
97 |
int currentPosition = textComponent.getCaretPosition();
|
98 |
try {
|
99 |
String strText = textDocument.getText(0, currentPosition); |
100 |
char arrText[] = strText.toCharArray(); |
101 |
for (int i = 0; i < strText.length(); i++) { |
102 |
if (arrText[i] == '\n') { |
103 |
lineNumber++; |
104 |
} |
105 |
} |
106 |
columnNumber = currentPosition - strText.lastIndexOf('\n');
|
107 |
} catch (BadLocationException ble) { |
108 |
System.err.println(ble);
|
109 |
} |
110 |
updateCaretPositionListener.actionPerformed( |
111 |
new UpdateCaretPositionActionEventImpl(
|
112 |
this, lineNumber, columnNumber, true |
113 |
) |
114 |
); |
115 |
}); |
116 |
|
117 |
} |
118 |
|
119 |
@Override
|
120 |
public JComponent asJComponent() { |
121 |
return this; |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void addChangeListener(ChangeListener listener) { |
126 |
this.changeListeners.addChangeListener(listener);
|
127 |
} |
128 |
|
129 |
@Override
|
130 |
public ChangeListener[] getChangeListeners() { |
131 |
return this.changeListeners.getChangeListeners(); |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public void removeChangeListener(ChangeListener listener) { |
136 |
this.changeListeners.removeChangeListener(listener);
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void removeAllChangeListener() { |
141 |
this.changeListeners.removeAllChangeListener();
|
142 |
} |
143 |
|
144 |
@Override
|
145 |
public boolean hasChangeListeners() { |
146 |
return this.changeListeners.hasChangeListeners(); |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public void selectLine(int line) { |
151 |
JTextComponent editor = this.getJTextComponent(); |
152 |
editor.requestFocusInWindow(); |
153 |
String code = editor.getText();
|
154 |
int lineCounter = 0; |
155 |
int initialSelection = 0; |
156 |
int finalSelection = 0; |
157 |
for (int j = 0; j < code.length(); j++) { |
158 |
if (code.charAt(j) == '\n') { |
159 |
lineCounter++; |
160 |
if (lineCounter == line - 1) { |
161 |
initialSelection = j; |
162 |
} |
163 |
if (lineCounter == line || (finalSelection == 0 && j == code.length() - 1)) { |
164 |
finalSelection = j; |
165 |
} |
166 |
} |
167 |
} |
168 |
editor.select(initialSelection, finalSelection); |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public void gotoline(int line) { |
173 |
JTextComponent component = getJTextComponent();
|
174 |
Element root = component.getDocument().getDefaultRootElement();
|
175 |
int lineno = Math.max(line, 1); |
176 |
int maxlines = root.getElementCount();
|
177 |
lineno = Math.min(lineno, maxlines);
|
178 |
int startOfLineOffset = root.getElement(lineno - 1).getStartOffset(); |
179 |
component.setCaretPosition(startOfLineOffset); |
180 |
Container container = SwingUtilities.getAncestorOfClass(JViewport.class, component); |
181 |
if (container == null) { |
182 |
return;
|
183 |
} |
184 |
int y = -1; |
185 |
try {
|
186 |
Rectangle r = component.modelToView(component.getCaretPosition());
|
187 |
JViewport viewport = (JViewport) container; |
188 |
int extentHeight = viewport.getExtentSize().height;
|
189 |
int viewHeight = viewport.getViewSize().height;
|
190 |
y = Math.max(0, r.y - ((extentHeight - r.height) / 2)); |
191 |
y = Math.min(y, viewHeight - extentHeight);
|
192 |
viewport.setViewPosition(new Point(0, y)); |
193 |
} catch (BadLocationException ble) { |
194 |
LOGGER.warn("Can't set view position (y=" + y + ").", ble); |
195 |
} |
196 |
try {
|
197 |
component.setCaretPosition(startOfLineOffset + 1);
|
198 |
} catch (Exception e) { |
199 |
LOGGER.warn("Can't set caret position (position=" + (startOfLineOffset + 1) + ").", e); |
200 |
} |
201 |
} |
202 |
|
203 |
@Override
|
204 |
public int getLineCount() { |
205 |
JTextComponent textComponent = this.getJTextComponent(); |
206 |
Element map = textComponent.getDocument().getDefaultRootElement();
|
207 |
return map.getElementCount();
|
208 |
} |
209 |
|
210 |
@Override
|
211 |
public void addUpdateCaretPositionActionListener(ActionListener listener) { |
212 |
this.updateCaretPositionListener = listener;
|
213 |
} |
214 |
|
215 |
@Override
|
216 |
public boolean isModified() { |
217 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
218 |
} |
219 |
|
220 |
} |