/[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 116 by alfonx, Wed May 13 17:37:11 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;
# Line 17  import javax.swing.Action; Line 16  import javax.swing.Action;
16  import javax.swing.Box;  import javax.swing.Box;
17  import javax.swing.JComponent;  import javax.swing.JComponent;
18  import javax.swing.JDialog;  import javax.swing.JDialog;
19    import javax.swing.JOptionPane;
20  import javax.swing.JPanel;  import javax.swing.JPanel;
21  import javax.swing.JRootPane;  import javax.swing.JRootPane;
22  import javax.swing.KeyStroke;  import javax.swing.KeyStroke;
# Line 45  public class TranslationAskJDialog exten Line 45  public class TranslationAskJDialog exten
45          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";          public static final String PROPERTY_CANCEL_AND_CLOSE = "CANCEL";
46          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";          public static final String PROPERTY_APPLY_AND_CLOSE = "APPLY";
47    
48          private final JComponent[] translationEditJPanelsOrJustComponents;          private JComponent[] translationEditJPanelsOrJustComponents;
49    
50          private boolean hasBeenCanceled;          private boolean hasBeenCanceled;
51    
# Line 74  public class TranslationAskJDialog exten Line 74  public class TranslationAskJDialog exten
74          }          }
75    
76          /**          /**
77             * The {@link TranslationAskJDialog} fills its content pane with an
78             * arbitrary number of components. If these {@link Component}s are
79             * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
80             * values and restore them if the dialog is canceled. Other
81             * {@link JComponent}s are just displayed.<br/>
82           * 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
83           * to PROPERTY_APPLY_AND_CLOSE events.           * to PROPERTY_APPLY_AND_CLOSE events. This dialog is modal. The dialog has
84             * to be set visible afterwards.<br/>
85           *           *
86           * This dialog is modal. The dialog has to be set visible afterwards.           * @param owner
87             *            A component of the GUI that this dialog is related to. If no
88             *            {@link Window} is passed, SwingUtil.getParentWindow(owner) is
89             *            called.
90           */           */
91          public TranslationAskJDialog(Dialog owner,          public TranslationAskJDialog(Component owner,
92                          final JComponent... translationEditJPanels) {                          final JComponent... translationEditJPanels) {
93                  super(owner);                  super(owner instanceof Window ? (Window) owner : SwingUtil
94                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;                                  .getParentWindow(owner));
95                  init();                  setComponents(translationEditJPanels);
96          }          }
97    
98          /**          /**
99             * The {@link TranslationAskJDialog} fills its content pane with an
100             * arbitrary number of components. If these {@link Component}s are
101             * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
102             * values and restore them if the dialog is canceled. Other
103             * {@link JComponent}s are just displayed.<br/>
104           * 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
105           * 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
106           * to be set visible afterwards.           * to be set visible afterwards.<br/>
107             * Using this constructor, you have to call setComponents afterwards.
108           */           */
109          public TranslationAskJDialog(Window owner,          public TranslationAskJDialog(Window owner) {
110                          final JComponent... translationEditJPanels) {                  this(owner, new JComponent[] {});
111                  super(owner);          }
112    
113            /**
114             * The {@link TranslationAskJDialog} fills its content pane with an
115             * arbitrary number of components. If these {@link Component}s are
116             * {@link TranslationEditJPanel}s, the {@link JDialog} manages to backup the
117             * values and restore them if the dialog is canceled. Other
118             * {@link JComponent}s are just displayed.
119             *
120             * @param translationEditJPanels
121             *            Arbitrary list of {@link JComponent}s and
122             *            {@link TranslationEditJPanel}s.
123             */
124            public void setComponents(final JComponent... translationEditJPanels) {
125                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;                  this.translationEditJPanelsOrJustComponents = translationEditJPanels;
                 init();  
126    
127                  // Rememebr backups for all the jtextpanels                  // Remember backups for all the TranslationEditJPanel
128                  int count = 0;                  int count = 0;
129                  for (JComponent component : translationEditJPanelsOrJustComponents) {                  for (JComponent component : translationEditJPanelsOrJustComponents) {
130                          if (component instanceof TranslationEditJPanel) {                          if (component instanceof TranslationEditJPanel) {
131                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;
132                                  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.  
133    
134                                    // We don't want to overwrite the Translation object on
135                                    // restore(). We just want to change its value.
136                                  backup[count++] = orig.toOneLine();                                  backup[count++] = orig.toOneLine();
137                          }                          }
138                  }                  }
139    
140                    init();
141          }          }
142    
143          private void init() {          private void init() {
# Line 130  public class TranslationAskJDialog exten Line 159  public class TranslationAskJDialog exten
159                  cp.add(getButtons(), BorderLayout.SOUTH);                  cp.add(getButtons(), BorderLayout.SOUTH);
160                  setContentPane(cp);                  setContentPane(cp);
161    
                 // dialog.getRootPane().setDefaultButton(okButton);  
   
162                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n                  setTitle(RESOURCE.getString("translation_dialog_title")); // i8n
163                  setModal(true);                  setModal(true);
164                  pack();                  pack();
# Line 144  public class TranslationAskJDialog exten Line 171  public class TranslationAskJDialog exten
171                  dispose();                  dispose();
172          }          }
173    
174          private void restore() {          protected void restore() {
175                  int count = 0;                  int count = 0;
176                  for (JComponent component : translationEditJPanelsOrJustComponents) {                  for (JComponent component : translationEditJPanelsOrJustComponents) {
177                          if (component instanceof TranslationEditJPanel) {                          if (component instanceof TranslationEditJPanel) {
# Line 178  public class TranslationAskJDialog exten Line 205  public class TranslationAskJDialog exten
205                                  public void actionPerformed(ActionEvent evt) {                                  public void actionPerformed(ActionEvent evt) {
206                                          TranslationAskJDialog.this.firePropertyChange(                                          TranslationAskJDialog.this.firePropertyChange(
207                                                          PROPERTY_APPLY_AND_CLOSE, null, null);                                                          PROPERTY_APPLY_AND_CLOSE, null, null);
208    
209                                            if (!checkValidInputs())
210                                                    return;
211    
212                                          setVisible(false);                                          setVisible(false);
213                                          dispose();                                          dispose();
                                         System.out.println("OK button action performed");  
214                                  }                                  }
215    
216                          });                          });
217                          // okButton.addKeyListener( new KeyListener() {  
                         //  
                         // public void keyTyped(KeyEvent e) {  
                         // if ()  
                         // okButton.action(new KEyPreEvent(), what)  
                         // }  
                         //                                
                         // });  
218                  }                  }
219                  jPanel.add(okButton);                  jPanel.add(okButton);
220    
221                  if (cancelButton == null) {                  if (cancelButton == null) {
222                          cancelButton = new CancelButton(new AbstractAction("") {                          cancelButton = new CancelButton(new AbstractAction("") {
223                                  public void actionPerformed(ActionEvent evt) {                                  public void actionPerformed(ActionEvent evt) {
224                                          // restore();                                          restore();
225                                          TranslationAskJDialog.this.firePropertyChange(                                          TranslationAskJDialog.this.firePropertyChange(
226                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);                                                          PROPERTY_CANCEL_AND_CLOSE, null, null);
227                                          setVisible(false);                                          setVisible(false);
228                                          setHasBeenCanceled(true);                                          setCancelled(true);
229                                          dispose();                                          dispose();
230                                  }                                  }
231                          });                          });
# Line 212  public class TranslationAskJDialog exten Line 235  public class TranslationAskJDialog exten
235                  return jPanel;                  return jPanel;
236          }          }
237    
238          public static void main(String[] args) {          /**
239                  ArrayList<String> lang = new ArrayList<String>();           * @return <code>true</code> if none of the translations contains illegal
240                  lang.add("de");           *         characters.
241                  lang.add("en");           */
242                  lang.add("fr");          protected boolean checkValidInputs() {
243    
244                  Translation transe = new Translation();                  for (JComponent component : translationEditJPanelsOrJustComponents) {
245                  transe.put("de", "Terciopelo-Lanzenotter");                          if (component instanceof TranslationEditJPanel) {
246                  TranslationEditJPanel p1 = new TranslationEditJPanel(                                  TranslationEditJPanel tep = (TranslationEditJPanel) component;
247                                  "Name von New Group", transe, lang);  
248                                    for (String l : tep.getTranslation().values()) {
249                  Translation transe2 = new Translation();                                          if (l.contains("{") || l.contains("}")) {
250                  transe2                                                  JOptionPane
251                                  .put(                                                                  .showMessageDialog(
252                                                  "de",                                                                                  this,
253                                                  "Terciopelo-Lanzenotter (Bothrops asper) ist eine in Mittelamerika und im Nordwesten Südamerikas weit verbreitete Schlangenart.");                                                                                  RESOURCE
254                  TranslationEditJPanel p2 = new TranslationEditJPanel(                                                                                                  .getString("ErrorMsg.InvalidCharacterInTranslation"));
255                                  "Description of Animal:", transe2, lang);                                                  return false;
256                                            }
257                  // JFrame frame = new JFrame();                                  }
258                  // frame.setContentPane(p1);  
259                  // frame.pack();                          }
260                  // frame.setVisible(true);                  }
261    
262                  TranslationAskJDialog dialog = new TranslationAskJDialog(null, p1, p2);                  return true;
                 dialog.setVisible(true);  
263          }          }
264    
265          private void setHasBeenCanceled(boolean hasBeenCanceled) {          private void setCancelled(boolean hasBeenCanceled) {
266                  this.hasBeenCanceled = hasBeenCanceled;                  this.hasBeenCanceled = hasBeenCanceled;
267          }          }
268    
269          /**          /**
270           * 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
271           * the dialog has been canceled.           * the dialog has been canceled.
272           *           *
273           * @return           * @return <code>true</code> if the {@link JDialog} has been canceled.
274           */           */
275          public boolean isHasBeenCanceled() {          public boolean isCancelled() {
276                  return hasBeenCanceled;                  return hasBeenCanceled;
277          }          }
278    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26