]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/ui/wizards/NewClassWizardPage.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / wizards / NewClassWizardPage.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2012 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.ui.wizards;
12
13import org.eclipse.swt.SWT;
14import org.eclipse.swt.layout.GridLayout;
15import org.eclipse.swt.widgets.Composite;
16
17import org.eclipse.core.runtime.CoreException;
18import org.eclipse.core.runtime.IProgressMonitor;
19import org.eclipse.core.runtime.IStatus;
20import org.eclipse.core.runtime.SubProgressMonitor;
21
22import org.eclipse.jface.dialogs.Dialog;
23import org.eclipse.jface.dialogs.IDialogSettings;
24import org.eclipse.jface.viewers.IStructuredSelection;
25
26import org.eclipse.ui.PlatformUI;
27
28import org.eclipse.jdt.core.IJavaElement;
29import org.eclipse.jdt.core.IType;
30import org.eclipse.jdt.core.Signature;
31
32import org.eclipse.jdt.ui.CodeGeneration;
33
34import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
35import org.eclipse.jdt.internal.ui.text.correction.proposals.NewCUUsingWizardProposal;
36import org.eclipse.jdt.internal.ui.wizards.NewClassCreationWizard;
37import org.eclipse.jdt.internal.ui.wizards.NewElementWizard;
38import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
39import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup;
40
41/**
42 * Wizard page to create a new class.
43 * <p>
44 * Note: This class is not intended to be subclassed, but clients can instantiate.
45 * To implement a different kind of a new class wizard page, extend <code>NewTypeWizardPage</code>.
46 * </p>
47 *
48 * @since 2.0
49 *
50 * @noextend This class is not intended to be subclassed by clients.
51 */
52public class NewClassWizardPage extends NewTypeWizardPage {
53
54 private final static String PAGE_NAME= "NewClassWizardPage"; //$NON-NLS-1$
55
56 private final static String SETTINGS_CREATEMAIN= "create_main"; //$NON-NLS-1$
57 private final static String SETTINGS_CREATECONSTR= "create_constructor"; //$NON-NLS-1$
58 private final static String SETTINGS_CREATEUNIMPLEMENTED= "create_unimplemented"; //$NON-NLS-1$
59
60 public SelectionButtonDialogFieldGroup fMethodStubsButtons;
61
62 /**
63 * Creates a new <code>NewClassWizardPage</code>
64 */
65 public NewClassWizardPage() {
66 super(true, PAGE_NAME);
67
68 setTitle(NewWizardMessages.NewClassWizardPage_title);
69 setDescription(NewWizardMessages.NewClassWizardPage_description);
70
71 String[] buttonNames3= new String[] {
72 NewWizardMessages.NewClassWizardPage_methods_main, NewWizardMessages.NewClassWizardPage_methods_constructors,
73 NewWizardMessages.NewClassWizardPage_methods_inherited
74 };
75 fMethodStubsButtons= new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames3, 1);
76 fMethodStubsButtons.setLabelText(NewWizardMessages.NewClassWizardPage_methods_label);
77 }
78
79 // -------- Initialization ---------
80
81 /**
82 * The wizard owning this page is responsible for calling this method with the
83 * current selection. The selection is used to initialize the fields of the wizard
84 * page.
85 *
86 * @param selection used to initialize the fields
87 */
88 public void init(IStructuredSelection selection) {
89 IJavaElement jelem= getInitialJavaElement(selection);
90 initContainerPage(jelem);
91 initTypePage(jelem);
92 doStatusUpdate();
93
94 boolean createMain= false;
95 boolean createConstructors= false;
96 boolean createUnimplemented= true;
97 IDialogSettings dialogSettings= getDialogSettings();
98 if (dialogSettings != null) {
99 IDialogSettings section= dialogSettings.getSection(PAGE_NAME);
100 if (section != null) {
101 createMain= section.getBoolean(SETTINGS_CREATEMAIN);
102 createConstructors= section.getBoolean(SETTINGS_CREATECONSTR);
103 createUnimplemented= section.getBoolean(SETTINGS_CREATEUNIMPLEMENTED);
104 }
105 }
106
107 setMethodStubSelection(createMain, createConstructors, createUnimplemented, true);
108 }
109
110 // ------ validation --------
111 private void doStatusUpdate() {
112 // status of all used components
113 IStatus[] status= new IStatus[] {
114 fContainerStatus,
115 isEnclosingTypeSelected() ? fEnclosingTypeStatus : fPackageStatus,
116 fTypeNameStatus,
117 fModifierStatus,
118 fSuperClassStatus,
119 fSuperInterfacesStatus
120 };
121
122 // the mode severe status will be displayed and the OK button enabled/disabled.
123 updateStatus(status);
124 }
125
126
127 /*
128 * @see NewContainerWizardPage#handleFieldChanged
129 */
130 @Override
131 protected void handleFieldChanged(String fieldName) {
132 super.handleFieldChanged(fieldName);
133
134 doStatusUpdate();
135 }
136
137
138 // ------ UI --------
139
140 /*
141 * @see WizardPage#createControl
142 */
143 public void createControl(Composite parent) {
144 initializeDialogUnits(parent);
145
146 Composite composite= new Composite(parent, SWT.NONE);
147 composite.setFont(parent.getFont());
148
149 int nColumns= 4;
150
151 GridLayout layout= new GridLayout();
152 layout.numColumns= nColumns;
153 composite.setLayout(layout);
154
155 // pick & choose the wanted UI components
156
157 createContainerControls(composite, nColumns);
158 createPackageControls(composite, nColumns);
159 createEnclosingTypeControls(composite, nColumns);
160
161 createSeparator(composite, nColumns);
162
163 createTypeNameControls(composite, nColumns);
164 createModifierControls(composite, nColumns);
165
166 createSuperClassControls(composite, nColumns);
167 createSuperInterfacesControls(composite, nColumns);
168
169 createMethodStubSelectionControls(composite, nColumns);
170
171 createCommentControls(composite, nColumns);
172 enableCommentControl(true);
173
174 setControl(composite);
175
176 Dialog.applyDialogFont(composite);
177 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_CLASS_WIZARD_PAGE);
178 }
179
180 /*
181 * @see WizardPage#becomesVisible
182 */
183 @Override
184 public void setVisible(boolean visible) {
185 super.setVisible(visible);
186 if (visible)
187 setFocus();
188 }
189
190 private void createMethodStubSelectionControls(Composite composite, int nColumns) {
191 fMethodStubsButtons.generated_1727347456463010400(composite, nColumns);
192 }
193
194 /**
195 * Returns the current selection state of the 'Create Main' checkbox.
196 *
197 * @return the selection state of the 'Create Main' checkbox
198 */
199 public boolean isCreateMain() {
200 return fMethodStubsButtons.isSelected(0);
201 }
202
203 /**
204 * Returns the current selection state of the 'Create Constructors' checkbox.
205 *
206 * @return the selection state of the 'Create Constructors' checkbox
207 */
208 public boolean isCreateConstructors() {
209 return fMethodStubsButtons.isSelected(1);
210 }
211
212 /**
213 * Returns the current selection state of the 'Create inherited abstract methods'
214 * checkbox.
215 *
216 * @return the selection state of the 'Create inherited abstract methods' checkbox
217 */
218 public boolean isCreateInherited() {
219 return fMethodStubsButtons.isSelected(2);
220 }
221
222 /**
223 * Sets the selection state of the method stub checkboxes.
224 *
225 * @param createMain initial selection state of the 'Create Main' checkbox.
226 * @param createConstructors initial selection state of the 'Create Constructors' checkbox.
227 * @param createInherited initial selection state of the 'Create inherited abstract methods' checkbox.
228 * @param canBeModified if <code>true</code> the method stub checkboxes can be changed by
229 * the user. If <code>false</code> the buttons are "read-only"
230 */
231 public void setMethodStubSelection(boolean createMain, boolean createConstructors, boolean createInherited, boolean canBeModified) {
232 fMethodStubsButtons.generated_8284357852518978517(createMain, createConstructors, createInherited, canBeModified);
233 }
234
235
236
237 // ---- creation ----------------
238
239 /*
240 * @see NewTypeWizardPage#createTypeMembers
241 */
242 @Override
243 protected void createTypeMembers(IType type, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
244 boolean doMain= isCreateMain();
245 boolean doConstr= isCreateConstructors();
246 boolean doInherited= isCreateInherited();
247 createInheritedMethods(type, doConstr, doInherited, imports, new SubProgressMonitor(monitor, 1));
248
249 if (doMain) {
250 StringBuffer buf= new StringBuffer();
251 final String lineDelim= "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$
252 String comment= CodeGeneration.getMethodComment(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", new String[] {"args"}, new String[0], Signature.createTypeSignature("void", true), null, lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
253 if (comment != null) {
254 buf.append(comment);
255 buf.append(lineDelim);
256 }
257 buf.append("public static void main("); //$NON-NLS-1$
258 buf.append(imports.addImport("java.lang.String")); //$NON-NLS-1$
259 buf.append("[] args) {"); //$NON-NLS-1$
260 buf.append(lineDelim);
261 final String content= CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", false, "", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$
262 if (content != null && content.length() != 0)
263 buf.append(content);
264 buf.append(lineDelim);
265 buf.append("}"); //$NON-NLS-1$
266 type.createMethod(buf.toString(), null, false, null);
267 }
268
269 IDialogSettings dialogSettings= getDialogSettings();
270 if (dialogSettings != null) {
271 IDialogSettings section= dialogSettings.getSection(PAGE_NAME);
272 if (section == null) {
273 section= dialogSettings.addNewSection(PAGE_NAME);
274 }
275 section.put(SETTINGS_CREATEMAIN, isCreateMain());
276 section.put(SETTINGS_CREATECONSTR, isCreateConstructors());
277 section.put(SETTINGS_CREATEUNIMPLEMENTED, isCreateInherited());
278 }
279
280 if (monitor != null) {
281 monitor.done();
282 }
283 }
284
285 public NewElementWizard generated_1485325614336726374(NewCUUsingWizardProposal newcuusingwizardproposal) {
286 newcuusingwizardproposal.configureWizardPage(this);
287 return new NewClassCreationWizard(this, true);
288 }
289
290 public void generated_3387906923916191674(NewClassCreationWizard newclasscreationwizard) {
291 setWizard(newclasscreationwizard);
292 init(newclasscreationwizard.getSelection());
293 }
294
295}