001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui;
034:
035: import java.awt.BorderLayout;
036: import java.awt.Component;
037: import java.awt.Dimension;
038: import java.awt.Font;
039: import java.awt.GridBagConstraints;
040: import java.awt.GridBagLayout;
041: import java.awt.Insets;
042: import java.awt.event.ActionEvent;
043: import java.awt.event.ActionListener;
044:
045: import javax.swing.JButton;
046: import javax.swing.JDialog;
047: import javax.swing.JOptionPane;
048: import javax.swing.JPanel;
049: import javax.swing.JScrollPane;
050: import javax.swing.JTextArea;
051: import javax.swing.ScrollPaneConstants;
052:
053: import com.vividsolutions.jump.I18N;
054: import com.vividsolutions.jump.util.StringUtil;
055:
056: /**
057: * UI for Displaying an Error Dialog.
058: * @see WorkbenchFrame#warnUser warnUser
059: */
060:
061: public class ErrorDialog extends JOptionPane {
062: private static final String SHOW_DETAILS = I18N
063: .get("ui.ErrorFialog.show-details");
064: private static final String HIDE_DETAILS = I18N
065: .get("ui.ErrorFialog.hide-details");
066:
067: //The actual width will be MIN_DIALOG_WIDTH or the width as determined by
068: //the error message (after applying StringUtil#split to it), whichever is
069: //larger. [Jon Aquino]
070: private final static int MIN_DIALOG_WIDTH = 500;
071: private String details;
072:
073: private ErrorDialog() {
074: super (I18N.get("ui.ErrorFialog.message"), ERROR_MESSAGE,
075: DEFAULT_OPTION);
076: }
077:
078: private void addDetailPanel(final JDialog dialog,
079: final JButton detailButton) {
080: final JPanel panel = new JPanel(new GridBagLayout());
081: final JScrollPane scrollPane = new JScrollPane();
082: final JTextArea textArea = new JTextArea();
083: textArea.setOpaque(false);
084: scrollPane.setOpaque(false);
085: textArea.setFont(new Font("Monospaced", 0, 12));
086: textArea.setEditable(false);
087: scrollPane
088: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
089: scrollPane
090: .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
091: scrollPane.getViewport().add(textArea);
092: panel.add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 1.0,
093: 0.0, GridBagConstraints.NORTHWEST,
094: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
095: textArea.setText(details);
096: detailButton.addActionListener(new ActionListener() {
097: private boolean showingDetails = false;
098:
099: public void actionPerformed(ActionEvent e) {
100: if (showingDetails) {
101: dialog.remove(panel);
102: detailButton.setText(SHOW_DETAILS);
103: } else {
104: scrollPane.setPreferredSize(new Dimension(dialog
105: .getWidth(), 200));
106: dialog.getContentPane().add(panel,
107: BorderLayout.SOUTH);
108: detailButton.setText(HIDE_DETAILS);
109: }
110:
111: showingDetails = !showingDetails;
112: dialog.pack();
113: }
114: });
115: }
116:
117: private void setDetails(String details) {
118: this .details = details;
119: }
120:
121: public JDialog createDialog(Component parentComponent, String title) {
122: JButton okButton = new JButton("OK");
123: JButton detailButton = new JButton(SHOW_DETAILS);
124: setOptions(new Object[] { okButton, detailButton });
125:
126: final JDialog dialog = super .createDialog(parentComponent,
127: title);
128: addDetailPanel(dialog, detailButton);
129: okButton.addActionListener(new ActionListener() {
130: public void actionPerformed(ActionEvent e) {
131: dialog.setVisible(false);
132: }
133: });
134:
135: JPanel horizontalStrut = new JPanel();
136: horizontalStrut.setPreferredSize(new Dimension(
137: MIN_DIALOG_WIDTH, 0));
138: horizontalStrut.setMinimumSize(horizontalStrut
139: .getPreferredSize());
140: horizontalStrut.setMaximumSize(horizontalStrut
141: .getPreferredSize());
142: dialog.getContentPane()
143: .add(horizontalStrut, BorderLayout.NORTH);
144: dialog.pack();
145:
146: if (parentComponent != null) {
147: //For testing. [Jon Aquino]
148: GUIUtil.centreOnWindow(dialog);
149: }
150:
151: //The details panel doesn't properly resize, so just make the dialog
152: //unresizable. [Jon Aquino]
153: dialog.setResizable(false);
154:
155: return dialog;
156: }
157:
158: public static void show(Component parentComponent, String title,
159: String message, String details) {
160: ErrorDialog dialog = new ErrorDialog();
161: dialog.setMessage(StringUtil.split(message, 80));
162: dialog.setDetails(details);
163: dialog.createDialog(parentComponent, title).setVisible(true);
164: }
165:
166: }
|