1 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 ; 26 import java.awt.GridBagConstraints ; 27 import java.awt.GridBagLayout ; 28 import java.awt.Insets ; 29 import java.awt.event.FocusAdapter ; 30 import java.io.IOException ; 31 import java.util.List ; 32 import java.util.NoSuchElementException ; 33 import java.util.ResourceBundle ; 34 import java.util.Set ; 35 import javax.lang.model.element.TypeElement; 36 import javax.swing.JComponent ; 37 import javax.swing.JLabel ; 38 import javax.swing.JTextField ; 39 import javax.swing.event.ChangeListener ; 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 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 bundle = NbBundle.getBundle(TemplateWizardIterator.class); 84 JComponent comp = (JComponent )delegateIterator.current().getComponent(); 85 String [] contentData = (String [])comp.getClientProperty("WizardPanel_contentData"); String [] newContentData = new String [contentData.length+1]; 87 System.arraycopy(contentData, 0, newContentData, 0, contentData.length); 88 newContentData[contentData.length] = bundle.getString("CTL_SuperclassTitle"); comp.putClientProperty("WizardPanel_contentData", newContentData); } 91 } 92 93 public void uninitialize(WizardDescriptor wizard) { 94 delegateIterator.uninitialize(wizard); 95 superclassPanel = null; 96 } 97 98 public Set instantiate() throws IOException , IllegalArgumentException { 99 Set set = delegateIterator.instantiate(); 100 FileObject template = (FileObject) set.iterator().next(); 101 102 if (specifySuperclass) { 103 final String className = template.getName(); 104 final String 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 { 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 <? 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); 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 (); 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 l) { 175 delegateIterator.addChangeListener(l); 176 } 177 178 public String name() { 179 return superclassPanelCurrent ? "" : delegateIterator.name(); } 181 182 public void removeChangeListener(ChangeListener l) { 183 delegateIterator.removeChangeListener(l); 184 } 185 186 188 static class SuperclassWizardPanel implements WizardDescriptor.FinishablePanel { 189 190 private SuperclassPanel panelUI; 191 192 String getSuperclassName() { 193 String name = panelUI != null ? 194 panelUI.superclassTextField.getText() : null; 195 return name != null && !"".equals(name) ? name : "java.lang.Object"; } 197 198 public Component 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 settings) { 209 } 210 211 public void storeSettings(Object settings) { 212 } 213 214 public void addChangeListener(ChangeListener l) { 215 } 216 217 public void removeChangeListener(ChangeListener l) { 218 } 219 220 public org.openide.util.HelpCtx getHelp () { 221 return new org.openide.util.HelpCtx("gui.creatingforms"); } 223 224 public boolean isFinishPanel() { 225 return true; 226 } 227 228 } 229 230 232 static class SuperclassPanel extends javax.swing.JPanel { 233 234 SuperclassPanel() { 235 ResourceBundle bundle = NbBundle.getBundle(TemplateWizardIterator.class); 236 setName(bundle.getString("CTL_SuperclassTitle")); putClientProperty("WizardPanel_contentSelectedIndex", new Integer (1)); getAccessibleContext() 239 .setAccessibleDescription(bundle.getString("ACSD_SuperclassPanel")); 241 setLayout(new GridBagLayout ()); 242 setBorder(new javax.swing.border.EmptyBorder (8, 8, 8, 8)); 243 244 label1 = new JLabel (); 245 superclassTextField = new JTextField (); 246 247 label1.setLabelFor(superclassTextField); 248 label1.setText(bundle.getString("CTL_SuperclassName")); GridBagConstraints gridBagConstraints = new GridBagConstraints (); 250 gridBagConstraints.gridx = 0; 251 gridBagConstraints.gridy = 0; 252 gridBagConstraints.anchor = GridBagConstraints.WEST; 253 gridBagConstraints.insets = new Insets (0, 0, 0, 12); 254 add(label1, gridBagConstraints); 255 256 superclassTextField.setText("java.lang.Object"); superclassTextField.setToolTipText(bundle.getString("CTL_SuperclassName_Hint")); superclassTextField.getAccessibleContext() 259 .setAccessibleDescription(bundle.getString("ACSD_SuperclassTextField")); superclassTextField.addFocusListener(new FocusAdapter () { 261 public void focusGained(java.awt.event.FocusEvent evt) { 262 superclassTextField.selectAll(); 263 } 264 }); 265 266 gridBagConstraints = new GridBagConstraints (); 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 label1; 282 private JTextField superclassTextField; 283 } 284 } 285 | Popular Tags |