blob: 6f289496f8410315b1c42e22aa6a10a6f5eaeefb (
plain) (
blame)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
package cuchaz.enigma.gui.elements;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.Document;
import cuchaz.enigma.gui.events.ConvertingTextFieldListener;
import cuchaz.enigma.gui.util.GuiUtil;
import cuchaz.enigma.utils.validation.ParameterizedMessage;
import cuchaz.enigma.utils.validation.Validatable;
/**
* A label that converts into an editable text field when you click it.
*/
public class ConvertingTextField implements Validatable {
private final JPanel ui;
private final ValidatableTextField textField;
private final JLabel label;
private boolean isEditing = false;
private final Set<ConvertingTextFieldListener> listeners = new HashSet<>();
public ConvertingTextField(String text) {
this.ui = new JPanel();
this.ui.setLayout(new GridLayout(1, 1, 0, 0));
this.textField = new ValidatableTextField(text);
this.label = GuiUtil.unboldLabel(new JLabel(text));
this.label.setBorder(BorderFactory.createLoweredBevelBorder());
this.label.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
startEditing();
}
});
this.textField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
if (!hasChanges()) {
stopEditing(true);
}
}
});
this.textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
stopEditing(true);
} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
stopEditing(false);
}
}
});
this.ui.add(this.label);
}
public void startEditing() {
if (isEditing) return;
this.ui.removeAll();
this.ui.add(this.textField);
this.isEditing = true;
this.ui.validate();
this.ui.repaint();
this.textField.requestFocusInWindow();
this.textField.selectAll();
this.listeners.forEach(l -> l.onStartEditing(this));
}
public void stopEditing(boolean abort) {
if (!isEditing) return;
if (!listeners.stream().allMatch(l -> l.tryStopEditing(this, abort))) return;
if (abort) {
this.textField.setText(this.label.getText());
} else {
this.label.setText(this.textField.getText());
}
this.ui.removeAll();
this.ui.add(this.label);
this.isEditing = false;
this.ui.validate();
this.ui.repaint();
this.listeners.forEach(l -> l.onStopEditing(this, abort));
}
public void setText(String text) {
stopEditing(true);
this.label.setText(text);
this.textField.setText(text);
}
public void setEditText(String text) {
if (!isEditing) return;
this.textField.setText(text);
}
public void selectAll() {
if (!isEditing) return;
this.textField.selectAll();
}
public void selectSubstring(int startIndex) {
if (!isEditing) return;
Document doc = this.textField.getDocument();
if (doc != null) {
this.selectSubstring(startIndex, doc.getLength());
}
}
public void selectSubstring(int startIndex, int endIndex) {
if (!isEditing) return;
this.textField.select(startIndex, endIndex);
}
public String getText() {
if (isEditing) {
return this.textField.getText();
} else {
return this.label.getText();
}
}
public String getPersistentText() {
return this.label.getText();
}
public boolean hasChanges() {
if (!isEditing) return false;
return !this.textField.getText().equals(this.label.getText());
}
@Override
public void addMessage(ParameterizedMessage message) {
textField.addMessage(message);
}
@Override
public void clearMessages() {
textField.clearMessages();
}
public void addListener(ConvertingTextFieldListener listener) {
this.listeners.add(listener);
}
public void removeListener(ConvertingTextFieldListener listener) {
this.listeners.remove(listener);
}
public JPanel getUi() {
return ui;
}
}
|