001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.x.impl.swing;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Component;
017: import java.awt.Dimension;
018: import java.awt.event.ActionEvent;
019:
020: import javax.swing.AbstractAction;
021: import javax.swing.BorderFactory;
022: import javax.swing.JButton;
023: import javax.swing.JDialog;
024: import javax.swing.JEditorPane;
025: import javax.swing.JOptionPane;
026: import javax.swing.JPanel;
027: import javax.swing.JScrollPane;
028:
029: import com.eviware.soapui.support.DefaultHyperlinkListener;
030: import com.eviware.soapui.support.UISupport;
031: import com.eviware.soapui.support.components.ProgressDialog;
032: import com.eviware.x.dialogs.XDialogs;
033: import com.eviware.x.dialogs.XProgressDialog;
034: import com.jgoodies.forms.factories.ButtonBarFactory;
035:
036: /**
037: *
038: * @author Lars
039: */
040: public class SwingDialogs implements XDialogs {
041: private Component parent;
042: private JDialog extendedInfoDialog;
043: private Boolean extendedInfoResult;
044:
045: public SwingDialogs(Component parent) {
046: this .parent = parent;
047: }
048:
049: public void showErrorMessage(String message) {
050: if (parent == null)
051: return;
052:
053: JOptionPane.showMessageDialog(parent, message, "Error",
054: JOptionPane.ERROR_MESSAGE);
055: }
056:
057: public boolean confirm(String question, String title) {
058: return JOptionPane.showConfirmDialog(parent, question, title,
059: JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION;
060: }
061:
062: public String prompt(String question, String title, String value) {
063: return (String) JOptionPane.showInputDialog(parent, question,
064: title, JOptionPane.QUESTION_MESSAGE, null, null, value);
065: }
066:
067: public String prompt(String question, String title) {
068: return JOptionPane.showInputDialog(parent, question, title,
069: JOptionPane.QUESTION_MESSAGE);
070: }
071:
072: public void showInfoMessage(String message) {
073: showInfoMessage(message, "Information");
074: }
075:
076: public void showInfoMessage(String message, String title) {
077: JOptionPane.showMessageDialog(parent, message, title,
078: JOptionPane.INFORMATION_MESSAGE);
079: }
080:
081: public Object prompt(String question, String title, Object[] objects) {
082: Object result = JOptionPane.showInputDialog(parent, question,
083: title, JOptionPane.OK_CANCEL_OPTION, null, objects,
084: null);
085: return result;
086: }
087:
088: public Object prompt(String question, String title,
089: Object[] objects, String value) {
090: Object result = JOptionPane.showInputDialog(parent, question,
091: title, JOptionPane.OK_CANCEL_OPTION, null, objects,
092: value);
093: return result;
094: }
095:
096: public Boolean confirmOrCancel(String question, String title) {
097: int result = JOptionPane.showConfirmDialog(parent, question,
098: title, JOptionPane.YES_NO_CANCEL_OPTION);
099:
100: if (result == JOptionPane.CANCEL_OPTION)
101: return null;
102:
103: return Boolean.valueOf(result == JOptionPane.YES_OPTION);
104: }
105:
106: public XProgressDialog createProgressDialog(String label,
107: int length, String initialValue, boolean canCancel) {
108: return new ProgressDialog("Progress", label, length,
109: initialValue, canCancel);
110: }
111:
112: public void showExtendedInfo(String title, String description,
113: String content, Dimension size) {
114: JPanel buttonBar = ButtonBarFactory
115: .buildRightAlignedBar(new JButton(new OkAction("OK")));
116:
117: showExtendedInfo(title, description, content, buttonBar, size);
118: }
119:
120: private void showExtendedInfo(String title, String description,
121: String content, JPanel buttonBar, Dimension size) {
122: extendedInfoDialog = new JDialog(UISupport.getMainFrame(),
123: title);
124: extendedInfoDialog.setModal(true);
125: JPanel panel = new JPanel(new BorderLayout());
126:
127: if (description != null) {
128: panel.add(UISupport.buildDescription(title, description,
129: null), BorderLayout.NORTH);
130: }
131:
132: JEditorPane editorPane = new JEditorPane("text/html", content);
133: editorPane.setCaretPosition(0);
134: editorPane.setEditable(false);
135: editorPane.addHyperlinkListener(new DefaultHyperlinkListener(
136: editorPane));
137:
138: JScrollPane scrollPane = new JScrollPane(editorPane);
139: scrollPane.setBorder(BorderFactory.createCompoundBorder(
140: BorderFactory.createEmptyBorder(5, 5, 5, 5), scrollPane
141: .getBorder()));
142:
143: panel.add(scrollPane);
144: buttonBar
145: .setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 5));
146: panel.add(buttonBar, BorderLayout.SOUTH);
147:
148: extendedInfoDialog.getRootPane().setContentPane(panel);
149: if (size == null)
150: extendedInfoDialog.setSize(400, 300);
151: else
152: extendedInfoDialog.setSize(size);
153:
154: UISupport.showDialog(extendedInfoDialog);
155: }
156:
157: public boolean confirmExtendedInfo(String title,
158: String description, String content, Dimension size) {
159: JPanel buttonBar = ButtonBarFactory.buildRightAlignedBar(
160: new JButton(new OkAction("OK")), new JButton(
161: new CancelAction("Cancel")));
162:
163: showExtendedInfo(title, description, content, buttonBar, size);
164:
165: return extendedInfoResult == null ? false : extendedInfoResult;
166: }
167:
168: public Boolean confirmOrCancleExtendedInfo(String title,
169: String description, String content, Dimension size) {
170: JPanel buttonBar = ButtonBarFactory.buildRightAlignedBar(
171: new JButton(new OkAction("Yes")), new JButton(
172: new NoAction("No")), new JButton(
173: new CancelAction("Cancel")));
174:
175: showExtendedInfo(title, description, content, buttonBar, size);
176:
177: return extendedInfoResult;
178: }
179:
180: private final class OkAction extends AbstractAction {
181: public OkAction(String name) {
182: super (name);
183: }
184:
185: public void actionPerformed(ActionEvent e) {
186: extendedInfoResult = true;
187: extendedInfoDialog.setVisible(false);
188: }
189: }
190:
191: private final class NoAction extends AbstractAction {
192: public NoAction(String name) {
193: super (name);
194: }
195:
196: public void actionPerformed(ActionEvent e) {
197: extendedInfoResult = false;
198: extendedInfoDialog.setVisible(false);
199: }
200: }
201:
202: private final class CancelAction extends AbstractAction {
203: public CancelAction(String name) {
204: super (name);
205: }
206:
207: public void actionPerformed(ActionEvent e) {
208: extendedInfoResult = null;
209: extendedInfoDialog.setVisible(false);
210: }
211: }
212: }
|