Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
|
45 |
package org.netbeans.modules.java.source.queriesimpl; |
46 |
|
47 |
import com.sun.source.tree.ClassTree; |
48 |
import com.sun.source.tree.ExpressionTree; |
49 |
import com.sun.source.tree.Tree; |
50 |
import java.awt.Component; |
51 |
import java.awt.GridBagConstraints; |
52 |
import java.awt.GridBagLayout; |
53 |
import java.awt.Insets; |
54 |
import java.awt.event.FocusAdapter; |
55 |
import java.io.IOException; |
56 |
import java.util.NoSuchElementException; |
57 |
import java.util.ResourceBundle; |
58 |
import java.util.Set; |
59 |
import java.util.logging.Level; |
60 |
import java.util.logging.LogRecord; |
61 |
import java.util.logging.Logger; |
62 |
import javax.lang.model.element.TypeElement; |
63 |
import javax.swing.JComponent; |
64 |
import javax.swing.JLabel; |
65 |
import javax.swing.JTextField; |
66 |
import javax.swing.event.ChangeListener; |
67 |
import org.netbeans.api.java.source.CancellableTask; |
68 |
import org.netbeans.api.java.source.JavaSource; |
69 |
import org.netbeans.api.java.source.TreeMaker; |
70 |
import org.netbeans.api.java.source.TreeUtilities; |
71 |
import org.netbeans.api.java.source.WorkingCopy; |
72 |
import org.netbeans.modules.java.source.queries.spi.TemplateWizardProvider; |
73 |
import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates; |
74 |
import org.openide.WizardDescriptor; |
75 |
import org.openide.filesystems.FileObject; |
76 |
import org.openide.loaders.TemplateWizard; |
77 |
import org.openide.util.NbBundle; |
78 |
import org.openide.util.lookup.ServiceProvider; |
79 |
|
80 |
/** |
81 |
* Special template wizard iterator for BeanForm template - requires to |
82 |
* specify superclass additionally. |
83 |
* |
84 |
* @author Tomas Pavek, Jan Stola |
85 |
*/ |
86 |
|
87 |
public class TemplateWizardIterator implements WizardDescriptor.InstantiatingIterator { |
88 |
private transient WizardDescriptor wiz; |
89 |
private transient WizardDescriptor.Panel superclassPanel; |
90 |
private transient boolean superclassPanelCurrent; |
91 |
private transient WizardDescriptor.InstantiatingIterator delegateIterator; |
92 |
|
93 |
private boolean specifySuperclass; |
94 |
|
95 |
public static TemplateWizardIterator createForSuperclass() { |
96 |
return new TemplateWizardIterator(true); |
97 |
} |
98 |
|
99 |
public static TemplateWizardIterator create() { |
100 |
return new TemplateWizardIterator(false); |
101 |
} |
102 |
|
103 |
public TemplateWizardIterator(boolean specifySuperclass) { |
104 |
delegateIterator = JavaTemplates.createJavaTemplateIterator(); |
105 |
this.specifySuperclass = specifySuperclass; |
106 |
} |
107 |
|
108 |
@Override |
109 |
public void initialize(WizardDescriptor wizard) { |
110 |
wiz = wizard; |
111 |
delegateIterator.initialize(wizard); |
112 |
superclassPanelCurrent = false; |
113 |
if (superclassPanel == null && specifySuperclass) { |
114 |
superclassPanel = new SuperclassWizardPanel(); |
115 |
|
116 |
ResourceBundle bundle = NbBundle.getBundle(TemplateWizardIterator.class); |
117 |
JComponent comp = (JComponent)delegateIterator.current().getComponent(); |
118 |
String[] contentData = (String[])comp.getClientProperty(WizardDescriptor.PROP_CONTENT_DATA); // NOI18N |
119 |
String[] newContentData = new String[contentData.length+1]; |
120 |
System.arraycopy(contentData, 0, newContentData, 0, contentData.length); |
121 |
newContentData[contentData.length] = bundle.getString("CTL_SuperclassTitle"); // NOI18N |
122 |
comp.putClientProperty(WizardDescriptor.PROP_CONTENT_DATA, newContentData); // NOI18N |
123 |
} |
124 |
} |
125 |
|
126 |
@Override |
127 |
public void uninitialize(WizardDescriptor wizard) { |
128 |
delegateIterator.uninitialize(wizard); |
129 |
superclassPanel = null; |
130 |
} |
131 |
|
132 |
@Override |
133 |
public Set instantiate() throws IOException, IllegalArgumentException { |
134 |
Set set = delegateIterator.instantiate(); |
135 |
FileObject template = (FileObject) set.iterator().next(); |
136 |
if (wiz instanceof TemplateWizard) { |
137 |
Logger logger = Logger.getLogger("org.netbeans.ui.metrics.form"); // NOI18N |
138 |
LogRecord rec = new LogRecord(Level.INFO, "USG_FORM_CREATED"); // NOI18N |
139 |
rec.setLoggerName(logger.getName()); |
140 |
rec.setParameters(new Object[] { ((TemplateWizard)wiz).getTemplate().getName() }); |
141 |
logger.log(rec); |
142 |
} |
143 |
|
144 |
if (specifySuperclass) { |
145 |
final String className = template.getName(); |
146 |
final String superclassName = |
147 |
((SuperclassWizardPanel) superclassPanel).getSuperclassName(); |
148 |
JavaSource js = JavaSource.forFileObject(template); |
149 |
js.runModificationTask(new CancellableTask<WorkingCopy>() { |
150 |
@Override |
151 |
public void cancel() { |
152 |
} |
153 |
@Override |
154 |
public void run(WorkingCopy wcopy) throws Exception { |
155 |
wcopy.toPhase(JavaSource.Phase.RESOLVED); |
156 |
|
157 |
for (Tree t: wcopy.getCompilationUnit().getTypeDecls()) { |
158 |
if (TreeUtilities.CLASS_TREE_KINDS.contains(t.getKind()) && className.equals(((ClassTree) t).getSimpleName().toString())) { |
159 |
ClassTree orig = (ClassTree) t; |
160 |
TreeMaker maker = wcopy.getTreeMaker(); |
161 |
TypeElement superclassElm = wcopy.getElements().getTypeElement(superclassName); |
162 |
ExpressionTree extendsTree = superclassElm != null |
163 |
? maker.QualIdent(superclassElm) |
164 |
: maker.Identifier(superclassName); |
165 |
ClassTree copy = maker.Class( |
166 |
orig.getModifiers(), |
167 |
orig.getSimpleName(), |
168 |
orig.getTypeParameters(), |
169 |
extendsTree, |
170 |
orig.getImplementsClause(), |
171 |
orig.getMembers()); |
172 |
wcopy.rewrite(orig, copy); |
173 |
break; |
174 |
} |
175 |
} |
176 |
} |
177 |
}).commit(); |
178 |
} |
179 |
|
180 |
template.setAttribute("justCreatedByNewWizard", Boolean.TRUE); // NOI18N |
181 |
|
182 |
return set; |
183 |
} |
184 |
|
185 |
@Override |
186 |
public WizardDescriptor.Panel current() { |
187 |
return superclassPanelCurrent ? superclassPanel : delegateIterator.current(); |
188 |
} |
189 |
|
190 |
@Override |
191 |
public boolean hasNext() { |
192 |
return delegateIterator.hasNext() || (!superclassPanelCurrent && superclassPanel != null); |
193 |
} |
194 |
|
195 |
@Override |
196 |
public boolean hasPrevious() { |
197 |
return superclassPanelCurrent ? true : delegateIterator.hasPrevious(); |
198 |
} |
199 |
|
200 |
@Override |
201 |
public void nextPanel() { |
202 |
if (delegateIterator.hasNext()) { |
203 |
delegateIterator.nextPanel(); |
204 |
} else { |
205 |
if (superclassPanelCurrent || superclassPanel == null) { |
206 |
throw new NoSuchElementException(); |
207 |
} else { |
208 |
superclassPanelCurrent = true; |
209 |
} |
210 |
} |
211 |
} |
212 |
|
213 |
@Override |
214 |
public void previousPanel() { |
215 |
if (superclassPanelCurrent) { |
216 |
superclassPanelCurrent = false; |
217 |
} else { |
218 |
delegateIterator.previousPanel(); |
219 |
} |
220 |
} |
221 |
|
222 |
@Override |
223 |
public void addChangeListener(ChangeListener l) { |
224 |
delegateIterator.addChangeListener(l); |
225 |
} |
226 |
|
227 |
@Override |
228 |
public String name() { |
229 |
return superclassPanelCurrent ? "" : delegateIterator.name(); // NOI18N |
230 |
} |
231 |
|
232 |
@Override |
233 |
public void removeChangeListener(ChangeListener l) { |
234 |
delegateIterator.removeChangeListener(l); |
235 |
} |
236 |
|
237 |
// --------- |
238 |
|
239 |
static class SuperclassWizardPanel implements WizardDescriptor.FinishablePanel { |
240 |
|
241 |
private SuperclassPanel panelUI; |
242 |
|
243 |
String getSuperclassName() { |
244 |
String name = panelUI != null ? |
245 |
panelUI.superclassTextField.getText() : null; |
246 |
return name != null && !"".equals(name) ? name : "java.lang.Object"; // NOI18N |
247 |
} |
248 |
|
249 |
@Override |
250 |
public Component getComponent() { |
251 |
if (panelUI == null) |
252 |
panelUI = new SuperclassPanel(); |
253 |
return panelUI; |
254 |
} |
255 |
|
256 |
@Override |
257 |
public boolean isValid() { |
258 |
return true; |
259 |
} |
260 |
|
261 |
@Override |
262 |
public void readSettings(Object settings) { |
263 |
} |
264 |
|
265 |
@Override |
266 |
public void storeSettings(Object settings) { |
267 |
} |
268 |
|
269 |
@Override |
270 |
public void addChangeListener(ChangeListener l) { |
271 |
} |
272 |
|
273 |
@Override |
274 |
public void removeChangeListener(ChangeListener l) { |
275 |
} |
276 |
|
277 |
@Override |
278 |
public org.openide.util.HelpCtx getHelp () { |
279 |
return new org.openide.util.HelpCtx("gui.creatingforms"); // NOI18N |
280 |
} |
281 |
|
282 |
@Override |
283 |
public boolean isFinishPanel() { |
284 |
return true; |
285 |
} |
286 |
|
287 |
} |
288 |
|
289 |
// ------- |
290 |
|
291 |
static class SuperclassPanel extends javax.swing.JPanel { |
292 |
|
293 |
SuperclassPanel() { |
294 |
ResourceBundle bundle = NbBundle.getBundle(TemplateWizardIterator.class); |
295 |
setName(bundle.getString("CTL_SuperclassTitle")); // NOI18N |
296 |
putClientProperty(WizardDescriptor.PROP_CONTENT_SELECTED_INDEX, new Integer(1)); //NOI18N |
297 |
getAccessibleContext() |
298 |
.setAccessibleDescription(bundle.getString("ACSD_SuperclassPanel")); // NOI18N |
299 |
|
300 |
setLayout(new GridBagLayout()); |
301 |
setBorder(new javax.swing.border.EmptyBorder(8, 8, 8, 8)); |
302 |
|
303 |
label1 = new JLabel(); |
304 |
superclassTextField = new JTextField(); |
305 |
|
306 |
label1.setLabelFor(superclassTextField); |
307 |
label1.setText(bundle.getString("CTL_SuperclassName")); // NOI18N |
308 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
309 |
gridBagConstraints.gridx = 0; |
310 |
gridBagConstraints.gridy = 0; |
311 |
gridBagConstraints.anchor = GridBagConstraints.WEST; |
312 |
gridBagConstraints.insets = new Insets(0, 0, 0, 12); |
313 |
add(label1, gridBagConstraints); |
314 |
|
315 |
superclassTextField.setText("java.lang.Object"); // NOI18N |
316 |
superclassTextField.setToolTipText(bundle.getString("CTL_SuperclassName_Hint")); // NOI18N |
317 |
superclassTextField.getAccessibleContext() |
318 |
.setAccessibleDescription(bundle.getString("ACSD_SuperclassTextField")); // NOI18N |
319 |
superclassTextField.addFocusListener(new FocusAdapter() { |
320 |
@Override |
321 |
public void focusGained(java.awt.event.FocusEvent evt) { |
322 |
superclassTextField.selectAll(); |
323 |
} |
324 |
}); |
325 |
|
326 |
gridBagConstraints = new GridBagConstraints(); |
327 |
gridBagConstraints.gridx = 1; |
328 |
gridBagConstraints.gridy = 0; |
329 |
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER; |
330 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; |
331 |
gridBagConstraints.weightx = 1.0; |
332 |
gridBagConstraints.weighty = 1.0; |
333 |
add(superclassTextField, gridBagConstraints); |
334 |
} |
335 |
|
336 |
@Override |
337 |
public void addNotify() { |
338 |
super.addNotify(); |
339 |
superclassTextField.requestFocus(); |
340 |
} |
341 |
|
342 |
private JLabel label1; |
343 |
private JTextField superclassTextField; |
344 |
} |
345 |
} |