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.jdt.internal.ui.wizards.buildpaths.newsourcepage;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IPath;
016:
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.resources.IResource;
019:
020: import org.eclipse.swt.widgets.Shell;
021:
022: import org.eclipse.jface.operation.IRunnableContext;
023: import org.eclipse.jface.resource.JFaceResources;
024: import org.eclipse.jface.viewers.IStructuredSelection;
025: import org.eclipse.jface.viewers.StructuredSelection;
026: import org.eclipse.jface.window.Window;
027: import org.eclipse.jface.wizard.WizardDialog;
028:
029: import org.eclipse.ui.IWorkbenchSite;
030: import org.eclipse.ui.PlatformUI;
031: import org.eclipse.ui.part.ISetSelectionTarget;
032:
033: import org.eclipse.jdt.core.IClasspathEntry;
034: import org.eclipse.jdt.core.IJavaProject;
035:
036: import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
037:
038: import org.eclipse.jdt.ui.PreferenceConstants;
039:
040: import org.eclipse.jdt.internal.ui.JavaPluginImages;
041: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
042: import org.eclipse.jdt.internal.ui.util.PixelConverter;
043: import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
044: import org.eclipse.jdt.internal.ui.wizards.buildpaths.AddSourceFolderWizard;
045: import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
046:
047: //SelectedElements iff enabled: IJavaProject && size == 1
048: public class CreateLinkedSourceFolderAction extends
049: BuildpathModifierAction {
050:
051: public CreateLinkedSourceFolderAction(IWorkbenchSite site) {
052: this (site, null, PlatformUI.getWorkbench().getProgressService());
053: }
054:
055: public CreateLinkedSourceFolderAction(IRunnableContext context,
056: ISetSelectionTarget selectionTarget) {
057: this (null, selectionTarget, context);
058: }
059:
060: private CreateLinkedSourceFolderAction(IWorkbenchSite site,
061: ISetSelectionTarget selectionTarget,
062: IRunnableContext context) {
063: super (site, selectionTarget,
064: BuildpathModifierAction.CREATE_LINK);
065:
066: setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Link_label);
067: setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Link_tooltip);
068: setImageDescriptor(JavaPluginImages.DESC_ELCL_ADD_LINKED_SOURCE_TO_BUILDPATH);
069: setDescription(NewWizardMessages.PackageExplorerActionGroup_FormText_createLinkedFolder);
070: }
071:
072: /**
073: * {@inheritDoc}
074: */
075: public String getDetailedDescription() {
076: return NewWizardMessages.PackageExplorerActionGroup_FormText_createLinkedFolder;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.jface.action.Action#run()
081: */
082: public void run() {
083: Shell shell = getShell();
084:
085: try {
086: IJavaProject javaProject = (IJavaProject) getSelectedElements()
087: .get(0);
088:
089: CPListElement newEntrie = new CPListElement(javaProject,
090: IClasspathEntry.CPE_SOURCE);
091: CPListElement[] existing = CPListElement
092: .createFromExisting(javaProject);
093: boolean isProjectSrcFolder = CPListElement
094: .isProjectSourceFolder(existing, javaProject);
095:
096: AddSourceFolderWizard wizard = new AddSourceFolderWizard(
097: existing, newEntrie,
098: getOutputLocation(javaProject), true, false, false,
099: isProjectSrcFolder, isProjectSrcFolder);
100: wizard.init(PlatformUI.getWorkbench(),
101: new StructuredSelection(javaProject));
102:
103: WizardDialog dialog = new WizardDialog(shell, wizard);
104: PixelConverter converter = new PixelConverter(
105: JFaceResources.getDialogFont());
106: dialog.setMinimumPageSize(converter
107: .convertWidthInCharsToPixels(70), converter
108: .convertHeightInCharsToPixels(20));
109: dialog.create();
110: int res = dialog.open();
111: if (res == Window.OK) {
112:
113: BuildpathDelta delta = new BuildpathDelta(
114: getToolTipText());
115:
116: ArrayList newEntries = wizard.getExistingEntries();
117: delta.setNewEntries((CPListElement[]) newEntries
118: .toArray(new CPListElement[newEntries.size()]));
119:
120: IResource resource = wizard.getCreatedElement()
121: .getCorrespondingResource();
122: delta.addCreatedResource(resource);
123:
124: delta.setDefaultOutputLocation(wizard
125: .getOutputLocation());
126:
127: informListeners(delta);
128:
129: selectAndReveal(new StructuredSelection(wizard
130: .getCreatedElement()));
131: }
132:
133: notifyResult(res == Window.OK);
134: } catch (CoreException e) {
135: String title = NewWizardMessages.AbstractOpenWizardAction_createerror_title;
136: String message = NewWizardMessages.AbstractOpenWizardAction_createerror_message;
137: ExceptionHandler.handle(e, shell, title, message);
138: }
139: }
140:
141: private IPath getOutputLocation(IJavaProject javaProject) {
142: try {
143: return javaProject.getOutputLocation();
144: } catch (CoreException e) {
145: IProject project = javaProject.getProject();
146: IPath projPath = project.getFullPath();
147: return projPath.append(PreferenceConstants
148: .getPreferenceStore().getString(
149: PreferenceConstants.SRCBIN_BINNAME));
150: }
151: }
152:
153: protected boolean canHandle(IStructuredSelection selection) {
154: if (selection.size() != 1)
155: return false;
156:
157: if (!(selection.getFirstElement() instanceof IJavaProject))
158: return false;
159:
160: return true;
161: }
162: }
|