/[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

revision 116 by alfonx, Wed May 13 17:37:11 2009 UTC revision 244 by alfonx, Wed Jul 29 09:33:33 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, 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.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 ;-)  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 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           * The {@link TranslationAskJDialog} fills its content pane with an           * Since the registerKeyboardAction() method is part of the JComponent class
78           * arbitrary number of components. If these {@link Component}s are           * definition, you must define the Escape keystroke and register the
79           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the           * keyboard action with a JComponent, not with a JDialog. The JRootPane for
80           * values and restore them if the dialog is canceled. Other           * the JDialog serves as an excellent choice to associate the registration,
81           * {@link JComponent}s are just displayed.<br/>           * as this will always be visible. If you override the protected
82           * This class handles the cancel button itself. You may still want to listen           * createRootPane() method of JDialog, you can return your custom JRootPane
83           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has           * with the keystroke enabled:
84           * to be set visible afterwards.<br/>           */
85           *          @Override
86           * @param owner          protected JRootPane createRootPane() {
87           *            A component of the GUI that this dialog is related to. If no                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
88           *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is                  JRootPane rootPane = new JRootPane();
89           *            called.                  rootPane.registerKeyboardAction(new ActionListener() {
90           */  
91          public TranslationAskJDialog(Component owner,                          public void actionPerformed(ActionEvent e) {
92                          final JComponent... translationEditJPanels) {                                  cancel();
93                  super(owner instanceof Window ? (Window) owner : SwingUtil                          }
94                                  .getParentWindow(owner));  
95                  setComponents(translationEditJPanels);                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
96          }  
97                    return rootPane;
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           * The {@link TranslationAskJDialog} fills its content pane with an
102           * values and restore them if the dialog is canceled. Other           * arbitrary number of components. If these {@link Component}s are
103           * {@link JComponent}s are just displayed.<br/>           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
104           * This class handles the cancel button itself. You may still want to listen           * values and restore them if the dialog is canceled. Other
105           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has           * {@link JComponent}s are just displayed.<br/>
106           * to be set visible afterwards.<br/>           * This class handles the cancel button itself. You may still want to listen
107           * Using this constructor, you have to call setComponents afterwards.           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
108           */           * to be set visible afterwards.<br/>
109          public TranslationAskJDialog(Window owner) {           *
110                  this(owner, new JComponent[] {});           * @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           * The {@link TranslationAskJDialog} fills its content pane with an           */
115           * arbitrary number of components. If these {@link Component}s are          public TranslationAskJDialog(Component owner,
116           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the                          final JComponent... translationEditJPanels) {
117           * values and restore them if the dialog is canceled. Other                  super(SwingUtil.getParentWindow(owner));
118           * {@link JComponent}s are just displayed.                  setComponents(translationEditJPanels);
119           *          }
120           * @param translationEditJPanels  
121           *            Arbitrary list of {@link JComponent}s and          /**
122           *            {@link TranslationEditJPanel}s.           * The {@link TranslationAskJDialog} fills its content pane with an
123           */           * arbitrary number of components. If these {@link Component}s are
124          public void setComponents(final JComponent... translationEditJPanels) {           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
125                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;           * values and restore them if the dialog is canceled. Other
126             * {@link JComponent}s are just displayed.<br/>
127                  // Remember backups for all the TranslationEditJPanel           * This class handles the cancel button itself. You may still want to listen
128                  int count = 0;           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
129                  for (JComponent component : translationEditJPanelsOrJustComponents) {           * to be set visible afterwards.<br/>
130                          if (component instanceof TranslationEditJPanel) {           * Using this constructor, you have to call setComponents afterwards.
131                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;           */
132                                  Translation orig = tep.getTranslation();          public TranslationAskJDialog(Component owner) {
133                    this(owner, new JComponent[] {});
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                          }           * The {@link TranslationAskJDialog} fills its content pane with an
138                  }           * arbitrary number of components. If these {@link Component}s are
139             * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
140                  init();           * values and restore them if the dialog is canceled. Other
141          }           * {@link JComponent}s are just displayed.
142             *
143          private void init() {           * @param translationEditJPanels
144                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);           *            Arbitrary list of {@link JComponent}s and
145                  addWindowListener(new WindowAdapter() {           *            {@link TranslationEditJPanel}s.
146             */
147                          public void windowClosing(WindowEvent e) {          public void setComponents(final JComponent... translationEditJPanels) {
148                                  cancel();                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;
149                          }  
150                    // Remember backups for all the TranslationEditJPanel
151                  });                  int count = 0;
152                  SwingUtil.centerFrameOnScreen(this);                  for (JComponent component : translationEditJPanelsOrJustComponents) {
153                  Box box = Box.createVerticalBox();                          if (component instanceof TranslationEditJPanel) {
154                  for (JComponent panel : translationEditJPanelsOrJustComponents) {                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;
155                          box.add(panel);                                  Translation orig = tep.getTranslation();
156                  }  
157                  JPanel cp = new JPanel(new BorderLayout());                                  // We don't want to overwrite the Translation object on
158                  cp.add(box, BorderLayout.CENTER);                                  // restore(). We just want to change its value.
159                  cp.add(getButtons(), BorderLayout.SOUTH);                                  backup[count++] = orig.toOneLine();
160                  setContentPane(cp);                          }
161                    }
162                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n  
163                  setModal(true);                  init();
164                  pack();          }
165          }  
166            private void init() {
167          protected void cancel() {                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
168                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);                  addWindowListener(new WindowAdapter() {
169                  restore();  
170                  setVisible(false);                          public void windowClosing(WindowEvent e) {
171                  dispose();                                  cancel();
172          }                          }
173    
174          protected void restore() {                  });
175                  int count = 0;                  SwingUtil.centerFrameOnScreen(this);
176                  for (JComponent component : translationEditJPanelsOrJustComponents) {                  Box box = Box.createVerticalBox();
177                          if (component instanceof TranslationEditJPanel) {                  for (JComponent panel : translationEditJPanelsOrJustComponents) {
178                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                          panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
179                                  tep.getTranslation().fromOneLine(backup[count++]);                          panel.setBorder( BorderFactory.createEmptyBorder(5, 6, 5, 6));
180                          }                          box.add(panel);
181                  }                          
182          }                  }
183                    JPanel cp = new JPanel(new BorderLayout());
184          private JComponent getButtons() {                  cp.add(box, BorderLayout.WEST);
185                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));                  cp.add(getButtons(), BorderLayout.SOUTH);
186                  if (okButton == null) {                  setContentPane(cp);
187                          okButton = new OkButton(new AbstractAction() {                  
188                                  {                  setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
189                                          // Set a mnemonic character. In most look and feels, this                  setModal(true);
190                                          // causes the                  pack();
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          public void setButtons(JButton... optionalButtons) {
194                                          // feels, this causes                  this.optionalButtons = optionalButtons;
195                                          // the specified character in the label to be underlined and                  init();
196                                          putValue(Action.MNEMONIC_KEY, new Integer(          }
197                                                          java.awt.event.KeyEvent.VK_E));  
198            protected void cancel() {
199                                          // Set tool tip text                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
200                                          putValue(Action.SHORT_DESCRIPTION,                  restore();
201                                                          "Accept the changes made to the translation.");                  setVisible(false);
202                    dispose();
203                                  }          }
204    
205                                  public void actionPerformed(ActionEvent evt) {          protected void restore() {
206                                          TranslationAskJDialog.this.firePropertyChange(                  int count = 0;
207                                                          PROPERTY_APPLY_AND_CLOSE, null, null);                  for (JComponent component : translationEditJPanelsOrJustComponents) {
208                            if (component instanceof TranslationEditJPanel) {
209                                          if (!checkValidInputs())                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;
210                                                  return;                                  tep.getTranslation().fromOneLine(backup[count++]);
211                            }
212                                          setVisible(false);                  }
213                                          dispose();          }
214                                  }  
215            private JComponent getButtons() {
216                          });                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
217    
218                  }                  if (optionalButtons != null)
219                  jPanel.add(okButton);                          for (JButton b : optionalButtons) {
220                                    jPanel.add(b);
221                  if (cancelButton == null) {                          }
222                          cancelButton = new CancelButton(new AbstractAction("") {  
223                                  public void actionPerformed(ActionEvent evt) {                  if (okButton == null) {
224                                          restore();                          okButton = new OkButton(new AbstractAction() {
225                                          TranslationAskJDialog.this.firePropertyChange(                                  {
226                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);                                          // Set a mnemonic character. In most look and feels, this
227                                          setVisible(false);                                          // causes the
228                                          setCancelled(true);                                          // specified character to be underlined This indicates that
229                                          dispose();                                          // if the component
230                                  }                                          // using this action has the focus and In some look and
231                          });                                          // feels, this causes
232                  }                                          // the specified character in the label to be underlined and
233                  jPanel.add(cancelButton);                                          putValue(Action.MNEMONIC_KEY, new Integer(
234                                                            java.awt.event.KeyEvent.VK_E));
235                  return jPanel;  
236          }                                          // Set tool tip text
237                                            putValue(Action.SHORT_DESCRIPTION,
238          /**                                                          "Accept the changes made to the translation.");
239           * @return <code>true</code> if none of the translations contains illegal  
240           *         characters.                                  }
241           */  
242          protected boolean checkValidInputs() {                                  public void actionPerformed(ActionEvent evt) {
243                                            TranslationAskJDialog.this.firePropertyChange(
244                  for (JComponent component : translationEditJPanelsOrJustComponents) {                                                          PROPERTY_APPLY_AND_CLOSE, null, null);
245                          if (component instanceof TranslationEditJPanel) {  
246                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                                          if (!checkValidInputs())
247                                                    return;
248                                  for (String l : tep.getTranslation().values()) {  
249                                          if (l.contains("{") || l.contains("}")) {                                          setVisible(false);
250                                                  JOptionPane                                          dispose();
251                                                                  .showMessageDialog(                                  }
252                                                                                  this,  
253                                                                                  RESOURCE                          });
254                                                                                                  .getString("ErrorMsg.InvalidCharacterInTranslation"));  
255                                                  return false;                  }
256                                          }                  jPanel.add(okButton);
257                                  }  
258                    if (cancelButton == null) {
259                          }                          cancelButton = new CancelButton(new AbstractAction("") {
260                  }                                  public void actionPerformed(ActionEvent evt) {
261                                            restore();
262                  return true;                                          TranslationAskJDialog.this.firePropertyChange(
263          }                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);
264                                            setVisible(false);
265          private void setCancelled(boolean hasBeenCanceled) {                                          setCancelled(true);
266                  this.hasBeenCanceled = hasBeenCanceled;                                          dispose();
267          }                                  }
268                            });
269          /**                  }
270           * After the modal dialog has been closed, this allows to find out, whether                  jPanel.add(cancelButton);
271           * the dialog has been canceled.  
272           *                  return jPanel;
273           * @return <code>true</code> if the {@link JDialog} has been canceled.          }
274           */  
275          public boolean isCancelled() {          /**
276                  return hasBeenCanceled;           * @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    }

Legend:
Removed from v.116  
changed lines
  Added in v.244

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26