1 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 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 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 58 public String getDetailedDescription() { 59 return null; 60 } 61 62 public void run() { 63 IProject project= null; 64 Object firstElement= getSelectedElements().get(0); 65 HashMap data= new HashMap (); 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 } 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); 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 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 |