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.plugin;
011:
012: import java.lang.reflect.InvocationTargetException;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.jdt.core.IJavaElement;
020: import org.eclipse.jdt.core.IJavaProject;
021: import org.eclipse.jdt.core.JavaCore;
022: import org.eclipse.jdt.ui.JavaUI;
023: import org.eclipse.jdt.ui.wizards.NewTypeWizardPage;
024: import org.eclipse.jface.dialogs.IDialogSettings;
025: import org.eclipse.jface.operation.IRunnableWithProgress;
026: import org.eclipse.jface.wizard.Wizard;
027: import org.eclipse.pde.core.plugin.IPluginModelBase;
028: import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
029: import org.eclipse.pde.internal.ui.PDEPlugin;
030: import org.eclipse.pde.internal.ui.PDEPluginImages;
031: import org.eclipse.pde.internal.ui.PDEUIMessages;
032: import org.eclipse.ui.IWorkbenchPage;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.actions.WorkspaceModifyOperation;
035: import org.eclipse.ui.ide.IDE;
036: import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
037:
038: public class JavaAttributeWizard extends Wizard {
039:
040: private static String STORE_SECTION = "JavaAttributeWizard"; //$NON-NLS-1$
041:
042: private String fClassName;
043: private IProject fProject;
044: private ISchemaAttribute fAttInfo;
045: private IPluginModelBase fModel;
046: protected NewTypeWizardPage fMainPage;
047:
048: public JavaAttributeWizard(JavaAttributeValue value) {
049: this (value.getProject(), value.getModel(), value
050: .getAttributeInfo(), value.getClassName());
051: }
052:
053: public JavaAttributeWizard(IProject project,
054: IPluginModelBase model, ISchemaAttribute attInfo,
055: String className) {
056: fClassName = className;
057: fModel = model;
058: fProject = project;
059: fAttInfo = attInfo;
060: setDefaultPageImageDescriptor(PDEPluginImages.DESC_NEWPPRJ_WIZ);
061: IDialogSettings masterSettings = PDEPlugin.getDefault()
062: .getDialogSettings();
063: setDialogSettings(getSettingsSection(masterSettings));
064: setWindowTitle(PDEUIMessages.JavaAttributeWizard_wtitle);
065: setNeedsProgressMonitor(true);
066: }
067:
068: private IDialogSettings getSettingsSection(IDialogSettings master) {
069: IDialogSettings setting = master.getSection(STORE_SECTION);
070: if (setting == null)
071: setting = master.addNewSection(STORE_SECTION);
072: return setting;
073: }
074:
075: public void addPages() {
076: fMainPage = new JavaAttributeWizardPage(fProject, fModel,
077: fAttInfo, fClassName);
078: addPage(fMainPage);
079: ((JavaAttributeWizardPage) fMainPage).init();
080: }
081:
082: public boolean performFinish() {
083: IRunnableWithProgress op = new WorkspaceModifyOperation() {
084: protected void execute(IProgressMonitor monitor)
085: throws CoreException, InterruptedException {
086: fMainPage.createType(monitor);
087: }
088: };
089: try {
090: PlatformUI.getWorkbench().getProgressService().runInUI(
091: PDEPlugin.getActiveWorkbenchWindow(), op,
092: PDEPlugin.getWorkspace().getRoot());
093: IResource resource = fMainPage.getModifiedResource();
094: if (resource != null) {
095: selectAndReveal(resource);
096: if (fProject.hasNature(JavaCore.NATURE_ID)) {
097: IJavaProject jProject = JavaCore.create(fProject);
098: IJavaElement jElement = jProject
099: .findElement(resource
100: .getProjectRelativePath()
101: .removeFirstSegments(1));
102: if (jElement != null)
103: JavaUI.openInEditor(jElement);
104: } else if (resource instanceof IFile) {
105: IWorkbenchPage page = PDEPlugin.getActivePage();
106: IDE.openEditor(page, (IFile) resource, true);
107: }
108: }
109: } catch (InvocationTargetException e) {
110: PDEPlugin.logException(e);
111: } catch (InterruptedException e) {
112: PDEPlugin.logException(e);
113: } catch (CoreException e) {
114: PDEPlugin.logException(e);
115: }
116: return true;
117: }
118:
119: protected void selectAndReveal(IResource newResource) {
120: BasicNewResourceWizard.selectAndReveal(newResource, PDEPlugin
121: .getActiveWorkbenchWindow());
122: }
123:
124: public String getQualifiedName() {
125: if (fMainPage.getCreatedType() == null)
126: return null;
127: return fMainPage.getCreatedType().getFullyQualifiedName('$');
128: }
129:
130: public String getQualifiedNameWithArgs() {
131: String name = getQualifiedName();
132: if (name == null)
133: return null;
134: if (fMainPage instanceof JavaAttributeWizardPage) {
135: String classArgs = ((JavaAttributeWizardPage) fMainPage)
136: .getClassArgs();
137: if (classArgs != null && classArgs.length() > 0)
138: return name + ':' + classArgs;
139: }
140: return name;
141: }
142: }
|