Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d94db829f43ea3bb9891e3b54e7896912d118639 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*******************************************************************************
 * Copyright (c) 2000, 2006 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
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.debug.ui.actions;


import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.jdt.internal.debug.ui.classpath.ClasspathEntry;
import org.eclipse.jdt.internal.debug.ui.classpath.IClasspathEditor;
import org.eclipse.jdt.internal.debug.ui.classpath.IClasspathEntry;
import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jface.viewers.IStructuredSelection;

/**
 * Moves selected enries in a runtime classpath viewer up one position.
 */
public class EditClasspathEntryAction extends RuntimeClasspathAction {
	
	private ILaunchConfiguration fConfiguration;

	public EditClasspathEntryAction(IClasspathViewer viewer, ILaunchConfiguration configuration) {
		super(ActionMessages.EditClasspathEntryAction_0, viewer);
		fConfiguration = configuration;
	}
	/**
	 * Moves all selected entries up one position (if possible).
	 * 
	 * @see IAction#run()
	 */
	public void run() {
		List targets = getOrderedSelection();
		if (targets.size() != 1) {
			return;
		}
		IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry) targets.get(0);
		IRuntimeClasspathEntry[] original = new IRuntimeClasspathEntry[]{entry};
		IRuntimeClasspathEntry[] delegtes = new IRuntimeClasspathEntry[original.length];
		IClasspathEntry[] parents = new IClasspathEntry[original.length];
		for (int i = 0; i < delegtes.length; i++) {
			ClasspathEntry classpathEntry = (ClasspathEntry)original[i];
			delegtes[i] = classpathEntry.getDelegate();
			parents[i] = classpathEntry.getParent();
		}
		IClasspathEditor editor = getEditor(entry);
		if (editor != null) {
			IRuntimeClasspathEntry[] replacements = editor.edit(getShell(), fConfiguration, delegtes);
			if (replacements != null) {
				IRuntimeClasspathEntry[] wrappers = new IRuntimeClasspathEntry[replacements.length];
				List list = getEntriesAsList();
				int index = 0;
				for (int i = 0; i < list.size(); i++) {
					Object element = list.get(i);
					if (element == original[index]) {
						wrappers[index] = new ClasspathEntry(replacements[index], parents[index]);
						list.set(i, wrappers[index]);
						index++;
					}
				}
				setEntries(list);
				for (int i = 0; i < wrappers.length; i++) {
					getViewer().refresh(wrappers[i]);
				}
			}
		}
		
	}

	/**
	 * @see SelectionListenerAction#updateSelection(IStructuredSelection)
	 */
	protected boolean updateSelection(IStructuredSelection selection) {
		if (selection.size() == 1) {
			Object element = selection.getFirstElement();
			if (element instanceof IRuntimeClasspathEntry) {
				IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry) element;
				IClasspathEditor editor = getEditor(entry);
				if (editor != null) {
					return editor.canEdit(fConfiguration, new IRuntimeClasspathEntry[]{((ClasspathEntry)entry).getDelegate()});
				}
			}
		}
		return false;
	}
	
	protected IClasspathEditor getEditor(IRuntimeClasspathEntry entry) {
		if (entry instanceof IAdaptable) {
			IAdaptable adaptable = (IAdaptable) entry;
			return (IClasspathEditor) adaptable.getAdapter(IClasspathEditor.class);
		}		
		return null;
	}
	
}

Back to the top