KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > classpath > RuntimeClasspathViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.classpath;
12  
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.ListenerList;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.jdt.internal.debug.ui.actions.RuntimeClasspathAction;
21 import org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer;
22 import org.eclipse.jdt.internal.debug.ui.launcher.IEntriesChangedListener;
23 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.StructuredSelection;
28 import org.eclipse.jface.viewers.TreeViewer;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.KeyAdapter;
31 import org.eclipse.swt.events.KeyEvent;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Shell;
35
36 /**
37  * A viewer that displays and manipulates runtime classpath entries.
38  */

39 public class RuntimeClasspathViewer extends TreeViewer implements IClasspathViewer {
40         
41     /**
42      * Entry changed listeners
43      */

44     private ListenerList fListeners = new ListenerList();
45     
46     private IClasspathEntry fCurrentParent= null;
47         
48     /**
49      * Creates a runtime classpath viewer with the given parent.
50      *
51      * @param parent the parent control
52      */

53     public RuntimeClasspathViewer(Composite parent) {
54         super(parent);
55         
56         GridData data = new GridData(GridData.FILL_BOTH);
57         data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
58         data.heightHint = getTree().getItemHeight();
59         getTree().setLayoutData(data);
60         
61         getTree().addKeyListener(new KeyAdapter() {
62             public void keyPressed(KeyEvent event) {
63                 if (updateSelection(RuntimeClasspathAction.REMOVE, (IStructuredSelection)getSelection()) && event.character == SWT.DEL && event.stateMask == 0) {
64                     List JavaDoc selection= getSelectionFromWidget();
65                     getClasspathContentProvider().removeAll(selection);
66                     notifyChanged();
67                 }
68             }
69         });
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#setEntries(org.eclipse.jdt.launching.IRuntimeClasspathEntry[])
74      */

75     public void setEntries(IRuntimeClasspathEntry[] entries) {
76         getClasspathContentProvider().setRefreshEnabled(false);
77         resolveCurrentParent(getSelection());
78         getClasspathContentProvider().removeAll(fCurrentParent);
79         getClasspathContentProvider().setEntries(entries);
80         getClasspathContentProvider().setRefreshEnabled(true);
81         notifyChanged();
82     }
83     
84     /* (non-Javadoc)
85      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#getEntries()
86      */

87     public IRuntimeClasspathEntry[] getEntries() {
88         return getClasspathContentProvider().getModel().getAllEntries();
89     }
90     
91     /* (non-Javadoc)
92      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#addEntries(org.eclipse.jdt.launching.IRuntimeClasspathEntry[])
93      */

94     public void addEntries(IRuntimeClasspathEntry[] entries) {
95         getClasspathContentProvider().setRefreshEnabled(false);
96         IStructuredSelection sel = (IStructuredSelection) getSelection();
97         Object JavaDoc beforeElement = sel.getFirstElement();
98         resolveCurrentParent(getSelection());
99         List JavaDoc existingEntries= Arrays.asList(fCurrentParent.getEntries());
100         for (int i = 0; i < entries.length; i++) {
101             if (!existingEntries.contains(entries[i])) {
102                 getClasspathContentProvider().add(fCurrentParent, entries[i], beforeElement);
103             }
104         }
105         getClasspathContentProvider().setRefreshEnabled(true);
106         notifyChanged();
107     }
108     
109     private boolean resolveCurrentParent(ISelection selection) {
110         fCurrentParent= null;
111         Iterator JavaDoc selected= ((IStructuredSelection)selection).iterator();
112         
113         while (selected.hasNext()) {
114             Object JavaDoc element = selected.next();
115             if (element instanceof ClasspathEntry) {
116                 IClasspathEntry parent= ((IClasspathEntry)element).getParent();
117                 if (fCurrentParent != null) {
118                     if (!fCurrentParent.equals(parent)) {
119                         return false;
120                     }
121                 } else {
122                     fCurrentParent= parent;
123                 }
124             } else {
125                 if (fCurrentParent != null) {
126                     if (!fCurrentParent.equals(element)) {
127                         return false;
128                     }
129                 } else {
130                     fCurrentParent= (IClasspathEntry)element;
131                 }
132             }
133         }
134         return true;
135     }
136     
137     /* (non-Javadoc)
138      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#isEnabled()
139      */

140     public boolean isEnabled() {
141         return true;
142     }
143     
144     /**
145      * Sets the launch configuration context for this viewer, if any
146      */

147     public void setLaunchConfiguration(ILaunchConfiguration configuration) {
148         if (getLabelProvider() != null) {
149             ((ClasspathLabelProvider)getLabelProvider()).setLaunchConfiguration(configuration);
150         }
151     }
152     
153     public void addEntriesChangedListener(IEntriesChangedListener listener) {
154         fListeners.add(listener);
155     }
156     
157     public void removeEntriesChangedListener(IEntriesChangedListener listener) {
158         fListeners.remove(listener);
159     }
160     
161     /* (non-Javadoc)
162      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#notifyChanged()
163      */

164     public void notifyChanged() {
165         Object JavaDoc[] listeners = fListeners.getListeners();
166         for (int i = 0; i < listeners.length; i++) {
167             ((IEntriesChangedListener)listeners[i]).entriesChanged(this);
168         }
169     }
170     
171     /* (non-Javadoc)
172      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#indexOf(org.eclipse.jdt.launching.IRuntimeClasspathEntry)
173      */

174     public int indexOf(IRuntimeClasspathEntry entry) {
175         IClasspathEntry[] entries= getClasspathContentProvider().getBootstrapClasspathEntries();
176         for (int i = 0; i < entries.length; i++) {
177             IClasspathEntry existingEntry = entries[i];
178             if (existingEntry.equals(entry)) {
179                 return 1;
180             }
181         }
182         entries= getClasspathContentProvider().getUserClasspathEntries();
183         for (int i = 0; i < entries.length; i++) {
184             IClasspathEntry existingEntry = entries[i];
185             if (existingEntry.equals(entry)) {
186                 return 1;
187             }
188         }
189         
190         return -1;
191         
192     }
193
194     /* (non-Javadoc)
195      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#getShell()
196      */

197     public Shell getShell() {
198         return getControl().getShell();
199     }
200     
201     private ClasspathContentProvider getClasspathContentProvider() {
202         return (ClasspathContentProvider)super.getContentProvider();
203     }
204
205     /* (non-Javadoc)
206      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#updateSelection(int, org.eclipse.jface.viewers.IStructuredSelection)
207      */

208     public boolean updateSelection(int actionType, IStructuredSelection selection) {
209         
210         if (selection.isEmpty()) {
211             return false;
212         }
213         switch (actionType) {
214             case RuntimeClasspathAction.ADD :
215                 Iterator JavaDoc selected= selection.iterator();
216                 while (selected.hasNext()) {
217                     IClasspathEntry entry = (IClasspathEntry)selected.next();
218                     if (!entry.isEditable() && entry instanceof ClasspathEntry) {
219                         return false;
220                     }
221                 }
222                 return selection.size() > 0;
223             case RuntimeClasspathAction.REMOVE :
224             case RuntimeClasspathAction.MOVE :
225                 selected= selection.iterator();
226                 while (selected.hasNext()) {
227                     IClasspathEntry entry = (IClasspathEntry)selected.next();
228                     if (!entry.isEditable()) {
229                         return false;
230                     }
231                 }
232                 return selection.size() > 0;
233             default :
234                 break;
235         }
236         
237         return true;
238     }
239
240     /* (non-Javadoc)
241      * @see org.eclipse.jdt.internal.debug.ui.launcher.IClasspathViewer#getSelectedEntries()
242      */

243     public ISelection getSelectedEntries() {
244         IStructuredSelection selection= (IStructuredSelection)getSelection();
245         List JavaDoc entries= new ArrayList JavaDoc(selection.size() * 2);
246         Iterator JavaDoc itr= selection.iterator();
247         while (itr.hasNext()) {
248             IClasspathEntry element = (IClasspathEntry) itr.next();
249             if (element.hasEntries()) {
250                 entries.addAll(Arrays.asList(element.getEntries()));
251             } else {
252                 entries.add(element);
253             }
254         }
255         
256         return new StructuredSelection(entries);
257     }
258 }
259
Popular Tags