KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > 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.ant.internal.ui.preferences;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import org.eclipse.ant.core.IAntClasspathEntry;
18
19 public class ClasspathModel extends AbstractClasspathEntry {
20     
21     public static final int ANT_HOME= 0;
22     public static final int GLOBAL_USER= 1;
23     public static final int USER= 2;
24     public static final int CONTRIBUTED= 3;
25     
26     private GlobalClasspathEntries fAntHomeEntry;
27     private GlobalClasspathEntries fUserGlobalEntry;
28     private GlobalClasspathEntries fContributedGlobalEntry;
29     
30     public Object JavaDoc addEntry(Object JavaDoc entry) {
31         if (entry instanceof GlobalClasspathEntries) {
32             if (!fChildEntries.contains(entry)) {
33                 fChildEntries.add(entry);
34                 return entry;
35             }
36             return null;
37         }
38         ClasspathEntry newEntry= createEntry(entry, null);
39         Iterator JavaDoc entries= fChildEntries.iterator();
40         while (entries.hasNext()) {
41             Object JavaDoc element = entries.next();
42             if (element instanceof GlobalClasspathEntries) {
43                 if(((GlobalClasspathEntries)element).contains(newEntry)) {
44                     return null;
45                 }
46             } else if (element.equals(newEntry)) {
47                 return null;
48             }
49         }
50         fChildEntries.add(newEntry);
51         return newEntry;
52     }
53     
54     public Object JavaDoc addEntry(int entryType, Object JavaDoc entry) {
55         IClasspathEntry entryParent= null;
56         switch (entryType) {
57             case ANT_HOME :
58                 if (fAntHomeEntry == null) {
59                     String JavaDoc name= AntPreferencesMessages.ClasspathModel_2;
60                     fAntHomeEntry= createGlobalEntry(new IAntClasspathEntry[0], name, false, true, ANT_HOME);
61                 }
62                 entryParent= fAntHomeEntry;
63                 break;
64             case GLOBAL_USER :
65                 if (fUserGlobalEntry == null) {
66                     String JavaDoc name= AntPreferencesMessages.ClasspathModel_3;
67                     fUserGlobalEntry= createGlobalEntry(new IAntClasspathEntry[0], name, true, true, GLOBAL_USER);
68                 }
69                 entryParent= fUserGlobalEntry;
70                 break;
71             case CONTRIBUTED :
72                 if (fContributedGlobalEntry == null) {
73                     String JavaDoc name= AntPreferencesMessages.ClasspathModel_0;
74                     fContributedGlobalEntry= createGlobalEntry(new IAntClasspathEntry[0], name, true, true, CONTRIBUTED);
75                 }
76                 entryParent= fContributedGlobalEntry;
77                 break;
78             default :
79                 break;
80         }
81             
82         ClasspathEntry newEntry= createEntry(entry, entryParent);
83         Iterator JavaDoc entries= fChildEntries.iterator();
84         while (entries.hasNext()) {
85             Object JavaDoc element = entries.next();
86             if (element instanceof GlobalClasspathEntries) {
87                 if(((GlobalClasspathEntries)element).contains(newEntry)) {
88                     return null;
89                 }
90             } else if (element.equals(newEntry)) {
91                 return null;
92             }
93         }
94         if (entryParent != null) {
95             ((GlobalClasspathEntries)entryParent).addEntry(newEntry);
96         } else {
97             fChildEntries.add(newEntry);
98         }
99         return newEntry;
100     }
101     
102     /**
103      * Returns entries of the specified kind, possibly empty.
104      *
105      * @param entryType kind of entries to retrieve
106      * @return entries of the specified kind, possibly empty
107      */

108     public IAntClasspathEntry[] getEntries(int entryType) {
109         switch (entryType) {
110             case ANT_HOME :
111                 if (fAntHomeEntry != null) {
112                     return fAntHomeEntry.getEntries();
113                 }
114                 break;
115             case GLOBAL_USER :
116                 if (fUserGlobalEntry != null) {
117                     return fUserGlobalEntry.getEntries();
118                 }
119                 break;
120             case USER :
121                 return getUserEntries();
122             case CONTRIBUTED :
123                 if (fContributedGlobalEntry != null) {
124                     return fContributedGlobalEntry.getEntries();
125                 }
126                 break;
127         }
128         return new IAntClasspathEntry[0];
129     }
130     
131     public void remove(Object JavaDoc entry) {
132         fChildEntries.remove(entry);
133         if (entry == fUserGlobalEntry) {
134             fUserGlobalEntry= null;
135         }
136     }
137     
138     public ClasspathEntry createEntry(Object JavaDoc entry, IClasspathEntry entryParent) {
139         if (entryParent == null) {
140             entryParent= this;
141         }
142         return new ClasspathEntry(entry, entryParent);
143     }
144
145     public void removeAll() {
146         if (fAntHomeEntry != null) {
147             fAntHomeEntry.removeAll();
148         }
149         if (fUserGlobalEntry != null) {
150             fUserGlobalEntry.removeAll();
151         }
152     }
153     
154     public void removeAll(int entryType) {
155         switch (entryType) {
156             case ANT_HOME :
157                 if (fAntHomeEntry != null) {
158                     fAntHomeEntry.removeAll();
159                 }
160                 break;
161             case GLOBAL_USER :
162                 if (fUserGlobalEntry != null) {
163                     fUserGlobalEntry.removeAll();
164                 }
165                 break;
166             default :
167                 break;
168         }
169     }
170     
171     public void removeAll(Object JavaDoc[] entries) {
172         
173         for (int i = 0; i < entries.length; i++) {
174             Object JavaDoc object = entries[i];
175             if (object instanceof ClasspathEntry) {
176                 IClasspathEntry entryParent= ((ClasspathEntry)object).getParent();
177                 if (entryParent instanceof GlobalClasspathEntries) {
178                     ((GlobalClasspathEntries)entryParent).removeEntry((ClasspathEntry) object);
179                 } else {
180                     remove(object);
181                 }
182             } else {
183                 remove(object);
184             }
185         }
186     }
187
188     public void setAntHomeEntries(IAntClasspathEntry[] entries) {
189         if (fAntHomeEntry == null) {
190             String JavaDoc name= AntPreferencesMessages.ClasspathModel_2;
191             fAntHomeEntry= createGlobalEntry(entries, name, false, true, ANT_HOME);
192         } else {
193             fAntHomeEntry.removeAll();
194             for (int i = 0; i < entries.length; i++) {
195                 fAntHomeEntry.addEntry(new ClasspathEntry(entries[i], fAntHomeEntry));
196             }
197         }
198     }
199
200     private GlobalClasspathEntries createGlobalEntry(IAntClasspathEntry[] entries, String JavaDoc name, boolean canBeRemoved, boolean addEntry, int type) {
201         
202         GlobalClasspathEntries global= new GlobalClasspathEntries(name, this, canBeRemoved, type);
203         
204         for (int i = 0; i < entries.length; i++) {
205             global.addEntry(new ClasspathEntry(entries[i], global));
206         }
207         
208         if (addEntry) {
209             addEntry(global);
210         }
211         return global;
212     }
213
214     public void setGlobalEntries(IAntClasspathEntry[] entries) {
215         if (fUserGlobalEntry == null) {
216             String JavaDoc name= AntPreferencesMessages.ClasspathModel_3;
217             fUserGlobalEntry= createGlobalEntry(entries, name, true, true, GLOBAL_USER);
218         } else {
219             fUserGlobalEntry.removeAll();
220             for (int i = 0; i < entries.length; i++) {
221                 fUserGlobalEntry.addEntry(new ClasspathEntry(entries[i], fUserGlobalEntry));
222             }
223         }
224     }
225     
226     public void setContributedEntries(IAntClasspathEntry[] entries) {
227         if (fContributedGlobalEntry == null) {
228             String JavaDoc name= AntPreferencesMessages.ClasspathModel_0;
229             fContributedGlobalEntry= createGlobalEntry(entries, name, false, true, CONTRIBUTED);
230         } else {
231             fContributedGlobalEntry.removeAll();
232             for (int i = 0; i < entries.length; i++) {
233                 fContributedGlobalEntry.addEntry(new ClasspathEntry(entries[i], fContributedGlobalEntry));
234             }
235         }
236     }
237     
238     private IAntClasspathEntry[] getUserEntries() {
239         List JavaDoc userEntries= new ArrayList JavaDoc(fChildEntries.size());
240         Iterator JavaDoc itr= fChildEntries.iterator();
241         while (itr.hasNext()) {
242             IClasspathEntry element = (IClasspathEntry) itr.next();
243             if (element instanceof GlobalClasspathEntries) {
244                 continue;
245             }
246             userEntries.add(element);
247         }
248         return (IAntClasspathEntry[])userEntries.toArray(new IAntClasspathEntry[userEntries.size()]);
249     }
250 }
251
Popular Tags