KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > wizards > NewClassWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

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 /**
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 public class NewClassWizardPage extends NewTypeWizardPage {
51     
52     private final static String JavaDoc PAGE_NAME= "NewClassWizardPage"; //$NON-NLS-1$
53

54     private final static String JavaDoc SETTINGS_CREATEMAIN= "create_main"; //$NON-NLS-1$
55
private final static String JavaDoc SETTINGS_CREATECONSTR= "create_constructor"; //$NON-NLS-1$
56
private final static String JavaDoc SETTINGS_CREATEUNIMPLEMENTED= "create_unimplemented"; //$NON-NLS-1$
57

58     private SelectionButtonDialogFieldGroup fMethodStubsButtons;
59     
60     /**
61      * Creates a new <code>NewClassWizardPage</code>
62      */

63     public NewClassWizardPage() {
64         super(true, PAGE_NAME);
65         
66         setTitle(NewWizardMessages.NewClassWizardPage_title);
67         setDescription(NewWizardMessages.NewClassWizardPage_description);
68         
69         String JavaDoc[] buttonNames3= new String JavaDoc[] {
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     // -------- Initialization ---------
78

79     /**
80      * The wizard owning this page is responsible for calling this method with the
81      * current selection. The selection is used to initialize the fields of the wizard
82      * page.
83      *
84      * @param selection used to initialize the fields
85      */

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     // ------ validation --------
109
private void doStatusUpdate() {
110         // status of all used components
111
IStatus[] status= new IStatus[] {
112             fContainerStatus,
113             isEnclosingTypeSelected() ? fEnclosingTypeStatus : fPackageStatus,
114             fTypeNameStatus,
115             fModifierStatus,
116             fSuperClassStatus,
117             fSuperInterfacesStatus
118         };
119         
120         // the mode severe status will be displayed and the OK button enabled/disabled.
121
updateStatus(status);
122     }
123     
124     
125     /*
126      * @see NewContainerWizardPage#handleFieldChanged
127      */

128     protected void handleFieldChanged(String JavaDoc fieldName) {
129         super.handleFieldChanged(fieldName);
130         
131         doStatusUpdate();
132     }
133     
134     
135     // ------ UI --------
136

137     /*
138      * @see WizardPage#createControl
139      */

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         // pick & choose the wanted UI components
153

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     /*
178      * @see WizardPage#becomesVisible
179      */

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     /**
209      * Returns the current selection state of the 'Create Main' checkbox.
210      *
211      * @return the selection state of the 'Create Main' checkbox
212      */

213     public boolean isCreateMain() {
214         return fMethodStubsButtons.isSelected(0);
215     }
216
217     /**
218      * Returns the current selection state of the 'Create Constructors' checkbox.
219      *
220      * @return the selection state of the 'Create Constructors' checkbox
221      */

222     public boolean isCreateConstructors() {
223         return fMethodStubsButtons.isSelected(1);
224     }
225     
226     /**
227      * Returns the current selection state of the 'Create inherited abstract methods'
228      * checkbox.
229      *
230      * @return the selection state of the 'Create inherited abstract methods' checkbox
231      */

232     public boolean isCreateInherited() {
233         return fMethodStubsButtons.isSelected(2);
234     }
235
236     /**
237      * Sets the selection state of the method stub checkboxes.
238      *
239      * @param createMain initial selection state of the 'Create Main' checkbox.
240      * @param createConstructors initial selection state of the 'Create Constructors' checkbox.
241      * @param createInherited initial selection state of the 'Create inherited abstract methods' checkbox.
242      * @param canBeModified if <code>true</code> the method stub checkboxes can be changed by
243      * the user. If <code>false</code> the buttons are "read-only"
244      */

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     // ---- creation ----------------
254

255     /*
256      * @see NewTypeWizardPage#createTypeMembers
257      */

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 JavaDoc buf= new StringBuffer JavaDoc();
266             final String JavaDoc lineDelim= "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$
267
String JavaDoc comment= CodeGeneration.getMethodComment(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", new String JavaDoc[] {"args"}, new String JavaDoc[0], Signature.createTypeSignature("void", true), null, lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
268
if (comment != null) {
269                 buf.append(comment);
270                 buf.append(lineDelim);
271             }
272             buf.append("public static void main("); //$NON-NLS-1$
273
buf.append(imports.addImport("java.lang.String")); //$NON-NLS-1$
274
buf.append("[] args) {"); //$NON-NLS-1$
275
buf.append(lineDelim);
276             final String JavaDoc content= CodeGeneration.getMethodBodyContent(type.getCompilationUnit(), type.getTypeQualifiedName('.'), "main", false, "", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$
277
if (content != null && content.length() != 0)
278                 buf.append(content);
279             buf.append(lineDelim);
280             buf.append("}"); //$NON-NLS-1$
281
type.createMethod(buf.toString(), null, false, null);
282         }
283         
284         if (monitor != null) {
285             monitor.done();
286         }
287     }
288     
289 }
290
Popular Tags