KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > metainfservices > ExportWizardIterator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.apisupport.metainfservices;
20
21 import java.awt.Component JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23 import javax.swing.JComponent JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import org.openide.WizardDescriptor;
26
27 public final class ExportWizardIterator implements WizardDescriptor.Iterator {
28
29     // To invoke this wizard, copy-paste and run the following code, e.g. from
30
// SomeAction.performAction():
31
/*
32     WizardDescriptor.Iterator iterator = new ExportWizardIterator();
33     WizardDescriptor wizardDescriptor = new WizardDescriptor(iterator);
34     // {0} will be replaced by WizardDescriptor.Panel.getComponent().getName()
35     // {1} will be replaced by WizardDescriptor.Iterator.name()
36     wizardDescriptor.setTitleFormat(new MessageFormat("{0} ({1})"));
37     wizardDescriptor.setTitle("Your wizard dialog title here");
38     Dialog dialog = DialogDisplayer.getDefault().createDialog(wizardDescriptor);
39     dialog.setVisible(true);
40     dialog.toFront();
41     boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
42     if (!cancelled) {
43         // do something
44     }
45      */

46     
47     private int index;
48     
49     private WizardDescriptor.Panel[] panels;
50     
51     /**
52      * Initialize panels representing individual wizard's steps and sets
53      * various properties for them influencing wizard appearance.
54      */

55     private WizardDescriptor.Panel[] getPanels() {
56         if (panels == null) {
57             panels = new WizardDescriptor.Panel[] {
58                 new ExportWizardPanel1(),
59             };
60             String JavaDoc[] steps = new String JavaDoc[panels.length];
61             for (int i = 0; i < panels.length; i++) {
62                 Component JavaDoc c = panels[i].getComponent();
63                 // Default step name to component name of panel.
64
steps[i] = c.getName();
65                 if (c instanceof JComponent JavaDoc) { // assume Swing components
66
JComponent JavaDoc jc = (JComponent JavaDoc) c;
67                     // Sets step number of a component
68
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i));
69                     // Sets steps names for a panel
70
jc.putClientProperty("WizardPanel_contentData", steps);
71                     // Turn on subtitle creation on each step
72
jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
73                     // Show steps on the left side with the image on the background
74
jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);
75                     // Turn on numbering of all steps
76
jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);
77                 }
78             }
79         }
80         return panels;
81     }
82     
83     public WizardDescriptor.Panel current() {
84         return getPanels()[index];
85     }
86     
87     public String JavaDoc name() {
88         return index + 1 + ". from " + getPanels().length;
89     }
90     
91     public boolean hasNext() {
92         return index < getPanels().length - 1;
93     }
94     
95     public boolean hasPrevious() {
96         return index > 0;
97     }
98     
99     public void nextPanel() {
100         if (!hasNext()) {
101             throw new NoSuchElementException JavaDoc();
102         }
103         index++;
104     }
105     
106     public void previousPanel() {
107         if (!hasPrevious()) {
108             throw new NoSuchElementException JavaDoc();
109         }
110         index--;
111     }
112     
113     // If nothing unusual changes in the middle of the wizard, simply:
114
public void addChangeListener(ChangeListener JavaDoc l) {}
115     public void removeChangeListener(ChangeListener JavaDoc l) {}
116     
117     // If something changes dynamically (besides moving between panels), e.g.
118
// the number of panels changes in response to user input, then uncomment
119
// the following and call when needed: fireChangeEvent();
120
/*
121     private Set<ChangeListener> listeners = new HashSet<ChangeListener>(1);
122     public final void addChangeListener(ChangeListener l) {
123         synchronized (listeners) {
124             listeners.add(l);
125         }
126     }
127     public final void removeChangeListener(ChangeListener l) {
128         synchronized (listeners) {
129             listeners.remove(l);
130         }
131     }
132     protected final void fireChangeEvent() {
133         Iterator<ChangeListener> it;
134         synchronized (listeners) {
135             it = new HashSet<ChangeListener>(listeners).iterator();
136         }
137         ChangeEvent ev = new ChangeEvent(this);
138         while (it.hasNext()) {
139             it.next().stateChanged(ev);
140         }
141     }
142      */

143     
144 }
145
Popular Tags