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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26