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