001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.wizards;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IProgressMonitor;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.SubProgressMonitor;
016:
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.layout.GridLayout;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.Control;
021:
022: import org.eclipse.jface.dialogs.Dialog;
023: import org.eclipse.jface.dialogs.IDialogSettings;
024: import org.eclipse.jface.viewers.IStructuredSelection;
025:
026: import org.eclipse.ui.PlatformUI;
027:
028: import org.eclipse.jdt.core.IJavaElement;
029: import org.eclipse.jdt.core.IType;
030: import org.eclipse.jdt.core.Signature;
031:
032: import org.eclipse.jdt.ui.CodeGeneration;
033:
034: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
035: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
036: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
037: import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
038: import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogFieldGroup;
039:
040: /**
041: * Wizard page to create a new class.
042: * <p>
043: * Note: This class is not intended to be subclassed, but clients can instantiate.
044: * To implement a different kind of a new class wizard page, extend <code>NewTypeWizardPage</code>.
045: * </p>
046: *
047: * @since 2.0
048: */
049: public class NewClassWizardPage extends NewTypeWizardPage {
050:
051: private final static String PAGE_NAME = "NewClassWizardPage"; //$NON-NLS-1$
052:
053: private final static String SETTINGS_CREATEMAIN = "create_main"; //$NON-NLS-1$
054: private final static String SETTINGS_CREATECONSTR = "create_constructor"; //$NON-NLS-1$
055: private final static String SETTINGS_CREATEUNIMPLEMENTED = "create_unimplemented"; //$NON-NLS-1$
056:
057: private SelectionButtonDialogFieldGroup fMethodStubsButtons;
058:
059: /**
060: * Creates a new <code>NewClassWizardPage</code>
061: */
062: public NewClassWizardPage() {
063: super (true, PAGE_NAME);
064:
065: setTitle(NewWizardMessages.NewClassWizardPage_title);
066: setDescription(NewWizardMessages.NewClassWizardPage_description);
067:
068: String[] buttonNames3 = new String[] {
069: NewWizardMessages.NewClassWizardPage_methods_main,
070: NewWizardMessages.NewClassWizardPage_methods_constructors,
071: NewWizardMessages.NewClassWizardPage_methods_inherited };
072: fMethodStubsButtons = new SelectionButtonDialogFieldGroup(
073: SWT.CHECK, buttonNames3, 1);
074: fMethodStubsButtons
075: .setLabelText(NewWizardMessages.NewClassWizardPage_methods_label);
076: }
077:
078: // -------- Initialization ---------
079:
080: /**
081: * The wizard owning this page is responsible for calling this method with the
082: * current selection. The selection is used to initialize the fields of the wizard
083: * page.
084: *
085: * @param selection used to initialize the fields
086: */
087: public void init(IStructuredSelection selection) {
088: IJavaElement jelem = getInitialJavaElement(selection);
089: initContainerPage(jelem);
090: initTypePage(jelem);
091: doStatusUpdate();
092:
093: boolean createMain = false;
094: boolean createConstructors = false;
095: boolean createUnimplemented = true;
096: IDialogSettings dialogSettings = getDialogSettings();
097: if (dialogSettings != null) {
098: IDialogSettings section = dialogSettings
099: .getSection(PAGE_NAME);
100: if (section != null) {
101: createMain = section.getBoolean(SETTINGS_CREATEMAIN);
102: createConstructors = section
103: .getBoolean(SETTINGS_CREATECONSTR);
104: createUnimplemented = section
105: .getBoolean(SETTINGS_CREATEUNIMPLEMENTED);
106: }
107: }
108:
109: setMethodStubSelection(createMain, createConstructors,
110: createUnimplemented, true);
111: }
112:
113: // ------ validation --------
114: private void doStatusUpdate() {
115: // status of all used components
116: IStatus[] status = new IStatus[] {
117: fContainerStatus,
118: isEnclosingTypeSelected() ? fEnclosingTypeStatus
119: : fPackageStatus, fTypeNameStatus,
120: fModifierStatus, fSuperClassStatus,
121: fSuperInterfacesStatus };
122:
123: // the mode severe status will be displayed and the OK button enabled/disabled.
124: updateStatus(status);
125: }
126:
127: /*
128: * @see NewContainerWizardPage#handleFieldChanged
129: */
130: protected void handleFieldChanged(String fieldName) {
131: super .handleFieldChanged(fieldName);
132:
133: doStatusUpdate();
134: }
135:
136: // ------ UI --------
137:
138: /*
139: * @see WizardPage#createControl
140: */
141: public void createControl(Composite parent) {
142: initializeDialogUnits(parent);
143:
144: Composite composite = new Composite(parent, SWT.NONE);
145: composite.setFont(parent.getFont());
146:
147: int nColumns = 4;
148:
149: GridLayout layout = new GridLayout();
150: layout.numColumns = nColumns;
151: composite.setLayout(layout);
152:
153: // pick & choose the wanted UI components
154:
155: createContainerControls(composite, nColumns);
156: createPackageControls(composite, nColumns);
157: createEnclosingTypeControls(composite, nColumns);
158:
159: createSeparator(composite, nColumns);
160:
161: createTypeNameControls(composite, nColumns);
162: createModifierControls(composite, nColumns);
163:
164: createSuperClassControls(composite, nColumns);
165: createSuperInterfacesControls(composite, nColumns);
166:
167: createMethodStubSelectionControls(composite, nColumns);
168:
169: createCommentControls(composite, nColumns);
170: enableCommentControl(true);
171:
172: setControl(composite);
173:
174: Dialog.applyDialogFont(composite);
175: PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
176: IJavaHelpContextIds.NEW_CLASS_WIZARD_PAGE);
177: }
178:
179: /*
180: * @see WizardPage#becomesVisible
181: */
182: public void setVisible(boolean visible) {
183: super .setVisible(visible);
184: if (visible) {
185: setFocus();
186: } else {
187: IDialogSettings dialogSettings = getDialogSettings();
188: if (dialogSettings != null) {
189: IDialogSettings section = dialogSettings
190: .getSection(PAGE_NAME);
191: if (section == null) {
192: section = dialogSettings.addNewSection(PAGE_NAME);
193: }
194: section.put(SETTINGS_CREATEMAIN, isCreateMain());
195: section.put(SETTINGS_CREATECONSTR,
196: isCreateConstructors());
197: section.put(SETTINGS_CREATEUNIMPLEMENTED,
198: isCreateInherited());
199: }
200: }
201: }
202:
203: private void createMethodStubSelectionControls(Composite composite,
204: int nColumns) {
205: Control labelControl = fMethodStubsButtons
206: .getLabelControl(composite);
207: LayoutUtil.setHorizontalSpan(labelControl, nColumns);
208:
209: DialogField.createEmptySpace(composite);
210:
211: Control buttonGroup = fMethodStubsButtons
212: .getSelectionButtonsGroup(composite);
213: LayoutUtil.setHorizontalSpan(buttonGroup, nColumns - 1);
214: }
215:
216: /**
217: * Returns the current selection state of the 'Create Main' checkbox.
218: *
219: * @return the selection state of the 'Create Main' checkbox
220: */
221: public boolean isCreateMain() {
222: return fMethodStubsButtons.isSelected(0);
223: }
224:
225: /**
226: * Returns the current selection state of the 'Create Constructors' checkbox.
227: *
228: * @return the selection state of the 'Create Constructors' checkbox
229: */
230: public boolean isCreateConstructors() {
231: return fMethodStubsButtons.isSelected(1);
232: }
233:
234: /**
235: * Returns the current selection state of the 'Create inherited abstract methods'
236: * checkbox.
237: *
238: * @return the selection state of the 'Create inherited abstract methods' checkbox
239: */
240: public boolean isCreateInherited() {
241: return fMethodStubsButtons.isSelected(2);
242: }
243:
244: /**
245: * Sets the selection state of the method stub checkboxes.
246: *
247: * @param createMain initial selection state of the 'Create Main' checkbox.
248: * @param createConstructors initial selection state of the 'Create Constructors' checkbox.
249: * @param createInherited initial selection state of the 'Create inherited abstract methods' checkbox.
250: * @param canBeModified if <code>true</code> the method stub checkboxes can be changed by
251: * the user. If <code>false</code> the buttons are "read-only"
252: */
253: public void setMethodStubSelection(boolean createMain,
254: boolean createConstructors, boolean createInherited,
255: boolean canBeModified) {
256: fMethodStubsButtons.setSelection(0, createMain);
257: fMethodStubsButtons.setSelection(1, createConstructors);
258: fMethodStubsButtons.setSelection(2, createInherited);
259:
260: fMethodStubsButtons.setEnabled(canBeModified);
261: }
262:
263: // ---- creation ----------------
264:
265: /*
266: * @see NewTypeWizardPage#createTypeMembers
267: */
268: protected void createTypeMembers(IType type,
269: ImportsManager imports, IProgressMonitor monitor)
270: throws CoreException {
271: boolean doMain = isCreateMain();
272: boolean doConstr = isCreateConstructors();
273: boolean doInherited = isCreateInherited();
274: createInheritedMethods(type, doConstr, doInherited, imports,
275: new SubProgressMonitor(monitor, 1));
276:
277: if (doMain) {
278: StringBuffer buf = new StringBuffer();
279: final String lineDelim = "\n"; // OK, since content is formatted afterwards //$NON-NLS-1$
280: String comment = CodeGeneration
281: .getMethodComment(
282: type.getCompilationUnit(),
283: type.getTypeQualifiedName('.'),
284: "main", new String[] { "args" }, new String[0], Signature.createTypeSignature("void", true), null, lineDelim); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
285: if (comment != null) {
286: buf.append(comment);
287: buf.append(lineDelim);
288: }
289: buf.append("public static void main("); //$NON-NLS-1$
290: buf.append(imports.addImport("java.lang.String")); //$NON-NLS-1$
291: buf.append("[] args) {"); //$NON-NLS-1$
292: buf.append(lineDelim);
293: final String content = CodeGeneration.getMethodBodyContent(
294: type.getCompilationUnit(), type
295: .getTypeQualifiedName('.'),
296: "main", false, "", lineDelim); //$NON-NLS-1$ //$NON-NLS-2$
297: if (content != null && content.length() != 0)
298: buf.append(content);
299: buf.append(lineDelim);
300: buf.append("}"); //$NON-NLS-1$
301: type.createMethod(buf.toString(), null, false, null);
302: }
303:
304: if (monitor != null) {
305: monitor.done();
306: }
307: }
308:
309: }
|