KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Matt McCutchen - Bug 148313 [build path] "Configure Build Path" incorrectly appears for non-Java projects
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
13
14 import java.util.HashMap JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.core.runtime.Path;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22
23 import org.eclipse.jface.viewers.IStructuredSelection;
24
25 import org.eclipse.ui.IWorkbenchSite;
26 import org.eclipse.ui.dialogs.PreferencesUtil;
27
28 import org.eclipse.jdt.core.IClasspathEntry;
29 import org.eclipse.jdt.core.IJavaElement;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jdt.core.IPackageFragmentRoot;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jdt.core.JavaModelException;
34
35 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
36
37 import org.eclipse.jdt.internal.ui.JavaPluginImages;
38 import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
39 import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer;
40 import org.eclipse.jdt.internal.ui.preferences.BuildPathsPropertyPage;
41 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
42
43 //SelectedElements iff enabled: (IJavaElement || ClassPathContainer || IAdaptable) && size == 1
44
public class ConfigureBuildPathAction extends BuildpathModifierAction {
45
46     public ConfigureBuildPathAction(IWorkbenchSite site) {
47         super(site, null, BuildpathModifierAction.CONFIGURE_BUILD_PATH);
48         
49         setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ConfigureBP_label);
50         setImageDescriptor(JavaPluginImages.DESC_ELCL_CONFIGURE_BUILDPATH);
51         setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ConfigureBP_tooltip);
52         setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CONFIGURE_BUILDPATH);
53     }
54     
55     /**
56      * {@inheritDoc}
57      */

58     public String JavaDoc getDetailedDescription() {
59         return null;
60     }
61     
62     public void run() {
63         IProject project= null;
64         Object JavaDoc firstElement= getSelectedElements().get(0);
65         HashMap JavaDoc data= new HashMap JavaDoc();
66         
67         if (firstElement instanceof IJavaElement) {
68             IJavaElement element= (IJavaElement) firstElement;
69             IPackageFragmentRoot root= (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
70             if (root != null) {
71                 try {
72                     data.put(BuildPathsPropertyPage.DATA_REVEAL_ENTRY, root.getRawClasspathEntry());
73                 } catch (JavaModelException e) {
74                     // ignore
75
}
76             }
77             project= element.getJavaProject().getProject();
78         } else if (firstElement instanceof PackageFragmentRootContainer) {
79             PackageFragmentRootContainer container= (PackageFragmentRootContainer) firstElement;
80             project= container.getJavaProject().getProject();
81             IClasspathEntry entry= container instanceof ClassPathContainer ? ((ClassPathContainer) container).getClasspathEntry() : JavaCore.newLibraryEntry(new Path("/x/y"), null, null); //$NON-NLS-1$
82
data.put(BuildPathsPropertyPage.DATA_REVEAL_ENTRY, entry);
83         } else {
84             project= ((IResource) ((IAdaptable) firstElement).getAdapter(IResource.class)).getProject();
85         }
86         PreferencesUtil.createPropertyDialogOn(getShell(), project, BuildPathsPropertyPage.PROP_ID, null, data).open();
87     }
88
89     protected boolean canHandle(IStructuredSelection elements) {
90         if (elements.size() != 1)
91             return false;
92     
93         Object JavaDoc firstElement= elements.getFirstElement();
94         
95         if (firstElement instanceof IJavaElement) {
96             IJavaElement element= (IJavaElement) firstElement;
97             IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element);
98             if (root != null && root != element && root.isArchive()) {
99                 return false;
100             }
101             IJavaProject project= element.getJavaProject();
102             if (project == null)
103                 return false;
104             
105             return project.getProject() != null;
106         } else if (firstElement instanceof PackageFragmentRootContainer) {
107             return true;
108         } else if (firstElement instanceof IAdaptable) {
109             IResource res= (IResource) ((IAdaptable) firstElement).getAdapter(IResource.class);
110             if (res == null)
111                 return false;
112             
113             IProject project = res.getProject();
114             if (project == null || !project.isOpen())
115                 return false;
116             
117             try {
118                 return project.hasNature(JavaCore.NATURE_ID);
119             } catch (CoreException e) {
120                 return false;
121             }
122         }
123         return false;
124     }
125
126 }
127
Popular Tags