/[schmitzm]/trunk/src/skrueger/swing/TranslationAskJDialog.java
ViewVC logotype

Contents of /trunk/src/skrueger/swing/TranslationAskJDialog.java

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26