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.wizard;
015:
016: import com.jgoodies.forms.builder.ButtonBarBuilder;
017: import com.jgoodies.forms.layout.CellConstraints;
018: import com.jgoodies.forms.layout.FormLayout;
019: import org.jetbrains.annotations.NonNls;
020: import org.jetbrains.annotations.NotNull;
021: import org.pentaho.reportdesigner.crm.report.IconLoader;
022: import org.pentaho.reportdesigner.crm.report.ReportDialog;
023: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
024: import org.pentaho.reportdesigner.lib.client.components.ComponentFactory;
025: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
026:
027: import javax.swing.*;
028: import java.awt.*;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.util.HashMap;
032:
033: /**
034: * User: Martin
035: * Date: 20.01.2006
036: * Time: 09:26:50
037: */
038: public class WizardDialog extends CenterPanelDialog implements
039: WizardPageListener {
040: @NotNull
041: private WizardPage currentPage;
042: @NotNull
043: private JComponent currentPageCenterPanel;
044:
045: @NotNull
046: private JPanel centerPanel;
047:
048: @NotNull
049: private JButton previousButton;
050: @NotNull
051: private JButton nextButton;
052: @NotNull
053: private JButton cancelButton;
054: @NotNull
055: private JButton finishButton;
056:
057: @NotNull
058: private HashMap<String, WizardData> wizardDatas;
059:
060: private boolean ok;
061:
062: @NotNull
063: private WizardHeaderComponent wizardHeaderComponent;
064: @NotNull
065: private ReportDialog reportDialog;
066:
067: public WizardDialog(@NotNull
068: final ReportDialog reportDialog, @NotNull
069: String title, @NotNull
070: ModalityType modalityType, @NotNull
071: @NonNls
072: final String key, @NotNull
073: WizardPage firstPage) {
074: super (reportDialog, title, modalityType);
075: //noinspection ConstantConditions
076: if (reportDialog == null) {
077: throw new IllegalArgumentException(
078: "reportDialog must not be null");
079: }
080: this .reportDialog = reportDialog;
081: //noinspection ConstantConditions
082: if (firstPage == null) {
083: throw new IllegalArgumentException(
084: "firstPage must not be null");
085: }
086:
087: wizardDatas = new HashMap<String, WizardData>();
088:
089: currentPage = firstPage;
090:
091: @NonNls
092: FormLayout formLayout = new FormLayout(
093: "4dlu, fill:pref:grow, 4dlu", "0dlu, " + "pref, "
094: + "0dlu, " + "pref," + "4dlu, "
095: + "fill:pref:grow, " + "4dlu");
096:
097: centerPanel = new JPanel(formLayout);
098: setCenterPanel(centerPanel);
099:
100: wizardHeaderComponent = new WizardHeaderComponent();
101: getContentPane().add(wizardHeaderComponent, BorderLayout.NORTH);
102:
103: currentPage.setWizardDialog(this );
104:
105: previousButton = ComponentFactory.createButton("R",
106: "WizardDialog.Previous");
107: nextButton = ComponentFactory.createButton("R",
108: "WizardDialog.Next");
109:
110: cancelButton = new JButton(TranslationManager.getInstance()
111: .getTranslation("R", "Button.cancel"));
112: finishButton = new JButton(TranslationManager.getInstance()
113: .getTranslation("R", "Button.finish"));
114:
115: JPanel buttonPanel = new JPanel();
116: ButtonBarBuilder buttonBarBuilder = new ButtonBarBuilder(
117: buttonPanel);
118: buttonBarBuilder.addGlue();
119: buttonBarBuilder.addGridded(cancelButton);
120: buttonBarBuilder.addUnrelatedGap();
121: buttonBarBuilder.addGriddedButtons(new JButton[] {
122: previousButton, nextButton });
123: buttonBarBuilder.addUnrelatedGap();
124: buttonBarBuilder.addGridded(finishButton);
125: setButtonPanel(nextButton, cancelButton, buttonPanel);
126:
127: previousButton.addActionListener(new ActionListener() {
128: public void actionPerformed(@NotNull
129: ActionEvent e) {
130: readData();
131:
132: WizardPage wizardPage = currentPage.getPrevious();
133: if (wizardPage != null) {
134: uninstallCurrentPage();
135: currentPage = wizardPage;
136: installCurrentPage();
137: }
138: }
139: });
140:
141: nextButton.addActionListener(new ActionListener() {
142: public void actionPerformed(@NotNull
143: ActionEvent e) {
144: readData();
145:
146: WizardPage next = currentPage.getNext();
147: if (next != null) {
148: uninstallCurrentPage();
149: currentPage = next;
150: installCurrentPage();
151: }
152: }
153: });
154:
155: cancelButton.addActionListener(new ActionListener() {
156: public void actionPerformed(@NotNull
157: ActionEvent e) {
158: uninstallCurrentPage();
159: ok = false;
160: reportDialog.getWorkspaceSettings().storeDialogBounds(
161: WizardDialog.this , key);
162: dispose();
163: }
164: });
165:
166: finishButton.addActionListener(new ActionListener() {
167: public void actionPerformed(@NotNull
168: ActionEvent e) {
169: readData();
170: uninstallCurrentPage();
171: ok = true;
172: reportDialog.getWorkspaceSettings().storeDialogBounds(
173: WizardDialog.this , key);
174: dispose();
175: }
176: });
177:
178: installCurrentPage();
179: }
180:
181: @NotNull
182: public ReportDialog getReportDialog() {
183: return reportDialog;
184: }
185:
186: private void readData() {
187: WizardData[] wd = currentPage.getWizardDatas();
188: for (WizardData wizardData : wd) {
189: wizardDatas.put(wizardData.getKey(), wizardData);
190: }
191: }
192:
193: @NotNull
194: public HashMap<String, WizardData> getWizardDatas() {
195: return wizardDatas;
196: }
197:
198: public boolean isOk() {
199: return ok;
200: }
201:
202: private void uninstallCurrentPage() {
203: centerPanel.remove(currentPageCenterPanel);
204: currentPage.removeWizardPageListener(this );
205: }
206:
207: private void installCurrentPage() {
208: CellConstraints cc = new CellConstraints();
209: currentPage.setWizardDialog(this );
210: currentPageCenterPanel = currentPage.getCenterPanel();
211: this .centerPanel.add(currentPageCenterPanel, cc.xy(2, 6));
212: currentPage.addWizardPageListener(this );
213: stateChanged();
214:
215: this .centerPanel.revalidate();
216: this .centerPanel.repaint();
217: }
218:
219: public void stateChanged() {
220: previousButton.setEnabled(currentPage.hasPrevious()
221: && currentPage.canPrevious());
222: nextButton.setEnabled(currentPage.hasNext()
223: && currentPage.canNext());
224: cancelButton.setEnabled(currentPage.canCancel());
225:
226: finishButton.setEnabled(canAllPagesFinish());
227:
228: if (currentPage.getIcon() != null) {
229: wizardHeaderComponent.setIcon(currentPage.getIcon());
230: } else {
231: wizardHeaderComponent.setIcon(IconLoader.getInstance()
232: .getReportWizardIcon());
233: }
234: wizardHeaderComponent.setTitle(currentPage.getTitle());
235: wizardHeaderComponent.setDescription(currentPage
236: .getDescription());
237: }
238:
239: private boolean canAllPagesFinish() {
240: if (!currentPage.canFinish()) {
241: return false;
242: }
243:
244: WizardPage previous = currentPage;
245: while ((previous = previous.getPrevious()) != null) {
246: if (!previous.canFinish()) {
247: return false;
248: }
249: }
250:
251: WizardPage next = currentPage;
252: while ((next = next.getNext()) != null) {
253: if (!next.canFinish()) {
254: return false;
255: }
256: }
257:
258: return true;
259: }
260:
261: public void dispose() {
262: super .dispose();
263:
264: currentPage.dispose();
265:
266: WizardPage previous = currentPage;
267: while ((previous = previous.getPrevious()) != null) {
268: previous.dispose();
269: }
270:
271: WizardPage next = currentPage;
272: while ((next = next.getNext()) != null) {
273: next.dispose();
274: }
275: }
276: }
|