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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 116 - (hide 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 mojays 2 package skrueger.swing;
2    
3     import java.awt.BorderLayout;
4 alfonx 38 import java.awt.Component;
5 mojays 2 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 alfonx 38 import javax.swing.JOptionPane;
20 mojays 2 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 alfonx 38 private JComponent[] translationEditJPanelsOrJustComponents;
49 mojays 2
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 alfonx 38 * 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 mojays 2 * This class handles the cancel button itself. You may still want to listen
83 alfonx 38 * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
84     * to be set visible afterwards.<br/>
85 alfonx 116 *
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 mojays 2 */
91 alfonx 116 public TranslationAskJDialog(Component owner,
92 mojays 2 final JComponent... translationEditJPanels) {
93 alfonx 116 super(owner instanceof Window ? (Window) owner : SwingUtil
94     .getParentWindow(owner));
95 alfonx 38 setComponents(translationEditJPanels);
96 mojays 2 }
97    
98     /**
99 alfonx 38 * 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 mojays 2 * This class handles the cancel button itself. You may still want to listen
105 alfonx 33 * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
106 alfonx 38 * to be set visible afterwards.<br/>
107     * Using this constructor, you have to call setComponents afterwards.
108 mojays 2 */
109 alfonx 38 public TranslationAskJDialog(Window owner) {
110 alfonx 116 this(owner, new JComponent[] {});
111 alfonx 38 }
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 mojays 2 this.translationEditJPanelsOrJustComponents = translationEditJPanels;
126 alfonx 33
127 alfonx 38 // Remember backups for all the TranslationEditJPanel
128 alfonx 33 int count = 0;
129     for (JComponent component : translationEditJPanelsOrJustComponents) {
130     if (component instanceof TranslationEditJPanel) {
131     TranslationEditJPanel tep = (TranslationEditJPanel) component;
132     Translation orig = tep.getTranslation();
133 alfonx 38
134 alfonx 34 // We don't want to overwrite the Translation object on
135     // restore(). We just want to change its value.
136 alfonx 33 backup[count++] = orig.toOneLine();
137     }
138     }
139 alfonx 38
140     init();
141 mojays 2 }
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 alfonx 38 protected void restore() {
175 mojays 2 int count = 0;
176     for (JComponent component : translationEditJPanelsOrJustComponents) {
177     if (component instanceof TranslationEditJPanel) {
178     TranslationEditJPanel tep = (TranslationEditJPanel) component;
179 alfonx 33 tep.getTranslation().fromOneLine(backup[count++]);
180 mojays 2 }
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 alfonx 116
209     if (!checkValidInputs())
210     return;
211    
212 mojays 2 setVisible(false);
213     dispose();
214     }
215    
216     });
217 alfonx 38
218 mojays 2 }
219     jPanel.add(okButton);
220    
221     if (cancelButton == null) {
222     cancelButton = new CancelButton(new AbstractAction("") {
223     public void actionPerformed(ActionEvent evt) {
224 alfonx 38 restore();
225 mojays 2 TranslationAskJDialog.this.firePropertyChange(
226     PROPERTY_CANCEL_AND_CLOSE, null, null);
227     setVisible(false);
228 alfonx 38 setCancelled(true);
229 mojays 2 dispose();
230     }
231     });
232     }
233     jPanel.add(cancelButton);
234    
235     return jPanel;
236     }
237    
238 alfonx 38 /**
239 alfonx 116 * @return <code>true</code> if none of the translations contains illegal
240     * characters.
241 alfonx 38 */
242     protected boolean checkValidInputs() {
243 alfonx 116
244 alfonx 38 for (JComponent component : translationEditJPanelsOrJustComponents) {
245     if (component instanceof TranslationEditJPanel) {
246     TranslationEditJPanel tep = (TranslationEditJPanel) component;
247 alfonx 116
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 alfonx 38 return false;
256     }
257     }
258 alfonx 116
259 alfonx 38 }
260     }
261 alfonx 116
262 alfonx 38 return true;
263 mojays 2 }
264    
265 alfonx 38 private void setCancelled(boolean hasBeenCanceled) {
266 mojays 2 this.hasBeenCanceled = hasBeenCanceled;
267     }
268    
269     /**
270 alfonx 34 * After the modal dialog has been closed, this allows to find out, whether
271 alfonx 33 * the dialog has been canceled.
272     *
273 alfonx 34 * @return <code>true</code> if the {@link JDialog} has been canceled.
274 mojays 2 */
275 alfonx 38 public boolean isCancelled() {
276 mojays 2 return hasBeenCanceled;
277     }
278    
279     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26