KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > EditClasspathEntryAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.internal.debug.ui.actions;
12
13
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.debug.core.ILaunchConfiguration;
18 import org.eclipse.jdt.internal.debug.ui.classpath.ClasspathEntry;
19 import org.eclipse.jdt.internal.debug.ui.classpath.IClasspathEditor;
20 import org.eclipse.jdt.internal.debug.ui.classpath.IClasspathEntry;
21 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer;
22 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24
25 /**
26  * Moves selected enries in a runtime classpath viewer up one position.
27  */

28 public class EditClasspathEntryAction extends RuntimeClasspathAction {
29     
30     private ILaunchConfiguration fConfiguration;
31
32     public EditClasspathEntryAction(IClasspathViewer viewer, ILaunchConfiguration configuration) {
33         super(ActionMessages.EditClasspathEntryAction_0, viewer);
34         fConfiguration = configuration;
35     }
36     /**
37      * Moves all selected entries up one position (if possible).
38      *
39      * @see IAction#run()
40      */

41     public void run() {
42         List JavaDoc targets = getOrderedSelection();
43         if (targets.size() != 1) {
44             return;
45         }
46         IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry) targets.get(0);
47         IRuntimeClasspathEntry[] original = new IRuntimeClasspathEntry[]{entry};
48         IRuntimeClasspathEntry[] delegtes = new IRuntimeClasspathEntry[original.length];
49         IClasspathEntry[] parents = new IClasspathEntry[original.length];
50         for (int i = 0; i < delegtes.length; i++) {
51             ClasspathEntry classpathEntry = (ClasspathEntry)original[i];
52             delegtes[i] = classpathEntry.getDelegate();
53             parents[i] = classpathEntry.getParent();
54         }
55         IClasspathEditor editor = getEditor(entry);
56         if (editor != null) {
57             IRuntimeClasspathEntry[] replacements = editor.edit(getShell(), fConfiguration, delegtes);
58             if (replacements != null) {
59                 IRuntimeClasspathEntry[] wrappers = new IRuntimeClasspathEntry[replacements.length];
60                 List JavaDoc list = getEntriesAsList();
61                 int index = 0;
62                 for (int i = 0; i < list.size(); i++) {
63                     Object JavaDoc element = list.get(i);
64                     if (element == original[index]) {
65                         wrappers[index] = new ClasspathEntry(replacements[index], parents[index]);
66                         list.set(i, wrappers[index]);
67                         index++;
68                     }
69                 }
70                 setEntries(list);
71                 for (int i = 0; i < wrappers.length; i++) {
72                     getViewer().refresh(wrappers[i]);
73                 }
74             }
75         }
76         
77     }
78
79     /**
80      * @see SelectionListenerAction#updateSelection(IStructuredSelection)
81      */

82     protected boolean updateSelection(IStructuredSelection selection) {
83         if (selection.size() == 1) {
84             Object JavaDoc element = selection.getFirstElement();
85             if (element instanceof IRuntimeClasspathEntry) {
86                 IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry) element;
87                 IClasspathEditor editor = getEditor(entry);
88                 if (editor != null) {
89                     return editor.canEdit(fConfiguration, new IRuntimeClasspathEntry[]{((ClasspathEntry)entry).getDelegate()});
90                 }
91             }
92         }
93         return false;
94     }
95     
96     protected IClasspathEditor getEditor(IRuntimeClasspathEntry entry) {
97         if (entry instanceof IAdaptable) {
98             IAdaptable adaptable = (IAdaptable) entry;
99             return (IClasspathEditor) adaptable.getAdapter(IClasspathEditor.class);
100         }
101         return null;
102     }
103     
104 }
105
Popular Tags