1 /******************************************************************************* 2 * Copyright (c) 2000, 2015 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.internal.debug.ui.actions; 15 16 17 import java.util.List; 18 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.debug.core.ILaunchConfiguration; 21 import org.eclipse.jdt.internal.debug.ui.classpath.ClasspathEntry; 22 import org.eclipse.jdt.internal.debug.ui.classpath.IClasspathEditor; 23 import org.eclipse.jdt.internal.debug.ui.classpath.IClasspathEntry; 24 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer; 25 import org.eclipse.jdt.launching.IRuntimeClasspathEntry; 26 import org.eclipse.jface.action.IAction; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.ui.actions.SelectionListenerAction; 29 30 /** 31 * Moves selected entries in a runtime classpath viewer up one position. 32 */ 33 public class EditClasspathEntryAction extends RuntimeClasspathAction { 34 35 private ILaunchConfiguration fConfiguration; 36 EditClasspathEntryAction(IClasspathViewer viewer, ILaunchConfiguration configuration)37 public EditClasspathEntryAction(IClasspathViewer viewer, ILaunchConfiguration configuration) { 38 super(ActionMessages.EditClasspathEntryAction_0, viewer); 39 fConfiguration = configuration; 40 } 41 /** 42 * Moves all selected entries up one position (if possible). 43 * 44 * @see IAction#run() 45 */ 46 @Override run()47 public void run() { 48 List<IRuntimeClasspathEntry> targets = getOrderedSelection(); 49 if (targets.size() != 1) { 50 return; 51 } 52 IRuntimeClasspathEntry entry = targets.get(0); 53 IRuntimeClasspathEntry[] original = new IRuntimeClasspathEntry[]{entry}; 54 IRuntimeClasspathEntry[] delegtes = new IRuntimeClasspathEntry[original.length]; 55 IClasspathEntry[] parents = new IClasspathEntry[original.length]; 56 for (int i = 0; i < delegtes.length; i++) { 57 ClasspathEntry classpathEntry = (ClasspathEntry)original[i]; 58 delegtes[i] = classpathEntry.getDelegate(); 59 parents[i] = classpathEntry.getParent(); 60 } 61 IClasspathEditor editor = getEditor(entry); 62 if (editor != null) { 63 IRuntimeClasspathEntry[] replacements = editor.edit(getShell(), fConfiguration, delegtes); 64 if (replacements != null) { 65 IRuntimeClasspathEntry[] wrappers = new IRuntimeClasspathEntry[replacements.length]; 66 List<IRuntimeClasspathEntry> list = getEntriesAsList(); 67 int index = 0; 68 for (int i = 0; i < list.size() && index < original.length && index < replacements.length; i++) { 69 Object element = list.get(i); 70 if (element == original[index]) { 71 wrappers[index] = new ClasspathEntry(replacements[index], parents[index]); 72 list.set(i, wrappers[index]); 73 index++; 74 } 75 } 76 setEntries(list); 77 for (IRuntimeClasspathEntry wrapper : wrappers) { 78 getViewer().refresh(wrapper); 79 } 80 } 81 } 82 83 } 84 85 /** 86 * @see SelectionListenerAction#updateSelection(IStructuredSelection) 87 */ 88 @Override updateSelection(IStructuredSelection selection)89 protected boolean updateSelection(IStructuredSelection selection) { 90 if (selection.size() == 1) { 91 Object element = selection.getFirstElement(); 92 if (element instanceof IRuntimeClasspathEntry) { 93 IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry) element; 94 IClasspathEditor editor = getEditor(entry); 95 if (editor != null) { 96 return editor.canEdit(fConfiguration, new IRuntimeClasspathEntry[]{((ClasspathEntry)entry).getDelegate()}); 97 } 98 } 99 } 100 return false; 101 } 102 getEditor(IRuntimeClasspathEntry entry)103 protected IClasspathEditor getEditor(IRuntimeClasspathEntry entry) { 104 if (entry instanceof IAdaptable) { 105 IAdaptable adaptable = (IAdaptable) entry; 106 return adaptable.getAdapter(IClasspathEditor.class); 107 } 108 return null; 109 } 110 111 } 112