1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.Path; 19 20 import org.eclipse.jdt.core.IClasspathContainer; 21 import org.eclipse.jdt.core.IClasspathEntry; 22 import org.eclipse.jdt.core.IJavaProject; 23 import org.eclipse.jdt.core.JavaCore; 24 25 public class CPUserLibraryElement { 26 27 private class UpdatedClasspathContainer implements IClasspathContainer { 28 29 32 public IClasspathEntry[] getClasspathEntries() { 33 CPListElement[] children= getChildren(); 34 IClasspathEntry[] entries= new IClasspathEntry[children.length]; 35 for (int i= 0; i < entries.length; i++) { 36 entries[i]= children[i].getClasspathEntry(); 37 } 38 return entries; 39 } 40 41 44 public String getDescription() { 45 return getName(); 46 } 47 48 51 public int getKind() { 52 return isSystemLibrary() ? IClasspathContainer.K_SYSTEM : K_APPLICATION; 53 } 54 55 58 public IPath getPath() { 59 return CPUserLibraryElement.this.getPath(); 60 } 61 } 62 63 64 private String fName; 65 private List fChildren; 66 private boolean fIsSystemLibrary; 67 68 public CPUserLibraryElement(String name, IClasspathContainer container, IJavaProject project) { 69 fName= name; 70 fChildren= new ArrayList (); 71 if (container != null) { 72 IClasspathEntry[] entries= container.getClasspathEntries(); 73 CPListElement[] res= new CPListElement[entries.length]; 74 for (int i= 0; i < res.length; i++) { 75 IClasspathEntry curr= entries[i]; 76 CPListElement elem= CPListElement.createFromExisting(this, curr, project); 77 fChildren.add(elem); 80 } 81 fIsSystemLibrary= container.getKind() == IClasspathContainer.K_SYSTEM; 82 } else { 83 fIsSystemLibrary= false; 84 } 85 } 86 87 public CPUserLibraryElement(String name, boolean isSystemLibrary, CPListElement[] children) { 88 fName= name; 89 fChildren= new ArrayList (); 90 if (children != null) { 91 for (int i= 0; i < children.length; i++) { 92 fChildren.add(children[i]); 93 } 94 } 95 fIsSystemLibrary= isSystemLibrary; 96 } 97 98 public CPListElement[] getChildren() { 99 return (CPListElement[]) fChildren.toArray(new CPListElement[fChildren.size()]); 100 } 101 102 public String getName() { 103 return fName; 104 } 105 106 public IPath getPath() { 107 return new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(fName); 108 } 109 110 public boolean isSystemLibrary() { 111 return fIsSystemLibrary; 112 } 113 114 public void add(CPListElement element) { 115 if (!fChildren.contains(element)) { 116 fChildren.add(element); 117 } 118 } 119 120 private List moveUp(List elements, List move) { 121 int nElements= elements.size(); 122 List res= new ArrayList (nElements); 123 Object floating= null; 124 for (int i= 0; i < nElements; i++) { 125 Object curr= elements.get(i); 126 if (move.contains(curr)) { 127 res.add(curr); 128 } else { 129 if (floating != null) { 130 res.add(floating); 131 } 132 floating= curr; 133 } 134 } 135 if (floating != null) { 136 res.add(floating); 137 } 138 return res; 139 } 140 141 public void moveUp(List toMoveUp) { 142 if (toMoveUp.size() > 0) { 143 fChildren= moveUp(fChildren, toMoveUp); 144 } 145 } 146 147 public void moveDown(List toMoveDown) { 148 if (toMoveDown.size() > 0) { 149 Collections.reverse(fChildren); 150 fChildren= moveUp(fChildren, toMoveDown); 151 Collections.reverse(fChildren); 152 } 153 } 154 155 156 public void remove(CPListElement element) { 157 fChildren.remove(element); 158 } 159 160 public void replace(CPListElement existingElement, CPListElement element) { 161 if (fChildren.contains(element)) { 162 fChildren.remove(existingElement); 163 } else { 164 int index= fChildren.indexOf(existingElement); 165 if (index != -1) { 166 fChildren.set(index, element); 167 } else { 168 fChildren.add(element); 169 } 170 element.setAttributesFromExisting(existingElement); 171 } 172 } 173 174 public IClasspathContainer getUpdatedContainer() { 175 return new UpdatedClasspathContainer(); 176 } 177 178 public boolean hasChanges(IClasspathContainer oldContainer) { 179 if (oldContainer == null || (oldContainer.getKind() == IClasspathContainer.K_SYSTEM) != fIsSystemLibrary) { 180 return true; 181 } 182 IClasspathEntry[] oldEntries= oldContainer.getClasspathEntries(); 183 if (fChildren.size() != oldEntries.length) { 184 return true; 185 } 186 for (int i= 0; i < oldEntries.length; i++) { 187 CPListElement child= (CPListElement) fChildren.get(i); 188 if (!child.getClasspathEntry().equals(oldEntries[i])) { 189 return true; 190 } 191 } 192 return false; 193 } 194 195 196 } 197 | Popular Tags |