1 /*******************************************************************************
2  * Copyright (c) 2000, 2012 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 import java.util.List;
17 
18 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer;
19 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 
22 /**
23  * Moves selected entries in a runtime classpath viewer down one position.
24  */
25 public class MoveDownAction extends RuntimeClasspathAction {
26 
MoveDownAction(IClasspathViewer viewer)27 	public MoveDownAction(IClasspathViewer viewer) {
28 		super(ActionMessages.MoveDownAction_M_ove_Down_1, viewer);
29 	}
30 
31 	/* (non-Javadoc)
32 	 * @see org.eclipse.jface.action.Action#run()
33 	 */
34 	@Override
run()35 	public void run() {
36 		List<IRuntimeClasspathEntry> targets = getOrderedSelection();
37 		if (targets.isEmpty()) {
38 			return;
39 		}
40 		List<IRuntimeClasspathEntry> list = getEntriesAsList();
41 		int bottom = list.size() - 1;
42 		int index = 0;
43 		for (int i = targets.size() - 1; i >= 0; i--) {
44 			IRuntimeClasspathEntry target = targets.get(i);
45 			index = list.indexOf(target);
46 			if (index < bottom) {
47 				bottom = index + 1;
48 				IRuntimeClasspathEntry temp = list.get(bottom);
49 				list.set(bottom, target);
50 				list.set(index, temp);
51 			}
52 			if (bottom == index){
53 				bottom--;
54 			} else {
55 				bottom = index;
56 			}
57 		}
58 		setEntries(list);
59 	}
60 
61 	/* (non-Javadoc)
62 	 * @see org.eclipse.jdt.internal.debug.ui.actions.RuntimeClasspathAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
63 	 */
64 	@Override
updateSelection(IStructuredSelection selection)65 	protected boolean updateSelection(IStructuredSelection selection) {
66 		if (selection.isEmpty()) {
67 			return false;
68 		}
69 		return getViewer().updateSelection(getActionType(), selection);
70 	}
71 
72 	/* (non-Javadoc)
73 	 * @see org.eclipse.jdt.internal.debug.ui.actions.RuntimeClasspathAction#getActionType()
74 	 */
75 	@Override
getActionType()76 	protected int getActionType() {
77 		return MOVE;
78 	}
79 }
80