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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/skrueger/swing/TranslationAskJDialog.java revision 38 by alfonx, Sun Apr 5 15:06:56 2009 UTC branches/1.0-gt2-2.6/src/skrueger/swing/TranslationAskJDialog.java revision 419 by alfonx, Wed Sep 30 15:36:39 2009 UTC
# Line 1  Line 1 
1  package skrueger.swing;  /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3  import java.awt.BorderLayout;   *
4  import java.awt.Component;   * This file is part of the SCHMITZM library - a collection of utility
5  import java.awt.FlowLayout;   * classes based on Java 1.6, focusing (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.Locale;   * modify it under the terms of the GNU Lesser General Public License
13     * as published by the Free Software Foundation; either version 3
14  import javax.swing.AbstractAction;   * of the License, or (at your option) any later version.
15  import javax.swing.Action;   *
16  import javax.swing.Box;   * This program is distributed in the hope that it will be useful,
17  import javax.swing.JComponent;   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  import javax.swing.JDialog;   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  import javax.swing.JOptionPane;   * 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 ;-)  
42          private OkButton okButton;  import javax.swing.AbstractAction;
43          private CancelButton cancelButton;  import javax.swing.Action;
44    import javax.swing.BorderFactory;
45          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";  import javax.swing.Box;
46          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";  import javax.swing.JButton;
47    import javax.swing.JComponent;
48          private JComponent[] translationEditJPanelsOrJustComponents;  import javax.swing.JDialog;
49    import javax.swing.JOptionPane;
50          private boolean hasBeenCanceled;  import javax.swing.JPanel;
51    import javax.swing.JRootPane;
52          /**  import javax.swing.KeyStroke;
53           * Since the registerKeyboardAction() method is part of the JComponent class  
54           * definition, you must define the Escape keystroke and register the  import schmitzm.swing.SwingUtil;
55           * keyboard action with a JComponent, not with a JDialog. The JRootPane for  import skrueger.i8n.Translation;
56           * the JDialog serves as an excellent choice to associate the registration,  
57           * as this will always be visible. If you override the protected  public class TranslationAskJDialog extends JDialog {
58           * createRootPane() method of JDialog, you can return your custom JRootPane  
59           * with the keystroke enabled:          private String[] backup = new String[50]; // Maximum 50 languages ;-)
60           */          private OkButton okButton;
61          @Override          private CancelButton cancelButton;
62          protected JRootPane createRootPane() {  
63                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
64                  JRootPane rootPane = new JRootPane();          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
65                  rootPane.registerKeyboardAction(new ActionListener() {  
66            private JComponent[] translationEditJPanelsOrJustComponents;
67                          public void actionPerformed(ActionEvent e) {  
68                                  cancel();          private boolean hasBeenCanceled;
69                          }  
70            private JButton[] optionalButtons;
71                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);  
72            /**
73                  return rootPane;           * Since the registerKeyboardAction() method is part of the JComponent class
74          }           * definition, you must define the Escape keystroke and register the
75             * keyboard action with a JComponent, not with a JDialog. The JRootPane for
76          /**           * the JDialog serves as an excellent choice to associate the registration,
77           * The {@link TranslationAskJDialog} fills its content pane with an           * as this will always be visible. If you override the protected
78           * arbitrary number of components. If these {@link Component}s are           * createRootPane() method of JDialog, you can return your custom JRootPane
79           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the           * with the keystroke enabled:
80           * values and restore them if the dialog is canceled. Other           */
81           * {@link JComponent}s are just displayed.<br/>          @Override
82           * This class handles the cancel button itself. You may still want to listen          protected JRootPane createRootPane() {
83           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
84           * to be set visible afterwards.<br/>                  JRootPane rootPane = new JRootPane();
85           */                  rootPane.registerKeyboardAction(new ActionListener() {
86          public TranslationAskJDialog(Window owner,  
87                          final JComponent... translationEditJPanels) {                          public void actionPerformed(ActionEvent e) {
88                  super(owner);                                  cancel();
89                  setComponents(translationEditJPanels);                          }
90          }  
91                    }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
92          /**  
93           * The {@link TranslationAskJDialog} fills its content pane with an                  return rootPane;
94           * arbitrary number of components. If these {@link Component}s are          }
95           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the  
96           * values and restore them if the dialog is canceled. Other          /**
97           * {@link JComponent}s are just displayed.<br/>           * The {@link TranslationAskJDialog} fills its content pane with an
98           * This class handles the cancel button itself. You may still want to listen           * arbitrary number of components. If these {@link Component}s are
99           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
100           * to be set visible afterwards.<br/>           * values and restore them if the dialog is canceled. Other
101           * Using this constructor, you have to call setComponents afterwards.           * {@link JComponent}s are just displayed.<br/>
102           */           * This class handles the cancel button itself. You may still want to listen
103          public TranslationAskJDialog(Window owner) {           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
104                  super(owner);           * to be set visible afterwards.<br/>
105          }           *
106             * @param owner
107          /**           *            A component of the GUI that this dialog is related to. If no
108           * The {@link TranslationAskJDialog} fills its content pane with an           *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is
109           * arbitrary number of components. If these {@link Component}s are           *            called.
110           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the           */
111           * values and restore them if the dialog is canceled. Other          public TranslationAskJDialog(Component owner,
112           * {@link JComponent}s are just displayed.                          final JComponent... translationEditJPanels) {
113           *                  super(SwingUtil.getParentWindow(owner));
114           * @param translationEditJPanels                  setComponents(translationEditJPanels);
115           *            Arbitrary list of {@link JComponent}s and          }
116           *            {@link TranslationEditJPanel}s.  
117           */          /**
118          public void setComponents(final JComponent... translationEditJPanels) {           * The {@link TranslationAskJDialog} fills its content pane with an
119                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;           * arbitrary number of components. If these {@link Component}s are
120             * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
121                  // Remember backups for all the TranslationEditJPanel           * values and restore them if the dialog is canceled. Other
122                  int count = 0;           * {@link JComponent}s are just displayed.<br/>
123                  for (JComponent component : translationEditJPanelsOrJustComponents) {           * This class handles the cancel button itself. You may still want to listen
124                          if (component instanceof TranslationEditJPanel) {           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
125                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;           * to be set visible afterwards.<br/>
126                                  Translation orig = tep.getTranslation();           * Using this constructor, you have to call setComponents afterwards.
127             */
128                                  // We don't want to overwrite the Translation object on          public TranslationAskJDialog(Component owner) {
129                                  // restore(). We just want to change its value.                  super(SwingUtil.getParentWindow(owner));
130                                  backup[count++] = orig.toOneLine();          }
131                          }  
132                  }          /**
133             * The {@link TranslationAskJDialog} fills its content pane with an
134                  init();           * arbitrary number of components. If these {@link Component}s are
135          }           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
136             * values and restore them if the dialog is canceled. Other
137          private void init() {           * {@link JComponent}s are just displayed.
138                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);           *
139                  addWindowListener(new WindowAdapter() {           * @param translationEditJPanels
140             *            Arbitrary list of {@link JComponent}s and
141                          public void windowClosing(WindowEvent e) {           *            {@link TranslationEditJPanel}s.
142                                  cancel();           */
143                          }          public void setComponents(final JComponent... translationEditJPanels) {
144                    this.translationEditJPanelsOrJustComponents = translationEditJPanels;
145                  });  
146                  SwingUtil.centerFrameOnScreen(this);                  backup();
147                  Box box = Box.createVerticalBox();  
148                  for (JComponent panel : translationEditJPanelsOrJustComponents) {                  init();
149                          box.add(panel);          }
150                  }  
151                  JPanel cp = new JPanel(new BorderLayout());          /**
152                  cp.add(box, BorderLayout.CENTER);           * Stores the original values of all {@link TranslationEditJPanel}s so
153                  cp.add(getButtons(), BorderLayout.SOUTH);           * cancel works.
154                  setContentPane(cp);           */
155            protected void backup() {
156                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n                  // Remember backups for all the TranslationEditJPanel
157                  setModal(true);                  int count = 0;
158                  pack();                  for (JComponent component : translationEditJPanelsOrJustComponents) {
159          }                          if (component instanceof TranslationEditJPanel) {
160                                    TranslationEditJPanel tep = (TranslationEditJPanel) component;
161          protected void cancel() {                                  Translation orig = tep.getTranslation();
162                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);  
163                  restore();                                  // We don't want to overwrite the Translation object on
164                  setVisible(false);                                  // restore(). We just want to change its value.
165                  dispose();                                  backup[count++] = orig.toOneLine();
166          }                          }
167                    }
168          protected void restore() {          }
169                  int count = 0;  
170                  for (JComponent component : translationEditJPanelsOrJustComponents) {          private void init() {
171                          if (component instanceof TranslationEditJPanel) {                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
172                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                  addWindowListener(new WindowAdapter() {
173                                  tep.getTranslation().fromOneLine(backup[count++]);  
174                          }                          public void windowClosing(WindowEvent e) {
175                  }                                  cancel();
176          }                          }
177    
178          private JComponent getButtons() {                  });
179                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));                  SwingUtil.centerFrameOnScreen(this);
180                  if (okButton == null) {                  Box box = Box.createVerticalBox();
181                          okButton = new OkButton(new AbstractAction() {                  for (JComponent panel : translationEditJPanelsOrJustComponents) {
182                                  {                          panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
183                                          // Set a mnemonic character. In most look and feels, this                          panel.setBorder(BorderFactory.createEmptyBorder(5, 6, 5, 6));
184                                          // causes the                          box.add(panel);
185                                          // specified character to be underlined This indicates that  
186                                          // if the component                  }
187                                          // using this action has the focus and In some look and                  JPanel cp = new JPanel(new BorderLayout());
188                                          // feels, this causes                  cp.add(box, BorderLayout.WEST);
189                                          // the specified character in the label to be underlined and                  cp.add(getButtons(), BorderLayout.SOUTH);
190                                          putValue(Action.MNEMONIC_KEY, new Integer(                  setContentPane(cp);
191                                                          java.awt.event.KeyEvent.VK_E));  
192                    setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
193                                          // Set tool tip text                  setModal(true);
194                                          putValue(Action.SHORT_DESCRIPTION,                  pack();
195                                                          "Accept the changes made to the translation.");          }
196    
197                                  }          public void setButtons(JButton... optionalButtons) {
198                    this.optionalButtons = optionalButtons;
199                                  public void actionPerformed(ActionEvent evt) {                  init();
200                                          TranslationAskJDialog.this.firePropertyChange(          }
201                                                          PROPERTY_APPLY_AND_CLOSE, null, null);  
202                                                    /**
203                                          if (!checkValidInputs()) return;           * Called when the dilaog is closed using the cancel button. When
204                                                     * overwriting this method, call super.cancel() after restoring your
205                                          setVisible(false);           * properties.
206                                          dispose();           */
207                                  }          public void cancel() {
208                    restore();
209                          });                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
210                    setCancelled(true);
211                  }                  setVisible(false);
212                  jPanel.add(okButton);                  dispose();
213            }
214                  if (cancelButton == null) {  
215                          cancelButton = new CancelButton(new AbstractAction("") {          /**
216                                  public void actionPerformed(ActionEvent evt) {           * Used to restore all the values when cancel has been pressed.
217                                          restore();           */
218                                          TranslationAskJDialog.this.firePropertyChange(          private void restore() {
219                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);                  int count = 0;
220                                          setVisible(false);                  for (JComponent component : translationEditJPanelsOrJustComponents) {
221                                          setCancelled(true);                          if (component instanceof TranslationEditJPanel) {
222                                          dispose();                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;
223                                  }                                  tep.getTranslation().fromOneLine(backup[count++]);
224                          });                          }
225                  }                  }
226                  jPanel.add(cancelButton);          }
227    
228                  return jPanel;          private JComponent getButtons() {
229          }                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
230    
231          /**                  if (optionalButtons != null)
232           * @return <code>true</code> if none of the translations contains illegal characters.                          for (JButton b : optionalButtons) {
233           */                                  jPanel.add(b);
234          protected boolean checkValidInputs() {                          }
235                    
236                  for (JComponent component : translationEditJPanelsOrJustComponents) {                  if (okButton == null) {
237                          if (component instanceof TranslationEditJPanel) {                          okButton = new OkButton(new AbstractAction() {
238                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                                  {
239                                                                            // Set a mnemonic character. In most look and feels, this
240                                  for (String l : tep.getTranslation().values()){                                          // causes the
241                                          if ( l.contains("{") || l.contains("}")) {                                          // specified character to be underlined This indicates that
242                                                  JOptionPane.showMessageDialog(this, RESOURCE.getString("ErrorMsg.InvalidCharacterInTranslation"));                                          // if the component
243                                                  return false;                                          // using this action has the focus and In some look and
244                                          }                                          // feels, this causes
245                                  }                                          // the specified character in the label to be underlined and
246                                                                            putValue(Action.MNEMONIC_KEY, new Integer(
247                          }                                                          java.awt.event.KeyEvent.VK_E));
248                  }  
249                                                            // // Set tool tip text
250                                                            // putValue(Action.SHORT_DESCRIPTION,
251                  return true;                                          // "Accept the changes made to the translation."); //i8n
252          }  
253                                    }
254          private void setCancelled(boolean hasBeenCanceled) {  
255                  this.hasBeenCanceled = hasBeenCanceled;                                  public void actionPerformed(ActionEvent evt) {
256          }                                          TranslationAskJDialog.this.firePropertyChange(
257                                                            PROPERTY_APPLY_AND_CLOSE, null, null);
258          /**  
259           * After the modal dialog has been closed, this allows to find out, whether                                          if (!checkValidInputs())
260           * the dialog has been canceled.                                                  return;
261           *  
262           * @return <code>true</code> if the {@link JDialog} has been canceled.                                          okClose();
263           */  
264          public boolean isCancelled() {                                  }
265                  return hasBeenCanceled;  
266          }                          });
267    
268  }                  }
269                    jPanel.add(okButton);
270    
271                    if (cancelButton == null) {
272                            cancelButton = new CancelButton(new AbstractAction("") {
273                                    public void actionPerformed(ActionEvent evt) {
274                                            // restore();
275                                            // TranslationAskJDialog.this.firePropertyChange(
276                                            // PROPERTY_CANCEL_AND_CLOSE, null, null);
277                                            // setVisible(false);
278                                            // setCancelled(true);
279                                            // dispose();
280                                            cancel();
281                                    }
282                            });
283                    }
284                    jPanel.add(cancelButton);
285    
286                    return jPanel;
287            }
288    
289            /**
290             * This method is only called when the dialog is closed and not cancelled.
291             * Can be overwritten to do anything when the dialog has been accepted.
292             */
293            protected void okClose() {
294                    setVisible(false);
295                    dispose();
296            }
297    
298            /**
299             * @return <code>true</code> if none of the translations contains illegal
300             *         characters.
301             */
302            protected boolean checkValidInputs() {
303    
304                    for (JComponent component : translationEditJPanelsOrJustComponents) {
305                            if (component instanceof TranslationEditJPanel) {
306                                    TranslationEditJPanel tep = (TranslationEditJPanel) component;
307    
308                                    for (String l : tep.getTranslation().values()) {
309                                            if (l.contains("{") || l.contains("}")) {
310                                                    JOptionPane
311                                                                    .showMessageDialog(
312                                                                                    this,
313                                                                                    SwingUtil
314                                                                                                    .R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation"));
315                                                    return false;
316                                            }
317                                    }
318    
319                            }
320                    }
321    
322                    return true;
323            }
324    
325            private void setCancelled(boolean hasBeenCanceled) {
326                    this.hasBeenCanceled = hasBeenCanceled;
327            }
328    
329            /**
330             * After the modal dialog has been closed, this allows to find out, whether
331             * the dialog has been canceled.
332             *
333             * @return <code>true</code> if the {@link JDialog} has been canceled.
334             */
335            public boolean isCancelled() {
336                    return hasBeenCanceled;
337            }
338    
339    }

Legend:
Removed from v.38  
changed lines
  Added in v.419

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26