/[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 2 by mojays, Tue Feb 24 22:43:52 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.Dialog;   * 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.ArrayList;   * modify it under the terms of the GNU Lesser General Public License
13  import java.util.Locale;   * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15  import javax.swing.AbstractAction;   *
16  import javax.swing.Action;   * 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.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.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.JOptionPane;
48          private final JComponent[] translationEditJPanelsOrJustComponents;  import javax.swing.JPanel;
49    import javax.swing.JRootPane;
50          private boolean hasBeenCanceled;  import javax.swing.KeyStroke;
51    
52          /**  import schmitzm.swing.SwingUtil;
53           * Since the registerKeyboardAction() method is part of the JComponent class  
54           * definition, you must define the Escape keystroke and register the  public class TranslationAskJDialog extends CancellableDialogAdapter{
55           * keyboard action with a JComponent, not with a JDialog. The JRootPane for  
56           * the JDialog serves as an excellent choice to associate the registration,          private OkButton okButton;
57           * as this will always be visible. If you override the protected          private CancelButton cancelButton;
58           * createRootPane() method of JDialog, you can return your custom JRootPane  
59           * with the keystroke enabled:          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
60           */          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
61          @Override  
62          protected JRootPane createRootPane() {          private JComponent[] translationEditJPanelsOrJustComponents;
63                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);  
64                  JRootPane rootPane = new JRootPane();          private boolean hasBeenCanceled;
65                  rootPane.registerKeyboardAction(new ActionListener() {  
66            private JButton[] optionalButtons;
67                          public void actionPerformed(ActionEvent e) {          private TranslationsAskJPanel translationsAskPane;
68                                  cancel();  
69                          }          /**
70             * Since the registerKeyboardAction() method is part of the JComponent class
71                  }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);           * definition, you must define the Escape keystroke and register the
72             * keyboard action with a JComponent, not with a JDialog. The JRootPane for
73                  return rootPane;           * the JDialog serves as an excellent choice to associate the registration,
74          }           * as this will always be visible. If you override the protected
75             * createRootPane() method of JDialog, you can return your custom JRootPane
76          /**           * 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.          @Override
79           *          protected JRootPane createRootPane() {
80           * This dialog is modal. The dialog has to be set visible afterwards.                  KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
81           */                  JRootPane rootPane = new JRootPane();
82          public TranslationAskJDialog(Dialog owner,                  rootPane.registerKeyboardAction(new ActionListener() {
83                          final JComponent... translationEditJPanels) {  
84                  super(owner);                          public void actionPerformed(ActionEvent e) {
85                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;                                  cancel();
86                  init();                          }
87          }  
88                    }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
89          /**  
90           * This class handles the cancel button itself. You may still want to listen                  return rootPane;
91           * to PROPERTY_APPLY_AND_CLOSE events.          }
92           * This dialog is modal. The dialog has to be set visible afterwards.  
93           */          /**
94          public TranslationAskJDialog(Window owner,           * The {@link TranslationAskJDialog} fills its content pane with an
95                          final JComponent... translationEditJPanels) {           * arbitrary number of components. If these {@link Component}s are
96                  super(owner);           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
97                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;           * values and restore them if the dialog is canceled. Other
98                  init();           * {@link JComponent}s are just displayed.<br/>
99          }           * 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
101          private void init() {           * to be set visible afterwards.<br/>
102                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);           *
103                  addWindowListener(new WindowAdapter() {           * @param owner
104             *            A component of the GUI that this dialog is related to. If no
105                          public void windowClosing(WindowEvent e) {           *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is
106                                  cancel();           *            called.
107                          }           */
108            public TranslationAskJDialog(Component owner,
109                  });                          final JComponent... translationEditJPanels) {
110                  SwingUtil.centerFrameOnScreen(this);                  super(SwingUtil.getParentWindow(owner));
111                  Box box = Box.createVerticalBox();                  setComponents(translationEditJPanels);
112                  for (JComponent panel : translationEditJPanelsOrJustComponents) {          }
113                          box.add(panel);  
114                  }          /**
115                  JPanel cp = new JPanel(new BorderLayout());           * The {@link TranslationAskJDialog} fills its content pane with an
116                  cp.add(box, BorderLayout.CENTER);           * arbitrary number of components. If these {@link Component}s are
117                  cp.add(getButtons(), BorderLayout.SOUTH);           * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
118                  setContentPane(cp);           * values and restore them if the dialog is canceled. Other
119             * {@link JComponent}s are just displayed.<br/>
120                  // dialog.getRootPane().setDefaultButton(okButton);           * 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                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n           * to be set visible afterwards.<br/>
123                  setModal(true);           * Using this constructor, you have to call setComponents afterwards.
124                  pack();           */
125          }          public TranslationAskJDialog(Component owner) {
126                    super(SwingUtil.getParentWindow(owner));
127          protected void cancel() {          }
128                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);  
129                  restore();          /**
130                  setVisible(false);           * The {@link TranslationAskJDialog} fills its content pane with an
131                  dispose();           * 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          private void restore() {           * {@link JComponent}s are just displayed.
135                  int count = 0;           *
136                  for (JComponent component : translationEditJPanelsOrJustComponents) {           * @param translationEditJPanels
137                          if (component instanceof TranslationEditJPanel) {           *            Arbitrary list of {@link JComponent}s and
138                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;           *            {@link TranslationEditJPanel}s.
139                                  tep.getTranslation().fromOneLine(backup[count]);           */
140                          }          public void setComponents(final JComponent... translationEditJPanels) {
141                          count++;                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;
142                  }  
143          }                  init();
144            }
145          private JComponent getButtons() {  
146                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));  
147                  if (okButton == null) {          private void init() {
148                          okButton = new OkButton(new AbstractAction() {                  setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
149                                  {                  addWindowListener(new WindowAdapter() {
150                                          // Set a mnemonic character. In most look and feels, this  
151                                          // causes the                          public void windowClosing(WindowEvent e) {
152                                          // specified character to be underlined This indicates that                                  cancel();
153                                          // if the component                          }
154                                          // using this action has the focus and In some look and  
155                                          // feels, this causes                  });
156                                          // the specified character in the label to be underlined and                  
157                                          putValue(Action.MNEMONIC_KEY, new Integer(                  translationsAskPane = new TranslationsAskJPanel(translationEditJPanelsOrJustComponents);
158                                                          java.awt.event.KeyEvent.VK_E));                  JPanel cp = new JPanel(new BorderLayout());
159                    cp.add(translationsAskPane, BorderLayout.WEST);
160                                          // Set tool tip text                  cp.add(getButtons(), BorderLayout.SOUTH);
161                                          putValue(Action.SHORT_DESCRIPTION,                  setContentPane(cp);
162                                                          "Accept the changes made to the translation.");  
163                    setTitle(SwingUtil.R("TranslationAskJDialog.Title"));
164                                  }                  setModal(true);
165                    pack();
166                                  public void actionPerformed(ActionEvent evt) {                  SwingUtil.centerFrameOnScreen(this);
167                                          TranslationAskJDialog.this.firePropertyChange(          }
168                                                          PROPERTY_APPLY_AND_CLOSE, null, null);  
169                                          setVisible(false);          public void setOptionalButtons(JButton... optionalButtons) {
170                                          dispose();                  this.optionalButtons = optionalButtons;
171                                          System.out.println("OK button action performed");                  init();
172                                  }          }
173    
174                          });          /**
175                          // okButton.addKeyListener( new KeyListener() {           * Called when the dilaog is closed using the cancel button. When
176                          //           * overwriting this method, call super.cancel() after restoring your
177                          // public void keyTyped(KeyEvent e) {           * properties.
178                          // if ()           */
179                          // okButton.action(new KEyPreEvent(), what)          @Override
180                          // }          public void cancel() {
181                          //                                                translationsAskPane.cancel();
182                          // });                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
183                  }                  hasBeenCanceled = true;
184                  jPanel.add(okButton);                  setVisible(false);
185                    dispose();
186                  if (cancelButton == null) {          }
187                          cancelButton = new CancelButton(new AbstractAction("") {  
188                                  public void actionPerformed(ActionEvent evt) {  
189                                          // restore();          private JComponent getButtons() {
190                                          TranslationAskJDialog.this.firePropertyChange(                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
191                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);  
192                                          setVisible(false);                  if (optionalButtons != null)
193                                          setHasBeenCanceled(true);                          for (JButton b : optionalButtons) {
194                                          dispose();                                  jPanel.add(b);
195                                  }                          }
196                          });  
197                  }                  if (okButton == null) {
198                  jPanel.add(cancelButton);                          okButton = new OkButton(new AbstractAction() {
199                                    {
200                  return jPanel;                                          // Set a mnemonic character. In most look and feels, this
201          }                                          // causes the
202                                            // specified character to be underlined This indicates that
203          public static void main(String[] args) {                                          // if the component
204                  ArrayList<String> lang = new ArrayList<String>();                                          // using this action has the focus and In some look and
205                  lang.add("de");                                          // feels, this causes
206                  lang.add("en");                                          // the specified character in the label to be underlined and
207                  lang.add("fr");                                          putValue(Action.MNEMONIC_KEY, new Integer(
208                                                            java.awt.event.KeyEvent.VK_E));
209                  Translation transe = new Translation();  
210                  transe.put("de", "Terciopelo-Lanzenotter");                                          // // Set tool tip text
211                  TranslationEditJPanel p1 = new TranslationEditJPanel(                                          // putValue(Action.SHORT_DESCRIPTION,
212                                  "Name von New Group", transe, lang);                                          // "Accept the changes made to the translation."); //i8n
213    
214                  Translation transe2 = new Translation();                                  }
215                  transe2  
216                                  .put(                                  public void actionPerformed(ActionEvent evt) {
217                                                  "de",  
218                                                  "Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart.");                                          okClose();
219                  TranslationEditJPanel p2 = new TranslationEditJPanel(  
220                                  "Description of Animal:", transe2, lang);                                  }
221    
222                  // JFrame frame = new JFrame();                          });
223                  // frame.setContentPane(p1);  
224                  // frame.pack();                  }
225                  // frame.setVisible(true);                  jPanel.add(okButton);
226    
227                  TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2);                  if (cancelButton == null) {
228                  dialog.setVisible(true);                          cancelButton = new CancelButton(new AbstractAction("") {
229          }                                  public void actionPerformed(ActionEvent evt) {
230                                            cancel();
231          private void setHasBeenCanceled(boolean hasBeenCanceled) {                                  }
232                  this.hasBeenCanceled = hasBeenCanceled;                          });
233          }                  }
234                    jPanel.add(cancelButton);
235          /**  
236           * After the modal dialog has been closed, this allows to find out, wether the dialog has been canceled.                  return jPanel;
237           * @return          }
238           */  
239          public boolean isHasBeenCanceled() {          /**
240                  return hasBeenCanceled;           * 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             */
243  }          public boolean okClose() {
244    
245                    if (!translationsAskPane.checkValidInputs())
246                            return false;
247                    
248                    
249                    dispose();
250                    
251                    TranslationAskJDialog.this.firePropertyChange(
252                                    PROPERTY_APPLY_AND_CLOSE, null, null);
253                    return true;
254            }
255    
256    
257    
258            /**
259             * After the modal dialog has been closed, this allows to find out, whether
260             * the {@link Component} has been canceled.
261             *
262             * @return <code>true</code> if the {@link JDialog} has been canceled.
263             */
264            public boolean isCancelled() {
265                    return hasBeenCanceled;
266            }
267    
268    }

Legend:
Removed from v.2  
changed lines
  Added in v.420

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26