001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: DialogSet.java,v $
023   Revision 1.13  2004/05/12 18:12:16  markl
024   javadoc updates
025
026   Revision 1.12  2004/03/16 06:43:39  markl
027   LocaleManager method change
028
029   Revision 1.10  2004/01/23 00:06:28  markl
030   javadoc corrections
031
032   Revision 1.9  2003/01/19 09:41:00  markl
033   Javadoc & comment header updates.
034
035   Revision 1.8  2001/03/20 00:54:54  markl
036   Fixed deprecated calls.
037
038   Revision 1.7  2001/03/12 05:19:58  markl
039   Source code cleanup.
040
041   Revision 1.6  1999/11/15 03:43:15  markl
042   Fix for phantomFrame reference.
043
044   Revision 1.5  1999/06/21 06:55:46  markl
045   Added yes/no dialog support.
046
047   Revision 1.4  1999/06/14 02:02:45  markl
048   Added new forms of showInputDialog().
049
050   Revision 1.3  1999/04/19 06:05:26  markl
051   I18N changes.
052
053   Revision 1.2  1999/01/10 03:22:17  markl
054   added GPL header & RCS tag
055   ----------------------------------------------------------------------------
056*/
057
058package kiwi.ui.dialog;
059
060import java.awt.*;
061import java.beans.*;
062import javax.swing.*;
063
064import kiwi.ui.*;
065import kiwi.util.*;
066
067/** This class is the Kiwi analogy to the Swing <i>JOptionPane</i>. It provides
068  * a simple interface for displaying some of the more commonly-used Kiwi
069  * dialogs without requiring any <code>Dialog</code> objects to be
070  * instantiated.
071  * <p>
072  * A placement policy may be assigned to a <code>DialogSet</code>; this
073  * policy defines where dialogs will appear relative to their owner
074  * <code>Window</code>. If there is no owner, dialogs will appear in the
075  * center of the screen.
076  * <p>
077  * Although each instance of <code>DialogSet</code> may have its own owner
078  * <code>Window</code> and placement policy, they will all share singleton
079  * instances of <code>KMessageDialog</code>, <code>KQuestionDialog</code>, and
080  * <code>KInputDialog</code>.
081  * <p>
082  * @see kiwi.ui.dialog.KMessageDialog
083  * @see kiwi.ui.dialog.KQuestionDialog
084  * @see kiwi.ui.dialog.KInputDialog
085  *
086  * @author Mark Lindner
087  */
088
089public class DialogSet implements PropertyChangeListener
090  {
091  private static KInputDialog d_input;
092  private static KMessageDialog d_message;
093  private static KQuestionDialog d_question;
094  private static PropertyChangeSupport support;
095  private String s_input, s_message, s_question = "Question";
096  private Window _owner = null;
097  /** Placement policy. Display dialogs centered within the bounds of their
098    * owner <code>Window</code>.
099    */
100  public static final int CENTER_PLACEMENT = 0;
101
102  /** Placement policy. Display dialogs cascaded off the top-left corner of
103    * their owner <code>Window</code>.
104    */
105  public static final int CASCADE_PLACEMENT = 1;
106
107  private int placement = CENTER_PLACEMENT;
108  private static DialogSet defaultSet;
109  
110  static
111    {
112    d_input = new KInputDialog(KiwiUtils.getPhantomFrame());
113    d_question = new KQuestionDialog(KiwiUtils.getPhantomFrame());
114    d_message = new KMessageDialog(KiwiUtils.getPhantomFrame());
115    defaultSet = new DialogSet();
116    }
117
118  /** Get a reference to the default instance of <code>DialogSet</code>. This
119    * instance has no owner <code>Window</code>, and hence its dialogs appear
120    * centered on the screen.
121    *
122    * @return The default (singleton) instance.
123    */
124  
125  public static DialogSet getInstance()
126    {
127    return(defaultSet);
128    }
129
130  /** Set the owner window for this <code>DialogSet</code>.
131   *
132   * @param owner The new owner window.
133   *
134   * @since Kiwi 2.0
135   */
136
137  public void setOwner(Window owner)
138    {
139    _owner = owner;
140    }
141  
142  /* Construct a new <code>DialogSet</code> with a phantom frame owner and
143   * centered-on-screen placement policy.
144   */
145
146  private DialogSet()
147    {
148    this(null, CENTER_PLACEMENT);
149    UIChangeManager.getInstance().addPropertyChangeListener(this);    
150    }
151  
152  /** Construct a new <code>DialogSet</code> with the given owner and
153    * placement policy.
154    *
155    * @param owner The <code>Window</code> that is the owner of this
156    * <code>DialogSet</code>.
157    * @param placement The placement for dialogs in this
158    * <code>DialogSet</code>; one of the numeric constants defined above.
159    */
160  
161  public DialogSet(Window owner, int placement) throws IllegalArgumentException
162    {
163    if((placement < 0) || (placement > 1))
164      throw(new IllegalArgumentException());
165    
166    _owner = owner;
167    this.placement = placement;
168
169    LocaleData loc = LocaleManager.getDefault().getLocaleData("KiwiDialogs");
170
171    s_input = loc.getMessage("kiwi.dialog.title.input");
172    s_message = loc.getMessage("kiwi.dialog.title.message");
173    s_question = loc.getMessage("kiwi.dialog.title.question");
174    }
175  
176  /** Show a <code>KInputDialog</code>. Displays an input dialog and returns
177    * when the dialog is dismissed.
178    *
179    * @param prompt The prompt to display in the dialog.
180    * @return The text that was entered in the dialog, or <code>null</code>
181    * if the dialog was cancelled.
182    */
183
184  public String showInputDialog(String prompt)
185    {
186    return(showInputDialog(s_input, null, prompt, null));
187    }
188
189  /** Show a <code>KInputDialog</code>. Displays an input dialog and returns
190    * when the dialog is dismissed.
191    *
192    * @param parent The parent window.
193    * @param prompt The prompt to display in the dialog.
194    * @return The text that was entered in the dialog, or <code>null</code>
195    * if the dialog was cancelled.
196    */
197  
198  public String showInputDialog(Window parent, String prompt)
199    {
200    return(_showInputDialog(parent, s_input, null, prompt, null));
201    }
202      
203  /** Show a <code>KInputDialog</code>. Displays an input dialog and returns
204    * when the dialog is dismissed.
205    *
206    * @param prompt The prompt to display in the dialog.
207    * @param defaultValue The default value to display in the input field.
208    * @return The text that was entered in the dialog, or <code>null</code>
209    * if the dialog was cancelled.
210    */
211  
212  public String showInputDialog(String prompt, String defaultValue)
213    {
214    return(showInputDialog(s_input, null, prompt, defaultValue));
215    }
216
217  /** Show a <code>KInputDialog</code>. Displays an input dialog and returns
218    * when the dialog is dismissed.
219    *
220    * @param title The title for the dialog window.
221    * @param icon The icon to display in the dialog.
222    * @param prompt The prompt to display in the dialog.
223    * @return The text that was entered in the dialog, or <code>null</code>
224    * if the dialog was cancelled.
225    */
226
227  public synchronized String showInputDialog(String title, Icon icon,
228                                             String prompt)
229    {
230    return(showInputDialog(title, icon, prompt, null));
231    }
232
233  /** Show a <code>KInputDialog</code>. Displays an input dialog and returns
234    * when the dialog is dismissed.
235    *
236    * @param title The title for the dialog window.
237    * @param icon The icon to display in the dialog.
238    * @param prompt The prompt to display in the dialog.
239    * @param defaultValue The default value to display in the input field.
240    * @return The text that was entered in the dialog, or <code>null</code>
241    * if the dialog was cancelled.
242    */
243  
244  public synchronized String showInputDialog(String title, Icon icon,
245                                             String prompt,
246                                             String defaultValue)
247    {
248    return(_showInputDialog(_owner, title, icon, prompt, defaultValue));
249    }
250
251  /** Show a <code>KInputDialog</code>. Displays an input dialog and returns
252    * when the dialog is dismissed.
253    *
254    * @param parent The parent window.
255    * @param title The title for the dialog window.
256    * @param icon The icon to display in the dialog.
257    * @param prompt The prompt to display in the dialog.
258    * @param defaultValue The default value to display in the input field.
259    * @return The text that was entered in the dialog, or <code>null</code>
260    * if the dialog was cancelled.
261    *
262    * @since Kiwi 2.0
263    */
264  
265  public synchronized String showInputDialog(Window parent, String title,
266                                             Icon icon, String prompt,
267                                             String defaultValue)
268    {
269    return(_showInputDialog(parent, title, icon, prompt, defaultValue));
270    }
271
272  /* show an input dialog */
273
274  private String _showInputDialog(Window owner, String title, Icon icon,
275                                  String prompt, String defaultValue)
276    {
277    d_input.setTitle((title == null) ? s_input : title);
278    d_input.setPrompt(prompt);
279    if(icon != null)
280      d_input.setIcon(icon);
281    KiwiUtils.centerWindow(d_input);
282    d_input.setText((defaultValue == null) ? "" : defaultValue);
283    _positionDialog(owner, d_input);
284    d_input.setVisible(true);
285    if(d_input.isCancelled())
286      return(null);
287    return(d_input.getText());
288    }
289
290  /** Show a <code>KQuestionDialog</code>. Displays a question dialog and
291   * returns when the dialog is dismissed.
292   *
293   * @param prompt The prompt to display in the dialog.
294   * @param type The question dialog type to display; one of the symbolic
295   * constants defined in <code>KQuestionDialog</code>.
296   * @return The status of the dialog; <code>true</code> if the dialog was
297   * accepted or <code>false</code> if it was cancelled.
298   */
299
300  public boolean showQuestionDialog(String prompt, int type)
301    {
302    return(showQuestionDialog(s_question, null, prompt, type));
303    }
304  
305  /** Show a <code>KQuestionDialog</code>. Displays a question dialog and
306   * returns when the dialog is dismissed.
307   *
308   * @param prompt The prompt to display in the dialog.
309   * @return The status of the dialog; <code>true</code> if the dialog was
310   * accepted or <code>false</code> if it was cancelled.
311   */
312  
313  public boolean showQuestionDialog(String prompt)
314    {
315    return(showQuestionDialog(s_question, null, prompt,
316                              KQuestionDialog.OK_CANCEL_DIALOG));
317    }
318
319  /** Show a <code>KQuestionDialog</code>. Displays a question dialog and
320   * returns when the dialog is dismissed.
321   *
322   * @param parent The parent window.
323   * @param prompt The prompt to display in the dialog.
324   * @return The status of the dialog; <code>true</code> if the dialog was
325   * accepted or <code>false</code> if it was cancelled.
326   *
327   * @since Kiwi 2.0
328   */
329  
330  public boolean showQuestionDialog(Window parent, String prompt)
331    {
332    return(showQuestionDialog(parent, s_question, null, prompt,
333                              KQuestionDialog.OK_CANCEL_DIALOG));
334    }
335
336  /** Show a <code>KQuestionDialog</code>. Displays a question dialog and
337   * returns when the dialog is dismissed.
338   *
339   * @param parent The parent window.
340   * @param prompt The prompt to display in the dialog.
341    * @param type The question dialog type to display; one of the symbolic
342    * constants defined in <code>KQuestionDialog</code>.
343   * @return The status of the dialog; <code>true</code> if the dialog was
344   * accepted or <code>false</code> if it was cancelled.
345   *
346   * @since Kiwi 2.0
347   */
348  
349  public boolean showQuestionDialog(Window parent, String prompt, int type)
350    {
351    return(showQuestionDialog(parent, s_question, null, prompt, type));
352    }
353  
354  /** Show a <code>KQuestionDialog</code>. Displays a question dialog and
355    * returns when the dialog is dismissed.
356    *
357    * @param title The title for the dialog window.
358    * @param icon The icon to display in the dialog.
359    * @param prompt The promopt to display in the dialog.
360    * @return The status of the dialog; <code>true</code> if the dialog was
361    * accepted or <code>false</code> if it was cancelled.
362    */
363
364  public synchronized boolean showQuestionDialog(String title, Icon icon,
365                                                 String prompt)
366    {
367    return(showQuestionDialog(title, icon, prompt,
368                              KQuestionDialog.OK_CANCEL_DIALOG));
369    }
370
371  /** Show a <code>KQuestionDialog</code>. Displays a question dialog and
372    * returns when the dialog is dismissed.
373    *
374    * @param title The title for the dialog window.
375    * @param icon The icon to display in the dialog.
376    * @param prompt The promopt to display in the dialog.
377    * @param type The question dialog type to display; one of the symbolic
378    * constants defined in <code>KQuestionDialog</code>.
379    * @return The status of the dialog; <code>true</code> if the dialog was
380    * accepted or <code>false</code> if it was cancelled.
381    */
382  
383  public synchronized boolean showQuestionDialog(String title, Icon icon,
384                                                 String prompt, int type)
385    {
386    return(_showQuestionDialog(_owner, title, icon, prompt, type));
387    }
388
389  /** Show a <code>KQuestionDialog</code>. Displays a question dialog and
390    * returns when the dialog is dismissed.
391    *
392    * @param parent The parent window.
393    * @param title The title for the dialog window.
394    * @param icon The icon to display in the dialog.
395    * @param prompt The promopt to display in the dialog.
396    * @param type The question dialog type to display; one of the symbolic
397    * constants defined in <code>KQuestionDialog</code>.
398    * @return The status of the dialog; <code>true</code> if the dialog was
399    * accepted or <code>false</code> if it was cancelled.
400    *
401    * @since Kiwi 2.0
402    */
403  
404  public synchronized boolean showQuestionDialog(Window parent, String title,
405                                                 Icon icon, String prompt,
406                                                 int type)
407    {
408    return(_showQuestionDialog(parent, title, icon, prompt, type));
409    }
410  
411  /* show a question dialog */
412
413  private boolean _showQuestionDialog(Window parent, String title, Icon icon,
414                                      String prompt, int type)
415    {
416    d_question.setTitle((title == null) ? s_input : title);
417    d_question.setType(type);
418    d_question.setMessage(prompt);
419    if(icon != null)
420      d_question.setIcon(icon);
421    KiwiUtils.centerWindow(d_question);
422    _positionDialog(parent, d_question);
423    d_question.setVisible(true);
424    if(d_question.isCancelled())
425      return(false);
426    return(d_question.getStatus());
427    }
428
429  /** Show a <code>KMessageDialog</code>. Displays a message dialog and returns
430    * when the dialog is dismissed.
431    *
432    * @param parent The parent window.
433    * @param message The prompt to display in the dialog.
434    *
435    * @since Kiwi 2.0
436    */
437
438  public void showMessageDialog(Window parent, String message)
439    {
440    showMessageDialog(parent, s_message, null, message);
441    }
442
443  /** Show a <code>KMessageDialog</code>. Displays a message dialog and returns
444    * when the dialog is dismissed.
445    *
446    * @param message The prompt to display in the dialog.
447    */
448
449  public void showMessageDialog(String message)
450    {
451    showMessageDialog(s_message, null, message);
452    }
453  
454  /** Show a <code>KMessageDialog</code>. Displays a message dialog and returns
455    * when the dialog is dismissed.
456    *
457    * @param title The title for the dialog window.
458    * @param icon The icon to display in the dialog.
459    * @param message The prompt to display in the dialog.
460    */
461
462  public synchronized void showMessageDialog(String title, Icon icon,
463                                             String message)
464    {
465    showMessageDialog(_owner, title, icon, message);
466    }
467
468  /** Show a <code>KMessageDialog</code>. Displays a message dialog and returns
469    * when the dialog is dismissed.
470    *
471    * @param parent The parent window.
472    * @param title The title for the dialog window.
473    * @param icon The icon to display in the dialog.
474    * @param message The prompt to display in the dialog.
475    *
476    * @since Kiwi 2.0
477    */
478  
479  public synchronized void showMessageDialog(Window parent, String title,
480                                             Icon icon, String message)
481    {
482    _showMessageDialog(parent, title, icon, message);
483    }
484  
485  /* show a message dialog */
486
487  private void _showMessageDialog(Window owner, String title, Icon icon,
488                                  String message)
489    {
490    d_message.setTitle((title == null) ? s_message : title);
491    d_message.setMessage(message);
492    KiwiUtils.centerWindow(d_message);
493    if(icon != null)
494      d_message.setIcon(icon);
495    _positionDialog(owner, d_message);
496    d_message.setVisible(true);
497    }
498
499  /* position a dialog */
500
501  private void _positionDialog(Window owner, Dialog dialog)
502    {
503    if(owner == null)
504      KiwiUtils.centerWindow(dialog);
505    else
506      {
507      switch(placement)
508        {
509        case CENTER_PLACEMENT:
510          KiwiUtils.centerWindow(owner, dialog);
511          break;
512        case CASCADE_PLACEMENT:
513          KiwiUtils.cascadeWindow(owner, dialog);
514          break;
515        }
516      }
517    }
518
519  /** Respond to property change events.
520    */
521  
522  public void propertyChange(PropertyChangeEvent evt)
523    {
524    String prop = evt.getPropertyName();
525
526    if(prop.equals(UIChangeManager.TEXTURE_PROPERTY))
527      {
528      Image img = (Image)evt.getNewValue();
529      d_input.setTexture(img);
530      d_message.setTexture(img);
531      d_question.setTexture(img);
532      }
533    else if(prop.equals(UIChangeManager.BUTTON_OPACITY_PROPERTY))
534      {
535      boolean flag = ((Boolean)evt.getNewValue()).booleanValue();
536      d_input.setButtonOpacity(flag);
537      d_message.setButtonOpacity(flag);
538      d_question.setButtonOpacity(flag);
539      }
540    }
541
542  }
543
544/* end of source file */