KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > wizard > I18nWizardDescriptor


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.i18n.wizard;
22
23 import java.awt.Component JavaDoc;
24 import java.awt.Dialog JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JRootPane JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
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 JavaDoc;
44 import org.openide.awt.Mnemonics;
45
46
47 /**
48  * Wizard descriptor of i18n wizard and i18n test wizard.
49  *
50  * @author Peter Zavadsky
51  * @see org.openide.WizardDescriptor
52  */

53 final class I18nWizardDescriptor extends WizardDescriptor {
54
55     /** Preferred size for panels in i18n wizard. */
56     public static final Dimension JavaDoc PREFERRED_DIMENSION = new Dimension JavaDoc(500, 300);
57     
58     /** Hack. In super it's private. */
59     private final WizardDescriptor.Iterator panels;
60
61     /** Hack. In super it's private. */
62     private final Settings settings;
63     
64     /** Next button. */
65     private final JButton JavaDoc nextButton = new JButton JavaDoc();
66     /** Previous button. */
67     private final JButton JavaDoc previousButton = new JButton JavaDoc();
68     /** Finish button. */
69     private final JButton JavaDoc finishButton = new JButton JavaDoc();
70     /** Cancel button. */
71     private final JButton JavaDoc cancelButton = new JButton JavaDoc();
72
73     /** Hack. Listener on root pane. In case not our button was set as default
74      * (the one from superclass) our one is set as default. */

75     private PropertyChangeListener JavaDoc rootListener;
76
77     /** Creates new I18nWizardDescriptor */
78     private I18nWizardDescriptor(WizardDescriptor.Iterator panels, Settings settings) {
79         super(panels, settings);
80         
81         Listener JavaDoc listener = new Listener JavaDoc();
82
83         // Button init.
84
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 JavaDoc[] { previousButton, nextButton, finishButton, cancelButton });
99         setClosingOptions(new Object JavaDoc[] { cancelButton });
100
101         this.panels = panels;
102         this.settings = settings;
103     }
104
105     /** Creates I18N wizard descriptor.
106      * @return <code>I18nWizardDescriptor</code> instance. */

107     static WizardDescriptor createI18nWizardDescriptor(WizardDescriptor.Iterator panels, Settings settings) {
108         return new I18nWizardDescriptor(panels, settings);
109     }
110     
111     /** Overrides superclass method. */
112     protected synchronized void updateState() {
113         // Do superclass typical job.
114
super.updateState();
115
116         // And do the same for our buttons.
117
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     /** Updates default button. */
141     private void updateDefaultButton() {
142         JRootPane JavaDoc 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     /** Gets root pane. It's retrieved from current panel if possible.
158      * @return root pane or null of not available */

159     private JRootPane JavaDoc getRootPane() {
160         JRootPane JavaDoc rootPane = null;
161         
162         Component JavaDoc comp = panels.current().getComponent();
163         if(comp instanceof JComponent JavaDoc)
164             rootPane = ((JComponent JavaDoc)comp).getRootPane();
165         
166         if(rootPane != null && rootListener == null)
167             // Set listener on root for cases some needless button
168
// would like to become default one (the ones from superclass).
169
rootPane.addPropertyChangeListener(WeakListeners.propertyChange(
170                 rootListener = new PropertyChangeListener JavaDoc() {
171                     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
172                         if("defaultButton".equals(evt.getPropertyName())) { // NOI18N
173
Object JavaDoc newValue = evt.getNewValue();
174                             if(newValue != nextButton && newValue != finishButton) {
175                                 RequestProcessor.getDefault().post(new Runnable JavaDoc() {
176                                     public void run() {
177                                         updateDefaultButton();
178                                     }
179                                 });
180                             }
181                         }
182                     }
183                 },
184                 rootPane
185             ));
186         
187         return rootPane;
188     }
189     
190     /** Listener to changes in the iterator and panels.
191      * Hack, it's private in super. */

192     private class Listener extends Object JavaDoc implements ActionListener JavaDoc {
193         public void actionPerformed(ActionEvent JavaDoc evt) {
194             if(evt.getSource () == nextButton) {
195                 
196                 final WizardDescriptor.Panel current = panels.current();
197                 
198                 if(current instanceof ProgressMonitor) {
199                     // Do the search job first.
200
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                     // Do the search job first.
219
RequestProcessor.getDefault().post(new ProgressThread((ProgressMonitor)current) {
220                         public void handleAction() {
221                             Dialog JavaDoc dialog = (Dialog JavaDoc)SwingUtilities.getAncestorOfClass(Dialog JavaDoc.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         /** Helper method. It's actually next button event handler. */
234         private void handleNextButton() {
235
236             // #40531 workaround
237
Runnable JavaDoc performer = new Runnable JavaDoc() {
238                 public void run() {
239                     panels.nextPanel();
240                     
241                     try {
242                         updateState ();
243                     } catch(IllegalStateException JavaDoc ise) {
244                         panels.previousPanel();
245                         DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(ise.getMessage()));
246                         updateState();
247                     }
248                     
249                     // focus the current panel
250
WizardDescriptor.Panel current = panels.current();
251                     current.getComponent().requestFocus();
252                 }
253             };
254             org.openide.util.Mutex.EVENT.writeAccess(performer);
255         }
256     } // End of inner class Listener;
257

258     
259     /** Class used to handle <code>ProgressMonitor</code> inseparate thread. */
260     private abstract class ProgressThread implements Runnable JavaDoc {
261         
262         /** <code>ProgressMonitor</code> to handle. */
263         private ProgressMonitor progressMonitor;
264         
265         
266         /** Constructor. */
267         public ProgressThread(ProgressMonitor progressMonitor) {
268             this.progressMonitor = progressMonitor;
269         }
270         
271         
272         /** Implements <code>Runnable</code> interface. */
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         /** Method which provides additional handling. */
290         public abstract void handleAction();
291     } // End of inner ProgressThread.
292

293     /** Interface which indicates for descriptor that the panel is provides long time changes and shows
294      * progress monitoring. */

295     public interface ProgressMonitor {
296         
297         /** Provides long time changes. */
298         public void doLongTimeChanges();
299         
300         /** Reset after finish of the changes. Call after previous method. */
301         public void reset();
302     } // End of interface.
303

304     
305     /**
306      * Kind of abstract "adapter" implementing <code>WizardDescriptor.Panel</code>
307      * interface. Used by i18n wizard.
308      *
309      * @see org.openide.WizardDescriptor.Panel
310      */

311     public static abstract class Panel implements WizardDescriptor.Panel {
312
313         /** Reference to panel. */
314         private Component JavaDoc component;
315
316         /** Keeps only one listener. It's fine since WizardDescriptor registers always the same listener. */
317         private ChangeListener JavaDoc changeListener;
318
319
320         /** initialized in read settings **/
321         private I18nWizardDescriptor.Settings settings = null;
322
323         /** Gets component to display. Implements <code>WizardDescriptor.Panel</code> interface method.
324          * @return this instance */

325         public synchronized final Component JavaDoc getComponent() {
326             if(component == null) {
327                 component = createComponent();
328             }
329
330             return component;
331         }
332
333         /** Creates component. */
334         protected abstract Component JavaDoc createComponent();
335
336         /** Indicates if panel is valid. Implements <code>WizardDescriptor.Panel</code> interface method.
337          * @return true */

338         public boolean isValid() {
339             return true;
340         }
341
342         /** Reads settings at the start when the panel comes to play. Implements <code>WizardDescriptor.Panel</code> interface method. */
343         public void readSettings(Object JavaDoc settings) {
344       this.settings = (I18nWizardDescriptor.Settings)settings;
345         }
346
347         /** Stores settings at the end of panel show. Implements <code>WizardDescriptor.Panel</code> interface method. */
348         public void storeSettings(Object JavaDoc settings) {
349         }
350
351         /** Implements <code>WizardDescriptor.Panel</code> interface method. */
352         public void addChangeListener(ChangeListener JavaDoc listener) {
353             changeListener = listener;
354         }
355
356         /** Implements <code>WizardDescriptor.Panel</code> interface method. */
357         public void removeChangeListener(ChangeListener JavaDoc listener) {
358             if(changeListener != null && changeListener == listener)
359                 changeListener = null;
360         }
361
362         /** Fires state changed event. Helper method. */
363         public final void fireStateChanged() {
364             if(changeListener != null)
365                 changeListener.stateChanged(new ChangeEvent JavaDoc(this));
366         }
367
368         public Project getProject() {
369       return settings.project;
370     }
371
372         public Map JavaDoc getMap() {
373       return settings.map;
374     }
375  
376                                 
377     } // End of nested class Panel.
378

379   public static class Settings {
380     public Settings(Map JavaDoc map, Project project) {
381       this.map = map;
382       this.project = project;
383     }
384     public Map JavaDoc map;
385     public Project project;
386   }
387     
388 }
389
390
391
Popular Tags