1 11 package org.eclipse.jdt.internal.debug.ui.jres; 12 13 import java.net.URL ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.jdt.launching.LibraryLocation; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.jface.viewers.ITreeContentProvider; 26 import org.eclipse.jface.viewers.StructuredSelection; 27 import org.eclipse.jface.viewers.Viewer; 28 29 38 public class LibraryContentProvider implements ITreeContentProvider { 39 40 private Viewer fViewer; 41 42 45 public class SubElement { 46 47 public static final int JAVADOC_URL= 1; 48 public static final int SOURCE_PATH= 2; 49 50 private LibraryStandin fParent; 51 private int fType; 52 53 public SubElement(LibraryStandin parent, int type) { 54 fParent= parent; 55 fType= type; 56 } 57 58 public LibraryStandin getParent() { 59 return fParent; 60 } 61 62 public int getType() { 63 return fType; 64 } 65 66 public void remove() { 67 switch (fType) { 68 case JAVADOC_URL: 69 fParent.setJavadocLocation(null); 70 break; 71 case SOURCE_PATH: 72 fParent.setSystemLibrarySourcePath(Path.EMPTY); 73 break; 74 } 75 } 76 } 77 78 private HashMap fChildren= new HashMap (); 79 80 private LibraryStandin[] fLibraries= new LibraryStandin[0]; 81 82 85 public void dispose() { 86 fChildren.clear(); 87 } 88 89 92 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 93 fViewer = viewer; 94 } 95 96 99 public Object [] getElements(Object inputElement) { 100 return fLibraries; 101 } 102 103 106 public Object [] getChildren(Object parentElement) { 107 if (parentElement instanceof LibraryStandin) { 108 LibraryStandin standin= (LibraryStandin) parentElement; 109 Object [] children= (Object [])fChildren.get(standin); 110 if (children == null) { 111 children= new Object [] {new SubElement(standin, SubElement.SOURCE_PATH), new SubElement(standin, SubElement.JAVADOC_URL)}; 112 fChildren.put(standin, children); 113 } 114 return children; 115 } 116 return null; 117 } 118 119 122 public Object getParent(Object element) { 123 if (element instanceof SubElement) { 124 return ((SubElement)element).getParent(); 125 } 126 return null; 127 } 128 129 132 public boolean hasChildren(Object element) { 133 return element instanceof LibraryStandin; 134 } 135 136 140 public void setLibraries(LibraryLocation[] libs) { 141 fLibraries = new LibraryStandin[libs.length]; 142 for (int i = 0; i < libs.length; i++) { 143 fLibraries[i] = new LibraryStandin(libs[i]); 144 } 145 fViewer.refresh(); 146 } 147 148 154 public LibraryLocation[] getLibraries() { 155 LibraryLocation[] locations = new LibraryLocation[fLibraries.length]; 156 for (int i = 0; i < locations.length; i++) { 157 locations[i] = fLibraries[i].toLibraryLocation(); 158 } 159 return locations; 160 } 161 162 170 private Set getSelectedLibraries(IStructuredSelection selection) { 171 Set libraries= new HashSet (); 172 for (Iterator iter= selection.iterator(); iter.hasNext();) { 173 Object element= iter.next(); 174 if (element instanceof LibraryStandin) { 175 libraries.add(element); 176 } else if (element instanceof SubElement) { 177 libraries.add(((SubElement)element).getParent()); 178 } 179 } 180 return libraries; 181 } 182 183 187 public void up(IStructuredSelection selection) { 188 Set libraries= getSelectedLibraries(selection); 189 for (int i= 0; i < fLibraries.length - 1; i++) { 190 if (libraries.contains(fLibraries[i + 1])) { 191 LibraryStandin temp= fLibraries[i]; 192 fLibraries[i]= fLibraries[i + 1]; 193 fLibraries[i + 1]= temp; 194 } 195 } 196 fViewer.refresh(); 197 fViewer.setSelection(selection); 198 } 199 200 204 public void down(IStructuredSelection selection) { 205 Set libraries= getSelectedLibraries(selection); 206 for (int i= fLibraries.length - 1; i > 0; i--) { 207 if (libraries.contains(fLibraries[i - 1])) { 208 LibraryStandin temp= fLibraries[i]; 209 fLibraries[i]= fLibraries[i - 1]; 210 fLibraries[i - 1]= temp; 211 } 212 } 213 fViewer.refresh(); 214 fViewer.setSelection(selection); 215 } 216 217 221 public void remove(IStructuredSelection selection) { 222 List newLibraries = new ArrayList (); 223 for (int i = 0; i < fLibraries.length; i++) { 224 newLibraries.add(fLibraries[i]); 225 } 226 Iterator iterator = selection.iterator(); 227 while (iterator.hasNext()) { 228 Object element = iterator.next(); 229 if (element instanceof LibraryStandin) { 230 newLibraries.remove(element); 231 } else { 232 SubElement subElement = (SubElement)element; 233 subElement.remove(); 234 } 235 } 236 fLibraries= (LibraryStandin[]) newLibraries.toArray(new LibraryStandin[newLibraries.size()]); 237 fViewer.refresh(); 238 } 239 240 247 public void add(LibraryLocation[] libs, IStructuredSelection selection) { 248 List newLibraries = new ArrayList (fLibraries.length + libs.length); 249 for (int i = 0; i < fLibraries.length; i++) { 250 newLibraries.add(fLibraries[i]); 251 } 252 List toAdd = new ArrayList (libs.length); 253 for (int i = 0; i < libs.length; i++) { 254 toAdd.add(new LibraryStandin(libs[i])); 255 } 256 if (selection.isEmpty()) { 257 newLibraries.addAll(toAdd); 258 } else { 259 Object element= selection.getFirstElement(); 260 LibraryStandin firstLib; 261 if (element instanceof LibraryStandin) { 262 firstLib= (LibraryStandin) element; 263 } else { 264 firstLib= ((SubElement) element).getParent(); 265 } 266 int index = newLibraries.indexOf(firstLib); 267 newLibraries.addAll(index, toAdd); 268 } 269 fLibraries= (LibraryStandin[]) newLibraries.toArray(new LibraryStandin[newLibraries.size()]); 270 fViewer.refresh(); 271 fViewer.setSelection(new StructuredSelection(libs), true); 272 } 273 274 280 public void setJavadoc(URL javadocLocation, IStructuredSelection selection) { 281 Set libraries= getSelectedLibraries(selection); 282 Iterator iterator = libraries.iterator(); 283 while (iterator.hasNext()) { 284 LibraryStandin standin = (LibraryStandin) iterator.next(); 285 standin.setJavadocLocation(javadocLocation); 286 } 287 fViewer.refresh(); 288 } 289 290 297 public void setSourcePath(IPath sourceAttachmentPath, IPath sourceAttachmentRootPath, IStructuredSelection selection) { 298 Set libraries= getSelectedLibraries(selection); 299 if (sourceAttachmentPath == null) { 300 sourceAttachmentPath = Path.EMPTY; 301 } 302 if (sourceAttachmentRootPath == null) { 303 sourceAttachmentRootPath = Path.EMPTY; 304 } 305 Iterator iterator = libraries.iterator(); 306 while (iterator.hasNext()) { 307 LibraryStandin standin = (LibraryStandin) iterator.next(); 308 standin.setSystemLibrarySourcePath(sourceAttachmentPath); 309 standin.setPackageRootPath(sourceAttachmentRootPath); 310 } 311 fViewer.refresh(); 312 } 313 314 319 LibraryStandin[] getStandins() { 320 return fLibraries; 321 } 322 } 323 | Popular Tags |