KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > wizard > TemplateWizardIterator


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.netbeans.modules.form.wizard;
21
22 import com.sun.source.tree.ClassTree;
23 import com.sun.source.tree.ExpressionTree;
24 import com.sun.source.tree.Tree;
25 import java.awt.Component JavaDoc;
26 import java.awt.GridBagConstraints JavaDoc;
27 import java.awt.GridBagLayout JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import java.awt.event.FocusAdapter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.NoSuchElementException JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34 import java.util.Set JavaDoc;
35 import javax.lang.model.element.TypeElement;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JTextField JavaDoc;
39 import javax.swing.event.ChangeListener JavaDoc;
40 import org.netbeans.api.java.source.CancellableTask;
41 import org.netbeans.api.java.source.JavaSource;
42 import org.netbeans.api.java.source.TreeMaker;
43 import org.netbeans.api.java.source.WorkingCopy;
44 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
45 import org.openide.WizardDescriptor;
46 import org.openide.filesystems.FileObject;
47 import org.openide.util.NbBundle;
48
49 /**
50  * Special template wizard iterator for BeanForm template - requires to
51  * specify superclass additionally.
52  *
53  * @author Tomas Pavek, Jan Stola
54  */

55
56 class TemplateWizardIterator implements WizardDescriptor.InstantiatingIterator {
57
58     private transient WizardDescriptor.Panel superclassPanel;
59     private transient boolean superclassPanelCurrent;
60     private transient WizardDescriptor.InstantiatingIterator delegateIterator;
61
62     private boolean specifySuperclass;
63
64     public static TemplateWizardIterator createForSuperclass() {
65         return new TemplateWizardIterator(true);
66     }
67
68     public static TemplateWizardIterator create() {
69         return new TemplateWizardIterator(false);
70     }
71
72     public TemplateWizardIterator(boolean specifySuperclass) {
73         delegateIterator = JavaTemplates.createJavaTemplateIterator();
74         this.specifySuperclass = specifySuperclass;
75     }
76
77     public void initialize(WizardDescriptor wizard) {
78         delegateIterator.initialize(wizard);
79         superclassPanelCurrent = false;
80         if (superclassPanel == null && specifySuperclass) {
81             superclassPanel = new SuperclassWizardPanel();
82             
83             ResourceBundle JavaDoc bundle = NbBundle.getBundle(TemplateWizardIterator.class);
84             JComponent JavaDoc comp = (JComponent JavaDoc)delegateIterator.current().getComponent();
85             String JavaDoc[] contentData = (String JavaDoc[])comp.getClientProperty("WizardPanel_contentData"); // NOI18N
86
String JavaDoc[] newContentData = new String JavaDoc[contentData.length+1];
87             System.arraycopy(contentData, 0, newContentData, 0, contentData.length);
88             newContentData[contentData.length] = bundle.getString("CTL_SuperclassTitle"); // NOI18N
89
comp.putClientProperty("WizardPanel_contentData", newContentData); // NOI18N
90
}
91     }
92
93     public void uninitialize(WizardDescriptor wizard) {
94         delegateIterator.uninitialize(wizard);
95         superclassPanel = null;
96     }
97
98     public Set JavaDoc instantiate() throws IOException JavaDoc, IllegalArgumentException JavaDoc {
99         Set JavaDoc set = delegateIterator.instantiate();
100         FileObject template = (FileObject) set.iterator().next();
101         
102         if (specifySuperclass) {
103             final String JavaDoc className = template.getName();
104             final String JavaDoc superclassName =
105                     ((SuperclassWizardPanel) superclassPanel).getSuperclassName();
106             JavaSource js = JavaSource.forFileObject(template);
107             js.runModificationTask(new CancellableTask<WorkingCopy>() {
108                 public void cancel() {
109                 }
110                 public void run(WorkingCopy wcopy) throws Exception JavaDoc {
111                     wcopy.toPhase(JavaSource.Phase.RESOLVED);
112
113                     for (Tree t: wcopy.getCompilationUnit().getTypeDecls()) {
114                         if (t.getKind() == Tree.Kind.CLASS && className.equals(((ClassTree) t).getSimpleName().toString())) {
115                             ClassTree orig = (ClassTree) t;
116                             TreeMaker maker = wcopy.getTreeMaker();
117                             TypeElement superclassElm = wcopy.getElements().getTypeElement(superclassName);
118                             ExpressionTree extendsTree = superclassElm != null
119                                     ? maker.QualIdent(superclassElm)
120                                     : maker.Identifier(superclassName);
121                             ClassTree copy = maker.Class(
122                                     orig.getModifiers(),
123                                     orig.getSimpleName(),
124                                     orig.getTypeParameters(),
125                                     extendsTree,
126                                     (List JavaDoc<? extends ExpressionTree>) orig.getImplementsClause(),
127                                     orig.getMembers()
128                                     );
129                             wcopy.rewrite(orig, copy);
130                             break;
131                         }
132                     }
133                 }
134             }).commit();
135         }
136         
137         template.setAttribute("justCreatedByNewWizard", Boolean.TRUE); // NOI18N
138

139         return set;
140     }
141
142     public WizardDescriptor.Panel current() {
143         return superclassPanelCurrent ? superclassPanel : delegateIterator.current();
144     }
145
146     public boolean hasNext() {
147         return !superclassPanelCurrent && superclassPanel != null;
148     }
149     
150     public boolean hasPrevious() {
151         return superclassPanelCurrent ? true : delegateIterator.hasPrevious();
152     }
153     
154     public void nextPanel() {
155         if (delegateIterator.hasNext()) {
156             delegateIterator.nextPanel();
157         } else {
158             if (superclassPanelCurrent || superclassPanel == null) {
159                 throw new NoSuchElementException JavaDoc();
160             } else {
161                 superclassPanelCurrent = true;
162             }
163         }
164     }
165     
166     public void previousPanel() {
167         if (superclassPanelCurrent) {
168             superclassPanelCurrent = false;
169         } else {
170             delegateIterator.previousPanel();
171         }
172     }
173     
174     public void addChangeListener(ChangeListener JavaDoc l) {
175         delegateIterator.addChangeListener(l);
176     }
177     
178     public String JavaDoc name() {
179         return superclassPanelCurrent ? "" : delegateIterator.name(); // NOI18N
180
}
181     
182     public void removeChangeListener(ChangeListener JavaDoc l) {
183         delegateIterator.removeChangeListener(l);
184     }
185
186     // ---------
187

188     static class SuperclassWizardPanel implements WizardDescriptor.FinishablePanel {
189
190         private SuperclassPanel panelUI;
191
192         String JavaDoc getSuperclassName() {
193             String JavaDoc name = panelUI != null ?
194                           panelUI.superclassTextField.getText() : null;
195             return name != null && !"".equals(name) ? name : "java.lang.Object"; // NOI18N
196
}
197
198         public Component JavaDoc getComponent() {
199             if (panelUI == null)
200                 panelUI = new SuperclassPanel();
201             return panelUI;
202         }
203
204         public boolean isValid() {
205             return true;
206         }
207
208         public void readSettings(Object JavaDoc settings) {
209         }
210
211         public void storeSettings(Object JavaDoc settings) {
212         }
213
214         public void addChangeListener(ChangeListener JavaDoc l) {
215         }
216
217         public void removeChangeListener(ChangeListener JavaDoc l) {
218         }
219
220         public org.openide.util.HelpCtx getHelp () {
221             return new org.openide.util.HelpCtx("gui.creatingforms"); // NOI18N
222
}
223         
224         public boolean isFinishPanel() {
225             return true;
226         }
227         
228     }
229
230     // -------
231

232     static class SuperclassPanel extends javax.swing.JPanel JavaDoc {
233
234         SuperclassPanel() {
235             ResourceBundle JavaDoc bundle = NbBundle.getBundle(TemplateWizardIterator.class);
236             setName(bundle.getString("CTL_SuperclassTitle")); // NOI18N
237
putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(1)); //NOI18N
238
getAccessibleContext()
239                 .setAccessibleDescription(bundle.getString("ACSD_SuperclassPanel")); // NOI18N
240

241             setLayout(new GridBagLayout JavaDoc());
242             setBorder(new javax.swing.border.EmptyBorder JavaDoc(8, 8, 8, 8));
243
244             label1 = new JLabel JavaDoc();
245             superclassTextField = new JTextField JavaDoc();
246
247             label1.setLabelFor(superclassTextField);
248             label1.setText(bundle.getString("CTL_SuperclassName")); // NOI18N
249
GridBagConstraints JavaDoc gridBagConstraints = new GridBagConstraints JavaDoc();
250             gridBagConstraints.gridx = 0;
251             gridBagConstraints.gridy = 0;
252             gridBagConstraints.anchor = GridBagConstraints.WEST;
253             gridBagConstraints.insets = new Insets JavaDoc(0, 0, 0, 12);
254             add(label1, gridBagConstraints);
255
256             superclassTextField.setText("java.lang.Object"); // NOI18N
257
superclassTextField.setToolTipText(bundle.getString("CTL_SuperclassName_Hint")); // NOI18N
258
superclassTextField.getAccessibleContext()
259                 .setAccessibleDescription(bundle.getString("ACSD_SuperclassTextField")); // NOI18N
260
superclassTextField.addFocusListener(new FocusAdapter JavaDoc() {
261                 public void focusGained(java.awt.event.FocusEvent JavaDoc evt) {
262                     superclassTextField.selectAll();
263                 }
264             });
265
266             gridBagConstraints = new GridBagConstraints JavaDoc();
267             gridBagConstraints.gridx = 1;
268             gridBagConstraints.gridy = 0;
269             gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
270             gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
271             gridBagConstraints.weightx = 1.0;
272             gridBagConstraints.weighty = 1.0;
273             add(superclassTextField, gridBagConstraints);
274         }
275
276         public void addNotify() {
277             super.addNotify();
278             superclassTextField.requestFocus();
279         }
280
281         private JLabel JavaDoc label1;
282         private JTextField JavaDoc superclassTextField;
283     }
284 }
285
Popular Tags