KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > wizards > shortcut > ShortcutIterator


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
20 package org.apache.tools.ant.module.wizards.shortcut;
21
22 import java.awt.Component JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import org.openide.WizardDescriptor;
30 import org.openide.filesystems.Repository;
31 import org.openide.loaders.DataFolder;
32 import org.openide.util.NbBundle;
33
34 final class ShortcutIterator implements WizardDescriptor.Iterator {
35
36     ShortcutIterator() {}
37
38     // You should define what panels you want to use here:
39

40     private WizardDescriptor.Panel[] createPanels () {
41         return new WizardDescriptor.Panel[] {
42             new IntroPanel.IntroWizardPanel (),
43             new SelectFolderPanel.SelectFolderWizardPanel(
44                 NbBundle.getMessage(ShortcutIterator.class, "SI_LBL_select_menu_to_add_to"),
45                 NbBundle.getMessage(ShortcutIterator.class, "SI_TEXT_menu_locn"),
46                 NbBundle.getMessage(ShortcutIterator.class, "SI_LBL_display_name_for_menu"),
47                 DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().findResource("Menu")), // NOI18N
48
true, ShortcutWizard.PROP_FOLDER_MENU),
49             new SelectFolderPanel.SelectFolderWizardPanel(
50                 NbBundle.getMessage(ShortcutIterator.class, "SI_LBL_select_toolbar"),
51                 NbBundle.getMessage(ShortcutIterator.class, "SI_TEXT_toolbar_locn"),
52                 NbBundle.getMessage(ShortcutIterator.class, "SI_LBL_display_name_for_toolbar"),
53                 DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().findResource("Toolbars")), // NOI18N
54
false, ShortcutWizard.PROP_FOLDER_TOOL),
55             new SelectKeyboardShortcutPanel.SelectKeyboardShortcutWizardPanel (),
56             new CustomizeScriptPanel.CustomizeScriptWizardPanel (),
57         };
58     }
59
60     // And the list of step names:
61

62     private String JavaDoc[] createSteps () {
63         return new String JavaDoc[] {
64             NbBundle.getMessage (ShortcutIterator.class, "SI_LBL_choose_options"),
65             NbBundle.getMessage (ShortcutIterator.class, "SI_LBL_add_to_menu"),
66             NbBundle.getMessage (ShortcutIterator.class, "SI_LBL_add_to_toolbar"),
67             NbBundle.getMessage (ShortcutIterator.class, "SI_LBL_make_keyboard_shortcut"),
68             NbBundle.getMessage (ShortcutIterator.class, "SI_LBL_cust_script"),
69         };
70     }
71     
72     private transient int index;
73     private transient WizardDescriptor.Panel[] panels;
74     private transient ShortcutWizard wiz;
75
76     // You can keep a reference to the TemplateWizard which can
77
// provide various kinds of useful information such as
78
// the currently selected target name.
79
// Also the panels will receive wiz as their "settings" object.
80
void initialize(ShortcutWizard wiz) {
81         this.wiz = wiz;
82         index = 0;
83         panels = createPanels ();
84         // #44409: make sure IntroWizardPanel knows about wiz
85
// XXX workaround should no longer be necessary...
86
((IntroPanel.IntroWizardPanel) panels[0]).initialize(wiz);
87         // Make sure list of steps is accurate.
88
String JavaDoc[] steps = createSteps ();
89         for (int i = 0; i < panels.length; i++) {
90             Component JavaDoc c = panels[i].getComponent ();
91             if (steps[i] == null) {
92                 // Default step name to component name of panel.
93
// Mainly useful for getting the name of the target
94
// chooser to appear in the list of steps.
95
steps[i] = c.getName ();
96             }
97             if (c instanceof JComponent JavaDoc) { // assume Swing components
98
JComponent JavaDoc jc = (JComponent JavaDoc) c;
99                 // Step #.
100
jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer JavaDoc (i)); // NOI18N
101
// Step name (actually the whole list for reference).
102
jc.putClientProperty ("WizardPanel_contentData", steps); // NOI18N
103
}
104         }
105     }
106
107     // --- WizardDescriptor.Iterator METHODS: ---
108

109     public String JavaDoc name () {
110         return NbBundle.getMessage (ShortcutIterator.class, "TITLE_x_of_y",
111             new Integer JavaDoc (index + 1), new Integer JavaDoc (panels.length));
112     }
113
114     boolean showing(String JavaDoc prop) {
115         Boolean JavaDoc s = (Boolean JavaDoc) wiz.getProperty (prop);
116         return (s == null) || s.booleanValue ();
117     }
118     private boolean showing (int index) throws NoSuchElementException JavaDoc {
119         switch (index) {
120         case 0:
121             return true;
122         case 1:
123             return showing(ShortcutWizard.PROP_SHOW_MENU);
124         case 2:
125             return showing(ShortcutWizard.PROP_SHOW_TOOL);
126         case 3:
127             return showing(ShortcutWizard.PROP_SHOW_KEYB);
128         case 4:
129             return showing(ShortcutWizard.PROP_SHOW_CUST);
130         default:
131             throw new NoSuchElementException JavaDoc ();
132         }
133     }
134     public boolean hasNext () {
135         for (int i = index + 1; i < panels.length; i++) {
136             if (showing (i)) {
137                 return true;
138             }
139         }
140         return false;
141     }
142     public boolean hasPrevious () {
143         return index > 0;
144     }
145     public void nextPanel() throws NoSuchElementException JavaDoc {
146         if (!hasNext()) {
147             throw new NoSuchElementException JavaDoc();
148         }
149         index++;
150         while (! showing (index)) index++;
151         if (index == 1) {
152             // User finished intro panel, list of panels may have changed:
153
fireChangeEvent ();
154         }
155     }
156     public void previousPanel() throws NoSuchElementException JavaDoc {
157         if (!hasPrevious()) {
158             throw new NoSuchElementException JavaDoc();
159         }
160         index--;
161         while (! showing (index)) index--;
162     }
163     public WizardDescriptor.Panel current () {
164         return panels[index];
165     }
166
167     private transient Set JavaDoc<ChangeListener JavaDoc> listeners = new HashSet JavaDoc<ChangeListener JavaDoc>(1);
168     public final void addChangeListener (ChangeListener JavaDoc l) {
169         synchronized (listeners) {
170             listeners.add (l);
171         }
172     }
173     public final void removeChangeListener (ChangeListener JavaDoc l) {
174         synchronized (listeners) {
175             listeners.remove (l);
176         }
177     }
178     protected final void fireChangeEvent () {
179         ChangeListener JavaDoc[] ls;
180         synchronized (listeners) {
181             ls = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
182         }
183         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc (this);
184         for (ChangeListener JavaDoc l : ls) {
185             l.stateChanged(ev);
186         }
187     }
188
189 }
190
Popular Tags