]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
15 | ||
16 | import org.eclipse.core.runtime.CoreException; | |
17 | import org.eclipse.core.runtime.IAdaptable; | |
18 | ||
19 | import org.eclipse.core.resources.IProject; | |
20 | import org.eclipse.core.resources.IResource; | |
21 | ||
22 | import org.eclipse.jface.viewers.IStructuredSelection; | |
23 | ||
24 | import org.eclipse.ui.IWorkbenchSite; | |
25 | import org.eclipse.ui.dialogs.PreferencesUtil; | |
26 | ||
27 | import org.eclipse.jdt.core.IClasspathEntry; | |
28 | import org.eclipse.jdt.core.IJavaElement; | |
29 | import org.eclipse.jdt.core.IJavaProject; | |
30 | import org.eclipse.jdt.core.IPackageFragmentRoot; | |
31 | import org.eclipse.jdt.core.JavaCore; | |
32 | import org.eclipse.jdt.core.JavaModelException; | |
33 | ||
34 | import org.eclipse.jdt.internal.corext.util.JavaModelUtil; | |
35 | ||
36 | import org.eclipse.jdt.internal.ui.JavaPluginImages; | |
37 | import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer; | |
38 | import org.eclipse.jdt.internal.ui.preferences.BuildPathsPropertyPage; | |
39 | import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; | |
40 | ||
41 | //SelectedElements iff enabled: (IJavaElement || ClassPathContainer || IAdaptable) && size == 1 | |
42 | public class ConfigureBuildPathAction extends BuildpathModifierAction { | |
43 | ||
44 | public ConfigureBuildPathAction(IWorkbenchSite site) { | |
45 | super(site, null, BuildpathModifierAction.CONFIGURE_BUILD_PATH); | |
46 | ||
47 | setText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ConfigureBP_label); | |
48 | setImageDescriptor(JavaPluginImages.DESC_ELCL_CONFIGURE_BUILDPATH); | |
49 | setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_ConfigureBP_tooltip); | |
50 | setDisabledImageDescriptor(JavaPluginImages.DESC_DLCL_CONFIGURE_BUILDPATH); | |
51 | } | |
52 | ||
53 | /** | |
54 | * {@inheritDoc} | |
55 | */ | |
56 | @Override | |
57 | public String getDetailedDescription() { | |
58 | return null; | |
59 | } | |
60 | ||
61 | @Override | |
62 | public void run() { | |
63 | IProject project= null; | |
64 | Object firstElement= getSelectedElements().get(0); | |
65 | HashMap<Object, IClasspathEntry> data= new HashMap<Object, IClasspathEntry>(); | |
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.generated_1205377586575683318(data); | |
81 | } else { | |
82 | project= ((IResource) ((IAdaptable) firstElement).getAdapter(IResource.class)).getProject(); | |
83 | } | |
84 | PreferencesUtil.createPropertyDialogOn(getShell(), project, BuildPathsPropertyPage.PROP_ID, null, data).open(); | |
85 | } | |
86 | ||
87 | @Override | |
88 | protected boolean canHandle(IStructuredSelection elements) { | |
89 | if (elements.size() != 1) | |
90 | return false; | |
91 | ||
92 | Object firstElement= elements.getFirstElement(); | |
93 | ||
94 | if (firstElement instanceof IJavaElement) { | |
95 | IJavaElement element= (IJavaElement) firstElement; | |
96 | IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element); | |
97 | if (root != null && root != element && (root.isArchive() || root.isExternal())) { | |
98 | return false; | |
99 | } | |
100 | IJavaProject project= element.getJavaProject(); | |
101 | if (project == null) | |
102 | return false; | |
103 | ||
104 | return project.getProject() != null; | |
105 | } else if (firstElement instanceof PackageFragmentRootContainer) { | |
106 | return true; | |
107 | } else if (firstElement instanceof IAdaptable) { | |
108 | IResource res= (IResource) ((IAdaptable) firstElement).getAdapter(IResource.class); | |
109 | if (res == null) | |
110 | return false; | |
111 | ||
112 | IProject project = res.getProject(); | |
113 | if (project == null || !project.isOpen()) | |
114 | return false; | |
115 | ||
116 | try { | |
117 | return project.hasNature(JavaCore.NATURE_ID); | |
118 | } catch (CoreException e) { | |
119 | return false; | |
120 | } | |
121 | } | |
122 | return false; | |
123 | } | |
124 | ||
125 | } |