001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.pde.internal.ui.editor.schema;
011:
012: import org.eclipse.core.resources.IFolder;
013: import org.eclipse.core.resources.IProject;
014: import org.eclipse.jdt.core.IJavaProject;
015: import org.eclipse.jdt.core.IPackageFragment;
016: import org.eclipse.jdt.core.IPackageFragmentRoot;
017: import org.eclipse.jdt.core.JavaCore;
018: import org.eclipse.jdt.core.JavaModelException;
019: import org.eclipse.jdt.ui.wizards.NewClassWizardPage;
020: import org.eclipse.jdt.ui.wizards.NewInterfaceWizardPage;
021: import org.eclipse.jface.viewers.StructuredSelection;
022: import org.eclipse.pde.internal.ui.editor.plugin.JavaAttributeWizard;
023: import org.eclipse.pde.internal.ui.util.TextUtil;
024:
025: public class NewClassCreationWizard extends JavaAttributeWizard {
026:
027: private boolean fIsInterface;
028:
029: private IPackageFragment packageName = null;
030: private String className = null;
031: private IPackageFragmentRoot packageRoot = null;
032:
033: public NewClassCreationWizard(IProject project,
034: boolean isInterface, String value) {
035: super (project, null, null, null);
036: try {
037: initializeValues(project, value);
038: } catch (JavaModelException e) {
039: // Ignore
040: }
041: fIsInterface = isInterface;
042: }
043:
044: public void addPages() {
045: if (fIsInterface)
046: fMainPage = new NewInterfaceWizardPage();
047: else
048: fMainPage = new NewClassWizardPage();
049: addPage(fMainPage);
050: if (fIsInterface) {
051: ((NewInterfaceWizardPage) fMainPage)
052: .init(StructuredSelection.EMPTY);
053: if (className != null)
054: ((NewInterfaceWizardPage) fMainPage).setTypeName(
055: className, true);
056: if (packageRoot != null)
057: ((NewInterfaceWizardPage) fMainPage)
058: .setPackageFragmentRoot(packageRoot, true);
059: if (packageName != null)
060: ((NewInterfaceWizardPage) fMainPage)
061: .setPackageFragment(packageName, true);
062: } else {
063: ((NewClassWizardPage) fMainPage)
064: .init(StructuredSelection.EMPTY);
065: if (className != null)
066: ((NewClassWizardPage) fMainPage).setTypeName(className,
067: true);
068: if (packageRoot != null)
069: ((NewClassWizardPage) fMainPage)
070: .setPackageFragmentRoot(packageRoot, true);
071: if (packageName != null)
072: ((NewClassWizardPage) fMainPage).setPackageFragment(
073: packageName, true);
074: }
075: }
076:
077: private void initializeValues(IProject project, String value)
078: throws JavaModelException {
079: value = TextUtil.trimNonAlphaChars(value);
080: IJavaProject javaProject = JavaCore.create(project);
081: IPackageFragmentRoot srcEntryDft = null;
082: IPackageFragmentRoot[] roots = javaProject
083: .getPackageFragmentRoots();
084: for (int i = 0; i < roots.length; i++) {
085: if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
086: srcEntryDft = roots[i];
087: break;
088: }
089: }
090: if (srcEntryDft != null)
091: packageRoot = srcEntryDft;
092: else
093: packageRoot = javaProject
094: .getPackageFragmentRoot(javaProject.getResource());
095:
096: String packageNameString = null;
097: int index = value.lastIndexOf("."); //$NON-NLS-1$
098: if (index == -1) {
099: className = value;
100: } else {
101: className = value.substring(index + 1);
102: packageNameString = value.substring(0, index);
103: }
104: if (packageNameString != null && packageRoot != null) {
105: IFolder packageFolder = project
106: .getFolder(packageNameString);
107: packageName = packageRoot.getPackageFragment(packageFolder
108: .getProjectRelativePath().toOSString());
109: }
110: }
111: }
|