/[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

trunk/src/skrueger/swing/TranslationAskJDialog.java revision 199 by alfonx, Tue Jul 7 17:03:18 2009 UTC branches/1.0-gt2-2.6/src/skrueger/swing/TranslationAskJDialog.java revision 315 by mojays, Wed Aug 26 11:03:27 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(owner instanceof Window ? (Window) owner : SwingUtil                                  cancel();
89                                  .getParentWindow(owner));                          }
90                  setComponents(translationEditJPanels);  
91          }                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
92    
93          /**                  return rootPane;
94           * The {@link TranslationAskJDialog} fills its content pane with an          }
95           * arbitrary number of components. If these {@link Component}s are  
96           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the          /**
97           * values and restore them if the dialog is canceled. Other           * The {@link TranslationAskJDialog} fills its content pane with an
98           * {@link JComponent}s are just displayed.<br/>           * arbitrary number of components. If these {@link Component}s are
99           * This class handles the cancel button itself. You may still want to listen           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
100           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has           * values and restore them if the dialog is canceled. Other
101           * to be set visible afterwards.<br/>           * {@link JComponent}s are just displayed.<br/>
102           * Using this constructor, you have to call setComponents afterwards.           * This class handles the cancel button itself. You may still want to listen
103           */           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
104          public TranslationAskJDialog(Window owner) {           * to be set visible afterwards.<br/>
105                  this(owner, new JComponent[] {});           *
106          }           * @param owner
107             *            A component of the GUI that this dialog is related to. If no
108          /**           *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is
109           * The {@link TranslationAskJDialog} fills its content pane with an           *            called.
110           * arbitrary number of components. If these {@link Component}s are           */
111           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the          public TranslationAskJDialog(Component owner,
112           * values and restore them if the dialog is canceled. Other                          final JComponent... translationEditJPanels) {
113           * {@link JComponent}s are just displayed.                  super(SwingUtil.getParentWindow(owner));
114           *                  setComponents(translationEditJPanels);
115           * @param translationEditJPanels          }
116           *            Arbitrary list of {@link JComponent}s and  
117           *            {@link TranslationEditJPanel}s.          /**
118           */           * The {@link TranslationAskJDialog} fills its content pane with an
119          public void setComponents(final JComponent... translationEditJPanels) {           * arbitrary number of components. If these {@link Component}s are
120                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
121             * values and restore them if the dialog is canceled. Other
122                  // Remember backups for all the TranslationEditJPanel           * {@link JComponent}s are just displayed.<br/>
123                  int count = 0;           * This class handles the cancel button itself. You may still want to listen
124                  for (JComponent component : translationEditJPanelsOrJustComponents) {           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
125                          if (component instanceof TranslationEditJPanel) {           * to be set visible afterwards.<br/>
126                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;           * Using this constructor, you have to call setComponents afterwards.
127                                  Translation orig = tep.getTranslation();           */
128            public TranslationAskJDialog(Component owner) {
129                                  // We don't want to overwrite the Translation object on                  super(SwingUtil.getParentWindow(owner));
130                                  // restore(). We just want to change its value.          }
131                                  backup[count++] = orig.toOneLine();  
132                          }          /**
133                  }           * The {@link TranslationAskJDialog} fills its content pane with an
134             * arbitrary number of components. If these {@link Component}s are
135                  init();           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
136          }           * values and restore them if the dialog is canceled. Other
137             * {@link JComponent}s are just displayed.
138          private void init() {           *
139                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);           * @param translationEditJPanels
140                  addWindowListener(new WindowAdapter() {           *            Arbitrary list of {@link JComponent}s and
141             *            {@link TranslationEditJPanel}s.
142                          public void windowClosing(WindowEvent e) {           */
143                                  cancel();          public void setComponents(final JComponent... translationEditJPanels) {
144                          }                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;
145    
146                  });                  backup();
147                  SwingUtil.centerFrameOnScreen(this);  
148                  Box box = Box.createVerticalBox();                  init();
149                  for (JComponent panel : translationEditJPanelsOrJustComponents) {          }
150                          panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);  
151                          panel.setBorder( BorderFactory.createEmptyBorder(5, 6, 5, 6));          /**
152                          box.add(panel);           * Stores the original values of all {@link TranslationEditJPanel}s so
153                                     * cancel works.
154                  }           */
155                  JPanel cp = new JPanel(new BorderLayout());          protected void backup() {
156                  cp.add(box, BorderLayout.WEST);                  // Remember backups for all the TranslationEditJPanel
157                  cp.add(getButtons(), BorderLayout.SOUTH);                  int count = 0;
158                  setContentPane(cp);                  for (JComponent component : translationEditJPanelsOrJustComponents) {
159                                            if (component instanceof TranslationEditJPanel) {
160                  setTitle(SwingUtil.R("TranslationAskJDialog.Title"));                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;
161                  setModal(true);                                  Translation orig = tep.getTranslation();
162                  pack();  
163          }                                  // We don't want to overwrite the Translation object on
164                                    // restore(). We just want to change its value.
165          public void setButtons(JButton... optionalButtons) {                                  backup[count++] = orig.toOneLine();
166                  this.optionalButtons = optionalButtons;                          }
167                  init();                  }
168          }          }
169    
170          protected void cancel() {          private void init() {
171                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
172                  restore();                  addWindowListener(new WindowAdapter() {
173                  setVisible(false);  
174                  dispose();                          public void windowClosing(WindowEvent e) {
175          }                                  cancel();
176                            }
177          protected void restore() {  
178                  int count = 0;                  });
179                  for (JComponent component : translationEditJPanelsOrJustComponents) {                  SwingUtil.centerFrameOnScreen(this);
180                          if (component instanceof TranslationEditJPanel) {                  Box box = Box.createVerticalBox();
181                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                  for (JComponent panel : translationEditJPanelsOrJustComponents) {
182                                  tep.getTranslation().fromOneLine(backup[count++]);                          panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
183                          }                          panel.setBorder(BorderFactory.createEmptyBorder(5, 6, 5, 6));
184                  }                          box.add(panel);
185          }  
186                    }
187          private JComponent getButtons() {                  JPanel cp = new JPanel(new BorderLayout());
188                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));                  cp.add(box, BorderLayout.WEST);
189                    cp.add(getButtons(), BorderLayout.SOUTH);
190                  if (optionalButtons != null)                  setContentPane(cp);
191                          for (JButton b : optionalButtons) {  
192                                  jPanel.add(b);                  setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
193                          }                  setModal(true);
194                    pack();
195                  if (okButton == null) {          }
196                          okButton = new OkButton(new AbstractAction() {  
197                                  {          public void setButtons(JButton... optionalButtons) {
198                                          // Set a mnemonic character. In most look and feels, this                  this.optionalButtons = optionalButtons;
199                                          // causes the                  init();
200                                          // specified character to be underlined This indicates that          }
201                                          // if the component  
202                                          // using this action has the focus and In some look and          /**
203                                          // feels, this causes           * Called when the dilaog is closed using the cancel button.
204                                          // the specified character in the label to be underlined and           */
205                                          putValue(Action.MNEMONIC_KEY, new Integer(          protected void cancel() {
206                                                          java.awt.event.KeyEvent.VK_E));                  restore();
207                    firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
208                                          // Set tool tip text                  setCancelled(true);
209                                          putValue(Action.SHORT_DESCRIPTION,                  setVisible(false);
210                                                          "Accept the changes made to the translation.");                  dispose();
211            }
212                                  }  
213            /**
214                                  public void actionPerformed(ActionEvent evt) {           * Used to restore all the values when cancel has been pressed.
215                                          TranslationAskJDialog.this.firePropertyChange(           */
216                                                          PROPERTY_APPLY_AND_CLOSE, null, null);          protected void restore() {
217                    int count = 0;
218                                          if (!checkValidInputs())                  for (JComponent component : translationEditJPanelsOrJustComponents) {
219                                                  return;                          if (component instanceof TranslationEditJPanel) {
220                                    TranslationEditJPanel tep = (TranslationEditJPanel) component;
221                                          setVisible(false);                                  tep.getTranslation().fromOneLine(backup[count++]);
222                                          dispose();                          }
223                                  }                  }
224            }
225                          });  
226            private JComponent getButtons() {
227                  }                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
228                  jPanel.add(okButton);  
229                    if (optionalButtons != null)
230                  if (cancelButton == null) {                          for (JButton b : optionalButtons) {
231                          cancelButton = new CancelButton(new AbstractAction("") {                                  jPanel.add(b);
232                                  public void actionPerformed(ActionEvent evt) {                          }
233                                          restore();  
234                                          TranslationAskJDialog.this.firePropertyChange(                  if (okButton == null) {
235                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);                          okButton = new OkButton(new AbstractAction() {
236                                          setVisible(false);                                  {
237                                          setCancelled(true);                                          // Set a mnemonic character. In most look and feels, this
238                                          dispose();                                          // causes the
239                                  }                                          // specified character to be underlined This indicates that
240                          });                                          // if the component
241                  }                                          // using this action has the focus and In some look and
242                  jPanel.add(cancelButton);                                          // feels, this causes
243                                            // the specified character in the label to be underlined and
244                  return jPanel;                                          putValue(Action.MNEMONIC_KEY, new Integer(
245          }                                                          java.awt.event.KeyEvent.VK_E));
246    
247          /**                                          // // Set tool tip text
248           * @return <code>true</code> if none of the translations contains illegal                                          // putValue(Action.SHORT_DESCRIPTION,
249           *         characters.                                          // "Accept the changes made to the translation."); //i8n
250           */  
251          protected boolean checkValidInputs() {                                  }
252    
253                  for (JComponent component : translationEditJPanelsOrJustComponents) {                                  public void actionPerformed(ActionEvent evt) {
254                          if (component instanceof TranslationEditJPanel) {                                          TranslationAskJDialog.this.firePropertyChange(
255                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                                                          PROPERTY_APPLY_AND_CLOSE, null, null);
256    
257                                  for (String l : tep.getTranslation().values()) {                                          if (!checkValidInputs())
258                                          if (l.contains("{") || l.contains("}")) {                                                  return;
259                                                  JOptionPane  
260                                                                  .showMessageDialog(                                          okClose();
261                                                                                  this,  
262                                                                                  SwingUtil.R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation"));                                  }
263                                                  return false;  
264                                          }                          });
265                                  }  
266                    }
267                          }                  jPanel.add(okButton);
268                  }  
269                    if (cancelButton == null) {
270                  return true;                          cancelButton = new CancelButton(new AbstractAction("") {
271          }                                  public void actionPerformed(ActionEvent evt) {
272    //                                      restore();
273          private void setCancelled(boolean hasBeenCanceled) {  //                                      TranslationAskJDialog.this.firePropertyChange(
274                  this.hasBeenCanceled = hasBeenCanceled;  //                                                      PROPERTY_CANCEL_AND_CLOSE, null, null);
275          }  //                                      setVisible(false);
276    //                                      setCancelled(true);
277          /**  //                                      dispose();
278           * After the modal dialog has been closed, this allows to find out, whether                                          cancel();
279           * the dialog has been canceled.                                  }
280           *                          });
281           * @return <code>true</code> if the {@link JDialog} has been canceled.                  }
282           */                  jPanel.add(cancelButton);
283          public boolean isCancelled() {  
284                  return hasBeenCanceled;                  return jPanel;
285          }          }
286    
287  }          /**
288             * This method is only called when the dialog is closed and not cancelled.
289             * Can be overwritten to do anything when the dialog has been accepted.
290             */
291            protected void okClose() {
292                    setVisible(false);
293                    dispose();
294            }
295    
296            /**
297             * @return <code>true</code> if none of the translations contains illegal
298             *         characters.
299             */
300            protected boolean checkValidInputs() {
301    
302                    for (JComponent component : translationEditJPanelsOrJustComponents) {
303                            if (component instanceof TranslationEditJPanel) {
304                                    TranslationEditJPanel tep = (TranslationEditJPanel) component;
305    
306                                    for (String l : tep.getTranslation().values()) {
307                                            if (l.contains("{") || l.contains("}")) {
308                                                    JOptionPane
309                                                                    .showMessageDialog(
310                                                                                    this,
311                                                                                    SwingUtil
312                                                                                                    .R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation"));
313                                                    return false;
314                                            }
315                                    }
316    
317                            }
318                    }
319    
320                    return true;
321            }
322    
323            private void setCancelled(boolean hasBeenCanceled) {
324                    this.hasBeenCanceled = hasBeenCanceled;
325            }
326    
327            /**
328             * After the modal dialog has been closed, this allows to find out, whether
329             * the dialog has been canceled.
330             *
331             * @return <code>true</code> if the {@link JDialog} has been canceled.
332             */
333            public boolean isCancelled() {
334                    return hasBeenCanceled;
335            }
336    
337    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26