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.wizard;
034:
035: import java.awt.BorderLayout;
036: import java.awt.CardLayout;
037: import java.awt.Color;
038: import java.awt.Component;
039: import java.awt.Frame;
040: import java.awt.GridBagConstraints;
041: import java.awt.GridBagLayout;
042: import java.awt.Insets;
043: import java.awt.event.ActionEvent;
044: import java.awt.event.WindowAdapter;
045: import java.awt.event.WindowEvent;
046: import java.util.ArrayList;
047: import java.util.Arrays;
048: import java.util.Collection;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.List;
052:
053: import javax.swing.BorderFactory;
054: import javax.swing.JButton;
055: import javax.swing.JDialog;
056: import javax.swing.JLabel;
057: import javax.swing.JPanel;
058: import javax.swing.JTextArea;
059: import javax.swing.WindowConstants;
060: import javax.swing.border.BevelBorder;
061: import javax.swing.border.Border;
062:
063: import com.vividsolutions.jts.util.Assert;
064: import com.vividsolutions.jump.I18N;
065: import com.vividsolutions.jump.workbench.ui.ErrorHandler;
066: import com.vividsolutions.jump.workbench.ui.InputChangedListener;
067:
068: public class WizardDialog extends JDialog implements WizardContext,
069: InputChangedListener {
070: private JPanel panel1 = new JPanel();
071: private BorderLayout borderLayout1 = new BorderLayout();
072: private ArrayList completedWizardPanels;
073: private JPanel buttonPanel = new JPanel();
074: private GridBagLayout gridBagLayout1 = new GridBagLayout();
075: private JButton cancelButton = new JButton();
076: private JButton nextButton = new JButton();
077: private JButton backButton = new JButton();
078: private JPanel fillerPanel = new JPanel();
079: private Border border2;
080: private Border border3;
081: private JPanel outerCenterPanel = new JPanel();
082: private GridBagLayout gridBagLayout2 = new GridBagLayout();
083: private JPanel centerPanel = new JPanel();
084: private Border border4;
085: private JLabel titleLabel = new JLabel();
086: private CardLayout cardLayout1 = new CardLayout();
087: private WizardPanel currentWizardPanel;
088: private List allWizardPanels;
089: private ErrorHandler errorHandler;
090: private JTextArea instructionTextArea = new JTextArea();
091: private boolean finishPressed = false;
092: private HashMap dataMap = new HashMap();
093: private Border border5;
094: private Border border6;
095: private Border border7;
096:
097: public WizardDialog(Frame frame, String title,
098: ErrorHandler errorHandler) {
099: super (frame, title, true);
100: this .errorHandler = errorHandler;
101:
102: try {
103: jbInit();
104: } catch (Exception ex) {
105: ex.printStackTrace();
106: }
107:
108: addWindowListener(new WindowAdapter() {
109: public void windowClosing(WindowEvent e) {
110: cancel();
111: }
112: });
113: }
114:
115: private void checkIDs(Collection wizardPanels) {
116: ArrayList ids = new ArrayList();
117:
118: for (Iterator i = wizardPanels.iterator(); i.hasNext();) {
119: WizardPanel panel = (WizardPanel) i.next();
120: ids.add(panel.getID());
121: }
122:
123: for (Iterator i = wizardPanels.iterator(); i.hasNext();) {
124: WizardPanel panel = (WizardPanel) i.next();
125:
126: if (panel.getNextID() == null) {
127: continue;
128: }
129:
130: Assert
131: .isTrue(
132: ids.contains(panel.getNextID()),
133: I18N
134: .get("ui.wizard.WizardDialog.required-panel-missing")
135: + " " + panel.getNextID());
136: }
137: }
138:
139: private void setCurrentWizardPanel(WizardPanel wizardPanel) {
140: if (currentWizardPanel != null) {
141: currentWizardPanel.remove(this );
142: }
143:
144: titleLabel.setText(wizardPanel.getTitle());
145: cardLayout1.show(centerPanel, wizardPanel.getID());
146: currentWizardPanel = wizardPanel;
147: updateButtons();
148: currentWizardPanel.add(this );
149: instructionTextArea.setText(currentWizardPanel
150: .getInstructions());
151: }
152:
153: private WizardPanel getCurrentWizardPanel() {
154: return currentWizardPanel;
155: }
156:
157: private void updateButtons() {
158: backButton.setEnabled(!completedWizardPanels.isEmpty());
159: nextButton.setEnabled(getCurrentWizardPanel().isInputValid());
160: nextButton
161: .setText((getCurrentWizardPanel().getNextID() == null) ? I18N
162: .get("ui.wizard.WizardDialog.finish")
163: : I18N.get("ui.wizard.WizardDialog.next")
164: + " >");
165: }
166:
167: public void inputChanged() {
168: updateButtons();
169: }
170:
171: /**
172: * @param wizardPanels the first of which will be the first WizardPanel that is displayed
173: */
174: public void init(WizardPanel[] wizardPanels) {
175: allWizardPanels = Arrays.asList(wizardPanels);
176: checkIDs(allWizardPanels);
177:
178: for (int i = 0; i < wizardPanels.length; i++) {
179: centerPanel.add((Component) wizardPanels[i],
180: wizardPanels[i].getID());
181: }
182:
183: completedWizardPanels = new ArrayList();
184: wizardPanels[0].enteredFromLeft(dataMap);
185: setCurrentWizardPanel(wizardPanels[0]);
186: pack();
187: }
188:
189: private void jbInit() throws Exception {
190: border7 = BorderFactory.createEmptyBorder(20, 20, 20, 10);
191: instructionTextArea.setEnabled(false);
192: instructionTextArea.setFont(new JLabel().getFont());
193: instructionTextArea.setOpaque(false);
194: instructionTextArea.setToolTipText("");
195: //Don't hardcode colour to black, as user may be using an inverted colour scheme
196: //[Jon Aquino 2004-05-31]
197: instructionTextArea.setDisabledTextColor(instructionTextArea
198: .getForeground());
199: instructionTextArea.setEditable(false);
200: instructionTextArea.setLineWrap(true);
201: instructionTextArea.setWrapStyleWord(true);
202: border5 = BorderFactory.createLineBorder(Color.black, 2);
203: border6 = BorderFactory.createCompoundBorder(BorderFactory
204: .createEtchedBorder(Color.white, new Color(148, 145,
205: 140)), BorderFactory.createEmptyBorder(20, 30,
206: 20, 10));
207: centerPanel.setLayout(cardLayout1);
208: border2 = BorderFactory.createEmptyBorder(5, 5, 5, 5);
209: border3 = BorderFactory.createEmptyBorder(10, 10, 10, 10);
210: border4 = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
211: Color.white, Color.white, new Color(103, 101, 98),
212: new Color(148, 145, 140));
213: panel1.setLayout(borderLayout1);
214: buttonPanel.setLayout(gridBagLayout1);
215: cancelButton.setText(I18N.get("ui.wizard.WizardDialog.cancel"));
216: cancelButton
217: .addActionListener(new java.awt.event.ActionListener() {
218: public void actionPerformed(ActionEvent e) {
219: cancelButton_actionPerformed(e);
220: }
221: });
222: nextButton.setText(I18N.get("ui.wizard.WizardDialog.next")
223: + " >");
224: nextButton
225: .addActionListener(new java.awt.event.ActionListener() {
226: public void actionPerformed(ActionEvent e) {
227: nextButton_actionPerformed(e);
228: }
229: });
230: backButton.setText("< "
231: + I18N.get("ui.wizard.WizardDialog.back"));
232: backButton
233: .addActionListener(new java.awt.event.ActionListener() {
234: public void actionPerformed(ActionEvent e) {
235: backButton_actionPerformed(e);
236: }
237: });
238: buttonPanel.setBorder(border3);
239: outerCenterPanel.setLayout(gridBagLayout2);
240: titleLabel.setBackground(Color.white);
241: titleLabel.setForeground(Color.black);
242: titleLabel.setFont(new java.awt.Font("Dialog", 1, 12));
243: titleLabel.setBorder(border7);
244: titleLabel.setOpaque(true);
245: titleLabel.setText("Title");
246: outerCenterPanel.setBorder(border6);
247: this
248: .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
249: instructionTextArea.setText("instructionTextArea");
250: getContentPane().add(panel1);
251: panel1.add(buttonPanel, BorderLayout.SOUTH);
252: buttonPanel
253: .add(cancelButton, new GridBagConstraints(3, 0, 1, 1,
254: 0.0, 0.0, GridBagConstraints.CENTER,
255: GridBagConstraints.NONE,
256: new Insets(0, 20, 0, 0), 0, 0));
257: buttonPanel.add(nextButton, new GridBagConstraints(2, 0, 1, 1,
258: 0.0, 0.0, GridBagConstraints.CENTER,
259: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
260: buttonPanel.add(backButton, new GridBagConstraints(1, 0, 1, 1,
261: 0.0, 0.0, GridBagConstraints.CENTER,
262: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
263: buttonPanel.add(fillerPanel, new GridBagConstraints(0, 0, 1, 1,
264: 1.0, 0.0, GridBagConstraints.CENTER,
265: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
266: 0, 0));
267: panel1.add(outerCenterPanel, BorderLayout.CENTER);
268: outerCenterPanel.add(centerPanel, new GridBagConstraints(0, 1,
269: 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
270: GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
271: outerCenterPanel.add(instructionTextArea,
272: new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
273: GridBagConstraints.CENTER,
274: GridBagConstraints.HORIZONTAL, new Insets(0, 0,
275: 20, 0), 0, 0));
276: this .getContentPane().add(titleLabel, BorderLayout.NORTH);
277: }
278:
279: void cancelButton_actionPerformed(ActionEvent e) {
280: cancel();
281: }
282:
283: private void cancel() {
284: setVisible(false);
285: }
286:
287: void nextButton_actionPerformed(ActionEvent e) {
288: try {
289: getCurrentWizardPanel().exitingToRight();
290:
291: if (getCurrentWizardPanel().getNextID() == null) {
292: finishPressed = true;
293: setVisible(false);
294:
295: return;
296: }
297:
298: completedWizardPanels.add(getCurrentWizardPanel());
299:
300: WizardPanel nextWizardPanel = find(getCurrentWizardPanel()
301: .getNextID());
302: nextWizardPanel.enteredFromLeft(dataMap);
303: setCurrentWizardPanel(nextWizardPanel);
304: } catch (Throwable x) {
305: errorHandler.handleThrowable(x);
306: }
307: }
308:
309: private WizardPanel find(String id) {
310: for (Iterator i = allWizardPanels.iterator(); i.hasNext();) {
311: WizardPanel wizardPanel = (WizardPanel) i.next();
312:
313: if (wizardPanel.getID().equals(id)) {
314: return wizardPanel;
315: }
316: }
317:
318: Assert.shouldNeverReachHere();
319:
320: return null;
321: }
322:
323: public boolean wasFinishPressed() {
324: return finishPressed;
325: }
326:
327: void backButton_actionPerformed(ActionEvent e) {
328: WizardPanel prevPanel = (WizardPanel) completedWizardPanels
329: .remove(completedWizardPanels.size() - 1);
330: setCurrentWizardPanel(prevPanel);
331:
332: //Don't init panel if we're going back. [Jon Aquino]
333: }
334:
335: public void setData(String name, Object value) {
336: dataMap.put(name, value);
337: }
338:
339: public Object getData(String name) {
340: return dataMap.get(name);
341: }
342: }
|