/[schmitzm]/branches/2.0-GP14/src/skrueger/swing/TranslationAskJDialog.java
ViewVC logotype

Diff of /branches/2.0-GP14/src/skrueger/swing/TranslationAskJDialog.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.199  
changed lines
  Added in v.256

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26