/******************************************************************************* * Copyright (c) 2000, 2017 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * *******************************************************************************/ package org.eclipse.dltk.internal.ui.wizards.buildpath.newsourcepage; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.dltk.core.DLTKCore; import org.eclipse.dltk.core.DLTKLanguageManager; import org.eclipse.dltk.core.IBuildpathEntry; import org.eclipse.dltk.core.IDLTKLanguageToolkit; import org.eclipse.dltk.core.IModelElement; import org.eclipse.dltk.core.IScriptProject; import org.eclipse.dltk.core.environment.EnvironmentManager; import org.eclipse.dltk.internal.corext.buildpath.BuildpathModifier; import org.eclipse.dltk.internal.ui.wizards.BuildpathDialogAccess; import org.eclipse.dltk.internal.ui.wizards.NewWizardMessages; import org.eclipse.dltk.internal.ui.wizards.buildpath.BPListElement; import org.eclipse.dltk.ui.DLTKPluginImages; import org.eclipse.dltk.ui.DLTKUIPlugin; import org.eclipse.jface.action.Action; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchPartReference; import org.eclipse.ui.IWorkbenchSite; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.ISetSelectionTarget; public class AddArchiveToBuildpathAction extends Action implements ISelectionChangedListener { private final IWorkbenchSite fSite; private IScriptProject fScriptProject; public AddArchiveToBuildpathAction(IWorkbenchSite site) { super(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddZIPCP_label, DLTKPluginImages.DESC_OBJS_EXTJAR); setToolTipText(NewWizardMessages.NewSourceContainerWorkbookPage_ToolBar_AddZIPCP_tooltip); fSite= site; } @Override public void run() { final Shell shell= fSite.getShell() != null ? fSite.getShell() : DLTKUIPlugin.getActiveWorkbenchShell(); final IPath[] selected = BuildpathDialogAccess .chooseExternalArchiveEntries(shell, EnvironmentManager.getEnvironment(this.fScriptProject)); try { final IRunnableWithProgress runnable = monitor -> { try { List result = addExternalArchives( selected == null ? new IPath[0] : selected, fScriptProject, monitor); if (result != null && result.size() > 0) selectAndReveal(new StructuredSelection(result)); } catch (CoreException e) { throw new InvocationTargetException(e); } }; PlatformUI.getWorkbench().getProgressService().run(true, false, runnable); } catch (final InvocationTargetException e) { if (e.getCause() instanceof CoreException) { showExceptionDialog((CoreException)e.getCause()); } else { DLTKUIPlugin.log(e); } } catch (final InterruptedException e) { } } protected List addExternalArchives(IPath[] archivePaths, IScriptProject project, IProgressMonitor monitor) throws CoreException { if (monitor == null) monitor= new NullProgressMonitor(); List addedEntries= new ArrayList(); try { monitor.beginTask(NewWizardMessages.BuildpathModifier_Monitor_AddToBuildpath, 4); if (archivePaths != null) { for (int i= 0; i < archivePaths.length; i++) { addedEntries.add(new BPListElement(project, IBuildpathEntry.BPE_LIBRARY, archivePaths[i], null, archivePaths[i].isAbsolute())); } monitor.worked(1); List existingEntries= BuildpathModifier.getExistingEntries(project); BuildpathModifier.setNewEntry(existingEntries, addedEntries, project, new SubProgressMonitor(monitor, 1)); BuildpathModifier.commitBuildPath(existingEntries, project, new SubProgressMonitor(monitor, 1)); List result= new ArrayList(addedEntries.size()); for (int i= 0; i < addedEntries.size(); i++) { IBuildpathEntry entry= ((BPListElement) addedEntries.get(i)).getBuildpathEntry(); IModelElement elem= project.findProjectFragment(entry.getPath()); if (elem != null) { result.add(elem); } } monitor.worked(1); return result; } } finally { monitor.done(); } return null; } @Override public void selectionChanged(final SelectionChangedEvent event) { final ISelection selection = event.getSelection(); if (selection instanceof IStructuredSelection) { setEnabled(canHandle((IStructuredSelection) selection)); } else { setEnabled(canHandle(StructuredSelection.EMPTY)); } } private boolean canHandle(IStructuredSelection selection) { if (selection.size() != 1) return false; Object first= selection.getFirstElement(); if (first instanceof IScriptProject) fScriptProject = (IScriptProject) first; else if (first instanceof IProject) { fScriptProject = DLTKCore.create((IProject) first); } else return false; if( fScriptProject == null || !fScriptProject.exists() ) { return false; } IDLTKLanguageToolkit toolkit = null; toolkit = DLTKLanguageManager.getLanguageToolkit(fScriptProject); if( toolkit != null ) { return toolkit.languageSupportZIPBuildpath(); } return false; } private void showExceptionDialog(CoreException exception) { showError(exception, fSite.getShell(), NewWizardMessages.AddArchiveToBuildpathAction_ErrorTitle, exception.getMessage()); } private void showError(CoreException e, Shell shell, String title, String message) { IStatus status= e.getStatus(); if (status != null) { ErrorDialog.openError(shell, message, title, status); } else { MessageDialog.openError(shell, title, message); } } private void selectAndReveal(final ISelection selection) { // validate the input IWorkbenchPage page= fSite.getPage(); if (page == null) return; // get all the view and editor parts List parts= new ArrayList(); IWorkbenchPartReference refs[]= page.getViewReferences(); for (int i= 0; i < refs.length; i++) { IWorkbenchPart part= refs[i].getPart(false); if (part != null) parts.add(part); } refs= page.getEditorReferences(); for (int i= 0; i < refs.length; i++) { if (refs[i].getPart(false) != null) parts.add(refs[i].getPart(false)); } Iterator itr= parts.iterator(); while (itr.hasNext()) { IWorkbenchPart part= (IWorkbenchPart) itr.next(); // get the part's ISetSelectionTarget implementation ISetSelectionTarget target= null; if (part instanceof ISetSelectionTarget) target= (ISetSelectionTarget) part; else target = part.getAdapter(ISetSelectionTarget.class); if (target != null) { // select and reveal resource final ISetSelectionTarget finalTarget= target; page.getWorkbenchWindow().getShell().getDisplay() .asyncExec(() -> finalTarget.selectReveal(selection)); } } } }