1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.MultiStatus;
22 import org.eclipse.core.runtime.Path;
24 import org.eclipse.jdt.core.ClasspathContainerInitializer;
25 import org.eclipse.jdt.core.IClasspathContainer;
26 import org.eclipse.jdt.core.IClasspathEntry;
27 import org.eclipse.jdt.core.IJavaProject;
28 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.jdt.core.JavaModelException;
31 import org.eclipse.jdt.internal.corext.util.Messages;
33 import org.eclipse.jdt.internal.ui.preferences.UserLibraryPreferencePage;
34 import org.eclipse.jdt.internal.ui.preferences.UserLibraryPreferencePage.LibraryNameDialog;
35 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
37 public class CPUserLibraryElement {
39 private class UpdatedClasspathContainer implements IClasspathContainer {
42 * @see org.eclipse.jdt.core.IClasspathContainer#getClasspathEntries()
44 public IClasspathEntry[] getClasspathEntries() {
45 CPListElement[] children= getChildren();
46 IClasspathEntry[] entries= new IClasspathEntry[children.length];
47 for (int i= 0; i < entries.length; i++) {
48 entries[i]= children[i].getClasspathEntry();
54 * @see org.eclipse.jdt.core.IClasspathContainer#getDescription()
56 public String getDescription() {
61 * @see org.eclipse.jdt.core.IClasspathContainer#getKind()
63 public int getKind() {
64 return isSystemLibrary() ? IClasspathContainer.K_SYSTEM : K_APPLICATION;
68 * @see org.eclipse.jdt.core.IClasspathContainer#getPath()
70 public IPath getPath() {
71 return CPUserLibraryElement.this.getPath();
77 List<CPListElement> fChildren;
78 private boolean fIsSystemLibrary;
80 public CPUserLibraryElement(String name, IClasspathContainer container, IJavaProject project) {
82 fChildren= new ArrayList<CPListElement>();
83 if (container != null) {
84 IClasspathEntry[] entries= container.getClasspathEntries();
85 CPListElement[] res= new CPListElement[entries.length];
86 for (int i= 0; i < res.length; i++) {
87 IClasspathEntry curr= entries[i];
88 CPListElement elem= CPListElement.createFromExisting(this, curr, project);
89 //elem.setAttribute(CPListElement.SOURCEATTACHMENT, curr.getSourceAttachmentPath());
90 //elem.setAttribute(CPListElement.JAVADOC, JavaUI.getLibraryJavadocLocation(curr.getPath()));
93 fIsSystemLibrary= container.getKind() == IClasspathContainer.K_SYSTEM;
95 fIsSystemLibrary= false;
100 * Creates a new user library element with the given name and children and sets itself as the
101 * parent container to each given child element.
103 * @param name the name of the library element
104 * @param isSystemLibrary <code>true</code> if the library is a system library,
105 * <code>false</code> otherwise
106 * @param children the children elements of the library element or <code>null</code>
108 public CPUserLibraryElement(String name, boolean isSystemLibrary, CPListElement[] children) {
110 fChildren= new ArrayList<CPListElement>();
111 if (children != null) {
112 for (int i= 0; i < children.length; i++) {
113 children[i].setParentContainer(this);
114 fChildren.add(children[i]);
117 fIsSystemLibrary= isSystemLibrary;
120 public CPListElement[] getChildren() {
121 return fChildren.toArray(new CPListElement[fChildren.size()]);
124 public String getName() {
128 public IPath getPath() {
129 return new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(fName);
132 public boolean isSystemLibrary() {
133 return fIsSystemLibrary;
136 public void add(CPListElement element) {
137 element.generated_5439650126968803820(this);
140 private List<CPListElement> moveUp(List<CPListElement> elements, List<CPListElement> move) {
141 int nElements= elements.size();
142 List<CPListElement> res= new ArrayList<CPListElement>(nElements);
143 CPListElement floating= null;
144 for (int i= 0; i < nElements; i++) {
145 CPListElement curr= elements.get(i);
146 floating= curr.generated_7677782357583573221(move, res, floating);
148 if (floating != null) {
154 public void moveUp(List<CPListElement> toMoveUp) {
155 if (toMoveUp.size() > 0) {
156 fChildren= moveUp(fChildren, toMoveUp);
160 public void moveDown(List<CPListElement> toMoveDown) {
161 if (toMoveDown.size() > 0) {
162 Collections.reverse(fChildren);
163 fChildren= moveUp(fChildren, toMoveDown);
164 Collections.reverse(fChildren);
169 public void remove(CPListElement element) {
170 fChildren.remove(element);
173 public void replace(CPListElement existingElement, CPListElement element) {
174 if (element.equals(existingElement)) {
175 // same element selected again: do nothing
176 } else if (fChildren.contains(element)) {
177 fChildren.remove(existingElement);
179 int index= fChildren.indexOf(existingElement);
180 element.generated_503674186899919308(existingElement, this, index);
184 public IClasspathContainer getUpdatedContainer() {
185 return new UpdatedClasspathContainer();
188 public boolean hasChanges(IClasspathContainer oldContainer) {
189 if (oldContainer == null || (oldContainer.getKind() == IClasspathContainer.K_SYSTEM) != fIsSystemLibrary) {
192 IClasspathEntry[] oldEntries= oldContainer.getClasspathEntries();
193 if (fChildren.size() != oldEntries.length) {
196 for (int i= 0; i < oldEntries.length; i++) {
197 CPListElement child= fChildren.get(i);
198 if (!child.getClasspathEntry().equals(oldEntries[i])) {
206 * Returns if a entry has children that are missing
207 * @return Returns a boolean
209 public boolean hasMissingChildren() {
210 for (int i= 0; i < fChildren.size(); i++) {
211 Object curr= fChildren.get(i);
212 if (curr instanceof CPListElement && ((CPListElement) curr).isMissing()) {
219 public void generated_654287954737608878(HashSet<CPUserLibraryElement> newEntries, MultiStatus multiStatus, ClasspathContainerInitializer initializer, IJavaProject jproject) throws JavaModelException {
220 IPath path= getPath();
221 if (newEntries.contains(this) || hasChanges(JavaCore.getClasspathContainer(path, jproject))) {
222 IClasspathContainer updatedContainer= getUpdatedContainer();
224 initializer.requestClasspathContainerUpdate(path, jproject, updatedContainer);
225 } catch (CoreException e) {
226 multiStatus.add(e.getStatus());
231 public void generated_6411699883792297303(CPListElement existingElement, UserLibraryPreferencePage userlibrarypreferencepage, CPListElement[] elements) {
232 for (int i= 0; i < elements.length; i++) {
233 if (existingElement != null) {
234 replace(existingElement, elements[i]);
239 userlibrarypreferencepage.fLibraryList.refresh(this);
242 public void generated_8458246444911177306(List<CPUserLibraryElement> existing, HashMap<String, CPUserLibraryElement> map) {
243 CPUserLibraryElement found= map.get(getName());
246 map.put(getName(), this);
248 existing.set(existing.indexOf(found), this); // replace
252 public void generated_3290808306039919345(UserLibraryPreferencePage userlibrarypreferencepage, List<CPListElement> cpElementList) {
253 moveUp(cpElementList);
254 userlibrarypreferencepage.fLibraryList.refresh(this);
257 public void generated_2119164516309830040(List<?> list, UserLibraryPreferencePage userlibrarypreferencepage) {
258 @SuppressWarnings("unchecked")
259 List<CPListElement> cpElementList= (List<CPListElement>) list;
260 moveDown(cpElementList);
261 userlibrarypreferencepage.fLibraryList.refresh(this);
264 public void generated_1556309657071644884(LibraryNameDialog librarynamedialog) {
265 librarynamedialog.fNameField.setText(getName());
266 librarynamedialog.fIsSystemField.setSelection(isSystemLibrary());
269 public IClasspathEntry generated_5770402405348747195(UserLibraryWizardPage userlibrarywizardpage) {
270 if (userlibrarywizardpage.fOldClasspathEntry != null && userlibrarywizardpage.fOldClasspathEntry.getPath().equals(getPath())) {
271 return JavaCore.newContainerEntry(getPath(), userlibrarywizardpage.fOldClasspathEntry.getAccessRules(), userlibrarywizardpage.fOldClasspathEntry.getExtraAttributes(), userlibrarywizardpage.fOldClasspathEntry.isExported());
273 return JavaCore.newContainerEntry(getPath(), false);
277 public String generated_6170624604143659829() {
278 String name= getName();
279 if (isSystemLibrary()) {
280 name= Messages.format(NewWizardMessages.CPListLabelProvider_systemlibrary, name);