KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > newsourcepage > CreateLinkedSourceFolderAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.operation.IRunnableContext;
24 import org.eclipse.jface.resource.JFaceResources;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.jface.wizard.WizardDialog;
29
30 import org.eclipse.ui.IWorkbenchSite;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.part.ISetSelectionTarget;
33
34 import org.eclipse.jdt.core.IClasspathEntry;
35 import org.eclipse.jdt.core.IJavaProject;
36
37 import org.eclipse.jdt.internal.corext.buildpath.BuildpathDelta;
38
39 import org.eclipse.jdt.ui.PreferenceConstants;
40
41 import org.eclipse.jdt.internal.ui.JavaPluginImages;
42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
43 import org.eclipse.jdt.internal.ui.util.PixelConverter;
44 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
45 import org.eclipse.jdt.internal.ui.wizards.buildpaths.AddSourceFolderWizard;
46 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPListElement;
47
48 //SelectedElements iff enabled: IJavaProject && size == 1
49
public class CreateLinkedSourceFolderAction extends BuildpathModifierAction {
50
51     public CreateLinkedSourceFolderAction(IWorkbenchSite site) {
52         this(site, null, PlatformUI.getWorkbench().getProgressService());
53     }
54     
55     public CreateLinkedSourceFolderAction(IRunnableContext context, ISetSelectionTarget selectionTarget) {
56         this(null, selectionTarget, context);
57     }
58     
59     private CreateLinkedSourceFolderAction(IWorkbenchSite site, ISetSelectionTarget selectionTarget, IRunnableContext context) {
60         super(site, selectionTarget, BuildpathModifierAction.CREATE_LINK);
61         
62         setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Link_label);
63         setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_Link_tooltip);
64         setImageDescriptor(JavaPluginImages.DESC_ELCL_ADD_LINKED_SOURCE_TO_BUILDPATH);
65         setDescription(NewWizardMessages.PackageExplorerActionGroup_FormText_createLinkedFolder);
66     }
67     
68     /**
69      * {@inheritDoc}
70      */

71     public String JavaDoc getDetailedDescription() {
72         return NewWizardMessages.PackageExplorerActionGroup_FormText_createLinkedFolder;
73     }
74     
75     /* (non-Javadoc)
76      * @see org.eclipse.jface.action.Action#run()
77      */

78     public void run() {
79         Shell shell= getShell();
80     
81         try {
82             IJavaProject javaProject= (IJavaProject)getSelectedElements().get(0);
83             
84             CPListElement newEntrie= new CPListElement(javaProject, IClasspathEntry.CPE_SOURCE);
85             CPListElement[] existing= CPListElement.createFromExisting(javaProject);
86             boolean isProjectSrcFolder= CPListElement.isProjectSourceFolder(existing, javaProject);
87             
88             AddSourceFolderWizard wizard= new AddSourceFolderWizard(existing, newEntrie, getOutputLocation(javaProject), true, false, false, isProjectSrcFolder, isProjectSrcFolder);
89             wizard.init(PlatformUI.getWorkbench(), new StructuredSelection(javaProject));
90             
91             WizardDialog dialog= new WizardDialog(shell, wizard);
92             PixelConverter converter= new PixelConverter(JFaceResources.getDialogFont());
93             dialog.setMinimumPageSize(converter.convertWidthInCharsToPixels(70), converter.convertHeightInCharsToPixels(20));
94             dialog.create();
95             int res= dialog.open();
96             if (res == Window.OK) {
97         
98                 BuildpathDelta delta= new BuildpathDelta(getToolTipText());
99                 
100                 ArrayList JavaDoc newEntries= wizard.getExistingEntries();
101                 delta.setNewEntries((CPListElement[])newEntries.toArray(new CPListElement[newEntries.size()]));
102                 
103                 IResource resource= wizard.getCreatedElement().getCorrespondingResource();
104                 delta.addCreatedResource(resource);
105                 
106                 delta.setDefaultOutputLocation(wizard.getOutputLocation());
107                 
108                 informListeners(delta);
109
110                 selectAndReveal(new StructuredSelection(wizard.getCreatedElement()));
111             }
112             
113             notifyResult(res == Window.OK);
114         } catch (CoreException e) {
115             String JavaDoc title= NewWizardMessages.AbstractOpenWizardAction_createerror_title;
116             String JavaDoc message= NewWizardMessages.AbstractOpenWizardAction_createerror_message;
117             ExceptionHandler.handle(e, shell, title, message);
118         }
119     }
120     
121     private IPath getOutputLocation(IJavaProject javaProject) {
122         try {
123             return javaProject.getOutputLocation();
124         } catch (CoreException e) {
125             IProject project= javaProject.getProject();
126             IPath projPath= project.getFullPath();
127             return projPath.append(PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
128         }
129     }
130
131     protected boolean canHandle(IStructuredSelection selection) {
132         if (selection.size() != 1)
133             return false;
134         
135         if (!(selection.getFirstElement() instanceof IJavaProject))
136             return false;
137         
138         return true;
139     }
140 }
141
Popular Tags