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

Legend:
Removed from v.116  
changed lines
  Added in v.604

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26