001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form.wizard;
043:
044: import com.sun.source.tree.ClassTree;
045: import com.sun.source.tree.ExpressionTree;
046: import com.sun.source.tree.Tree;
047: import java.awt.Component;
048: import java.awt.GridBagConstraints;
049: import java.awt.GridBagLayout;
050: import java.awt.Insets;
051: import java.awt.event.FocusAdapter;
052: import java.io.IOException;
053: import java.util.NoSuchElementException;
054: import java.util.ResourceBundle;
055: import java.util.Set;
056: import javax.lang.model.element.TypeElement;
057: import javax.swing.JComponent;
058: import javax.swing.JLabel;
059: import javax.swing.JTextField;
060: import javax.swing.event.ChangeListener;
061: import org.netbeans.api.java.source.CancellableTask;
062: import org.netbeans.api.java.source.JavaSource;
063: import org.netbeans.api.java.source.TreeMaker;
064: import org.netbeans.api.java.source.WorkingCopy;
065: import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
066: import org.openide.WizardDescriptor;
067: import org.openide.filesystems.FileObject;
068: import org.openide.util.NbBundle;
069:
070: /**
071: * Special template wizard iterator for BeanForm template - requires to
072: * specify superclass additionally.
073: *
074: * @author Tomas Pavek, Jan Stola
075: */
076:
077: class TemplateWizardIterator implements
078: WizardDescriptor.InstantiatingIterator {
079:
080: private transient WizardDescriptor.Panel super classPanel;
081: private transient boolean super classPanelCurrent;
082: private transient WizardDescriptor.InstantiatingIterator delegateIterator;
083:
084: private boolean specifySuperclass;
085:
086: public static TemplateWizardIterator createForSuperclass() {
087: return new TemplateWizardIterator(true);
088: }
089:
090: public static TemplateWizardIterator create() {
091: return new TemplateWizardIterator(false);
092: }
093:
094: public TemplateWizardIterator(boolean specifySuperclass) {
095: delegateIterator = JavaTemplates.createJavaTemplateIterator();
096: this .specifySuperclass = specifySuperclass;
097: }
098:
099: public void initialize(WizardDescriptor wizard) {
100: delegateIterator.initialize(wizard);
101: super classPanelCurrent = false;
102: if (super classPanel == null && specifySuperclass) {
103: super classPanel = new SuperclassWizardPanel();
104:
105: ResourceBundle bundle = NbBundle
106: .getBundle(TemplateWizardIterator.class);
107: JComponent comp = (JComponent) delegateIterator.current()
108: .getComponent();
109: String[] contentData = (String[]) comp
110: .getClientProperty("WizardPanel_contentData"); // NOI18N
111: String[] newContentData = new String[contentData.length + 1];
112: System.arraycopy(contentData, 0, newContentData, 0,
113: contentData.length);
114: newContentData[contentData.length] = bundle
115: .getString("CTL_SuperclassTitle"); // NOI18N
116: comp.putClientProperty("WizardPanel_contentData",
117: newContentData); // NOI18N
118: }
119: }
120:
121: public void uninitialize(WizardDescriptor wizard) {
122: delegateIterator.uninitialize(wizard);
123: super classPanel = null;
124: }
125:
126: public Set instantiate() throws IOException,
127: IllegalArgumentException {
128: Set set = delegateIterator.instantiate();
129: FileObject template = (FileObject) set.iterator().next();
130:
131: if (specifySuperclass) {
132: final String className = template.getName();
133: final String super className = ((SuperclassWizardPanel) super classPanel)
134: .getSuperclassName();
135: JavaSource js = JavaSource.forFileObject(template);
136: js.runModificationTask(new CancellableTask<WorkingCopy>() {
137: public void cancel() {
138: }
139:
140: public void run(WorkingCopy wcopy) throws Exception {
141: wcopy.toPhase(JavaSource.Phase.RESOLVED);
142:
143: for (Tree t : wcopy.getCompilationUnit()
144: .getTypeDecls()) {
145: if (t.getKind() == Tree.Kind.CLASS
146: && className.equals(((ClassTree) t)
147: .getSimpleName().toString())) {
148: ClassTree orig = (ClassTree) t;
149: TreeMaker maker = wcopy.getTreeMaker();
150: TypeElement super classElm = wcopy
151: .getElements().getTypeElement(
152: super className);
153: ExpressionTree extendsTree = super classElm != null ? maker
154: .QualIdent(super classElm)
155: : maker.Identifier(super className);
156: ClassTree copy = maker.Class(orig
157: .getModifiers(), orig
158: .getSimpleName(), orig
159: .getTypeParameters(), extendsTree,
160: orig.getImplementsClause(), orig
161: .getMembers());
162: wcopy.rewrite(orig, copy);
163: break;
164: }
165: }
166: }
167: }).commit();
168: }
169:
170: template.setAttribute("justCreatedByNewWizard", Boolean.TRUE); // NOI18N
171:
172: return set;
173: }
174:
175: public WizardDescriptor.Panel current() {
176: return super classPanelCurrent ? super classPanel
177: : delegateIterator.current();
178: }
179:
180: public boolean hasNext() {
181: return !super classPanelCurrent && super classPanel != null;
182: }
183:
184: public boolean hasPrevious() {
185: return super classPanelCurrent ? true : delegateIterator
186: .hasPrevious();
187: }
188:
189: public void nextPanel() {
190: if (delegateIterator.hasNext()) {
191: delegateIterator.nextPanel();
192: } else {
193: if (super classPanelCurrent || super classPanel == null) {
194: throw new NoSuchElementException();
195: } else {
196: super classPanelCurrent = true;
197: }
198: }
199: }
200:
201: public void previousPanel() {
202: if (super classPanelCurrent) {
203: super classPanelCurrent = false;
204: } else {
205: delegateIterator.previousPanel();
206: }
207: }
208:
209: public void addChangeListener(ChangeListener l) {
210: delegateIterator.addChangeListener(l);
211: }
212:
213: public String name() {
214: return super classPanelCurrent ? "" : delegateIterator.name(); // NOI18N
215: }
216:
217: public void removeChangeListener(ChangeListener l) {
218: delegateIterator.removeChangeListener(l);
219: }
220:
221: // ---------
222:
223: static class SuperclassWizardPanel implements
224: WizardDescriptor.FinishablePanel {
225:
226: private SuperclassPanel panelUI;
227:
228: String getSuperclassName() {
229: String name = panelUI != null ? panelUI.super classTextField
230: .getText() : null;
231: return name != null && !"".equals(name) ? name
232: : "java.lang.Object"; // NOI18N
233: }
234:
235: public Component getComponent() {
236: if (panelUI == null)
237: panelUI = new SuperclassPanel();
238: return panelUI;
239: }
240:
241: public boolean isValid() {
242: return true;
243: }
244:
245: public void readSettings(Object settings) {
246: }
247:
248: public void storeSettings(Object settings) {
249: }
250:
251: public void addChangeListener(ChangeListener l) {
252: }
253:
254: public void removeChangeListener(ChangeListener l) {
255: }
256:
257: public org.openide.util.HelpCtx getHelp() {
258: return new org.openide.util.HelpCtx("gui.creatingforms"); // NOI18N
259: }
260:
261: public boolean isFinishPanel() {
262: return true;
263: }
264:
265: }
266:
267: // -------
268:
269: static class SuperclassPanel extends javax.swing.JPanel {
270:
271: SuperclassPanel() {
272: ResourceBundle bundle = NbBundle
273: .getBundle(TemplateWizardIterator.class);
274: setName(bundle.getString("CTL_SuperclassTitle")); // NOI18N
275: putClientProperty("WizardPanel_contentSelectedIndex",
276: new Integer(1)); //NOI18N
277: getAccessibleContext().setAccessibleDescription(
278: bundle.getString("ACSD_SuperclassPanel")); // NOI18N
279:
280: setLayout(new GridBagLayout());
281: setBorder(new javax.swing.border.EmptyBorder(8, 8, 8, 8));
282:
283: label1 = new JLabel();
284: super classTextField = new JTextField();
285:
286: label1.setLabelFor(super classTextField);
287: label1.setText(bundle.getString("CTL_SuperclassName")); // NOI18N
288: GridBagConstraints gridBagConstraints = new GridBagConstraints();
289: gridBagConstraints.gridx = 0;
290: gridBagConstraints.gridy = 0;
291: gridBagConstraints.anchor = GridBagConstraints.WEST;
292: gridBagConstraints.insets = new Insets(0, 0, 0, 12);
293: add(label1, gridBagConstraints);
294:
295: super classTextField.setText("java.lang.Object"); // NOI18N
296: super classTextField.setToolTipText(bundle
297: .getString("CTL_SuperclassName_Hint")); // NOI18N
298: super classTextField
299: .getAccessibleContext()
300: .setAccessibleDescription(
301: bundle
302: .getString("ACSD_SuperclassTextField")); // NOI18N
303: super classTextField.addFocusListener(new FocusAdapter() {
304: @Override
305: public void focusGained(java.awt.event.FocusEvent evt) {
306: super classTextField.selectAll();
307: }
308: });
309:
310: gridBagConstraints = new GridBagConstraints();
311: gridBagConstraints.gridx = 1;
312: gridBagConstraints.gridy = 0;
313: gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
314: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
315: gridBagConstraints.weightx = 1.0;
316: gridBagConstraints.weighty = 1.0;
317: add(super classTextField, gridBagConstraints);
318: }
319:
320: @Override
321: public void addNotify() {
322: super .addNotify();
323: super classTextField.requestFocus();
324: }
325:
326: private JLabel label1;
327: private JTextField superclassTextField;
328: }
329: }
|