-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCaretPositionListener.java
More file actions
146 lines (126 loc) · 3.17 KB
/
CaretPositionListener.java
File metadata and controls
146 lines (126 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.awt.event.*;
import javax.swing.*;
/**
* This class will retain the caret positioning at the character where
* the mouse was clicked.
*/
public class CaretPositionListener implements MouseListener
{
private boolean dynamicFormatting;
/**
* Default constructor.
*/
public CaretPositionListener()
{
}
/**
* Convenience constructor. This class is automatically added as a
* MouseListener to the specified formatted text fields.
*/
public CaretPositionListener(JFormattedTextField... components)
{
registerComponent( components );
}
/**
*
*/
public boolean isDynamicFormatting()
{
return dynamicFormatting;
}
/**
* Indicates that the formatting of the text in the formatted text field
* can change depending on whether the text field has focus or not.
* The listner must be aware of this so the proper caret position can
* be calculated.
*
* @param dynamicFormatting when true dynamic formatting must be considered
*/
public void setDynamicFormatting(boolean dynamicFormatting)
{
this.dynamicFormatting = dynamicFormatting;
}
/**
* Remove listeners from the specified component
*
* @param component the component the listeners are removed from
*/
public void deregisterComponent(JFormattedTextField... components)
{
for (JFormattedTextField component : components)
component.removeMouseListener( this );
}
/**
* Add the required listeners to the specified component
*
* @param component the component the listeners are added to
*/
public void registerComponent(JFormattedTextField... components)
{
for (JFormattedTextField component : components)
component.addMouseListener( this );
}
public void mousePressed(final MouseEvent me)
{
// Ignore double click to allow default behavour which will
// select the entire text
if (me.getClickCount() > 1) return;
final JFormattedTextField ftf = (JFormattedTextField)me.getSource();
if (dynamicFormatting
&& ftf.getValue() != null)
{
determineCaretPosition(ftf);
}
else
{
int offset = ftf.getCaretPosition();
setCaretPosition(ftf, offset);
}
}
private void determineCaretPosition(final JFormattedTextField ftf)
{
int offset = ftf.getCaretPosition();
String text = ftf.getText();
String value = ftf.getValue().toString();
if (text.equals(value))
{
setCaretPosition(ftf, offset);
return;
}
int i = 0;
int j = 0;
// Exclude formatting characters
while (j < offset)
{
if (text.charAt(i) == value.charAt(j))
{
i++;
j++;
}
else
{
offset--;
i++;
}
}
setCaretPosition(ftf, offset);
}
private void setCaretPosition(final JFormattedTextField ftf, final int offset)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
ftf.setCaretPosition(offset);
}
catch (IllegalArgumentException e) {}
}
});
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased( MouseEvent e ) {}
}