001: /*
002: ** $Id: Dialogs.java,v 1.1 2000/10/26 08:34:34 mrw Exp $
003: **
004: ** Mike Wilson, July 2000, mrw@whisperingwind.co.uk
005: **
006: ** (C) Copyright 2000, Mike Wilson, Reading, Berkshire, UK
007: **
008: ** This program is free software; you can redistribute it and/or modify
009: ** it under the terms of the GNU General Public License as published by
010: ** the Free Software Foundation; either version 2 of the License, or
011: ** (at your option) any later version.
012: **
013: ** This program is distributed in the hope that it will be useful,
014: ** but WITHOUT ANY WARRANTY; without even the implied warranty of
015: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: ** GNU General Public License for more details.
017: **
018: ** You should have received a copy of the GNU Library General
019: ** Public License along with this library; if not, write to the
020: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
021: ** Boston, MA 02111-1307 USA.
022: */
023:
024: package uk.co.whisperingwind.framework;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import javax.swing.JOptionPane;
028: import javax.swing.SwingUtilities;
029:
030: /**
031: ** Class contains static methods to display JOptionPane message
032: ** dialogs. In all cases, the JOptionPane dialog is opened in the
033: ** event dispatch thread.
034: */
035:
036: public class Dialogs {
037: /**
038: ** Show an error dialog.
039: */
040:
041: public static void showError(String title, String text) {
042: MessageHelper messageHelper = new MessageHelper(title, text,
043: JOptionPane.ERROR_MESSAGE);
044:
045: if (SwingUtilities.isEventDispatchThread()) {
046: messageHelper.run();
047: } else {
048: try {
049: SwingUtilities.invokeAndWait(messageHelper);
050: } catch (InterruptedException ex) {
051: // Do nothing
052: } catch (InvocationTargetException ex) {
053: // Do nothing
054: }
055: }
056: }
057:
058: /**
059: ** Show a warning dialog.
060: */
061:
062: public static void showWarning(String title, String text) {
063: MessageHelper messageHelper = new MessageHelper(title, text,
064: JOptionPane.WARNING_MESSAGE);
065:
066: if (SwingUtilities.isEventDispatchThread()) {
067: messageHelper.run();
068: } else {
069: try {
070: SwingUtilities.invokeAndWait(messageHelper);
071: } catch (InterruptedException ex) {
072: // Do nothing
073: } catch (InvocationTargetException ex) {
074: // Do nothing
075: }
076: }
077: }
078:
079: /**
080: ** Show an information dialog.
081: */
082:
083: public static void showInformation(String title, String text) {
084: MessageHelper messageHelper = new MessageHelper(title, text,
085: JOptionPane.INFORMATION_MESSAGE);
086:
087: if (SwingUtilities.isEventDispatchThread()) {
088: messageHelper.run();
089: } else {
090: try {
091: SwingUtilities.invokeAndWait(messageHelper);
092: } catch (InterruptedException ex) {
093: // Do nothing
094: } catch (InvocationTargetException ex) {
095: // Do nothing
096: }
097: }
098: }
099:
100: /**
101: ** Show a yes/no/cancel dialog and return the result.
102: */
103:
104: public static int getConfirm(String title, String text) {
105: ConfirmHelper confirmHelper = new ConfirmHelper(title, text,
106: JOptionPane.YES_NO_CANCEL_OPTION);
107:
108: if (SwingUtilities.isEventDispatchThread()) {
109: confirmHelper.run();
110: } else {
111: try {
112: SwingUtilities.invokeAndWait(confirmHelper);
113: } catch (InterruptedException ex) {
114: // Do nothing
115: } catch (InvocationTargetException ex) {
116: // Do nothing
117: }
118: }
119:
120: return confirmHelper.get();
121: }
122:
123: /**
124: ** Show an option dialog and return the result.
125: */
126:
127: public static int getOption(String title, String text,
128: Object[] options, Object defaultOption) {
129: OptionHelper optionHelper = new OptionHelper(title, text,
130: options, defaultOption);
131:
132: if (SwingUtilities.isEventDispatchThread()) {
133: optionHelper.run();
134: } else {
135: try {
136: SwingUtilities.invokeAndWait(optionHelper);
137: } catch (InterruptedException ex) {
138: // Do nothing
139: } catch (InvocationTargetException ex) {
140: // Do nothing
141: }
142: }
143:
144: return optionHelper.get();
145: }
146:
147: /**
148: ** Superclass of message and confirm helpers.
149: */
150:
151: private static class Helper {
152: protected String title = null;
153: protected String text = null;
154: protected int type = 0;
155:
156: protected Helper(String title, String text, int type) {
157: this .title = title;
158: this .text = text;
159: this .type = type;
160: }
161: }
162:
163: /**
164: ** Message display helper class. Each message dialog uses an
165: ** instance of this which it passes to invokeAndWait to make sure
166: ** JOptionPane is called from the event thread.
167: */
168:
169: private static class MessageHelper extends Helper implements
170: Runnable {
171: public MessageHelper(String title, String text, int type) {
172: super (title, text, type);
173: }
174:
175: public void run() {
176: JOptionPane.showMessageDialog(null, text, title, type);
177: }
178: }
179:
180: private static class ConfirmHelper extends Helper implements
181: Runnable {
182: private int result = 0;
183:
184: public ConfirmHelper(String title, String text, int type) {
185: super (title, text, type);
186: }
187:
188: public void run() {
189: result = JOptionPane.showConfirmDialog(null, text, title,
190: type);
191: }
192:
193: public int get() {
194: return result;
195: }
196: }
197:
198: private static class OptionHelper extends Helper implements
199: Runnable {
200: private int result = 0;
201: Object[] options = null;
202: Object defaultOption = null;
203:
204: public OptionHelper(String title, String text,
205: Object[] options, Object defaultOption) {
206: super (title, text, 0);
207: this .options = options;
208: this .defaultOption = defaultOption;
209: }
210:
211: public void run() {
212: result = JOptionPane.showOptionDialog(null, text, title,
213: JOptionPane.DEFAULT_OPTION,
214: JOptionPane.WARNING_MESSAGE, null, options,
215: defaultOption);
216: }
217:
218: public int get() {
219: return result;
220: }
221: }
222: }
|