1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.util.Set ; 24 import javax.swing.event.*; 25 import org.netbeans.api.progress.ProgressHandle; 26 27 import org.openide.WizardDescriptor; 28 29 34 class TemplateWizardIterImpl extends Object 35 implements WizardDescriptor.Iterator, ChangeListener { 36 37 38 private TemplateWizard.Iterator iterator; 39 40 41 private ChangeListener iteratorListener; 42 43 44 private boolean showingPanel = false; 45 private boolean newIteratorInstalled = false; 46 47 48 private javax.swing.event.EventListenerList listenerList = null; 49 50 52 private TemplateWizard wizardInstance; 53 54 55 private boolean iteratorInitialized = false; 56 57 60 private WizardDescriptor.Panel<WizardDescriptor> firstPanel () { 61 return wizardInstance.templateChooser(); 62 } 63 64 66 public void first () { 67 showingPanel = true; 68 fireStateChanged (); 69 } 70 71 73 public void setIterator (TemplateWizard.Iterator it, boolean notify) { 74 if ((this.iterator != null) && (iteratorInitialized)) { 75 this.iterator.removeChangeListener(iteratorListener); 76 this.iterator.uninitialize(wizardInstance); 77 } 78 it.initialize(wizardInstance); 79 iteratorListener = new ChangeListener() { 80 public void stateChanged(ChangeEvent e) { 81 TemplateWizardIterImpl.this.stateChanged(e); 82 } 83 }; 84 it.addChangeListener(iteratorListener); 85 iteratorInitialized = true; 86 87 iterator = it; 88 if (notify) { 89 if (showingPanel) { 91 newIteratorInstalled = true; 92 } 93 showingPanel = false; 94 fireStateChanged (); 95 } 96 } 97 98 100 public TemplateWizard.Iterator getIterator () { 101 if (iterator == null) { 102 setIterator (wizardInstance.defaultIterator (), false); 104 } 105 if (!iteratorInitialized) { 106 if (iterator != null) { 107 iterator.initialize(wizardInstance); 108 iteratorInitialized = true; 109 } 110 } 111 return iterator; 112 } 113 114 117 public WizardDescriptor.Panel<WizardDescriptor> current() { 118 return showingPanel ? firstPanel () : getIterator ().current (); 119 } 120 121 122 125 public String name() { 126 return showingPanel ? "" : getIterator ().name (); } 128 129 132 public boolean hasNext() { 133 return showingPanel || getIterator ().hasNext(); 134 } 135 136 139 public boolean hasPrevious() { 140 return !showingPanel; 141 } 142 143 147 public void nextPanel() { 148 if (showingPanel || newIteratorInstalled) { 149 showingPanel = false; 150 newIteratorInstalled = false; 151 } else { 152 getIterator ().nextPanel (); 153 } 154 } 155 156 160 public void previousPanel() { 161 if (getIterator ().hasPrevious ()) { 162 getIterator ().previousPanel (); 163 } else { 164 showingPanel = true; 165 newIteratorInstalled = false; 166 } 167 } 168 169 170 public void stateChanged(final javax.swing.event.ChangeEvent p1) { 171 fireStateChanged (); 172 } 173 174 177 public synchronized void addChangeListener(javax.swing.event.ChangeListener listener) { 178 if (listenerList == null ) { 179 listenerList = new javax.swing.event.EventListenerList (); 180 } 181 listenerList.add (javax.swing.event.ChangeListener .class, listener); 182 } 183 186 public synchronized void removeChangeListener( 187 javax.swing.event.ChangeListener listener 188 ) { 189 if (listenerList != null) { 190 listenerList.remove (javax.swing.event.ChangeListener .class, listener); 191 } 192 } 193 194 public void initialize (WizardDescriptor wiz) { 195 if (!(wiz instanceof TemplateWizard)) { 196 throw new IllegalArgumentException ("WizardDescriptor must be instance of TemplateWizard, but is " + wiz); } 198 this.wizardInstance = (TemplateWizard)wiz; 199 if (wizardInstance.getTemplate () == null) { 200 showingPanel = true; 201 } else { 202 newIteratorInstalled = false; 203 showingPanel = false; 204 } 205 TemplateWizard.Iterator it = iterator; 206 if ((it != null)&&(!iteratorInitialized)) { 207 it.initialize(wizardInstance); 208 iteratorInitialized = true; 209 } 210 } 211 212 public void uninitialize() { 213 if (iterator != null && wizardInstance != null) { 214 iterator.uninitialize(wizardInstance); 215 iteratorInitialized = false; 216 } 217 showingPanel = true; 218 } 219 220 public void uninitialize (WizardDescriptor wiz) { 221 uninitialize (); 222 } 223 224 public Set <DataObject> instantiate () throws IOException { 225 assert wizardInstance != null : "wizardInstance cannot be null when instantiate() called."; return wizardInstance.instantiateNewObjects (null); 227 } 228 229 public Set <DataObject> instantiate (ProgressHandle handle) throws IOException { 230 assert wizardInstance != null : "wizardInstance cannot be null when instantiate() called."; return wizardInstance.instantiateNewObjects (handle); 232 } 233 234 238 private void fireStateChanged() { 239 if (listenerList == null) 240 return; 241 242 javax.swing.event.ChangeEvent e = null; 243 Object [] listeners = listenerList.getListenerList (); 244 for (int i = listeners.length-2; i>=0; i-=2) { 245 if (listeners[i]==javax.swing.event.ChangeListener .class) { 246 if (e == null) 247 e = new javax.swing.event.ChangeEvent (this); 248 ((javax.swing.event.ChangeListener )listeners[i+1]).stateChanged (e); 249 } 250 } 251 } 252 253 } 254 255 | Popular Tags |