/[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 420 by alfonx, Thu Oct 1 20:22:48 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.JButton;
45          private JButton[] optionalButtons;  import javax.swing.JComponent;
46    import javax.swing.JDialog;
47          /**  import javax.swing.JOptionPane;
48           * Since the registerKeyboardAction() method is part of the JComponent class  import javax.swing.JPanel;
49           * definition, you must define the Escape keystroke and register the  import javax.swing.JRootPane;
50           * keyboard action with a JComponent, not with a JDialog. The JRootPane for  import javax.swing.KeyStroke;
51           * the JDialog serves as an excellent choice to associate the registration,  
52           * as this will always be visible. If you override the protected  import schmitzm.swing.SwingUtil;
53           * createRootPane() method of JDialog, you can return your custom JRootPane  
54           * with the keystroke enabled:  public class TranslationAskJDialog extends CancellableDialogAdapter{
55           */  
56          @Override          private OkButton okButton;
57          protected JRootPane createRootPane() {          private CancelButton cancelButton;
58                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);  
59                  JRootPane rootPane = new JRootPane();          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
60                  rootPane.registerKeyboardAction(new ActionListener() {          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
61    
62                          public void actionPerformed(ActionEvent e) {          private JComponent[] translationEditJPanelsOrJustComponents;
63                                  cancel();  
64                          }          private boolean hasBeenCanceled;
65    
66                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);          private JButton[] optionalButtons;
67            private TranslationsAskJPanel translationsAskPane;
68                  return rootPane;  
69          }          /**
70             * Since the registerKeyboardAction() method is part of the JComponent class
71          /**           * definition, you must define the Escape keystroke and register the
72           * The {@link TranslationAskJDialog} fills its content pane with an           * keyboard action with a JComponent, not with a JDialog. The JRootPane for
73           * arbitrary number of components. If these {@link Component}s are           * the JDialog serves as an excellent choice to associate the registration,
74           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the           * as this will always be visible. If you override the protected
75           * values and restore them if the dialog is canceled. Other           * createRootPane() method of JDialog, you can return your custom JRootPane
76           * {@link JComponent}s are just displayed.<br/>           * with the keystroke enabled:
77           * This class handles the cancel button itself. You may still want to listen           */
78           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has          @Override
79           * to be set visible afterwards.<br/>          protected JRootPane createRootPane() {
80           *                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
81           * @param owner                  JRootPane rootPane = new JRootPane();
82           *            A component of the GUI that this dialog is related to. If no                  rootPane.registerKeyboardAction(new ActionListener() {
83           *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is  
84           *            called.                          public void actionPerformed(ActionEvent e) {
85           */                                  cancel();
86          public TranslationAskJDialog(Component owner,                          }
87                          final JComponent... translationEditJPanels) {  
88                  super(owner instanceof Window ? (Window) owner : SwingUtil                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
89                                  .getParentWindow(owner));  
90                  setComponents(translationEditJPanels);                  return rootPane;
91          }          }
92    
93          /**          /**
94           * The {@link TranslationAskJDialog} fills its content pane with an           * The {@link TranslationAskJDialog} fills its content pane with an
95           * arbitrary number of components. If these {@link Component}s are           * arbitrary number of components. If these {@link Component}s are
96           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
97           * values and restore them if the dialog is canceled. Other           * values and restore them if the dialog is canceled. Other
98           * {@link JComponent}s are just displayed.<br/>           * {@link JComponent}s are just displayed.<br/>
99           * This class handles the cancel button itself. You may still want to listen           * This class handles the cancel button itself. You may still want to listen
100           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
101           * to be set visible afterwards.<br/>           * to be set visible afterwards.<br/>
102           * Using this constructor, you have to call setComponents afterwards.           *
103           */           * @param owner
104          public TranslationAskJDialog(Window owner) {           *            A component of the GUI that this dialog is related to. If no
105                  this(owner, new JComponent[] {});           *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is
106          }           *            called.
107             */
108          /**          public TranslationAskJDialog(Component owner,
109           * The {@link TranslationAskJDialog} fills its content pane with an                          final JComponent... translationEditJPanels) {
110           * arbitrary number of components. If these {@link Component}s are                  super(SwingUtil.getParentWindow(owner));
111           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the                  setComponents(translationEditJPanels);
112           * values and restore them if the dialog is canceled. Other          }
113           * {@link JComponent}s are just displayed.  
114           *          /**
115           * @param translationEditJPanels           * The {@link TranslationAskJDialog} fills its content pane with an
116           *            Arbitrary list of {@link JComponent}s and           * arbitrary number of components. If these {@link Component}s are
117           *            {@link TranslationEditJPanel}s.           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
118           */           * values and restore them if the dialog is canceled. Other
119          public void setComponents(final JComponent... translationEditJPanels) {           * {@link JComponent}s are just displayed.<br/>
120                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;           * This class handles the cancel button itself. You may still want to listen
121             * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
122                  // Remember backups for all the TranslationEditJPanel           * to be set visible afterwards.<br/>
123                  int count = 0;           * Using this constructor, you have to call setComponents afterwards.
124                  for (JComponent component : translationEditJPanelsOrJustComponents) {           */
125                          if (component instanceof TranslationEditJPanel) {          public TranslationAskJDialog(Component owner) {
126                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                  super(SwingUtil.getParentWindow(owner));
127                                  Translation orig = tep.getTranslation();          }
128    
129                                  // We don't want to overwrite the Translation object on          /**
130                                  // restore(). We just want to change its value.           * The {@link TranslationAskJDialog} fills its content pane with an
131                                  backup[count++] = orig.toOneLine();           * arbitrary number of components. If these {@link Component}s are
132                          }           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
133                  }           * values and restore them if the dialog is canceled. Other
134             * {@link JComponent}s are just displayed.
135                  init();           *
136          }           * @param translationEditJPanels
137             *            Arbitrary list of {@link JComponent}s and
138          private void init() {           *            {@link TranslationEditJPanel}s.
139                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);           */
140                  addWindowListener(new WindowAdapter() {          public void setComponents(final JComponent... translationEditJPanels) {
141                    this.translationEditJPanelsOrJustComponents = translationEditJPanels;
142                          public void windowClosing(WindowEvent e) {  
143                                  cancel();                  init();
144                          }          }
145    
146                  });  
147                  SwingUtil.centerFrameOnScreen(this);          private void init() {
148                  Box box = Box.createVerticalBox();                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
149                  for (JComponent panel : translationEditJPanelsOrJustComponents) {                  addWindowListener(new WindowAdapter() {
150                          panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);  
151                          panel.setBorder( BorderFactory.createEmptyBorder(5, 6, 5, 6));                          public void windowClosing(WindowEvent e) {
152                          box.add(panel);                                  cancel();
153                                                    }
154                  }  
155                  JPanel cp = new JPanel(new BorderLayout());                  });
156                  cp.add(box, BorderLayout.WEST);                  
157                  cp.add(getButtons(), BorderLayout.SOUTH);                  translationsAskPane = new TranslationsAskJPanel(translationEditJPanelsOrJustComponents);
158                  setContentPane(cp);                  JPanel cp = new JPanel(new BorderLayout());
159                                    cp.add(translationsAskPane, BorderLayout.WEST);
160                  setTitle(SwingUtil.R("TranslationAskJDialog.Title"));                  cp.add(getButtons(), BorderLayout.SOUTH);
161                  setModal(true);                  setContentPane(cp);
162                  pack();  
163          }                  setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
164                    setModal(true);
165          public void setButtons(JButton... optionalButtons) {                  pack();
166                  this.optionalButtons = optionalButtons;                  SwingUtil.centerFrameOnScreen(this);
167                  init();          }
168          }  
169            public void setOptionalButtons(JButton... optionalButtons) {
170          protected void cancel() {                  this.optionalButtons = optionalButtons;
171                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);                  init();
172                  restore();          }
173                  setVisible(false);  
174                  dispose();          /**
175          }           * Called when the dilaog is closed using the cancel button. When
176             * overwriting this method, call super.cancel() after restoring your
177          protected void restore() {           * properties.
178                  int count = 0;           */
179                  for (JComponent component : translationEditJPanelsOrJustComponents) {          @Override
180                          if (component instanceof TranslationEditJPanel) {          public void cancel() {
181                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                  translationsAskPane.cancel();
182                                  tep.getTranslation().fromOneLine(backup[count++]);                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
183                          }                  hasBeenCanceled = true;
184                  }                  setVisible(false);
185          }                  dispose();
186            }
187          private JComponent getButtons() {  
188                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));  
189            private JComponent getButtons() {
190                  if (optionalButtons != null)                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
191                          for (JButton b : optionalButtons) {  
192                                  jPanel.add(b);                  if (optionalButtons != null)
193                          }                          for (JButton b : optionalButtons) {
194                                    jPanel.add(b);
195                  if (okButton == null) {                          }
196                          okButton = new OkButton(new AbstractAction() {  
197                                  {                  if (okButton == null) {
198                                          // Set a mnemonic character. In most look and feels, this                          okButton = new OkButton(new AbstractAction() {
199                                          // causes the                                  {
200                                          // specified character to be underlined This indicates that                                          // Set a mnemonic character. In most look and feels, this
201                                          // if the component                                          // causes the
202                                          // using this action has the focus and In some look and                                          // specified character to be underlined This indicates that
203                                          // feels, this causes                                          // if the component
204                                          // the specified character in the label to be underlined and                                          // using this action has the focus and In some look and
205                                          putValue(Action.MNEMONIC_KEY, new Integer(                                          // feels, this causes
206                                                          java.awt.event.KeyEvent.VK_E));                                          // the specified character in the label to be underlined and
207                                            putValue(Action.MNEMONIC_KEY, new Integer(
208                                          // Set tool tip text                                                          java.awt.event.KeyEvent.VK_E));
209                                          putValue(Action.SHORT_DESCRIPTION,  
210                                                          "Accept the changes made to the translation.");                                          // // Set tool tip text
211                                            // putValue(Action.SHORT_DESCRIPTION,
212                                  }                                          // "Accept the changes made to the translation."); //i8n
213    
214                                  public void actionPerformed(ActionEvent evt) {                                  }
215                                          TranslationAskJDialog.this.firePropertyChange(  
216                                                          PROPERTY_APPLY_AND_CLOSE, null, null);                                  public void actionPerformed(ActionEvent evt) {
217    
218                                          if (!checkValidInputs())                                          okClose();
219                                                  return;  
220                                    }
221                                          setVisible(false);  
222                                          dispose();                          });
223                                  }  
224                    }
225                          });                  jPanel.add(okButton);
226    
227                  }                  if (cancelButton == null) {
228                  jPanel.add(okButton);                          cancelButton = new CancelButton(new AbstractAction("") {
229                                    public void actionPerformed(ActionEvent evt) {
230                  if (cancelButton == null) {                                          cancel();
231                          cancelButton = new CancelButton(new AbstractAction("") {                                  }
232                                  public void actionPerformed(ActionEvent evt) {                          });
233                                          restore();                  }
234                                          TranslationAskJDialog.this.firePropertyChange(                  jPanel.add(cancelButton);
235                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);  
236                                          setVisible(false);                  return jPanel;
237                                          setCancelled(true);          }
238                                          dispose();  
239                                  }          /**
240                          });           * This method is only called when the dialog is closed and not canceled.
241                  }           * Can be overwritten to do anything when the dialog has been accepted.
242                  jPanel.add(cancelButton);           */
243            public boolean okClose() {
244                  return jPanel;  
245          }                  if (!translationsAskPane.checkValidInputs())
246                            return false;
247          /**                  
248           * @return <code>true</code> if none of the translations contains illegal                  
249           *         characters.                  dispose();
250           */                  
251          protected boolean checkValidInputs() {                  TranslationAskJDialog.this.firePropertyChange(
252                                    PROPERTY_APPLY_AND_CLOSE, null, null);
253                  for (JComponent component : translationEditJPanelsOrJustComponents) {                  return true;
254                          if (component instanceof TranslationEditJPanel) {          }
255                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;  
256    
257                                  for (String l : tep.getTranslation().values()) {  
258                                          if (l.contains("{") || l.contains("}")) {          /**
259                                                  JOptionPane           * After the modal dialog has been closed, this allows to find out, whether
260                                                                  .showMessageDialog(           * the {@link Component} has been canceled.
261                                                                                  this,           *
262                                                                                  SwingUtil.R("TranslationAskJDialog.ErrorMsg.InvalidCharacterInTranslation"));           * @return <code>true</code> if the {@link JDialog} has been canceled.
263                                                  return false;           */
264                                          }          public boolean isCancelled() {
265                                  }                  return hasBeenCanceled;
266            }
267                          }  
268                  }  }
   
                 return true;  
         }  
   
         private void setCancelled(boolean hasBeenCanceled) {  
                 this.hasBeenCanceled = hasBeenCanceled;  
         }  
   
         /**  
          * 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.199  
changed lines
  Added in v.420

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26