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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26