/[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 261 by mojays, Fri Jul 31 17:05:18 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           * @param owner  
87           *            A component of the GUI that this dialog is related to. If no                          public void actionPerformed(ActionEvent e) {
88           *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is                                  cancel();
89           *            called.                          }
90           */  
91          public TranslationAskJDialog(Component owner,                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
92                          final JComponent... translationEditJPanels) {  
93                  super(owner instanceof Window ? (Window) owner : SwingUtil                  return rootPane;
94                                  .getParentWindow(owner));          }
95                  setComponents(translationEditJPanels);  
96          }          /**
97             * The {@link TranslationAskJDialog} fills its content pane with an
98          /**           * arbitrary number of components. If these {@link Component}s are
99           * The {@link TranslationAskJDialog} fills its content pane with an           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
100           * arbitrary number of components. If these {@link Component}s are           * values and restore them if the dialog is canceled. Other
101           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the           * {@link JComponent}s are just displayed.<br/>
102           * values and restore them if the dialog is canceled. Other           * This class handles the cancel button itself. You may still want to listen
103           * {@link JComponent}s are just displayed.<br/>           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
104           * This class handles the cancel button itself. You may still want to listen           * to be set visible afterwards.<br/>
105           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has           *
106           * to be set visible afterwards.<br/>           * @param owner
107           * Using this constructor, you have to call setComponents afterwards.           *            A component of the GUI that this dialog is related to. If no
108           */           *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is
109          public TranslationAskJDialog(Window owner) {           *            called.
110                  this(owner, new JComponent[] {});           */
111          }          public TranslationAskJDialog(Component owner,
112                            final JComponent... translationEditJPanels) {
113          /**                  super(SwingUtil.getParentWindow(owner));
114           * The {@link TranslationAskJDialog} fills its content pane with an                  setComponents(translationEditJPanels);
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.           * The {@link TranslationAskJDialog} fills its content pane with an
119           *           * arbitrary number of components. If these {@link Component}s are
120           * @param translationEditJPanels           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
121           *            Arbitrary list of {@link JComponent}s and           * values and restore them if the dialog is canceled. Other
122           *            {@link TranslationEditJPanel}s.           * {@link JComponent}s are just displayed.<br/>
123           */           * This class handles the cancel button itself. You may still want to listen
124          public void setComponents(final JComponent... translationEditJPanels) {           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
125                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;           * to be set visible afterwards.<br/>
126             * Using this constructor, you have to call setComponents afterwards.
127                  // Remember backups for all the TranslationEditJPanel           */
128                  int count = 0;          public TranslationAskJDialog(Component owner) {
129                  for (JComponent component : translationEditJPanelsOrJustComponents) {                  this(owner, new JComponent[] {});
130                          if (component instanceof TranslationEditJPanel) {          }
131                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;  
132                                  Translation orig = tep.getTranslation();          /**
133             * The {@link TranslationAskJDialog} fills its content pane with an
134                                  // We don't want to overwrite the Translation object on           * arbitrary number of components. If these {@link Component}s are
135                                  // restore(). We just want to change its value.           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
136                                  backup[count++] = orig.toOneLine();           * values and restore them if the dialog is canceled. Other
137                          }           * {@link JComponent}s are just displayed.
138                  }           *
139             * @param translationEditJPanels
140                  init();           *            Arbitrary list of {@link JComponent}s and
141          }           *            {@link TranslationEditJPanel}s.
142             */
143          private void init() {          public void setComponents(final JComponent... translationEditJPanels) {
144                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;
145                  addWindowListener(new WindowAdapter() {  
146                    // Remember backups for all the TranslationEditJPanel
147                          public void windowClosing(WindowEvent e) {                  int count = 0;
148                                  cancel();                  for (JComponent component : translationEditJPanelsOrJustComponents) {
149                          }                          if (component instanceof TranslationEditJPanel) {
150                                    TranslationEditJPanel tep = (TranslationEditJPanel) component;
151                  });                                  Translation orig = tep.getTranslation();
152                  SwingUtil.centerFrameOnScreen(this);  
153                  Box box = Box.createVerticalBox();                                  // We don't want to overwrite the Translation object on
154                  for (JComponent panel : translationEditJPanelsOrJustComponents) {                                  // restore(). We just want to change its value.
155                          box.add(panel);                                  backup[count++] = orig.toOneLine();
156                  }                          }
157                  JPanel cp = new JPanel(new BorderLayout());                  }
158                  cp.add(box, BorderLayout.CENTER);  
159                  cp.add(getButtons(), BorderLayout.SOUTH);                  init();
160                  setContentPane(cp);          }
161    
162                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n          private void init() {
163                  setModal(true);                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
164                  pack();                  addWindowListener(new WindowAdapter() {
165          }  
166                            public void windowClosing(WindowEvent e) {
167          protected void cancel() {                                  cancel();
168                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);                          }
169                  restore();  
170                  setVisible(false);                  });
171                  dispose();                  SwingUtil.centerFrameOnScreen(this);
172          }                  Box box = Box.createVerticalBox();
173                    for (JComponent panel : translationEditJPanelsOrJustComponents) {
174          protected void restore() {                          panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
175                  int count = 0;                          panel.setBorder( BorderFactory.createEmptyBorder(5, 6, 5, 6));
176                  for (JComponent component : translationEditJPanelsOrJustComponents) {                          box.add(panel);
177                          if (component instanceof TranslationEditJPanel) {                          
178                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                  }
179                                  tep.getTranslation().fromOneLine(backup[count++]);                  JPanel cp = new JPanel(new BorderLayout());
180                          }                  cp.add(box, BorderLayout.WEST);
181                  }                  cp.add(getButtons(), BorderLayout.SOUTH);
182          }                  setContentPane(cp);
183                    
184          private JComponent getButtons() {                  setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
185                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));                  setModal(true);
186                  if (okButton == null) {                  pack();
187                          okButton = new OkButton(new AbstractAction() {          }
188                                  {  
189                                          // Set a mnemonic character. In most look and feels, this          public void setButtons(JButton... optionalButtons) {
190                                          // causes the                  this.optionalButtons = optionalButtons;
191                                          // specified character to be underlined This indicates that                  init();
192                                          // if the component          }
193                                          // using this action has the focus and In some look and  
194                                          // feels, this causes          protected void cancel() {
195                                          // the specified character in the label to be underlined and                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
196                                          putValue(Action.MNEMONIC_KEY, new Integer(                  restore();
197                                                          java.awt.event.KeyEvent.VK_E));                  setVisible(false);
198                    dispose();
199                                          // Set tool tip text          }
200                                          putValue(Action.SHORT_DESCRIPTION,  
201                                                          "Accept the changes made to the translation.");          protected void restore() {
202                    int count = 0;
203                                  }                  for (JComponent component : translationEditJPanelsOrJustComponents) {
204                            if (component instanceof TranslationEditJPanel) {
205                                  public void actionPerformed(ActionEvent evt) {                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;
206                                          TranslationAskJDialog.this.firePropertyChange(                                  tep.getTranslation().fromOneLine(backup[count++]);
207                                                          PROPERTY_APPLY_AND_CLOSE, null, null);                          }
208                    }
209                                          if (!checkValidInputs())          }
210                                                  return;  
211            private JComponent getButtons() {
212                                          setVisible(false);                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
213                                          dispose();  
214                                  }                  if (optionalButtons != null)
215                            for (JButton b : optionalButtons) {
216                          });                                  jPanel.add(b);
217                            }
218                  }  
219                  jPanel.add(okButton);                  if (okButton == null) {
220                            okButton = new OkButton(new AbstractAction() {
221                  if (cancelButton == null) {                                  {
222                          cancelButton = new CancelButton(new AbstractAction("") {                                          // Set a mnemonic character. In most look and feels, this
223                                  public void actionPerformed(ActionEvent evt) {                                          // causes the
224                                          restore();                                          // specified character to be underlined This indicates that
225                                          TranslationAskJDialog.this.firePropertyChange(                                          // if the component
226                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);                                          // using this action has the focus and In some look and
227                                          setVisible(false);                                          // feels, this causes
228                                          setCancelled(true);                                          // the specified character in the label to be underlined and
229                                          dispose();                                          putValue(Action.MNEMONIC_KEY, new Integer(
230                                  }                                                          java.awt.event.KeyEvent.VK_E));
231                          });  
232                  }                                          // Set tool tip text
233                  jPanel.add(cancelButton);                                          putValue(Action.SHORT_DESCRIPTION,
234                                                            "Accept the changes made to the translation.");
235                  return jPanel;  
236          }                                  }
237    
238          /**                                  public void actionPerformed(ActionEvent evt) {
239           * @return <code>true</code> if none of the translations contains illegal                                          TranslationAskJDialog.this.firePropertyChange(
240           *         characters.                                                          PROPERTY_APPLY_AND_CLOSE, null, null);
241           */  
242          protected boolean checkValidInputs() {                                          if (!checkValidInputs())
243                                                    return;
244                  for (JComponent component : translationEditJPanelsOrJustComponents) {  
245                          if (component instanceof TranslationEditJPanel) {                                          setVisible(false);
246                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                                          dispose();
247                                    }
248                                  for (String l : tep.getTranslation().values()) {  
249                                          if (l.contains("{") || l.contains("}")) {                          });
250                                                  JOptionPane  
251                                                                  .showMessageDialog(                  }
252                                                                                  this,                  jPanel.add(okButton);
253                                                                                  RESOURCE  
254                                                                                                  .getString("ErrorMsg.InvalidCharacterInTranslation"));                  if (cancelButton == null) {
255                                                  return false;                          cancelButton = new CancelButton(new AbstractAction("") {
256                                          }                                  public void actionPerformed(ActionEvent evt) {
257                                  }                                          restore();
258                                            TranslationAskJDialog.this.firePropertyChange(
259                          }                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);
260                  }                                          setVisible(false);
261                                            setCancelled(true);
262                  return true;                                          dispose();
263          }                                  }
264                            });
265          private void setCancelled(boolean hasBeenCanceled) {                  }
266                  this.hasBeenCanceled = hasBeenCanceled;                  jPanel.add(cancelButton);
267          }  
268                    return jPanel;
269          /**          }
270           * After the modal dialog has been closed, this allows to find out, whether  
271           * the dialog has been canceled.          /**
272           *           * @return <code>true</code> if none of the translations contains illegal
273           * @return <code>true</code> if the {@link JDialog} has been canceled.           *         characters.
274           */           */
275          public boolean isCancelled() {          protected boolean checkValidInputs() {
276                  return hasBeenCanceled;  
277          }                  for (JComponent component : translationEditJPanelsOrJustComponents) {
278                            if (component instanceof TranslationEditJPanel) {
279  }                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;
280    
281                                    for (String l : tep.getTranslation().values()) {
282                                            if (l.contains("{") || l.contains("}")) {
283                                                    JOptionPane
284                                                                    .showMessageDialog(
285                                                                                    this,
286                                                                                    SwingUtil.R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation"));
287                                                    return false;
288                                            }
289                                    }
290    
291                            }
292                    }
293    
294                    return true;
295            }
296    
297            private void setCancelled(boolean hasBeenCanceled) {
298                    this.hasBeenCanceled = hasBeenCanceled;
299            }
300    
301            /**
302             * After the modal dialog has been closed, this allows to find out, whether
303             * the dialog has been canceled.
304             *
305             * @return <code>true</code> if the {@link JDialog} has been canceled.
306             */
307            public boolean isCancelled() {
308                    return hasBeenCanceled;
309            }
310    
311    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26