/[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 2 by mojays, Tue Feb 24 22:43:52 2009 UTC revision 122 by alfonx, Tue May 19 17:16:13 2009 UTC
# Line 1  Line 1 
1  package skrueger.swing;  package skrueger.swing;
2    
3  import java.awt.BorderLayout;  import java.awt.BorderLayout;
4  import java.awt.Dialog;  import java.awt.Component;
5  import java.awt.FlowLayout;  import java.awt.FlowLayout;
6  import java.awt.Window;  import java.awt.Window;
7  import java.awt.event.ActionEvent;  import java.awt.event.ActionEvent;
# Line 9  import java.awt.event.ActionListener; Line 9  import java.awt.event.ActionListener;
9  import java.awt.event.KeyEvent;  import java.awt.event.KeyEvent;
10  import java.awt.event.WindowAdapter;  import java.awt.event.WindowAdapter;
11  import java.awt.event.WindowEvent;  import java.awt.event.WindowEvent;
 import java.util.ArrayList;  
12  import java.util.Locale;  import java.util.Locale;
13    
14  import javax.swing.AbstractAction;  import javax.swing.AbstractAction;
15  import javax.swing.Action;  import javax.swing.Action;
16    import javax.swing.BorderFactory;
17  import javax.swing.Box;  import javax.swing.Box;
18    import javax.swing.JButton;
19  import javax.swing.JComponent;  import javax.swing.JComponent;
20  import javax.swing.JDialog;  import javax.swing.JDialog;
21    import javax.swing.JOptionPane;
22  import javax.swing.JPanel;  import javax.swing.JPanel;
23  import javax.swing.JRootPane;  import javax.swing.JRootPane;
24  import javax.swing.KeyStroke;  import javax.swing.KeyStroke;
# Line 45  public class TranslationAskJDialog exten Line 47  public class TranslationAskJDialog exten
47          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
48          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
49    
50          private final JComponent[] translationEditJPanelsOrJustComponents;          private JComponent[] translationEditJPanelsOrJustComponents;
51    
52          private boolean hasBeenCanceled;          private boolean hasBeenCanceled;
53    
54            private JButton[] optionalButtons;
55    
56          /**          /**
57           * Since the registerKeyboardAction() method is part of the JComponent class           * Since the registerKeyboardAction() method is part of the JComponent class
58           * definition, you must define the Escape keystroke and register the           * definition, you must define the Escape keystroke and register the
# Line 74  public class TranslationAskJDialog exten Line 78  public class TranslationAskJDialog exten
78          }          }
79    
80          /**          /**
81             * The {@link TranslationAskJDialog} fills its content pane with an
82             * arbitrary number of components. If these {@link Component}s are
83             * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
84             * values and restore them if the dialog is canceled. Other
85             * {@link JComponent}s are just displayed.<br/>
86           * 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
87           * to PROPERTY_APPLY_AND_CLOSE events.           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
88             * to be set visible afterwards.<br/>
89           *           *
90           * This dialog is modal. The dialog has to be set visible afterwards.           * @param owner
91             *            A component of the GUI that this dialog is related to. If no
92             *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is
93             *            called.
94           */           */
95          public TranslationAskJDialog(Dialog owner,          public TranslationAskJDialog(Component owner,
96                          final JComponent... translationEditJPanels) {                          final JComponent... translationEditJPanels) {
97                  super(owner);                  super(owner instanceof Window ? (Window) owner : SwingUtil
98                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;                                  .getParentWindow(owner));
99                  init();                  setComponents(translationEditJPanels);
100          }          }
101    
102          /**          /**
103             * The {@link TranslationAskJDialog} fills its content pane with an
104             * arbitrary number of components. If these {@link Component}s are
105             * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
106             * values and restore them if the dialog is canceled. Other
107             * {@link JComponent}s are just displayed.<br/>
108           * 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
109           * to PROPERTY_APPLY_AND_CLOSE events.           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
110           * This dialog is modal. The dialog has to be set visible afterwards.           * to be set visible afterwards.<br/>
111             * Using this constructor, you have to call setComponents afterwards.
112           */           */
113          public TranslationAskJDialog(Window owner,          public TranslationAskJDialog(Window owner) {
114                          final JComponent... translationEditJPanels) {                  this(owner, new JComponent[] {});
115                  super(owner);          }
116    
117            /**
118             * The {@link TranslationAskJDialog} fills its content pane with an
119             * arbitrary number of components. If these {@link Component}s are
120             * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
121             * values and restore them if the dialog is canceled. Other
122             * {@link JComponent}s are just displayed.
123             *
124             * @param translationEditJPanels
125             *            Arbitrary list of {@link JComponent}s and
126             *            {@link TranslationEditJPanel}s.
127             */
128            public void setComponents(final JComponent... translationEditJPanels) {
129                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;
130    
131                    // Remember backups for all the TranslationEditJPanel
132                    int count = 0;
133                    for (JComponent component : translationEditJPanelsOrJustComponents) {
134                            if (component instanceof TranslationEditJPanel) {
135                                    TranslationEditJPanel tep = (TranslationEditJPanel) component;
136                                    Translation orig = tep.getTranslation();
137    
138                                    // We don't want to overwrite the Translation object on
139                                    // restore(). We just want to change its value.
140                                    backup[count++] = orig.toOneLine();
141                            }
142                    }
143    
144                  init();                  init();
145          }          }
146    
# Line 110  public class TranslationAskJDialog exten Line 156  public class TranslationAskJDialog exten
156                  SwingUtil.centerFrameOnScreen(this);                  SwingUtil.centerFrameOnScreen(this);
157                  Box box = Box.createVerticalBox();                  Box box = Box.createVerticalBox();
158                  for (JComponent panel : translationEditJPanelsOrJustComponents) {                  for (JComponent panel : translationEditJPanelsOrJustComponents) {
159                            panel.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
160                            panel.setBorder( BorderFactory.createEmptyBorder(5, 6, 5, 6));
161                          box.add(panel);                          box.add(panel);
162                            
163                  }                  }
164                  JPanel cp = new JPanel(new BorderLayout());                  JPanel cp = new JPanel(new BorderLayout());
165                  cp.add(box, BorderLayout.CENTER);                  cp.add(box, BorderLayout.WEST);
166                  cp.add(getButtons(), BorderLayout.SOUTH);                  cp.add(getButtons(), BorderLayout.SOUTH);
167                  setContentPane(cp);                  setContentPane(cp);
168                    
                 // dialog.getRootPane().setDefaultButton(okButton);  
   
169                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n
170                  setModal(true);                  setModal(true);
171                  pack();                  pack();
172          }          }
173    
174            public void setButtons(JButton... optionalButtons) {
175                    this.optionalButtons = optionalButtons;
176                    init();
177            }
178    
179          protected void cancel() {          protected void cancel() {
180                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);                  firePropertyChange(PROPERTY_CANCEL_AND_CLOSE, null, null);
181                  restore();                  restore();
# Line 131  public class TranslationAskJDialog exten Line 183  public class TranslationAskJDialog exten
183                  dispose();                  dispose();
184          }          }
185    
186          private void restore() {          protected void restore() {
187                  int count = 0;                  int count = 0;
188                  for (JComponent component : translationEditJPanelsOrJustComponents) {                  for (JComponent component : translationEditJPanelsOrJustComponents) {
189                          if (component instanceof TranslationEditJPanel) {                          if (component instanceof TranslationEditJPanel) {
190                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;
191                                  tep.getTranslation().fromOneLine(backup[count]);                                  tep.getTranslation().fromOneLine(backup[count++]);
192                          }                          }
                         count++;  
193                  }                  }
194          }          }
195    
196          private JComponent getButtons() {          private JComponent getButtons() {
197                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));                  JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
198    
199                    if (optionalButtons != null)
200                            for (JButton b : optionalButtons) {
201                                    jPanel.add(b);
202                            }
203    
204                  if (okButton == null) {                  if (okButton == null) {
205                          okButton = new OkButton(new AbstractAction() {                          okButton = new OkButton(new AbstractAction() {
206                                  {                                  {
# Line 166  public class TranslationAskJDialog exten Line 223  public class TranslationAskJDialog exten
223                                  public void actionPerformed(ActionEvent evt) {                                  public void actionPerformed(ActionEvent evt) {
224                                          TranslationAskJDialog.this.firePropertyChange(                                          TranslationAskJDialog.this.firePropertyChange(
225                                                          PROPERTY_APPLY_AND_CLOSE, null, null);                                                          PROPERTY_APPLY_AND_CLOSE, null, null);
226    
227                                            if (!checkValidInputs())
228                                                    return;
229    
230                                          setVisible(false);                                          setVisible(false);
231                                          dispose();                                          dispose();
                                         System.out.println("OK button action performed");  
232                                  }                                  }
233    
234                          });                          });
235                          // okButton.addKeyListener( new KeyListener() {  
                         //  
                         // public void keyTyped(KeyEvent e) {  
                         // if ()  
                         // okButton.action(new KEyPreEvent(), what)  
                         // }  
                         //                                
                         // });  
236                  }                  }
237                  jPanel.add(okButton);                  jPanel.add(okButton);
238    
239                  if (cancelButton == null) {                  if (cancelButton == null) {
240                          cancelButton = new CancelButton(new AbstractAction("") {                          cancelButton = new CancelButton(new AbstractAction("") {
241                                  public void actionPerformed(ActionEvent evt) {                                  public void actionPerformed(ActionEvent evt) {
242                                          // restore();                                          restore();
243                                          TranslationAskJDialog.this.firePropertyChange(                                          TranslationAskJDialog.this.firePropertyChange(
244                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);
245                                          setVisible(false);                                          setVisible(false);
246                                          setHasBeenCanceled(true);                                          setCancelled(true);
247                                          dispose();                                          dispose();
248                                  }                                  }
249                          });                          });
# Line 200  public class TranslationAskJDialog exten Line 253  public class TranslationAskJDialog exten
253                  return jPanel;                  return jPanel;
254          }          }
255    
256          public static void main(String[] args) {          /**
257                  ArrayList<String> lang = new ArrayList<String>();           * @return <code>true</code> if none of the translations contains illegal
258                  lang.add("de");           *         characters.
259                  lang.add("en");           */
260                  lang.add("fr");          protected boolean checkValidInputs() {
261    
262                  Translation transe = new Translation();                  for (JComponent component : translationEditJPanelsOrJustComponents) {
263                  transe.put("de", "Terciopelo-Lanzenotter");                          if (component instanceof TranslationEditJPanel) {
264                  TranslationEditJPanel p1 = new TranslationEditJPanel(                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;
265                                  "Name von New Group", transe, lang);  
266                                    for (String l : tep.getTranslation().values()) {
267                  Translation transe2 = new Translation();                                          if (l.contains("{") || l.contains("}")) {
268                  transe2                                                  JOptionPane
269                                  .put(                                                                  .showMessageDialog(
270                                                  "de",                                                                                  this,
271                                                  "Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart.");                                                                                  RESOURCE
272                  TranslationEditJPanel p2 = new TranslationEditJPanel(                                                                                                  .getString("ErrorMsg.InvalidCharacterInTranslation"));
273                                  "Description of Animal:", transe2, lang);                                                  return false;
274                                            }
275                  // JFrame frame = new JFrame();                                  }
                 // frame.setContentPane(p1);  
                 // frame.pack();  
                 // frame.setVisible(true);  
276    
277                  TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2);                          }
278                  dialog.setVisible(true);                  }
279    
280                    return true;
281          }          }
282    
283          private void setHasBeenCanceled(boolean hasBeenCanceled) {          private void setCancelled(boolean hasBeenCanceled) {
284                  this.hasBeenCanceled = hasBeenCanceled;                  this.hasBeenCanceled = hasBeenCanceled;
285          }          }
286    
287          /**          /**
288           * After the modal dialog has been closed, this allows to find out, wether the dialog has been canceled.           * After the modal dialog has been closed, this allows to find out, whether
289           * @return           * the dialog has been canceled.
290             *
291             * @return <code>true</code> if the {@link JDialog} has been canceled.
292           */           */
293          public boolean isHasBeenCanceled() {          public boolean isCancelled() {
294                  return hasBeenCanceled;                  return hasBeenCanceled;
295          }          }
296    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26