KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.jdt.internal.debug.ui.classpath;
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
17
18 public class ClasspathModel extends AbstractClasspathEntry {
19     
20     public static final int BOOTSTRAP= 0;
21     public static final int USER= 1;
22     
23     private ClasspathGroup bootstrapEntries;
24     private ClasspathGroup userEntries;
25     
26     public Object JavaDoc addEntry(Object JavaDoc entry) {
27         if (entry instanceof ClasspathGroup) {
28             if (!childEntries.contains(entry)) {
29                 childEntries.add(entry);
30                 return entry;
31             }
32             return null;
33         }
34         ClasspathEntry newEntry= createEntry((IRuntimeClasspathEntry)entry, null);
35         Iterator JavaDoc entries= childEntries.iterator();
36         while (entries.hasNext()) {
37             Object JavaDoc element = entries.next();
38             if (element instanceof ClasspathGroup) {
39                 if(((ClasspathGroup)element).contains(newEntry)) {
40                     return null;
41                 }
42             } else if (element.equals(newEntry)) {
43                 return null;
44             }
45         }
46         childEntries.add(newEntry);
47         return newEntry;
48     }
49     
50     public Object JavaDoc addEntry(int entryType, IRuntimeClasspathEntry entry) {
51         IClasspathEntry entryParent= null;
52         switch (entryType) {
53             case BOOTSTRAP :
54                 entryParent= getBootstrapEntry();
55                 break;
56             case USER :
57                 entryParent= getUserEntry();
58                 break;
59             default :
60                 break;
61         }
62             
63         ClasspathEntry newEntry= createEntry(entry, entryParent);
64         Iterator JavaDoc entries= childEntries.iterator();
65         while (entries.hasNext()) {
66             Object JavaDoc element = entries.next();
67             if (element instanceof ClasspathGroup) {
68                 if(((ClasspathGroup)element).contains(newEntry)) {
69                     return null;
70                 }
71             } else if (element.equals(newEntry)) {
72                 return null;
73             }
74         }
75         if (entryParent != null) {
76             ((ClasspathGroup)entryParent).addEntry(newEntry, null);
77         } else {
78             childEntries.add(newEntry);
79         }
80         return newEntry;
81     }
82     
83     /**
84      * Returns the entries of the given type, or an empty
85      * collection if none.
86      *
87      * @param entryType
88      * @return the entries of the given type, or an empty
89      * collection if none
90      */

91     public IClasspathEntry[] getEntries(int entryType) {
92         switch (entryType) {
93             case BOOTSTRAP :
94                 if (bootstrapEntries != null) {
95                     return bootstrapEntries.getEntries();
96                 }
97                 break;
98             case USER :
99                 if (userEntries != null) {
100                     return userEntries.getEntries();
101                 }
102                 break;
103         }
104         return new IClasspathEntry[0];
105     }
106     
107     public IRuntimeClasspathEntry[] getAllEntries() {
108         IClasspathEntry[] boot = getEntries(BOOTSTRAP);
109         IClasspathEntry[] user = getEntries(USER);
110         IRuntimeClasspathEntry[] all = new IRuntimeClasspathEntry[boot.length + user.length];
111         if (boot.length > 0) {
112             System.arraycopy(boot, 0, all, 0, boot.length);
113         }
114         if (user.length > 0) {
115             System.arraycopy(user, 0, all, boot.length, user.length);
116         }
117         return all;
118     }
119     
120     public void remove(Object JavaDoc entry) {
121         childEntries.remove(entry);
122     }
123     
124     public ClasspathEntry createEntry(IRuntimeClasspathEntry entry, IClasspathEntry entryParent) {
125         if (entry instanceof ClasspathEntry) {
126             entry = ((ClasspathEntry)entry).getDelegate();
127         }
128         if (entryParent == null) {
129             entryParent= this;
130         }
131         return new ClasspathEntry(entry, entryParent);
132     }
133
134     public void removeAll() {
135         if (bootstrapEntries != null) {
136             bootstrapEntries.removeAll();
137         }
138         if (userEntries != null) {
139             userEntries.removeAll();
140         }
141     }
142     
143     public void removeAll(Object JavaDoc[] entries) {
144         
145         for (int i = 0; i < entries.length; i++) {
146             Object JavaDoc object = entries[i];
147             if (object instanceof ClasspathEntry) {
148                 IClasspathEntry entryParent= ((ClasspathEntry)object).getParent();
149                 if (entryParent instanceof ClasspathGroup) {
150                     ((ClasspathGroup)entryParent).removeEntry((ClasspathEntry) object);
151                 } else {
152                     remove(object);
153                 }
154             } else {
155                 remove(object);
156             }
157         }
158     }
159     
160     public void setBootstrapEntries(IRuntimeClasspathEntry[] entries) {
161         if (bootstrapEntries == null) {
162             getBootstrapEntry();
163         }
164         bootstrapEntries.removeAll();
165         for (int i = 0; i < entries.length; i++) {
166             bootstrapEntries.addEntry(new ClasspathEntry(entries[i], bootstrapEntries), null);
167         }
168     }
169
170     private ClasspathGroup createGroupEntry(IRuntimeClasspathEntry[] entries, ClasspathGroup entryParent, String JavaDoc name, boolean canBeRemoved, boolean addEntry) {
171         
172         ClasspathGroup group= new ClasspathGroup(name, entryParent, canBeRemoved);
173         
174         for (int i = 0; i < entries.length; i++) {
175             group.addEntry(new ClasspathEntry(entries[i], group), null);
176         }
177         
178         if (addEntry) {
179             addEntry(group);
180         }
181         return group;
182     }
183
184     public void setUserEntries(IRuntimeClasspathEntry[] entries) {
185         if (userEntries == null) {
186             getUserEntry();
187         }
188         userEntries.removeAll();
189         for (int i = 0; i < entries.length; i++) {
190             userEntries.addEntry(new ClasspathEntry(entries[i], userEntries), null);
191         }
192     }
193
194     public IClasspathEntry getBootstrapEntry() {
195         if (bootstrapEntries == null) {
196             String JavaDoc name= ClasspathMessages.ClasspathModel_0;
197             bootstrapEntries= createGroupEntry(new IRuntimeClasspathEntry[0], null, name, false, true);
198         }
199         return bootstrapEntries;
200     }
201     
202     public IClasspathEntry getUserEntry() {
203         if (userEntries == null) {
204             String JavaDoc name= ClasspathMessages.ClasspathModel_1;
205             userEntries= createGroupEntry(new IRuntimeClasspathEntry[0], null, name, false, true);
206         }
207         return userEntries;
208     }
209     
210     /**
211      * Constructs a new classpath model with root entries
212      */

213     public ClasspathModel() {
214         super();
215         getBootstrapEntry();
216         getUserEntry();
217     }
218
219     /* (non-Javadoc)
220      * @see org.eclipse.jdt.internal.debug.ui.classpath.IClasspathEntry#isEditable()
221      */

222     public boolean isEditable() {
223         return false;
224     }
225
226 }
227
Popular Tags