1 |
package skrueger.swing; |
/******************************************************************************* |
2 |
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
import java.awt.BorderLayout; |
* |
4 |
import java.awt.Dialog; |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
import java.awt.FlowLayout; |
* classes based on Java 1.6, focussing (not only) on Java Swing |
6 |
import java.awt.Window; |
* and the Geotools library. |
7 |
import java.awt.event.ActionEvent; |
* |
8 |
import java.awt.event.ActionListener; |
* The SCHMITZM project is hosted at: |
9 |
import java.awt.event.KeyEvent; |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
import java.awt.event.WindowAdapter; |
* |
11 |
import java.awt.event.WindowEvent; |
* This program is free software; you can redistribute it and/or |
12 |
import java.util.ArrayList; |
* modify it under the terms of the GNU Lesser General Public License |
13 |
import java.util.Locale; |
* as published by the Free Software Foundation; either version 3 |
14 |
|
* of the License, or (at your option) any later version. |
15 |
import javax.swing.AbstractAction; |
* |
16 |
import javax.swing.Action; |
* This program is distributed in the hope that it will be useful, |
17 |
import javax.swing.Box; |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
import javax.swing.JComponent; |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
import javax.swing.JDialog; |
* GNU General Public License for more details. |
20 |
import javax.swing.JPanel; |
* |
21 |
import javax.swing.JRootPane; |
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
import javax.swing.KeyStroke; |
* along with this program; if not, write to the Free Software |
23 |
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
import schmitzm.lang.LangUtil; |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
import schmitzm.lang.ResourceProvider; |
* |
26 |
import schmitzm.swing.SwingUtil; |
* Contributors: |
27 |
import skrueger.i8n.Translation; |
* Martin O. J. Schmitz - initial API and implementation |
28 |
|
* Stefan A. Krüger - additional utility classes |
29 |
public class TranslationAskJDialog extends JDialog { |
******************************************************************************/ |
30 |
|
package skrueger.swing; |
31 |
/** |
|
32 |
* {@link ResourceProvider}, der die Lokalisation fuer GUI-Komponenten des |
import java.awt.BorderLayout; |
33 |
* Package {@code skrueger.swing} zur Verfuegung stellt. Diese sind in |
import java.awt.Component; |
34 |
* properties-Datein unter {@code skrueger.swing.resource.locales} |
import java.awt.FlowLayout; |
35 |
* hinterlegt. |
import java.awt.Window; |
36 |
*/ |
import java.awt.event.ActionEvent; |
37 |
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
import java.awt.event.ActionListener; |
38 |
.extendPackagePath(TranslationAskJDialog.class, |
import java.awt.event.KeyEvent; |
39 |
"resource.locales.SwingResourceBundle"), Locale.ENGLISH); |
import java.awt.event.WindowAdapter; |
40 |
|
import java.awt.event.WindowEvent; |
41 |
private String[] backup = new String[50]; // Maximum 50 languages ;-) |
import java.util.Locale; |
42 |
private OkButton okButton; |
|
43 |
private CancelButton cancelButton; |
import javax.swing.AbstractAction; |
44 |
|
import javax.swing.Action; |
45 |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
import javax.swing.BorderFactory; |
46 |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
import javax.swing.Box; |
47 |
|
import javax.swing.JButton; |
48 |
private final JComponent[] translationEditJPanelsOrJustComponents; |
import javax.swing.JComponent; |
49 |
|
import javax.swing.JDialog; |
50 |
private boolean hasBeenCanceled; |
import javax.swing.JOptionPane; |
51 |
|
import javax.swing.JPanel; |
52 |
/** |
import javax.swing.JRootPane; |
53 |
* Since the registerKeyboardAction() method is part of the JComponent class |
import javax.swing.KeyStroke; |
54 |
* definition, you must define the Escape keystroke and register the |
|
55 |
* keyboard action with a JComponent, not with a JDialog. The JRootPane for |
import schmitzm.geotools.styling.StylingUtil; |
56 |
* the JDialog serves as an excellent choice to associate the registration, |
import schmitzm.lang.LangUtil; |
57 |
* as this will always be visible. If you override the protected |
import schmitzm.lang.ResourceProvider; |
58 |
* createRootPane() method of JDialog, you can return your custom JRootPane |
import schmitzm.swing.SwingUtil; |
59 |
* with the keystroke enabled: |
import skrueger.i8n.Translation; |
60 |
*/ |
|
61 |
@Override |
public class TranslationAskJDialog extends JDialog { |
62 |
protected JRootPane createRootPane() { |
|
63 |
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
private String[] backup = new String[50]; // Maximum 50 languages ;-) |
64 |
JRootPane rootPane = new JRootPane(); |
private OkButton okButton; |
65 |
rootPane.registerKeyboardAction(new ActionListener() { |
private CancelButton cancelButton; |
66 |
|
|
67 |
public void actionPerformed(ActionEvent e) { |
public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL"; |
68 |
cancel(); |
public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY"; |
69 |
} |
|
70 |
|
private JComponent[] translationEditJPanelsOrJustComponents; |
71 |
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
|
72 |
|
private boolean hasBeenCanceled; |
73 |
return rootPane; |
|
74 |
} |
private JButton[] optionalButtons; |
75 |
|
|
76 |
/** |
/** |
77 |
* This class handles the cancel button itself. You may still want to listen |
* Since the registerKeyboardAction() method is part of the JComponent class |
78 |
* to PROPERTY_APPLY_AND_CLOSE events. |
* definition, you must define the Escape keystroke and register the |
79 |
* |
* keyboard action with a JComponent, not with a JDialog. The JRootPane for |
80 |
* This dialog is modal. The dialog has to be set visible afterwards. |
* the JDialog serves as an excellent choice to associate the registration, |
81 |
*/ |
* as this will always be visible. If you override the protected |
82 |
public TranslationAskJDialog(Dialog owner, |
* createRootPane() method of JDialog, you can return your custom JRootPane |
83 |
final JComponent... translationEditJPanels) { |
* with the keystroke enabled: |
84 |
super(owner); |
*/ |
85 |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
@Override |
86 |
init(); |
protected JRootPane createRootPane() { |
87 |
} |
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
88 |
|
JRootPane rootPane = new JRootPane(); |
89 |
/** |
rootPane.registerKeyboardAction(new ActionListener() { |
90 |
* This class handles the cancel button itself. You may still want to listen |
|
91 |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
public void actionPerformed(ActionEvent e) { |
92 |
* to be set visible afterwards. |
cancel(); |
93 |
*/ |
} |
94 |
public TranslationAskJDialog(Window owner, |
|
95 |
final JComponent... translationEditJPanels) { |
}, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); |
96 |
super(owner); |
|
97 |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
return rootPane; |
98 |
init(); |
} |
99 |
|
|
100 |
// Rememebr backups for all the jtextpanels |
/** |
101 |
int count = 0; |
* The {@link TranslationAskJDialog} fills its content pane with an |
102 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
* arbitrary number of components. If these {@link Component}s are |
103 |
if (component instanceof TranslationEditJPanel) { |
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
104 |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
* values and restore them if the dialog is canceled. Other |
105 |
Translation orig = tep.getTranslation(); |
* {@link JComponent}s are just displayed.<br/> |
106 |
|
* This class handles the cancel button itself. You may still want to listen |
107 |
// We don't want to overwrite the Translation object on |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
108 |
// restore(). We just want to change its value. |
* to be set visible afterwards.<br/> |
109 |
|
* |
110 |
backup[count++] = orig.toOneLine(); |
* @param owner |
111 |
} |
* A component of the GUI that this dialog is related to. If no |
112 |
} |
* {@link Window} is passed, SwingUtil.getParentWindow(owner) is |
113 |
} |
* called. |
114 |
|
*/ |
115 |
private void init() { |
public TranslationAskJDialog(Component owner, |
116 |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
final JComponent... translationEditJPanels) { |
117 |
addWindowListener(new WindowAdapter() { |
super(SwingUtil.getParentWindow(owner)); |
118 |
|
setComponents(translationEditJPanels); |
119 |
public void windowClosing(WindowEvent e) { |
} |
120 |
cancel(); |
|
121 |
} |
/** |
122 |
|
* The {@link TranslationAskJDialog} fills its content pane with an |
123 |
}); |
* arbitrary number of components. If these {@link Component}s are |
124 |
SwingUtil.centerFrameOnScreen(this); |
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
125 |
Box box = Box.createVerticalBox(); |
* values and restore them if the dialog is canceled. Other |
126 |
for (JComponent panel : translationEditJPanelsOrJustComponents) { |
* {@link JComponent}s are just displayed.<br/> |
127 |
box.add(panel); |
* This class handles the cancel button itself. You may still want to listen |
128 |
} |
* to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has |
129 |
JPanel cp = new JPanel(new BorderLayout()); |
* to be set visible afterwards.<br/> |
130 |
cp.add(box, BorderLayout.CENTER); |
* Using this constructor, you have to call setComponents afterwards. |
131 |
cp.add(getButtons(), BorderLayout.SOUTH); |
*/ |
132 |
setContentPane(cp); |
public TranslationAskJDialog(Component owner) { |
133 |
|
this(owner, new JComponent[] {}); |
134 |
// dialog.getRootPane().setDefaultButton(okButton); |
} |
135 |
|
|
136 |
setTitle(RESOURCE.getString("translation_dialog_title")); // i8n |
/** |
137 |
setModal(true); |
* The {@link TranslationAskJDialog} fills its content pane with an |
138 |
pack(); |
* arbitrary number of components. If these {@link Component}s are |
139 |
} |
* {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the |
140 |
|
* values and restore them if the dialog is canceled. Other |
141 |
protected void cancel() { |
* {@link JComponent}s are just displayed. |
142 |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
* |
143 |
restore(); |
* @param translationEditJPanels |
144 |
setVisible(false); |
* Arbitrary list of {@link JComponent}s and |
145 |
dispose(); |
* {@link TranslationEditJPanel}s. |
146 |
} |
*/ |
147 |
|
public void setComponents(final JComponent... translationEditJPanels) { |
148 |
private void restore() { |
this.translationEditJPanelsOrJustComponents = translationEditJPanels; |
149 |
int count = 0; |
|
150 |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
// Remember backups for all the TranslationEditJPanel |
151 |
if (component instanceof TranslationEditJPanel) { |
int count = 0; |
152 |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
153 |
tep.getTranslation().fromOneLine(backup[count++]); |
if (component instanceof TranslationEditJPanel) { |
154 |
} |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
155 |
} |
Translation orig = tep.getTranslation(); |
156 |
} |
|
157 |
|
// We don't want to overwrite the Translation object on |
158 |
private JComponent getButtons() { |
// restore(). We just want to change its value. |
159 |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
backup[count++] = orig.toOneLine(); |
160 |
if (okButton == null) { |
} |
161 |
okButton = new OkButton(new AbstractAction() { |
} |
162 |
{ |
|
163 |
// Set a mnemonic character. In most look and feels, this |
init(); |
164 |
// causes the |
} |
165 |
// specified character to be underlined This indicates that |
|
166 |
// if the component |
private void init() { |
167 |
// using this action has the focus and In some look and |
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); |
168 |
// feels, this causes |
addWindowListener(new WindowAdapter() { |
169 |
// the specified character in the label to be underlined and |
|
170 |
putValue(Action.MNEMONIC_KEY, new Integer( |
public void windowClosing(WindowEvent e) { |
171 |
java.awt.event.KeyEvent.VK_E)); |
cancel(); |
172 |
|
} |
173 |
// Set tool tip text |
|
174 |
putValue(Action.SHORT_DESCRIPTION, |
}); |
175 |
"Accept the changes made to the translation."); |
SwingUtil.centerFrameOnScreen(this); |
176 |
|
Box box = Box.createVerticalBox(); |
177 |
} |
for (JComponent panel : translationEditJPanelsOrJustComponents) { |
178 |
|
panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT); |
179 |
public void actionPerformed(ActionEvent evt) { |
panel.setBorder( BorderFactory.createEmptyBorder(5, 6, 5, 6)); |
180 |
TranslationAskJDialog.this.firePropertyChange( |
box.add(panel); |
181 |
PROPERTY_APPLY_AND_CLOSE, null, null); |
|
182 |
setVisible(false); |
} |
183 |
dispose(); |
JPanel cp = new JPanel(new BorderLayout()); |
184 |
System.out.println("OK button action performed"); |
cp.add(box, BorderLayout.WEST); |
185 |
} |
cp.add(getButtons(), BorderLayout.SOUTH); |
186 |
|
setContentPane(cp); |
187 |
}); |
|
188 |
// okButton.addKeyListener( new KeyListener() { |
setTitle(SwingUtil.R("TranslationAskJDialog.Title")); |
189 |
// |
setModal(true); |
190 |
// public void keyTyped(KeyEvent e) { |
pack(); |
191 |
// if () |
} |
192 |
// okButton.action(new KEyPreEvent(), what) |
|
193 |
// } |
public void setButtons(JButton... optionalButtons) { |
194 |
// |
this.optionalButtons = optionalButtons; |
195 |
// }); |
init(); |
196 |
} |
} |
197 |
jPanel.add(okButton); |
|
198 |
|
protected void cancel() { |
199 |
if (cancelButton == null) { |
firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null); |
200 |
cancelButton = new CancelButton(new AbstractAction("") { |
restore(); |
201 |
public void actionPerformed(ActionEvent evt) { |
setVisible(false); |
202 |
// restore(); |
dispose(); |
203 |
TranslationAskJDialog.this.firePropertyChange( |
} |
204 |
PROPERTY_CANCEL_AND_CLOSE, null, null); |
|
205 |
setVisible(false); |
protected void restore() { |
206 |
setHasBeenCanceled(true); |
int count = 0; |
207 |
dispose(); |
for (JComponent component : translationEditJPanelsOrJustComponents) { |
208 |
} |
if (component instanceof TranslationEditJPanel) { |
209 |
}); |
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
210 |
} |
tep.getTranslation().fromOneLine(backup[count++]); |
211 |
jPanel.add(cancelButton); |
} |
212 |
|
} |
213 |
return jPanel; |
} |
214 |
} |
|
215 |
|
private JComponent getButtons() { |
216 |
public static void main(String[] args) { |
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
217 |
ArrayList<String> lang = new ArrayList<String>(); |
|
218 |
lang.add("de"); |
if (optionalButtons != null) |
219 |
lang.add("en"); |
for (JButton b : optionalButtons) { |
220 |
lang.add("fr"); |
jPanel.add(b); |
221 |
|
} |
222 |
Translation transe = new Translation(); |
|
223 |
transe.put("de", "Terciopelo-Lanzenotter"); |
if (okButton == null) { |
224 |
TranslationEditJPanel p1 = new TranslationEditJPanel( |
okButton = new OkButton(new AbstractAction() { |
225 |
"Name von New Group", transe, lang); |
{ |
226 |
|
// Set a mnemonic character. In most look and feels, this |
227 |
Translation transe2 = new Translation(); |
// causes the |
228 |
transe2 |
// specified character to be underlined This indicates that |
229 |
.put( |
// if the component |
230 |
"de", |
// using this action has the focus and In some look and |
231 |
"Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart."); |
// feels, this causes |
232 |
TranslationEditJPanel p2 = new TranslationEditJPanel( |
// the specified character in the label to be underlined and |
233 |
"Description of Animal:", transe2, lang); |
putValue(Action.MNEMONIC_KEY, new Integer( |
234 |
|
java.awt.event.KeyEvent.VK_E)); |
235 |
// JFrame frame = new JFrame(); |
|
236 |
// frame.setContentPane(p1); |
// Set tool tip text |
237 |
// frame.pack(); |
putValue(Action.SHORT_DESCRIPTION, |
238 |
// frame.setVisible(true); |
"Accept the changes made to the translation."); |
239 |
|
|
240 |
TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2); |
} |
241 |
dialog.setVisible(true); |
|
242 |
} |
public void actionPerformed(ActionEvent evt) { |
243 |
|
TranslationAskJDialog.this.firePropertyChange( |
244 |
private void setHasBeenCanceled(boolean hasBeenCanceled) { |
PROPERTY_APPLY_AND_CLOSE, null, null); |
245 |
this.hasBeenCanceled = hasBeenCanceled; |
|
246 |
} |
if (!checkValidInputs()) |
247 |
|
return; |
248 |
/** |
|
249 |
* After the modal dialog has been closed, this allows to find out, whether |
setVisible(false); |
250 |
* the dialog has been canceled. |
dispose(); |
251 |
* |
} |
252 |
* @return <code>true</code> if the {@link JDialog} has been canceled. |
|
253 |
*/ |
}); |
254 |
public boolean isHasBeenCanceled() { |
|
255 |
return hasBeenCanceled; |
} |
256 |
} |
jPanel.add(okButton); |
257 |
|
|
258 |
} |
if (cancelButton == null) { |
259 |
|
cancelButton = new CancelButton(new AbstractAction("") { |
260 |
|
public void actionPerformed(ActionEvent evt) { |
261 |
|
restore(); |
262 |
|
TranslationAskJDialog.this.firePropertyChange( |
263 |
|
PROPERTY_CANCEL_AND_CLOSE, null, null); |
264 |
|
setVisible(false); |
265 |
|
setCancelled(true); |
266 |
|
dispose(); |
267 |
|
} |
268 |
|
}); |
269 |
|
} |
270 |
|
jPanel.add(cancelButton); |
271 |
|
|
272 |
|
return jPanel; |
273 |
|
} |
274 |
|
|
275 |
|
/** |
276 |
|
* @return <code>true</code> if none of the translations contains illegal |
277 |
|
* characters. |
278 |
|
*/ |
279 |
|
protected boolean checkValidInputs() { |
280 |
|
|
281 |
|
for (JComponent component : translationEditJPanelsOrJustComponents) { |
282 |
|
if (component instanceof TranslationEditJPanel) { |
283 |
|
TranslationEditJPanel tep = (TranslationEditJPanel) component; |
284 |
|
|
285 |
|
for (String l : tep.getTranslation().values()) { |
286 |
|
if (l.contains("{") || l.contains("}")) { |
287 |
|
JOptionPane |
288 |
|
.showMessageDialog( |
289 |
|
this, |
290 |
|
SwingUtil.R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation")); |
291 |
|
return false; |
292 |
|
} |
293 |
|
} |
294 |
|
|
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
|
return true; |
299 |
|
} |
300 |
|
|
301 |
|
private void setCancelled(boolean hasBeenCanceled) { |
302 |
|
this.hasBeenCanceled = hasBeenCanceled; |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
|
* After the modal dialog has been closed, this allows to find out, whether |
307 |
|
* the dialog has been canceled. |
308 |
|
* |
309 |
|
* @return <code>true</code> if the {@link JDialog} has been canceled. |
310 |
|
*/ |
311 |
|
public boolean isCancelled() { |
312 |
|
return hasBeenCanceled; |
313 |
|
} |
314 |
|
|
315 |
|
} |