KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > jres > LibraryContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.jres;
12
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jdt.launching.LibraryLocation;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.ITreeContentProvider;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.jface.viewers.Viewer;
28
29 /**
30  * Provides the content for the JREs selection/edit viewer
31  *
32  * @see {@link ITreeContentProvider}
33  * @see {@link VMDetailsDialog}
34  * @see {@link VMLibraryBlock}
35  * @see {@link LibraryLocation}
36  * @see {@link LibraryStandin}
37  */

38 public class LibraryContentProvider implements ITreeContentProvider {
39     
40     private Viewer fViewer;
41     
42     /**
43      * Represents a subelement of a <code>LibraryStandin</code>
44      */

45     public class SubElement {
46         
47         public static final int JAVADOC_URL= 1;
48         public static final int SOURCE_PATH= 2;
49         
50         private LibraryStandin fParent;
51         private int fType;
52
53         public SubElement(LibraryStandin parent, int type) {
54             fParent= parent;
55             fType= type;
56         }
57         
58         public LibraryStandin getParent() {
59             return fParent;
60         }
61         
62         public int getType() {
63             return fType;
64         }
65         
66         public void remove() {
67             switch (fType) {
68                 case JAVADOC_URL:
69                     fParent.setJavadocLocation(null);
70                     break;
71                 case SOURCE_PATH:
72                     fParent.setSystemLibrarySourcePath(Path.EMPTY);
73                     break;
74             }
75         }
76     }
77
78     private HashMap JavaDoc fChildren= new HashMap JavaDoc();
79
80     private LibraryStandin[] fLibraries= new LibraryStandin[0];
81
82     /* (non-Javadoc)
83      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
84      */

85     public void dispose() {
86         fChildren.clear();
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
91      */

92     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
93         fViewer = viewer;
94     }
95
96     /* (non-Javadoc)
97      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
98      */

99     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
100         return fLibraries;
101     }
102
103     /* (non-Javadoc)
104      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
105      */

106     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
107         if (parentElement instanceof LibraryStandin) {
108             LibraryStandin standin= (LibraryStandin) parentElement;
109             Object JavaDoc[] children= (Object JavaDoc[])fChildren.get(standin);
110             if (children == null) {
111                 children= new Object JavaDoc[] {new SubElement(standin, SubElement.SOURCE_PATH), new SubElement(standin, SubElement.JAVADOC_URL)};
112                 fChildren.put(standin, children);
113             }
114             return children;
115         }
116         return null;
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
121      */

122     public Object JavaDoc getParent(Object JavaDoc element) {
123         if (element instanceof SubElement) {
124             return ((SubElement)element).getParent();
125         }
126         return null;
127     }
128
129     /* (non-Javadoc)
130      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
131      */

132     public boolean hasChildren(Object JavaDoc element) {
133         return element instanceof LibraryStandin;
134     }
135
136     /**
137      * Sets the array of libraries to be the specified array of libraries
138      * @param libs the new array of libraries to set
139      */

140     public void setLibraries(LibraryLocation[] libs) {
141         fLibraries = new LibraryStandin[libs.length];
142         for (int i = 0; i < libs.length; i++) {
143             fLibraries[i] = new LibraryStandin(libs[i]);
144         }
145         fViewer.refresh();
146     }
147
148     /**
149      * Returns the listing of <code>LibraryLocation</code>s
150      *
151      * @return the listing of <code>LibraryLocation</code>s, or an empty
152      * array, never <code>null</code>
153      */

154     public LibraryLocation[] getLibraries() {
155         LibraryLocation[] locations = new LibraryLocation[fLibraries.length];
156         for (int i = 0; i < locations.length; i++) {
157             locations[i] = fLibraries[i].toLibraryLocation();
158         }
159         return locations;
160     }
161
162     /**
163      * Returns the list of libraries in the given selection. SubElements
164      * are replaced by their parent libraries.
165      * @param selection the current selection
166      *
167      * @return the current set of selected <code>LibraryStandin</code>s from
168      * the current viewer selection, or an empty set, never <code>null</code>
169      */

170     private Set JavaDoc getSelectedLibraries(IStructuredSelection selection) {
171         Set JavaDoc libraries= new HashSet JavaDoc();
172         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
173             Object JavaDoc element= iter.next();
174             if (element instanceof LibraryStandin) {
175                 libraries.add(element);
176             } else if (element instanceof SubElement) {
177                 libraries.add(((SubElement)element).getParent());
178             }
179         }
180         return libraries;
181     }
182
183     /**
184      * Move the libraries of the given selection up.
185      * @param selection the current viewer selection
186      */

187     public void up(IStructuredSelection selection) {
188         Set JavaDoc libraries= getSelectedLibraries(selection);
189         for (int i= 0; i < fLibraries.length - 1; i++) {
190             if (libraries.contains(fLibraries[i + 1])) {
191                 LibraryStandin temp= fLibraries[i];
192                 fLibraries[i]= fLibraries[i + 1];
193                 fLibraries[i + 1]= temp;
194             }
195         }
196         fViewer.refresh();
197         fViewer.setSelection(selection);
198     }
199
200     /**
201      * Move the libraries of the given selection down.
202      * @param selection the current viewer selection
203      */

204     public void down(IStructuredSelection selection) {
205         Set JavaDoc libraries= getSelectedLibraries(selection);
206         for (int i= fLibraries.length - 1; i > 0; i--) {
207             if (libraries.contains(fLibraries[i - 1])) {
208                 LibraryStandin temp= fLibraries[i];
209                 fLibraries[i]= fLibraries[i - 1];
210                 fLibraries[i - 1]= temp;
211             }
212         }
213         fViewer.refresh();
214         fViewer.setSelection(selection);
215     }
216
217     /**
218      * Remove the libraries contained in the given selection.
219      * @param selection the current viewer selection
220      */

221     public void remove(IStructuredSelection selection) {
222         List JavaDoc newLibraries = new ArrayList JavaDoc();
223         for (int i = 0; i < fLibraries.length; i++) {
224             newLibraries.add(fLibraries[i]);
225         }
226         Iterator JavaDoc iterator = selection.iterator();
227         while (iterator.hasNext()) {
228             Object JavaDoc element = iterator.next();
229             if (element instanceof LibraryStandin) {
230                 newLibraries.remove(element);
231             } else {
232                 SubElement subElement = (SubElement)element;
233                 subElement.remove();
234             }
235         }
236         fLibraries= (LibraryStandin[]) newLibraries.toArray(new LibraryStandin[newLibraries.size()]);
237         fViewer.refresh();
238     }
239
240     /**
241      * Add the given libraries before the selection, or after the existing libraries
242      * if the selection is empty.
243      * @param libs the array of <code>LibraryLocation</code>s to add
244      * @param selection the selection to add the new libraries before in the list, or after if the selection
245      * is empty.
246      */

247     public void add(LibraryLocation[] libs, IStructuredSelection selection) {
248         List JavaDoc newLibraries = new ArrayList JavaDoc(fLibraries.length + libs.length);
249         for (int i = 0; i < fLibraries.length; i++) {
250             newLibraries.add(fLibraries[i]);
251         }
252         List JavaDoc toAdd = new ArrayList JavaDoc(libs.length);
253         for (int i = 0; i < libs.length; i++) {
254             toAdd.add(new LibraryStandin(libs[i]));
255         }
256         if (selection.isEmpty()) {
257             newLibraries.addAll(toAdd);
258         } else {
259             Object JavaDoc element= selection.getFirstElement();
260             LibraryStandin firstLib;
261             if (element instanceof LibraryStandin) {
262                 firstLib= (LibraryStandin) element;
263             } else {
264                 firstLib= ((SubElement) element).getParent();
265             }
266             int index = newLibraries.indexOf(firstLib);
267             newLibraries.addAll(index, toAdd);
268         }
269         fLibraries= (LibraryStandin[]) newLibraries.toArray(new LibraryStandin[newLibraries.size()]);
270         fViewer.refresh();
271         fViewer.setSelection(new StructuredSelection(libs), true);
272     }
273
274     /**
275      * Set the given URL as the javadoc location for the libraries contained in
276      * the given selection.
277      * @param javadocLocation the new java doc location to set
278      * @param selection the selection of libraries to set the new javadoc location for
279      */

280     public void setJavadoc(URL JavaDoc javadocLocation, IStructuredSelection selection) {
281         Set JavaDoc libraries= getSelectedLibraries(selection);
282         Iterator JavaDoc iterator = libraries.iterator();
283         while (iterator.hasNext()) {
284             LibraryStandin standin = (LibraryStandin) iterator.next();
285             standin.setJavadocLocation(javadocLocation);
286         }
287         fViewer.refresh();
288     }
289
290     /**
291      * Set the given paths as the source info for the libraries contained in
292      * the given selection.
293      * @param sourceAttachmentPath the path of the new attachment
294      * @param sourceAttachmentRootPath the root path of the new attachment
295      * @param selection the selection of libraries to set the new paths in
296      */

297     public void setSourcePath(IPath sourceAttachmentPath, IPath sourceAttachmentRootPath, IStructuredSelection selection) {
298         Set JavaDoc libraries= getSelectedLibraries(selection);
299         if (sourceAttachmentPath == null) {
300             sourceAttachmentPath = Path.EMPTY;
301         }
302         if (sourceAttachmentRootPath == null) {
303             sourceAttachmentRootPath = Path.EMPTY;
304         }
305         Iterator JavaDoc iterator = libraries.iterator();
306         while (iterator.hasNext()) {
307             LibraryStandin standin = (LibraryStandin) iterator.next();
308             standin.setSystemLibrarySourcePath(sourceAttachmentPath);
309             standin.setPackageRootPath(sourceAttachmentRootPath);
310         }
311         fViewer.refresh();
312     }
313     
314     /**
315      * Returns the standin libraries being edited.
316      *
317      * @return standins
318      */

319     LibraryStandin[] getStandins() {
320         return fLibraries;
321     }
322 }
323
Popular Tags