Changes:
[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 kind of 
32  * 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   private final static String SETTINGS_CREATECONSTR = "create_constructor"; //$NON-NLS-1$
43   private final static String SETTINGS_CREATEUNIMPLEMENTED = "create_unimplemented"; //$NON-NLS-1$
44
45   private SelectionButtonDialogFieldGroup fMethodStubsButtons;
46
47   /**
48    * Creates a new <code>NewClassWizardPage</code>
49    */
50   public NewClassWizardPage() {
51     super(true, PAGE_NAME);
52
53     setTitle(NewWizardMessages.getString("NewClassWizardPage.title")); //$NON-NLS-1$
54     setDescription(NewWizardMessages.getString("NewClassWizardPage.description")); //$NON-NLS-1$
55
56     String[] buttonNames3 = new String[] { NewWizardMessages.getString("NewClassWizardPage.methods.main"), NewWizardMessages.getString("NewClassWizardPage.methods.constructors"), //$NON-NLS-1$ //$NON-NLS-2$
57       NewWizardMessages.getString("NewClassWizardPage.methods.inherited") //$NON-NLS-1$
58     };
59     fMethodStubsButtons = new SelectionButtonDialogFieldGroup(SWT.CHECK, buttonNames3, 1);
60     fMethodStubsButtons.setLabelText(NewWizardMessages.getString("NewClassWizardPage.methods.label")); //$NON-NLS-1$
61   }
62
63   // -------- Initialization ---------
64
65   /**
66    * The wizard owning this page is responsible for calling this method with the
67    * current selection. The selection is used to initialize the fields of the wizard 
68    * page.
69    * 
70    * @param selection used to initialize the fields
71    */
72   public void init(IStructuredSelection selection) {
73     IJavaElement jelem = getInitialJavaElement(selection);
74     initContainerPage(jelem);
75     initTypePage(jelem);
76     doStatusUpdate();
77
78     boolean createMain = false;
79     boolean createConstructors = false;
80     boolean createUnimplemented = true;
81     IDialogSettings section = getDialogSettings().getSection(PAGE_NAME);
82     if (section != null) {
83       createMain = section.getBoolean(SETTINGS_CREATEMAIN);
84       createConstructors = section.getBoolean(SETTINGS_CREATECONSTR);
85       createUnimplemented = section.getBoolean(SETTINGS_CREATEUNIMPLEMENTED);
86     }
87
88     setMethodStubSelection(createMain, createConstructors, createUnimplemented, true);
89   }
90
91   // ------ validation --------
92   private void doStatusUpdate() {
93     // status of all used components
94     IStatus[] status =
95       new IStatus[] {
96         fContainerStatus,
97         isEnclosingTypeSelected() ? fEnclosingTypeStatus : fPackageStatus,
98         fTypeNameStatus,
99         fModifierStatus,
100       //                        fSuperClassStatus,
101       //                        fSuperInterfacesStatus
102     };
103
104     // the mode severe status will be displayed and the ok button enabled/disabled.
105     updateStatus(status);
106   }
107
108   /*
109    * @see NewContainerWizardPage#handleFieldChanged
110    */
111   protected void handleFieldChanged(String fieldName) {
112     super.handleFieldChanged(fieldName);
113
114     doStatusUpdate();
115   }
116
117   // ------ ui --------
118
119   /*
120    * @see WizardPage#createControl
121    */
122   public void createControl(Composite parent) {
123     initializeDialogUnits(parent);
124
125     Composite composite = new Composite(parent, SWT.NONE);
126
127     int nColumns = 4;
128
129     GridLayout layout = new GridLayout();
130     layout.numColumns = nColumns;
131     composite.setLayout(layout);
132
133     // pick & choose the wanted UI components
134
135     createContainerControls(composite, nColumns);
136     createPackageControls(composite, nColumns);
137     createEnclosingTypeControls(composite, nColumns);
138
139     createSeparator(composite, nColumns);
140
141     createTypeNameControls(composite, nColumns);
142     createModifierControls(composite, nColumns);
143
144     createSuperClassControls(composite, nColumns);
145     createSuperInterfacesControls(composite, nColumns);
146
147     createMethodStubSelectionControls(composite, nColumns);
148
149     setControl(composite);
150
151     Dialog.applyDialogFont(composite);
152     //          WorkbenchHelp.setHelp(composite, IJavaHelpContextIds.NEW_CLASS_WIZARD_PAGE);    
153   }
154
155   /*
156    * @see WizardPage#becomesVisible
157    */
158   public void setVisible(boolean visible) {
159     super.setVisible(visible);
160     if (visible) {
161       setFocus();
162     }
163   }
164
165   private void createMethodStubSelectionControls(Composite composite, int nColumns) {
166     Control labelControl = fMethodStubsButtons.getLabelControl(composite);
167     LayoutUtil.setHorizontalSpan(labelControl, nColumns);
168
169     DialogField.createEmptySpace(composite);
170
171     Control buttonGroup = fMethodStubsButtons.getSelectionButtonsGroup(composite);
172     LayoutUtil.setHorizontalSpan(buttonGroup, nColumns - 1);
173   }
174
175   /**
176    * Returns the current selection state of the 'Create Main' checkbox.
177    * 
178    * @return the selection state of the 'Create Main' checkbox
179    */
180   public boolean isCreateMain() {
181     return fMethodStubsButtons.isSelected(0);
182   }
183
184   /**
185    * Returns the current selection state of the 'Create Constructors' checkbox.
186    * 
187    * @return the selection state of the 'Create Constructors' checkbox
188    */
189   public boolean isCreateConstructors() {
190     return fMethodStubsButtons.isSelected(1);
191   }
192
193   /**
194    * Returns the current selection state of the 'Create inherited abstract methods' 
195    * checkbox.
196    * 
197    * @return the selection state of the 'Create inherited abstract methods' checkbox
198    */
199   public boolean isCreateInherited() {
200     return fMethodStubsButtons.isSelected(2);
201   }
202
203   /**
204    * Sets the selection state of the method stub checkboxes.
205    * 
206    * @param createMain initial selection state of the 'Create Main' checkbox.
207    * @param createConstructors initial selection state of the 'Create Constructors' checkbox.
208    * @param createInherited initial selection state of the 'Create inherited abstract methods' checkbox.
209    * @param canBeModified if <code>true</code> the method stub checkboxes can be changed by 
210    * the user. If <code>false</code> the buttons are "read-only"
211    */
212   public void setMethodStubSelection(
213     boolean createMain,
214     boolean createConstructors,
215     boolean createInherited,
216     boolean canBeModified) {
217     fMethodStubsButtons.setSelection(0, createMain);
218     fMethodStubsButtons.setSelection(1, createConstructors);
219     fMethodStubsButtons.setSelection(2, createInherited);
220
221     fMethodStubsButtons.setEnabled(canBeModified);
222   }
223
224   // ---- creation ----------------
225
226   /*
227    * @see NewTypeWizardPage#createTypeMembers
228    */
229   //    protected void createTypeMembers(IType type, ImportsManager imports, IProgressMonitor monitor) throws CoreException {
230   //            boolean doMain= isCreateMain();
231   //            boolean doConstr= isCreateConstructors();
232   //            boolean doInherited= isCreateInherited();
233   //            createInheritedMethods(type, doConstr, doInherited, imports, new SubProgressMonitor(monitor, 1));
234   //
235   //            if (doMain) {
236   //                    StringBuffer buf= new StringBuffer();
237   //                    buf.append("public static void main("); //$NON-NLS-1$
238   //                    buf.append(imports.addImport("java.lang.String")); //$NON-NLS-1$
239   //                    buf.append("[] args) {}"); //$NON-NLS-1$
240   //                    type.createMethod(buf.toString(), null, false, null);
241   //            }
242   //            
243   //            IDialogSettings section= getDialogSettings().getSection(PAGE_NAME);
244   //            if (section == null) {
245   //                    section= getDialogSettings().addNewSection(PAGE_NAME);
246   //            }
247   //            section.put(SETTINGS_CREATEMAIN, doMain);
248   //            section.put(SETTINGS_CREATECONSTR, doConstr);
249   //            section.put(SETTINGS_CREATEUNIMPLEMENTED, doInherited);
250   //            
251   //            if (monitor != null) {
252   //                    monitor.done();
253   //            }       
254   //    }
255
256 }