1 11 package org.eclipse.jdt.ui.wizards; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.SubProgressMonitor; 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.layout.GridLayout; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Control; 22 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.dialogs.IDialogSettings; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 27 import org.eclipse.ui.PlatformUI; 28 29 import org.eclipse.jdt.core.IJavaElement; 30 import org.eclipse.jdt.core.IType; 31 import org.eclipse.jdt.core.Signature; 32 33 import org.eclipse.jdt.ui.CodeGeneration; 34 35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 36 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 37 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 38 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 39 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup; 40 41 50 public class NewClassWizardPage extends NewTypeWizardPage { 51 52 private final static String PAGE_NAME= "NewClassWizardPage"; 54 private final static String SETTINGS_CREATEMAIN= "create_main"; private final static String SETTINGS_CREATECONSTR= "create_constructor"; private final static String SETTINGS_CREATEUNIMPLEMENTED= "create_unimplemented"; 58 private SelectionButtonDialogFieldGroup fMethodStubsButtons; 59 60 63 public NewClassWizardPage() { 64 super(true, PAGE_NAME); 65 66 setTitle(NewWizardMessages.NewClassWizardPage_title); 67 setDescription(NewWizardMessages.NewClassWizardPage_description); 68 69 String [] buttonNames3= new String [] { 70 NewWizardMessages.NewClassWizardPage_methods_main, NewWizardMessages.NewClassWizardPage_methods_constructors, 71 NewWizardMessages.NewClassWizardPage_methods_inherited 72 }; 73 fMethodStubsButtons= new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames3, 1); 74 fMethodStubsButtons.setLabelText(NewWizardMessages.NewClassWizardPage_methods_label); 75 } 76 77 79 86 public void init(IStructuredSelection selection) { 87 IJavaElement jelem= getInitialJavaElement(selection); 88 initContainerPage(jelem); 89 initTypePage(jelem); 90 doStatusUpdate(); 91 92 boolean createMain= false; 93 boolean createConstructors= false; 94 boolean createUnimplemented= true; 95 IDialogSettings dialogSettings= getDialogSettings(); 96 if (dialogSettings != null) { 97 IDialogSettings section= dialogSettings.getSection(PAGE_NAME); 98 if (section != null) { 99 createMain= section.getBoolean(SETTINGS_CREATEMAIN); 100 createConstructors= section.getBoolean(SETTINGS_CREATECONSTR); 101 createUnimplemented= section.getBoolean(SETTINGS_CREATEUNIMPLEMENTED); 102 } 103 } 104 105 setMethodStubSelection(createMain, createConstructors, createUnimplemented, true); 106 } 107 108 private void doStatusUpdate() { 110 IStatus[] status= new IStatus[] { 112 fContainerStatus, 113 isEnclosingTypeSelected() ? fEnclosingTypeStatus : fPackageStatus, 114 fTypeNameStatus, 115 fModifierStatus, 116 fSuperClassStatus, 117 fSuperInterfacesStatus 118 }; 119 120 updateStatus(status); 122 } 123 124 125 128 protected void handleFieldChanged(String fieldName) { 129 super.handleFieldChanged(fieldName); 130 131 doStatusUpdate(); 132 } 133 134 135 137 140 public void createControl(Composite parent) { 141 initializeDialogUnits(parent); 142 143 Composite composite= new Composite(parent, SWT.NONE); 144 composite.setFont(parent.getFont()); 145 146 int nColumns= 4; 147 148 GridLayout layout= new GridLayout(); 149 layout.numColumns= nColumns; 150 composite.setLayout(layout); 151 152 154 createContainerControls(composite, nColumns); 155 createPackageControls(composite, nColumns); 156 createEnclosingTypeControls(composite, nColumns); 157 158 createSeparator(composite, nColumns); 159 160 createTypeNameControls(composite, nColumns); 161 createModifierControls(composite, nColumns); 162 163 createSuperClassControls(composite, nColumns); 164 createSuperInterfacesControls(composite, nColumns); 165 166 createMethodStubSelectionControls(composite, nColumns); 167 168 createCommentControls(composite, nColumns); 169 enableCommentControl(true); 170 171 setControl(composite); 172 173 Dialog.applyDialogFont(composite); 174 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_CLASS_WIZARD_PAGE); 175 } 176 177 180 public void setVisible(boolean visible) { 181 super.setVisible(visible); 182 if (visible) { 183 setFocus(); 184 } else { 185 IDialogSettings dialogSettings= getDialogSettings(); 186 if (dialogSettings != null) { 187 IDialogSettings section= dialogSettings.getSection(PAGE_NAME); 188 if (section == null) { 189 section= dialogSettings.addNewSection(PAGE_NAME); 190 } 191 section.put(SETTINGS_CREATEMAIN, isCreateMain()); 192 section.put(SETTINGS_CREATECONSTR, isCreateConstructors()); 193 section.put(SETTINGS_CREATEUNIMPLEMENTED, isCreateInherited()); 194 } 195 } 196 } 197 198 private void createMethodStubSelectionControls(Composite composite, int nColumns) { 199 Control labelControl= fMethodStubsButtons.getLabelControl(composite); 200 LayoutUtil.setHorizontalSpan(labelControl, nColumns); 201 202 DialogField.createEmptySpace(composite); 203 204 Control buttonGroup= fMethodStubsButtons.getSelectionButtonsGroup(composite); 205 LayoutUtil.setHorizontalSpan(buttonGroup, nColumns - 1); 206 } 207 208 213 public boolean isCreateMain() { 214 return fMethodStubsButtons.isSelected(0); 215 } 216 217 222 public boolean isCreateConstructors() { 223 return fMethodStubsButtons.isSelected(1); 224 } 225 226 232 public boolean isCreateInherited() { 233 return fMethodStubsButtons.isSelected(2); 234 } 235 236 245 public void setMethodStubSelection(boolean createMain, boolean createConstructors, boolean createInherited, boolean canBeModified) { 246 fMethodStubsButtons.setSelection(0, createMain); 247 fMethodStubsButtons.setSelection(1, createConstructors); 248 fMethodStubsButtons.setSelection(2, createInherited); 249 250 fMethodStubsButtons.setEnabled(canBeModified); 251 } 252 253 255 258 protected void createTypeMembers(IType type, ImportsManager imports, IProgressMonitor monitor) throws CoreException { 259 boolean doMain= isCreateMain(); 260 boolean doConstr= isCreateConstructors(); 261 boolean doInherited= isCreateInherited(); 262 createInheritedMethods(type, doConstr, doInherited, imports, new SubProgressMonitor(monitor, 1)); 263 264 if (doMain) { 265 StringBuffer buf= new StringBuffer (); 266 final String lineDelim= "\n"; String comment= CodeGeneration.getMethodComment(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", new String [] {"args"}, new String [0], Signature.createTypeSignature("void", true), null, lineDelim); if (comment != null) { 269 buf.append(comment); 270 buf.append(lineDelim); 271 } 272 buf.append("public static void main("); buf.append(imports.addImport("java.lang.String")); buf.append("[] args) {"); buf.append(lineDelim); 276 final String content= CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", false, "", lineDelim); if (content != null && content.length() != 0) 278 buf.append(content); 279 buf.append(lineDelim); 280 buf.append("}"); type.createMethod(buf.toString(), null, false, null); 282 } 283 284 if (monitor != null) { 285 monitor.done(); 286 } 287 } 288 289 } 290 | Popular Tags |