PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.ui.source_3.7.1.r371_v20110824-0800/org/eclipse/jdt/ui/actions/RemoveFromClasspathAction.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 144 lines | 91 code | 16 blank | 37 comment | 12 complexity | 1fb12ba975c7122d106c161e81872cab MD5 | raw file
  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. *******************************************************************************/
  11. package org.eclipse.jdt.ui.actions;
  12. import java.lang.reflect.InvocationTargetException;
  13. import java.util.ArrayList;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import org.eclipse.core.runtime.CoreException;
  17. import org.eclipse.core.runtime.IProgressMonitor;
  18. import org.eclipse.core.runtime.SubProgressMonitor;
  19. import org.eclipse.core.resources.IResource;
  20. import org.eclipse.core.resources.IWorkspaceRunnable;
  21. import org.eclipse.jface.viewers.IStructuredSelection;
  22. import org.eclipse.ui.IWorkbenchSite;
  23. import org.eclipse.ui.PlatformUI;
  24. import org.eclipse.jdt.core.IClasspathEntry;
  25. import org.eclipse.jdt.core.IPackageFragmentRoot;
  26. import org.eclipse.jdt.core.JavaModelException;
  27. import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
  28. import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
  29. import org.eclipse.jdt.internal.ui.JavaPlugin;
  30. import org.eclipse.jdt.internal.ui.actions.ActionMessages;
  31. import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
  32. import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
  33. /**
  34. * Action to remove package fragment roots from the classpath of its parent
  35. * project. Currently, the action is applicable to selections containing
  36. * non-external archives (JAR or zip).
  37. *
  38. * <p>
  39. * This class may be instantiated; it is not intended to be subclassed.
  40. * </p>
  41. *
  42. * @since 2.1
  43. *
  44. * @noextend This class is not intended to be subclassed by clients.
  45. */
  46. public class RemoveFromClasspathAction extends SelectionDispatchAction {
  47. /**
  48. * Creates a new <code>RemoveFromClasspathAction</code>. The action requires
  49. * that the selection provided by the site's selection provider is of type
  50. * <code> org.eclipse.jface.viewers.IStructuredSelection</code>.
  51. *
  52. * @param site the site providing context information for this action
  53. */
  54. public RemoveFromClasspathAction(IWorkbenchSite site) {
  55. super(site);
  56. setText(ActionMessages.RemoveFromClasspathAction_Remove);
  57. setToolTipText(ActionMessages.RemoveFromClasspathAction_tooltip);
  58. PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.REMOVE_FROM_CLASSPATH_ACTION);
  59. }
  60. /* (non-Javadoc)
  61. * Method declared in SelectionDispatchAction
  62. */
  63. @Override
  64. public void selectionChanged(IStructuredSelection selection) {
  65. setEnabled(checkEnabled(selection));
  66. }
  67. private static boolean checkEnabled(IStructuredSelection selection) {
  68. if (selection.isEmpty())
  69. return false;
  70. for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
  71. if (! canRemove(iter.next()))
  72. return false;
  73. }
  74. return true;
  75. }
  76. /* (non-Javadoc)
  77. * Method declared in SelectionDispatchAction
  78. */
  79. @Override
  80. public void run(final IStructuredSelection selection) {
  81. try {
  82. PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, new WorkbenchRunnableAdapter(new IWorkspaceRunnable() {
  83. public void run(IProgressMonitor pm) throws CoreException {
  84. try{
  85. IPackageFragmentRoot[] roots= getRootsToRemove(selection);
  86. pm.beginTask(ActionMessages.RemoveFromClasspathAction_Removing, roots.length);
  87. for (int i= 0; i < roots.length; i++) {
  88. int jCoreFlags= IPackageFragmentRoot.NO_RESOURCE_MODIFICATION | IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH;
  89. roots[i].delete(IResource.NONE, jCoreFlags, new SubProgressMonitor(pm, 1));
  90. }
  91. } finally {
  92. pm.done();
  93. }
  94. }
  95. }));
  96. } catch (InvocationTargetException e) {
  97. ExceptionHandler.handle(e, getShell(),
  98. ActionMessages.RemoveFromClasspathAction_exception_dialog_title,
  99. ActionMessages.RemoveFromClasspathAction_Problems_occurred);
  100. } catch (InterruptedException e) {
  101. // canceled
  102. }
  103. }
  104. private static IPackageFragmentRoot[] getRootsToRemove(IStructuredSelection selection){
  105. List<Object> result= new ArrayList<Object>(selection.size());
  106. for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
  107. Object element= iter.next();
  108. if (canRemove(element))
  109. result.add(element);
  110. }
  111. return result.toArray(new IPackageFragmentRoot[result.size()]);
  112. }
  113. private static boolean canRemove(Object element){
  114. if (! (element instanceof IPackageFragmentRoot))
  115. return false;
  116. IPackageFragmentRoot root= (IPackageFragmentRoot)element;
  117. try {
  118. IClasspathEntry cpe= root.getRawClasspathEntry();
  119. if (cpe == null || cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER)
  120. return false; // don't want to remove the container if only a child is selected
  121. return true;
  122. } catch (JavaModelException e) {
  123. if (JavaModelUtil.isExceptionToBeLogged(e))
  124. JavaPlugin.log(e);
  125. }
  126. return false;
  127. }
  128. }