KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > AntClasspathContentProvider


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.ant.internal.ui.preferences;
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.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.ITreeContentProvider;
20 import org.eclipse.jface.viewers.StructuredSelection;
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 AntClasspathContentProvider implements ITreeContentProvider {
29     private TreeViewer treeViewer;
30     private ClasspathModel model= null;
31     private boolean refreshEnabled= false;
32     private boolean refreshRequested= false;
33         
34     public void add(IClasspathEntry parent, Object JavaDoc child) {
35         Object JavaDoc newEntry= null;
36         boolean added= false;
37         if (parent == null || parent == model) {
38             added= true;
39             newEntry= model.addEntry(child);
40             if (newEntry == null) {
41                 //entry already exists
42
newEntry= model.createEntry(child, model);
43                 added= false;
44             }
45             parent= model;
46         } else if (parent instanceof GlobalClasspathEntries) {
47             GlobalClasspathEntries globalParent= (GlobalClasspathEntries) parent;
48             newEntry= model.createEntry(child, globalParent);
49             ClasspathEntry newClasspathEntry= (ClasspathEntry) newEntry;
50             if (!globalParent.contains(newClasspathEntry)) {
51                 added= true;
52                 globalParent.addEntry(newClasspathEntry);
53             }
54         }
55         if (newEntry != null) {
56             if (added) {
57                 treeViewer.add(parent, newEntry);
58             }
59             treeViewer.setExpandedState(parent, true);
60             treeViewer.setSelection(new StructuredSelection(newEntry), true);
61             refresh();
62         }
63     }
64     
65     public void add(int entryType, Object JavaDoc child) {
66         Object JavaDoc newEntry= model.addEntry(entryType, child);
67         if (newEntry != null) {
68             treeViewer.add(getParent(newEntry), newEntry);
69             refresh();
70         }
71     }
72
73     public void removeAll() {
74         model.removeAll();
75         refresh();
76     }
77     
78     private void refresh() {
79         if (refreshEnabled) {
80             treeViewer.refresh();
81             refreshRequested= false;
82         } else {
83             refreshRequested= true;
84         }
85     }
86     
87     public void removeAllGlobalAntClasspathEntries() {
88         model.removeAll(ClasspathModel.ANT_HOME);
89         refresh();
90     }
91
92     /**
93      * @see ITreeContentProvider#getParent(Object)
94      */

95     public Object JavaDoc getParent(Object JavaDoc element) {
96         if (element instanceof ClasspathEntry) {
97             return ((ClasspathEntry)element).getParent();
98         }
99         if (element instanceof GlobalClasspathEntries) {
100             return model;
101         }
102         
103         return null;
104     }
105
106     /**
107      * @see ITreeContentProvider#hasChildren(Object)
108      */

109     public boolean hasChildren(Object JavaDoc element) {
110         if (element instanceof ClasspathEntry) {
111             return false;
112         }
113         if (element instanceof GlobalClasspathEntries) {
114             return ((GlobalClasspathEntries)element).hasEntries();
115             
116         }
117         
118         if (element instanceof ClasspathModel) {
119             return ((ClasspathModel) element).hasEntries();
120         }
121         return false;
122     }
123
124     /* (non-Javadoc)
125      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
126      */

127     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
128         return getChildren(inputElement);
129     }
130
131     
132     /* (non-Javadoc)
133      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
134      */

135     public void dispose() {
136
137     }
138
139     /* (non-Javadoc)
140      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
141      */

142     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
143         treeViewer = (TreeViewer) viewer;
144         
145         if (newInput != null) {
146             model= (ClasspathModel)newInput;
147         } else {
148             if (model != null) {
149                 model.removeAll();
150             }
151             model= null;
152         }
153     }
154
155     /* (non-Javadoc)
156      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
157      */

158     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
159         if (parentElement instanceof GlobalClasspathEntries) {
160             return ((GlobalClasspathEntries)parentElement).getEntries();
161         }
162         if (parentElement instanceof ClasspathModel) {
163             return ((ClasspathModel)parentElement).getEntries();
164         }
165         if (parentElement == null) {
166             List JavaDoc all= new ArrayList JavaDoc();
167             Object JavaDoc[] topEntries= model.getEntries();
168             for (int i = 0; i < topEntries.length; i++) {
169                 Object JavaDoc object = topEntries[i];
170                 if (object instanceof ClasspathEntry) {
171                     all.add(object);
172                 } else if (object instanceof GlobalClasspathEntries) {
173                     all.addAll(Arrays.asList(((GlobalClasspathEntries)object).getEntries()));
174                 }
175             }
176             return all.toArray();
177         }
178         
179         return null;
180     }
181
182     public void remove(IStructuredSelection selection) {
183         Object JavaDoc[] array= selection.toArray();
184         model.removeAll(array);
185         treeViewer.remove(array);
186         refresh();
187     }
188
189     public ClasspathModel getModel() {
190         return model;
191     }
192
193     public void setRefreshEnabled(boolean refreshEnabled) {
194         this.refreshEnabled = refreshEnabled;
195         treeViewer.getTree().setRedraw(refreshEnabled);
196         if (refreshEnabled && refreshRequested) {
197             refresh();
198         }
199     }
200
201     /**
202      * @param currentParent
203      */

204     public void setEntries(IClasspathEntry currentParent, List JavaDoc entries) {
205         if (currentParent instanceof GlobalClasspathEntries) {
206             GlobalClasspathEntries group= (GlobalClasspathEntries) currentParent;
207             group.setEntries(entries);
208         }
209         
210     }
211 }
212
Popular Tags