1 11 package org.eclipse.ant.internal.ui.preferences; 12 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 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 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 child) { 35 Object 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 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 child) { 66 Object 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 95 public Object getParent(Object 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 109 public boolean hasChildren(Object 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 127 public Object [] getElements(Object inputElement) { 128 return getChildren(inputElement); 129 } 130 131 132 135 public void dispose() { 136 137 } 138 139 142 public void inputChanged(Viewer viewer, Object oldInput, Object 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 158 public Object [] getChildren(Object 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 all= new ArrayList (); 167 Object [] topEntries= model.getEntries(); 168 for (int i = 0; i < topEntries.length; i++) { 169 Object 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 [] 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 204 public void setEntries(IClasspathEntry currentParent, List entries) { 205 if (currentParent instanceof GlobalClasspathEntries) { 206 GlobalClasspathEntries group= (GlobalClasspathEntries) currentParent; 207 group.setEntries(entries); 208 } 209 210 } 211 } 212 | Popular Tags |