1 19 20 21 package org.netbeans.modules.i18n.wizard; 22 23 import java.awt.Component ; 24 import java.awt.Dialog ; 25 import java.awt.Dimension ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.beans.PropertyChangeEvent ; 29 import java.beans.PropertyChangeListener ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 import javax.swing.JButton ; 33 import javax.swing.JComponent ; 34 import javax.swing.JRootPane ; 35 import javax.swing.SwingUtilities ; 36 import org.openide.NotifyDescriptor; 37 import org.openide.util.NbBundle; 38 import org.openide.util.RequestProcessor; 39 import org.openide.util.WeakListeners; 40 import org.openide.WizardDescriptor; 41 import org.openide.DialogDisplayer; 42 import org.netbeans.api.project.Project; 43 import java.util.Map ; 44 import org.openide.awt.Mnemonics; 45 46 47 53 final class I18nWizardDescriptor extends WizardDescriptor { 54 55 56 public static final Dimension PREFERRED_DIMENSION = new Dimension (500, 300); 57 58 59 private final WizardDescriptor.Iterator panels; 60 61 62 private final Settings settings; 63 64 65 private final JButton nextButton = new JButton (); 66 67 private final JButton previousButton = new JButton (); 68 69 private final JButton finishButton = new JButton (); 70 71 private final JButton cancelButton = new JButton (); 72 73 75 private PropertyChangeListener rootListener; 76 77 78 private I18nWizardDescriptor(WizardDescriptor.Iterator panels, Settings settings) { 79 super(panels, settings); 80 81 Listener listener = new Listener (); 82 83 Mnemonics.setLocalizedText(nextButton, NbBundle.getMessage(I18nWizardDescriptor.class, "CTL_Next")); 85 nextButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(I18nWizardDescriptor.class).getString("ACSD_NEXT")); 86 Mnemonics.setLocalizedText(previousButton, NbBundle.getMessage(I18nWizardDescriptor.class, "CTL_Previous")); 87 previousButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(I18nWizardDescriptor.class).getString("ACSD_PREVIOUS")); 88 Mnemonics.setLocalizedText(finishButton, NbBundle.getMessage(I18nWizardDescriptor.class, "CTL_Finish")); 89 finishButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(I18nWizardDescriptor.class).getString("ACSD_FINISH")); 90 Mnemonics.setLocalizedText(cancelButton, NbBundle.getMessage(I18nWizardDescriptor.class, "CTL_Cancel")); 91 cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(I18nWizardDescriptor.class).getString("ACSD_CANCEL")); 92 93 nextButton.addActionListener(listener); 94 previousButton.addActionListener(listener); 95 finishButton.addActionListener(listener); 96 cancelButton.addActionListener(listener); 97 98 setOptions(new Object [] { previousButton, nextButton, finishButton, cancelButton }); 99 setClosingOptions(new Object [] { cancelButton }); 100 101 this.panels = panels; 102 this.settings = settings; 103 } 104 105 107 static WizardDescriptor createI18nWizardDescriptor(WizardDescriptor.Iterator panels, Settings settings) { 108 return new I18nWizardDescriptor(panels, settings); 109 } 110 111 112 protected synchronized void updateState() { 113 super.updateState(); 115 116 WizardDescriptor.Panel current = panels.current(); 118 119 boolean next = panels.hasNext (); 120 boolean prev = panels.hasPrevious (); 121 boolean valid = current.isValid(); 122 123 nextButton.setEnabled(next && valid); 124 previousButton.setEnabled(prev); 125 finishButton.setEnabled( 126 valid && (!next 127 || ((current instanceof FinishablePanel) 128 && ((FinishablePanel) current).isFinishPanel()))); 129 130 if(next) 131 setValue(nextButton); 132 else 133 setValue(finishButton); 134 135 setHelpCtx(current.getHelp()); 136 137 updateDefaultButton(); 138 } 139 140 141 private void updateDefaultButton() { 142 JRootPane root = getRootPane(); 143 144 if(root == null) 145 return; 146 147 final WizardDescriptor.Panel panel = panels.current(); 148 if ((panel instanceof WizardDescriptor.FinishablePanel) 149 && ((WizardDescriptor.FinishablePanel) panel).isFinishPanel()) { 150 root.setDefaultButton(finishButton); 151 } else { 152 root.setDefaultButton(nextButton); 153 } 154 } 155 156 157 159 private JRootPane getRootPane() { 160 JRootPane rootPane = null; 161 162 Component comp = panels.current().getComponent(); 163 if(comp instanceof JComponent ) 164 rootPane = ((JComponent )comp).getRootPane(); 165 166 if(rootPane != null && rootListener == null) 167 rootPane.addPropertyChangeListener(WeakListeners.propertyChange( 170 rootListener = new PropertyChangeListener () { 171 public void propertyChange(PropertyChangeEvent evt) { 172 if("defaultButton".equals(evt.getPropertyName())) { Object newValue = evt.getNewValue(); 174 if(newValue != nextButton && newValue != finishButton) { 175 RequestProcessor.getDefault().post(new Runnable () { 176 public void run() { 177 updateDefaultButton(); 178 } 179 }); 180 } 181 } 182 } 183 }, 184 rootPane 185 )); 186 187 return rootPane; 188 } 189 190 192 private class Listener extends Object implements ActionListener { 193 public void actionPerformed(ActionEvent evt) { 194 if(evt.getSource () == nextButton) { 195 196 final WizardDescriptor.Panel current = panels.current(); 197 198 if(current instanceof ProgressMonitor) { 199 RequestProcessor.getDefault().post(new ProgressThread((ProgressMonitor)current) { 201 public void handleAction() { 202 handleNextButton(); 203 } 204 }); 205 } else { 206 handleNextButton(); 207 } 208 } else if(evt.getSource () == previousButton) { 209 panels.previousPanel (); 210 updateState (); 211 } else if(evt.getSource () == finishButton) { 212 final WizardDescriptor.Panel current = panels.current(); 213 214 current.storeSettings(settings); 215 setValue(OK_OPTION); 216 217 if(current instanceof ProgressMonitor) { 218 RequestProcessor.getDefault().post(new ProgressThread((ProgressMonitor)current) { 220 public void handleAction() { 221 Dialog dialog = (Dialog )SwingUtilities.getAncestorOfClass(Dialog .class, current.getComponent()); 222 dialog.setVisible(false); 223 dialog.dispose(); 224 } 225 }); 226 } 227 } else if(evt.getSource () == cancelButton) { 228 panels.current().storeSettings(settings); 229 setValue(CANCEL_OPTION); 230 } 231 } 232 233 234 private void handleNextButton() { 235 236 Runnable performer = new Runnable () { 238 public void run() { 239 panels.nextPanel(); 240 241 try { 242 updateState (); 243 } catch(IllegalStateException ise) { 244 panels.previousPanel(); 245 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(ise.getMessage())); 246 updateState(); 247 } 248 249 WizardDescriptor.Panel current = panels.current(); 251 current.getComponent().requestFocus(); 252 } 253 }; 254 org.openide.util.Mutex.EVENT.writeAccess(performer); 255 } 256 } 258 259 260 private abstract class ProgressThread implements Runnable { 261 262 263 private ProgressMonitor progressMonitor; 264 265 266 267 public ProgressThread(ProgressMonitor progressMonitor) { 268 this.progressMonitor = progressMonitor; 269 } 270 271 272 273 public void run() { 274 try { 275 previousButton.setEnabled(false); 276 nextButton.setEnabled(false); 277 finishButton.setEnabled(false); 278 cancelButton.setEnabled(false); 279 280 progressMonitor.doLongTimeChanges(); 281 282 handleAction(); 283 } finally { 284 progressMonitor.reset(); 285 cancelButton.setEnabled(true); 286 } 287 } 288 289 290 public abstract void handleAction(); 291 } 293 295 public interface ProgressMonitor { 296 297 298 public void doLongTimeChanges(); 299 300 301 public void reset(); 302 } 304 305 311 public static abstract class Panel implements WizardDescriptor.Panel { 312 313 314 private Component component; 315 316 317 private ChangeListener changeListener; 318 319 320 321 private I18nWizardDescriptor.Settings settings = null; 322 323 325 public synchronized final Component getComponent() { 326 if(component == null) { 327 component = createComponent(); 328 } 329 330 return component; 331 } 332 333 334 protected abstract Component createComponent(); 335 336 338 public boolean isValid() { 339 return true; 340 } 341 342 343 public void readSettings(Object settings) { 344 this.settings = (I18nWizardDescriptor.Settings)settings; 345 } 346 347 348 public void storeSettings(Object settings) { 349 } 350 351 352 public void addChangeListener(ChangeListener listener) { 353 changeListener = listener; 354 } 355 356 357 public void removeChangeListener(ChangeListener listener) { 358 if(changeListener != null && changeListener == listener) 359 changeListener = null; 360 } 361 362 363 public final void fireStateChanged() { 364 if(changeListener != null) 365 changeListener.stateChanged(new ChangeEvent (this)); 366 } 367 368 public Project getProject() { 369 return settings.project; 370 } 371 372 public Map getMap() { 373 return settings.map; 374 } 375 376 377 } 379 public static class Settings { 380 public Settings(Map map, Project project) { 381 this.map = map; 382 this.project = project; 383 } 384 public Map map; 385 public Project project; 386 } 387 388 } 389 390 391 | Popular Tags |