1 |
package skrueger.swing; |
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Component; |
5 |
import java.awt.FlowLayout; |
6 |
import java.awt.Window; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.awt.event.KeyEvent; |
10 |
import java.awt.event.WindowAdapter; |
11 |
import java.awt.event.WindowEvent; |
12 |
import java.util.Locale; |
13 |
|
14 |
import javax.swing.AbstractAction; |
15 |
import javax.swing.Action; |
16 |
import javax.swing.BorderFactory; |
17 |
import javax.swing.Box; |
18 |
import javax.swing.JButton; |
19 |
import javax.swing.JComponent; |
20 |
import javax.swing.JDialog; |
21 |
import javax.swing.JOptionPane; |
22 |
import javax.swing.JPanel; |
23 |
import javax.swing.JRootPane; |
24 |
import javax.swing.KeyStroke; |
25 |
|
26 |
import schmitzm.geotools.styling.StylingUtil; |
27 |
import schmitzm.lang.LangUtil; |
28 |
import schmitzm.lang.ResourceProvider; |
29 |
import schmitzm.swing.SwingUtil; |
30 |
import skrueger.i8n.Translation; |
31 |
|
32 |
public class TranslationAskJDialog extends JDialog { |
33 |
|
34 |
private String[] backup = new String[50]; // Maximum 50 languages ;-) |
35 |
private OkButton okButton; |
36 |
private CancelButton cancelButton; |
37 |
|
38 |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
39 |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
40 |
|
41 |
private JComponent[] translationEditJPanelsOrJustComponents; |
42 |
|
43 |
private boolean hasBeenCanceled; |
44 |
|
45 |
private JButton[] optionalButtons; |
46 |
|
47 |
/** |
48 |
* Since the registerKeyboardAction() method is part of the JComponent class |
49 |
* definition, you must define the Escape keystroke and register the |
50 |
* keyboard action with a JComponent, not with a JDialog. The JRootPane for |
51 |
* the JDialog serves as an excellent choice to associate the registration, |
52 |
* as this will always be visible. If you override the protected |
53 |
* createRootPane() method of JDialog, you can return your custom JRootPane |
54 |
* with the keystroke enabled: |
55 |
*/ |
56 |
@Override |
57 |
protected JRootPane createRootPane() { |
58 |
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
59 |
JRootPane rootPane = new JRootPane(); |
60 |
rootPane.registerKeyboardAction(new ActionListener() { |
61 |
|
62 |
public void actionPerformed(ActionEvent e) { |
63 |
cancel(); |
64 |
} |
65 |
|
66 |
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
67 |
|
68 |
return rootPane; |
69 |
} |
70 |
|
71 |
/** |
72 |
* The {@link TranslationAskJDialog} fills its content pane with an |
73 |
* arbitrary number of components. If these {@link Component}s are |
74 |
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
75 |
* values and restore them if the dialog is canceled. Other |
76 |
* {@link JComponent}s are just displayed.<br/> |
77 |
* This class handles the cancel button itself. You may still want to listen |
78 |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
79 |
* to be set visible afterwards.<br/> |
80 |
* |
81 |
* @param owner |
82 |
* A component of the GUI that this dialog is related to. If no |
83 |
* {@link Window} is passed, SwingUtil.getParentWindow(owner) is |
84 |
* called. |
85 |
*/ |
86 |
public TranslationAskJDialog(Component owner, |
87 |
final JComponent... translationEditJPanels) { |
88 |
super(owner instanceof Window ? (Window) owner : SwingUtil |
89 |
.getParentWindow(owner)); |
90 |
setComponents(translationEditJPanels); |
91 |
} |
92 |
|
93 |
/** |
94 |
* The {@link TranslationAskJDialog} fills its content pane with an |
95 |
* arbitrary number of components. If these {@link Component}s are |
96 |
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
97 |
* values and restore them if the dialog is canceled. Other |
98 |
* {@link JComponent}s are just displayed.<br/> |
99 |
* This class handles the cancel button itself. You may still want to listen |
100 |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
101 |
* to be set visible afterwards.<br/> |
102 |
* Using this constructor, you have to call setComponents afterwards. |
103 |
*/ |
104 |
public TranslationAskJDialog(Window owner) { |
105 |
this(owner, new JComponent[] {}); |
106 |
} |
107 |
|
108 |
/** |
109 |
* The {@link TranslationAskJDialog} fills its content pane with an |
110 |
* arbitrary number of components. If these {@link Component}s are |
111 |
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
112 |
* values and restore them if the dialog is canceled. Other |
113 |
* {@link JComponent}s are just displayed. |
114 |
* |
115 |
* @param translationEditJPanels |
116 |
* Arbitrary list of {@link JComponent}s and |
117 |
* {@link TranslationEditJPanel}s. |
118 |
*/ |
119 |
public void setComponents(final JComponent... translationEditJPanels) { |
120 |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
121 |
|
122 |
// Remember backups for all the TranslationEditJPanel |
123 |
int count = 0; |
124 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
125 |
if (component instanceof TranslationEditJPanel) { |
126 |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
127 |
Translation orig = tep.getTranslation(); |
128 |
|
129 |
// We don't want to overwrite the Translation object on |
130 |
// restore(). We just want to change its value. |
131 |
backup[count++] = orig.toOneLine(); |
132 |
} |
133 |
} |
134 |
|
135 |
init(); |
136 |
} |
137 |
|
138 |
private void init() { |
139 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
140 |
addWindowListener(new WindowAdapter() { |
141 |
|
142 |
public void windowClosing(WindowEvent e) { |
143 |
cancel(); |
144 |
} |
145 |
|
146 |
}); |
147 |
SwingUtil.centerFrameOnScreen(this); |
148 |
Box box = Box.createVerticalBox(); |
149 |
for (JComponent panel : translationEditJPanelsOrJustComponents) { |
150 |
panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT); |
151 |
panel.setBorder( BorderFactory.createEmptyBorder(5, 6, 5, 6)); |
152 |
box.add(panel); |
153 |
|
154 |
} |
155 |
JPanel cp = new JPanel(new BorderLayout()); |
156 |
cp.add(box, BorderLayout.WEST); |
157 |
cp.add(getButtons(), BorderLayout.SOUTH); |
158 |
setContentPane(cp); |
159 |
|
160 |
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
161 |
setModal(true); |
162 |
pack(); |
163 |
} |
164 |
|
165 |
public void setButtons(JButton... optionalButtons) { |
166 |
this.optionalButtons = optionalButtons; |
167 |
init(); |
168 |
} |
169 |
|
170 |
protected void cancel() { |
171 |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
172 |
restore(); |
173 |
setVisible(false); |
174 |
dispose(); |
175 |
} |
176 |
|
177 |
protected void restore() { |
178 |
int count = 0; |
179 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
180 |
if (component instanceof TranslationEditJPanel) { |
181 |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
182 |
tep.getTranslation().fromOneLine(backup[count++]); |
183 |
} |
184 |
} |
185 |
} |
186 |
|
187 |
private JComponent getButtons() { |
188 |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
189 |
|
190 |
if (optionalButtons != null) |
191 |
for (JButton b : optionalButtons) { |
192 |
jPanel.add(b); |
193 |
} |
194 |
|
195 |
if (okButton == null) { |
196 |
okButton = new OkButton(new AbstractAction() { |
197 |
{ |
198 |
// Set a mnemonic character. In most look and feels, this |
199 |
// causes the |
200 |
// specified character to be underlined This indicates that |
201 |
// if the component |
202 |
// using this action has the focus and In some look and |
203 |
// feels, this causes |
204 |
// the specified character in the label to be underlined and |
205 |
putValue(Action.MNEMONIC_KEY, new Integer( |
206 |
java.awt.event.KeyEvent.VK_E)); |
207 |
|
208 |
// Set tool tip text |
209 |
putValue(Action.SHORT_DESCRIPTION, |
210 |
"Accept the changes made to the translation."); |
211 |
|
212 |
} |
213 |
|
214 |
public void actionPerformed(ActionEvent evt) { |
215 |
TranslationAskJDialog.this.firePropertyChange( |
216 |
PROPERTY_APPLY_AND_CLOSE, null, null); |
217 |
|
218 |
if (!checkValidInputs()) |
219 |
return; |
220 |
|
221 |
setVisible(false); |
222 |
dispose(); |
223 |
} |
224 |
|
225 |
}); |
226 |
|
227 |
} |
228 |
jPanel.add(okButton); |
229 |
|
230 |
if (cancelButton == null) { |
231 |
cancelButton = new CancelButton(new AbstractAction("") { |
232 |
public void actionPerformed(ActionEvent evt) { |
233 |
restore(); |
234 |
TranslationAskJDialog.this.firePropertyChange( |
235 |
PROPERTY_CANCEL_AND_CLOSE, null, null); |
236 |
setVisible(false); |
237 |
setCancelled(true); |
238 |
dispose(); |
239 |
} |
240 |
}); |
241 |
} |
242 |
jPanel.add(cancelButton); |
243 |
|
244 |
return jPanel; |
245 |
} |
246 |
|
247 |
/** |
248 |
* @return <code>true</code> if none of the translations contains illegal |
249 |
* characters. |
250 |
*/ |
251 |
protected boolean checkValidInputs() { |
252 |
|
253 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
254 |
if (component instanceof TranslationEditJPanel) { |
255 |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
256 |
|
257 |
for (String l : tep.getTranslation().values()) { |
258 |
if (l.contains("{") || l.contains("}")) { |
259 |
JOptionPane |
260 |
.showMessageDialog( |
261 |
this, |
262 |
SwingUtil.R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation")); |
263 |
return false; |
264 |
} |
265 |
} |
266 |
|
267 |
} |
268 |
} |
269 |
|
270 |
return true; |
271 |
} |
272 |
|
273 |
private void setCancelled(boolean hasBeenCanceled) { |
274 |
this.hasBeenCanceled = hasBeenCanceled; |
275 |
} |
276 |
|
277 |
/** |
278 |
* After the modal dialog has been closed, this allows to find out, whether |
279 |
* the dialog has been canceled. |
280 |
* |
281 |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
282 |
*/ |
283 |
public boolean isCancelled() { |
284 |
return hasBeenCanceled; |
285 |
} |
286 |
|
287 |
} |