KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > CPUserLibraryElement


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.ui.wizards.buildpaths;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19
20 import org.eclipse.jdt.core.IClasspathContainer;
21 import org.eclipse.jdt.core.IClasspathEntry;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.JavaCore;
24
25 public class CPUserLibraryElement {
26     
27     private class UpdatedClasspathContainer implements IClasspathContainer {
28                 
29         /* (non-Javadoc)
30          * @see org.eclipse.jdt.core.IClasspathContainer#getClasspathEntries()
31          */

32         public IClasspathEntry[] getClasspathEntries() {
33             CPListElement[] children= getChildren();
34             IClasspathEntry[] entries= new IClasspathEntry[children.length];
35             for (int i= 0; i < entries.length; i++) {
36                 entries[i]= children[i].getClasspathEntry();
37             }
38             return entries;
39         }
40
41         /* (non-Javadoc)
42          * @see org.eclipse.jdt.core.IClasspathContainer#getDescription()
43          */

44         public String JavaDoc getDescription() {
45             return getName();
46         }
47
48         /* (non-Javadoc)
49          * @see org.eclipse.jdt.core.IClasspathContainer#getKind()
50          */

51         public int getKind() {
52             return isSystemLibrary() ? IClasspathContainer.K_SYSTEM : K_APPLICATION;
53         }
54
55         /* (non-Javadoc)
56          * @see org.eclipse.jdt.core.IClasspathContainer#getPath()
57          */

58         public IPath getPath() {
59             return CPUserLibraryElement.this.getPath();
60         }
61     }
62     
63     
64     private String JavaDoc fName;
65     private List JavaDoc fChildren;
66     private boolean fIsSystemLibrary;
67
68     public CPUserLibraryElement(String JavaDoc name, IClasspathContainer container, IJavaProject project) {
69         fName= name;
70         fChildren= new ArrayList JavaDoc();
71         if (container != null) {
72             IClasspathEntry[] entries= container.getClasspathEntries();
73             CPListElement[] res= new CPListElement[entries.length];
74             for (int i= 0; i < res.length; i++) {
75                 IClasspathEntry curr= entries[i];
76                 CPListElement elem= CPListElement.createFromExisting(this, curr, project);
77                 //elem.setAttribute(CPListElement.SOURCEATTACHMENT, curr.getSourceAttachmentPath());
78
//elem.setAttribute(CPListElement.JAVADOC, JavaUI.getLibraryJavadocLocation(curr.getPath()));
79
fChildren.add(elem);
80             }
81             fIsSystemLibrary= container.getKind() == IClasspathContainer.K_SYSTEM;
82         } else {
83             fIsSystemLibrary= false;
84         }
85     }
86     
87     public CPUserLibraryElement(String JavaDoc name, boolean isSystemLibrary, CPListElement[] children) {
88         fName= name;
89         fChildren= new ArrayList JavaDoc();
90         if (children != null) {
91             for (int i= 0; i < children.length; i++) {
92                 fChildren.add(children[i]);
93             }
94         }
95         fIsSystemLibrary= isSystemLibrary;
96     }
97     
98     public CPListElement[] getChildren() {
99         return (CPListElement[]) fChildren.toArray(new CPListElement[fChildren.size()]);
100     }
101
102     public String JavaDoc getName() {
103         return fName;
104     }
105     
106     public IPath getPath() {
107         return new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(fName);
108     }
109
110     public boolean isSystemLibrary() {
111         return fIsSystemLibrary;
112     }
113     
114     public void add(CPListElement element) {
115         if (!fChildren.contains(element)) {
116             fChildren.add(element);
117         }
118     }
119         
120     private List JavaDoc moveUp(List JavaDoc elements, List JavaDoc move) {
121         int nElements= elements.size();
122         List JavaDoc res= new ArrayList JavaDoc(nElements);
123         Object JavaDoc floating= null;
124         for (int i= 0; i < nElements; i++) {
125             Object JavaDoc curr= elements.get(i);
126             if (move.contains(curr)) {
127                 res.add(curr);
128             } else {
129                 if (floating != null) {
130                     res.add(floating);
131                 }
132                 floating= curr;
133             }
134         }
135         if (floating != null) {
136             res.add(floating);
137         }
138         return res;
139     }
140     
141     public void moveUp(List JavaDoc toMoveUp) {
142         if (toMoveUp.size() > 0) {
143             fChildren= moveUp(fChildren, toMoveUp);
144         }
145     }
146     
147     public void moveDown(List JavaDoc toMoveDown) {
148         if (toMoveDown.size() > 0) {
149             Collections.reverse(fChildren);
150             fChildren= moveUp(fChildren, toMoveDown);
151             Collections.reverse(fChildren);
152         }
153     }
154     
155     
156     public void remove(CPListElement element) {
157         fChildren.remove(element);
158     }
159     
160     public void replace(CPListElement existingElement, CPListElement element) {
161         if (fChildren.contains(element)) {
162             fChildren.remove(existingElement);
163         } else {
164             int index= fChildren.indexOf(existingElement);
165             if (index != -1) {
166                 fChildren.set(index, element);
167             } else {
168                 fChildren.add(element);
169             }
170             element.setAttributesFromExisting(existingElement);
171         }
172     }
173     
174     public IClasspathContainer getUpdatedContainer() {
175         return new UpdatedClasspathContainer();
176     }
177         
178     public boolean hasChanges(IClasspathContainer oldContainer) {
179         if (oldContainer == null || (oldContainer.getKind() == IClasspathContainer.K_SYSTEM) != fIsSystemLibrary) {
180             return true;
181         }
182         IClasspathEntry[] oldEntries= oldContainer.getClasspathEntries();
183         if (fChildren.size() != oldEntries.length) {
184             return true;
185         }
186         for (int i= 0; i < oldEntries.length; i++) {
187             CPListElement child= (CPListElement) fChildren.get(i);
188             if (!child.getClasspathEntry().equals(oldEntries[i])) {
189                 return true;
190             }
191         }
192         return false;
193     }
194     
195     
196 }
197
Popular Tags