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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26