001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 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 GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.swing;
023:
024: import java.awt.BorderLayout;
025: import java.awt.CardLayout;
026: import java.awt.Color;
027: import java.awt.Dimension;
028: import java.awt.Font;
029: import java.awt.Toolkit;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.awt.event.WindowAdapter;
033: import java.awt.event.WindowEvent;
034: import java.util.ArrayList;
035: import java.util.HashMap;
036:
037: import javax.swing.BorderFactory;
038: import javax.swing.Box;
039: import javax.swing.BoxLayout;
040: import javax.swing.ImageIcon;
041: import javax.swing.JButton;
042: import javax.swing.JFrame;
043: import javax.swing.JInternalFrame;
044: import javax.swing.JLabel;
045: import javax.swing.JPanel;
046:
047: import org.beryl.gui.GUIException;
048: import org.beryl.gui.InternationalizationManager;
049: import org.beryl.gui.WizardListener;
050: import org.beryl.gui.WizardPageAdapter;
051: import org.beryl.gui.model.MapDataModel;
052: import org.beryl.gui.model.ModelChangeEvent;
053: import org.beryl.gui.model.ModelChangeListener;
054: import org.beryl.gui.widgets.Wizard;
055: import org.beryl.gui.widgets.WizardPage;
056:
057: import cz.autel.dmi.HIGConstraints;
058: import cz.autel.dmi.HIGLayout;
059:
060: /**
061: * Java XML WizardItem Framework
062: * @version 2.0
063: * @author Wenzel Jakob
064: */
065:
066: public class JWizard extends JFrame {
067: private static final Font headingFont = new Font("SansSerif",
068: Font.BOLD, 16);
069: private MapDataModel model = null;
070: private ModelChangeListener modelListener = null;
071: private CardLayout layout = null;
072: private JPanel componentPanel = null;
073: private ArrayList pages = null;
074: private ArrayList listeners = null;
075: private HashMap pagesByName = null;
076: private JButton backButton = null;
077: private JButton nextButton = null;
078: private JButton cancelButton = null;
079: private JLabel sideImage, titleLabel = null;
080: private JBreakingLabel descriptionLabel = null;
081: private Wizard widget = null;
082: private int totalPages = 0;
083: private int currentPage = 0;
084:
085: public JWizard(Wizard widget) {
086: this .widget = widget;
087: pages = new ArrayList();
088: listeners = new ArrayList();
089: layout = new CardLayout();
090: pagesByName = new HashMap();
091: componentPanel = new JPanel();
092: componentPanel.setLayout(layout);
093:
094: JPanel buttonPanel = new JPanel();
095: buttonPanel.setLayout(new BoxLayout(buttonPanel,
096: BoxLayout.X_AXIS));
097: buttonPanel.add(Box.createGlue());
098: backButton = new JButton(InternationalizationManager
099: .getString("xmlgui.wizard.back"));
100: backButton.setActionCommand("wizard.back");
101: backButton.setEnabled(false);
102: backButton.addActionListener(new ActionListener() {
103: public void actionPerformed(ActionEvent evt) {
104: back();
105: }
106: });
107: buttonPanel.add(backButton);
108: buttonPanel.add(Box.createRigidArea(new Dimension(5, 0)));
109:
110: nextButton = new JButton(InternationalizationManager
111: .getString("xmlgui.wizard.next"));
112: nextButton.setActionCommand("wizard.next");
113: nextButton.setEnabled(false);
114: nextButton.addActionListener(new ActionListener() {
115: public void actionPerformed(ActionEvent evt) {
116: next();
117: }
118: });
119: buttonPanel.add(nextButton);
120: buttonPanel.add(Box.createRigidArea(new Dimension(15, 0)));
121: cancelButton = new JButton(InternationalizationManager
122: .getString("xmlgui.wizard.cancel"));
123: cancelButton.addActionListener(new ActionListener() {
124: public void actionPerformed(ActionEvent evt) {
125: fireCancel();
126: }
127: });
128: buttonPanel.add(cancelButton);
129: JPanel lowerPanel = new JPanel();
130: lowerPanel
131: .setLayout(new BoxLayout(lowerPanel, BoxLayout.Y_AXIS));
132: lowerPanel.add(new JHorizontalSeparator(10));
133: lowerPanel.add(Box.createVerticalStrut(5));
134: lowerPanel.add(buttonPanel);
135: lowerPanel.add(Box.createVerticalStrut(10));
136:
137: JPanel mainPanel = new JPanel();
138: HIGLayout higLayout = new HIGLayout(new int[] { 0, 30, 0 },
139: new int[] { 10, 30, 0, 0, 20, 50 });
140: higLayout.setColumnWeight(3, 1);
141: higLayout.setRowWeight(3, 1);
142: HIGConstraints constraints = new HIGConstraints();
143: sideImage = new JLabel();
144: titleLabel = new JLabel();
145: descriptionLabel = new JBreakingLabel();
146: titleLabel.setForeground(Color.black);
147: titleLabel.setFont(headingFont);
148: mainPanel.setLayout(higLayout);
149: mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5,
150: 10));
151: mainPanel.add(sideImage, constraints.rcwh(1, 1, 1, 6, "rlt"));
152: mainPanel.add(titleLabel, constraints.rcwh(2, 3, 1, 1, "lt"));
153: mainPanel.add(componentPanel, constraints.rc(4, 3, "lrb"));
154: mainPanel.add(descriptionLabel, constraints.rc(3, 3));
155: mainPanel.add(lowerPanel, constraints.rcwh(6, 1, 3, 1));
156: getContentPane().add(mainPanel, BorderLayout.CENTER);
157: getRootPane().setDefaultButton(nextButton);
158: setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
159:
160: addWindowListener(new WindowAdapter() {
161: public void windowClosing(WindowEvent e) {
162: fireCancel();
163: }
164: });
165:
166: setSize(480, 360);
167:
168: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
169: int x = (int) ((d.getWidth() - getWidth()) / 2);
170: int y = (int) ((d.getHeight() - getHeight()) / 2);
171: setLocation(x, y);
172:
173: show();
174: }
175:
176: public void setSideImage(ImageIcon icon) {
177: sideImage.setIcon(icon);
178: }
179:
180: public void setDataModel(MapDataModel newModel) throws GUIException {
181: if (model != null && modelListener != null)
182: model.removeModelChangeListener(modelListener);
183: modelListener = new ModelChangeListener() {
184: public void modelChanged(ModelChangeEvent e)
185: throws GUIException {
186: updateButtons();
187: }
188: };
189: newModel.addModelChangeListener(modelListener);
190: }
191:
192: public MapDataModel getDataModel() {
193: return model;
194: }
195:
196: public WizardPage getPage(String name) {
197: return (WizardPage) pagesByName.get(name);
198: }
199:
200: public void addPage(WizardPage page) {
201: pages.add(page);
202: pagesByName.put(page.getName(), page);
203: componentPanel.add(page.getWidget(), page.getName());
204: totalPages++;
205: updateButtons();
206: updatePanel();
207: }
208:
209: private void back() {
210: if (currentPage > 0) {
211: WizardPage current = null;
212: do {
213: current = (WizardPage) pages.get(--currentPage);
214: } while (!current.isEnabled());
215: if (current.getAdapter() != null)
216: current.getAdapter().preparePage(current);
217: updateButtons();
218: updatePanel();
219: }
220: }
221:
222: private void next() {
223: if (currentPage < totalPages - 1) {
224: WizardPage current = (WizardPage) pages.get(currentPage);
225: WizardPageAdapter wpAdapter = current.getAdapter();
226: if (wpAdapter != null)
227: wpAdapter.preparePage(current);
228: if (wpAdapter == null || wpAdapter.finalizePage(current)) {
229: do {
230: current = (WizardPage) pages.get(++currentPage);
231: } while (!current.isEnabled());
232: if (current.getAdapter() != null)
233: current.getAdapter().preparePage(current);
234: updateButtons();
235: updatePanel();
236: }
237: } else {
238: WizardPage current = (WizardPage) pages.get(currentPage);
239: WizardPageAdapter wpAdapter = current.getAdapter();
240: if (wpAdapter == null || wpAdapter.finalizePage(current)) {
241: fireFinish();
242: }
243: }
244: }
245:
246: private void fireCancel() {
247: for (int i = 0; i < listeners.size(); i++) {
248: ((WizardListener) listeners.get(i)).wizardCanceled(widget);
249: }
250: dispose();
251: }
252:
253: private void fireFinish() {
254: for (int i = 0; i < listeners.size(); i++) {
255: ((WizardListener) listeners.get(i)).wizardFinished(widget);
256: }
257: }
258:
259: private void updatePanel() {
260: WizardPage page = (WizardPage) pages.get(currentPage);
261: titleLabel.setText(page.getTitle() != null ? page.getTitle()
262: : "");
263: descriptionLabel.setText(page.getDescription() != null ? page
264: .getDescription() : "");
265: layout.show(componentPanel, page.getName());
266: }
267:
268: public void updateButtons() {
269: WizardPage page = (WizardPage) pages.get(currentPage);
270: WizardPageAdapter wpAdapter = page.getAdapter();
271:
272: if (page != null) {
273: if (wpAdapter != null)
274: nextButton.setEnabled(wpAdapter.isPageReady(page));
275: else
276: nextButton.setEnabled(true);
277: if (currentPage < (totalPages - 1)) {
278: nextButton.setText(InternationalizationManager
279: .getString("xmlgui.wizard.next"));
280: } else {
281: nextButton.setText(InternationalizationManager
282: .getString("xmlgui.wizard.finish"));
283: }
284: if (currentPage > 0)
285: backButton.setEnabled(true);
286: else
287: backButton.setEnabled(false);
288: } else {
289: nextButton.setEnabled(false);
290: backButton.setEnabled(false);
291: }
292: }
293:
294: public void addWizardListener(WizardListener listener) {
295: listeners.add(listener);
296: }
297:
298: public void removeWizardListener(WizardListener listener) {
299: listeners.remove(listener);
300: }
301:
302: public JButton getBackButton() {
303: return backButton;
304: }
305:
306: public JButton getNextButton() {
307: return nextButton;
308: }
309: }
|