001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.buildpaths;
011:
012: import java.util.ArrayList;
013: import java.util.Collections;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IPath;
017: import org.eclipse.core.runtime.Path;
018:
019: import org.eclipse.jdt.core.IClasspathContainer;
020: import org.eclipse.jdt.core.IClasspathEntry;
021: import org.eclipse.jdt.core.IJavaProject;
022: import org.eclipse.jdt.core.JavaCore;
023:
024: public class CPUserLibraryElement {
025:
026: private class UpdatedClasspathContainer implements
027: IClasspathContainer {
028:
029: /* (non-Javadoc)
030: * @see org.eclipse.jdt.core.IClasspathContainer#getClasspathEntries()
031: */
032: public IClasspathEntry[] getClasspathEntries() {
033: CPListElement[] children = getChildren();
034: IClasspathEntry[] entries = new IClasspathEntry[children.length];
035: for (int i = 0; i < entries.length; i++) {
036: entries[i] = children[i].getClasspathEntry();
037: }
038: return entries;
039: }
040:
041: /* (non-Javadoc)
042: * @see org.eclipse.jdt.core.IClasspathContainer#getDescription()
043: */
044: public String getDescription() {
045: return getName();
046: }
047:
048: /* (non-Javadoc)
049: * @see org.eclipse.jdt.core.IClasspathContainer#getKind()
050: */
051: public int getKind() {
052: return isSystemLibrary() ? IClasspathContainer.K_SYSTEM
053: : K_APPLICATION;
054: }
055:
056: /* (non-Javadoc)
057: * @see org.eclipse.jdt.core.IClasspathContainer#getPath()
058: */
059: public IPath getPath() {
060: return CPUserLibraryElement.this .getPath();
061: }
062: }
063:
064: private String fName;
065: private List fChildren;
066: private boolean fIsSystemLibrary;
067:
068: public CPUserLibraryElement(String name,
069: IClasspathContainer container, IJavaProject project) {
070: fName = name;
071: fChildren = new ArrayList();
072: if (container != null) {
073: IClasspathEntry[] entries = container.getClasspathEntries();
074: CPListElement[] res = new CPListElement[entries.length];
075: for (int i = 0; i < res.length; i++) {
076: IClasspathEntry curr = entries[i];
077: CPListElement elem = CPListElement.createFromExisting(
078: this , curr, project);
079: //elem.setAttribute(CPListElement.SOURCEATTACHMENT, curr.getSourceAttachmentPath());
080: //elem.setAttribute(CPListElement.JAVADOC, JavaUI.getLibraryJavadocLocation(curr.getPath()));
081: fChildren.add(elem);
082: }
083: fIsSystemLibrary = container.getKind() == IClasspathContainer.K_SYSTEM;
084: } else {
085: fIsSystemLibrary = false;
086: }
087: }
088:
089: public CPUserLibraryElement(String name, boolean isSystemLibrary,
090: CPListElement[] children) {
091: fName = name;
092: fChildren = new ArrayList();
093: if (children != null) {
094: for (int i = 0; i < children.length; i++) {
095: fChildren.add(children[i]);
096: }
097: }
098: fIsSystemLibrary = isSystemLibrary;
099: }
100:
101: public CPListElement[] getChildren() {
102: return (CPListElement[]) fChildren
103: .toArray(new CPListElement[fChildren.size()]);
104: }
105:
106: public String getName() {
107: return fName;
108: }
109:
110: public IPath getPath() {
111: return new Path(JavaCore.USER_LIBRARY_CONTAINER_ID)
112: .append(fName);
113: }
114:
115: public boolean isSystemLibrary() {
116: return fIsSystemLibrary;
117: }
118:
119: public void add(CPListElement element) {
120: if (!fChildren.contains(element)) {
121: fChildren.add(element);
122: }
123: }
124:
125: private List moveUp(List elements, List move) {
126: int nElements = elements.size();
127: List res = new ArrayList(nElements);
128: Object floating = null;
129: for (int i = 0; i < nElements; i++) {
130: Object curr = elements.get(i);
131: if (move.contains(curr)) {
132: res.add(curr);
133: } else {
134: if (floating != null) {
135: res.add(floating);
136: }
137: floating = curr;
138: }
139: }
140: if (floating != null) {
141: res.add(floating);
142: }
143: return res;
144: }
145:
146: public void moveUp(List toMoveUp) {
147: if (toMoveUp.size() > 0) {
148: fChildren = moveUp(fChildren, toMoveUp);
149: }
150: }
151:
152: public void moveDown(List toMoveDown) {
153: if (toMoveDown.size() > 0) {
154: Collections.reverse(fChildren);
155: fChildren = moveUp(fChildren, toMoveDown);
156: Collections.reverse(fChildren);
157: }
158: }
159:
160: public void remove(CPListElement element) {
161: fChildren.remove(element);
162: }
163:
164: public void replace(CPListElement existingElement,
165: CPListElement element) {
166: if (fChildren.contains(element)) {
167: fChildren.remove(existingElement);
168: } else {
169: int index = fChildren.indexOf(existingElement);
170: if (index != -1) {
171: fChildren.set(index, element);
172: } else {
173: fChildren.add(element);
174: }
175: element.setAttributesFromExisting(existingElement);
176: }
177: }
178:
179: public IClasspathContainer getUpdatedContainer() {
180: return new UpdatedClasspathContainer();
181: }
182:
183: public boolean hasChanges(IClasspathContainer oldContainer) {
184: if (oldContainer == null
185: || (oldContainer.getKind() == IClasspathContainer.K_SYSTEM) != fIsSystemLibrary) {
186: return true;
187: }
188: IClasspathEntry[] oldEntries = oldContainer
189: .getClasspathEntries();
190: if (fChildren.size() != oldEntries.length) {
191: return true;
192: }
193: for (int i = 0; i < oldEntries.length; i++) {
194: CPListElement child = (CPListElement) fChildren.get(i);
195: if (!child.getClasspathEntry().equals(oldEntries[i])) {
196: return true;
197: }
198: }
199: return false;
200: }
201:
202: }
|