/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/swing/TranslationAskJDialog.java
ViewVC logotype

Annotation of /branches/1.0-gt2-2.6/src/skrueger/swing/TranslationAskJDialog.java

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26