/[schmitzm]/trunk/src/skrueger/swing/TranslationAskJDialog.java
ViewVC logotype

Diff of /trunk/src/skrueger/swing/TranslationAskJDialog.java

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26