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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26