KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jdt.debug.ui.launchConfigurations.JavaClasspathTab;
19 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
20 import org.eclipse.jface.viewers.ITreeContentProvider;
21 import org.eclipse.jface.viewers.TreeViewer;
22 import org.eclipse.jface.viewers.Viewer;
23
24 /**
25  * Content provider that maintains a list of classpath entries which are shown in a tree
26  * viewer.
27  */

28 public class ClasspathContentProvider implements ITreeContentProvider {
29     private TreeViewer treeViewer;
30     private ClasspathModel model= null;
31     private boolean refreshEnabled= false;
32     private boolean refreshRequested= false;
33     private JavaClasspathTab fTab;
34     
35     public ClasspathContentProvider(JavaClasspathTab tab) {
36         fTab = tab;
37     }
38         
39     public void add(IClasspathEntry parent, IRuntimeClasspathEntry child, Object JavaDoc beforeElement) {
40         Object JavaDoc newEntry= null;
41         if (parent == null || parent == model) {
42             newEntry= model.addEntry(child);
43             parent= model;
44         } else if (parent instanceof ClasspathGroup) {
45             newEntry= model.createEntry(child, parent);
46             ((ClasspathGroup)parent).addEntry((ClasspathEntry)newEntry, beforeElement);
47         }
48         if (newEntry != null) {
49             treeViewer.add(parent, newEntry);
50             treeViewer.setExpandedState(parent, true);
51             treeViewer.reveal(newEntry);
52             refresh();
53         }
54     }
55     
56     public void add(int entryType, IRuntimeClasspathEntry child) {
57         Object JavaDoc newEntry= model.addEntry(entryType, child);
58         if (newEntry != null) {
59             treeViewer.add(getParent(newEntry), newEntry);
60             refresh();
61         }
62     }
63
64     public void removeAll() {
65         model.removeAll();
66         refresh();
67     }
68     
69     private void refresh() {
70         if (refreshEnabled) {
71             treeViewer.refresh();
72             refreshRequested= false;
73         } else {
74             refreshRequested= true;
75         }
76     }
77     
78     public void removeAll(IClasspathEntry parent) {
79         if (parent instanceof ClasspathGroup) {
80             ((ClasspathGroup)parent).removeAll();
81         }
82         refresh();
83     }
84
85     /**
86      * @see ITreeContentProvider#getParent(Object)
87      */

88     public Object JavaDoc getParent(Object JavaDoc element) {
89         if (element instanceof ClasspathEntry) {
90             return ((ClasspathEntry)element).getParent();
91         }
92         if (element instanceof ClasspathGroup) {
93             return model;
94         }
95         
96         return null;
97     }
98
99     /**
100      * @see ITreeContentProvider#hasChildren(Object)
101      */

102     public boolean hasChildren(Object JavaDoc element) {
103         if (element instanceof ClasspathEntry) {
104             return (((ClasspathEntry)element).hasChildren());
105         }
106         if (element instanceof ClasspathGroup) {
107             return ((ClasspathGroup)element).hasEntries();
108             
109         }
110         
111         if (element instanceof ClasspathModel) {
112             return ((ClasspathModel) element).hasEntries();
113         }
114         return false;
115     }
116
117     /**
118      * @see IStructuredContentProvider#getElements(Object)
119      */

120     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
121         return getChildren(inputElement);
122     }
123
124     
125     /* (non-Javadoc)
126      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
127      */

128     public void dispose() {
129
130     }
131
132     /* (non-Javadoc)
133      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
134      */

135     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
136         treeViewer = (TreeViewer) viewer;
137         
138         if (newInput != null) {
139             model= (ClasspathModel)newInput;
140         } else {
141             if (model != null) {
142                 model.removeAll();
143             }
144             model= null;
145         }
146     }
147
148     /* (non-Javadoc)
149      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
150      */

151     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
152         if (parentElement instanceof ClasspathGroup) {
153             return ((ClasspathGroup)parentElement).getEntries();
154         }
155         if (parentElement instanceof ClasspathModel) {
156             return ((ClasspathModel)parentElement).getEntries();
157         }
158         if (parentElement instanceof ClasspathEntry) {
159             return ((ClasspathEntry)parentElement).getChildren(fTab.getLaunchConfiguration());
160         }
161         if (parentElement == null) {
162             List JavaDoc all= new ArrayList JavaDoc();
163             Object JavaDoc[] topEntries= model.getEntries();
164             for (int i = 0; i < topEntries.length; i++) {
165                 Object JavaDoc object = topEntries[i];
166                 if (object instanceof ClasspathEntry) {
167                     all.add(object);
168                 } else if (object instanceof ClasspathGroup) {
169                     all.addAll(Arrays.asList(((ClasspathGroup)object).getEntries()));
170                 }
171             }
172             return all.toArray();
173         }
174         
175         return null;
176     }
177
178     public void removeAll(List JavaDoc selection) {
179         Object JavaDoc[] array= selection.toArray();
180         model.removeAll(array);
181         treeViewer.remove(array);
182         refresh();
183     }
184     
185     public IClasspathEntry[] getUserClasspathEntries() {
186         return model.getEntries(ClasspathModel.USER);
187     }
188
189     public IClasspathEntry[] getBootstrapClasspathEntries() {
190         return model.getEntries(ClasspathModel.BOOTSTRAP);
191     }
192     
193     public void handleMove(boolean direction, IClasspathEntry entry) {
194         IClasspathEntry parent = (IClasspathEntry)getParent(entry);
195         parent.moveChild(direction, entry);
196     }
197
198     public ClasspathModel getModel() {
199         return model;
200     }
201
202     public void setRefreshEnabled(boolean refreshEnabled) {
203         this.refreshEnabled = refreshEnabled;
204         treeViewer.getTree().setRedraw(refreshEnabled);
205         if (refreshEnabled && refreshRequested) {
206             refresh();
207         }
208     }
209
210     public void setEntries(IRuntimeClasspathEntry[] entries) {
211         model.removeAll();
212         IRuntimeClasspathEntry entry;
213         for (int i = 0; i < entries.length; i++) {
214             entry= entries[i];
215             switch (entry.getClasspathProperty()) {
216                 case IRuntimeClasspathEntry.USER_CLASSES:
217                     model.addEntry(ClasspathModel.USER, entry);
218                     break;
219                 default:
220                     model.addEntry(ClasspathModel.BOOTSTRAP, entry);
221                     break;
222             }
223         }
224         refresh();
225     }
226 }
227
Popular Tags