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

Legend:
Removed from v.120  
changed lines
  Added in v.302

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26