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

Legend:
Removed from v.33  
changed lines
  Added in v.120

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26