001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.util;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.jetbrains.annotations.Nullable;
018: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
019:
020: import javax.swing.*;
021: import java.awt.*;
022: import java.util.Locale;
023:
024: /**
025: * User: Martin
026: * Date: Oct 22, 2007
027: * Time: 4:53:48 PM
028: */
029: public class JOptionPaneHelper extends JOptionPane {
030:
031: @Nullable
032: public static String showInputDialog(@NotNull
033: Object message, @NotNull
034: CenterPanelDialog.ModalityType modalityType)
035: throws HeadlessException {
036: return showInputDialog(null, message, modalityType);
037: }
038:
039: @Nullable
040: public static String showInputDialog(@NotNull
041: Object message, @Nullable
042: Object initialSelectionValue, @NotNull
043: CenterPanelDialog.ModalityType modalityType) {
044: return showInputDialog(null, message, initialSelectionValue,
045: modalityType);
046: }
047:
048: @Nullable
049: public static String showInputDialog(@Nullable
050: Component parentComponent, @NotNull
051: Object message, @NotNull
052: CenterPanelDialog.ModalityType modalityType)
053: throws HeadlessException {
054: return showInputDialog(parentComponent, message, UIManager
055: .getString("OptionPane.inputDialogTitle",
056: parentComponent != null ? parentComponent
057: .getLocale() : Locale.getDefault()),
058: QUESTION_MESSAGE, modalityType);//NON-NLS
059: }
060:
061: @Nullable
062: public static String showInputDialog(@Nullable
063: Component parentComponent, @NotNull
064: Object message, @Nullable
065: Object initialSelectionValue, @NotNull
066: CenterPanelDialog.ModalityType modalityType) {
067: return (String) showInputDialog(parentComponent, message,
068: UIManager.getString("OptionPane.inputDialogTitle",
069: parentComponent != null ? parentComponent
070: .getLocale() : Locale.getDefault()),
071: QUESTION_MESSAGE, null, null, initialSelectionValue,
072: modalityType);//NON-NLS
073: }
074:
075: @Nullable
076: public static String showInputDialog(@Nullable
077: Component parentComponent, @NotNull
078: Object message, @Nullable
079: String title, int messageType, @NotNull
080: CenterPanelDialog.ModalityType modalityType)
081: throws HeadlessException {
082: return (String) showInputDialog(parentComponent, message,
083: title, messageType, null, null, null, modalityType);
084: }
085:
086: @Nullable
087: public static Object showInputDialog(@Nullable
088: Component parentComponent, @NotNull
089: Object message, @Nullable
090: String title, int messageType, @Nullable
091: Icon icon, @Nullable
092: Object[] selectionValues, @Nullable
093: Object initialSelectionValue, @NotNull
094: CenterPanelDialog.ModalityType modalityType)
095: throws HeadlessException {
096: JOptionPane pane = new JOptionPane(message, messageType,
097: OK_CANCEL_OPTION, icon, null, null);
098:
099: pane.setWantsInput(true);
100: pane.setSelectionValues(selectionValues);
101: pane.setInitialSelectionValue(initialSelectionValue);
102: pane
103: .setComponentOrientation(((parentComponent == null) ? getRootFrame()
104: : parentComponent).getComponentOrientation());
105:
106: JDialog dialog = pane.createDialog(parentComponent, title);
107:
108: pane.selectInitialValue();
109: CenterPanelDialog.setModality(dialog, modalityType);
110: dialog.setVisible(true);
111: dialog.dispose();
112:
113: Object value = pane.getInputValue();
114:
115: //noinspection ObjectEquality
116: if (value == UNINITIALIZED_VALUE) {
117: return null;
118: }
119: return value;
120: }
121:
122: public static void showMessageDialog(@Nullable
123: Component parentComponent, @NotNull
124: Object message, @NotNull
125: CenterPanelDialog.ModalityType modalityType)
126: throws HeadlessException {
127: showMessageDialog(parentComponent, message, UIManager
128: .getString("OptionPane.messageDialogTitle",
129: parentComponent != null ? parentComponent
130: .getLocale() : Locale.getDefault()),
131: INFORMATION_MESSAGE, modalityType);//NON-NLS
132: }
133:
134: public static void showMessageDialog(@Nullable
135: Component parentComponent, @NotNull
136: Object message, @Nullable
137: String title, int messageType, @NotNull
138: CenterPanelDialog.ModalityType modalityType)
139: throws HeadlessException {
140: showMessageDialog(parentComponent, message, title, messageType,
141: null, modalityType);
142: }
143:
144: public static void showMessageDialog(@Nullable
145: Component parentComponent, @NotNull
146: Object message, @Nullable
147: String title, int messageType, @Nullable
148: Icon icon, @NotNull
149: CenterPanelDialog.ModalityType modalityType)
150: throws HeadlessException {
151: showOptionDialog(parentComponent, message, title,
152: DEFAULT_OPTION, messageType, icon, null, null,
153: modalityType);
154: }
155:
156: public static int showConfirmDialog(@Nullable
157: Component parentComponent, @NotNull
158: Object message, @NotNull
159: CenterPanelDialog.ModalityType modalityType)
160: throws HeadlessException {
161: return showConfirmDialog(parentComponent, message, UIManager
162: .getString("OptionPane.titleText"),
163: YES_NO_CANCEL_OPTION, modalityType);//NON-NLS
164: }
165:
166: public static int showConfirmDialog(@Nullable
167: Component parentComponent, @NotNull
168: Object message, @Nullable
169: String title, int optionType, @NotNull
170: CenterPanelDialog.ModalityType modalityType)
171: throws HeadlessException {
172: return showConfirmDialog(parentComponent, message, title,
173: optionType, QUESTION_MESSAGE, modalityType);
174: }
175:
176: public static int showConfirmDialog(@Nullable
177: Component parentComponent, @NotNull
178: Object message, @Nullable
179: String title, int optionType, int messageType, @NotNull
180: CenterPanelDialog.ModalityType modalityType)
181: throws HeadlessException {
182: return showConfirmDialog(parentComponent, message, title,
183: optionType, messageType, null, modalityType);
184: }
185:
186: public static int showConfirmDialog(@Nullable
187: Component parentComponent, @NotNull
188: Object message, @Nullable
189: String title, int optionType, int messageType, @Nullable
190: Icon icon, @NotNull
191: CenterPanelDialog.ModalityType modalityType)
192: throws HeadlessException {
193: return showOptionDialog(parentComponent, message, title,
194: optionType, messageType, icon, null, null, modalityType);
195: }
196:
197: public static int showOptionDialog(@Nullable
198: Component parentComponent, @NotNull
199: Object message, @Nullable
200: String title, int optionType, int messageType, @Nullable
201: Icon icon, @Nullable
202: Object[] options, @Nullable
203: Object initialValue, @NotNull
204: CenterPanelDialog.ModalityType modalityType)
205: throws HeadlessException {
206: JOptionPane pane = new JOptionPane(message, messageType,
207: optionType, icon, options, initialValue);
208:
209: pane.setInitialValue(initialValue);
210: pane
211: .setComponentOrientation(((parentComponent == null) ? getRootFrame()
212: : parentComponent).getComponentOrientation());
213:
214: JDialog dialog = pane.createDialog(parentComponent, title);
215:
216: pane.selectInitialValue();
217: CenterPanelDialog.setModality(dialog, modalityType);
218: dialog.setVisible(true);
219: dialog.dispose();
220:
221: Object selectedValue = pane.getValue();
222:
223: if (selectedValue == null) {
224: return CLOSED_OPTION;
225: }
226: if (options == null) {
227: if (selectedValue instanceof Integer) {
228: return ((Integer) selectedValue).intValue();
229: }
230: return CLOSED_OPTION;
231: }
232: for (int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) {
233: if (options[counter].equals(selectedValue)) {
234: return counter;
235: }
236: }
237: return CLOSED_OPTION;
238: }
239:
240: }
|