]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/wizards/buildpaths/CPUserLibraryElement.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / wizards / buildpaths / CPUserLibraryElement.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
12
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;
18
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;
23
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;
30
31 import org.eclipse.jdt.internal.corext.util.Messages;
32
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;
36
37 public class CPUserLibraryElement {
38
39         private  class UpdatedClasspathContainer implements IClasspathContainer {
40
41                 /* (non-Javadoc)
42                  * @see org.eclipse.jdt.core.IClasspathContainer#getClasspathEntries()
43                  */
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();
49                         }
50                         return entries;
51                 }
52
53                 /* (non-Javadoc)
54                  * @see org.eclipse.jdt.core.IClasspathContainer#getDescription()
55                  */
56                 public String getDescription() {
57                         return getName();
58                 }
59
60                 /* (non-Javadoc)
61                  * @see org.eclipse.jdt.core.IClasspathContainer#getKind()
62                  */
63                 public int getKind() {
64                         return isSystemLibrary() ? IClasspathContainer.K_SYSTEM : K_APPLICATION;
65                 }
66
67                 /* (non-Javadoc)
68                  * @see org.eclipse.jdt.core.IClasspathContainer#getPath()
69                  */
70                 public IPath getPath() {
71                         return CPUserLibraryElement.this.getPath();
72                 }
73         }
74
75
76         private String fName;
77         List<CPListElement> fChildren;
78         private boolean fIsSystemLibrary;
79
80         public CPUserLibraryElement(String name, IClasspathContainer container, IJavaProject project) {
81                 fName= name;
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()));
91                                 fChildren.add(elem);
92                         }
93                         fIsSystemLibrary= container.getKind() == IClasspathContainer.K_SYSTEM;
94                 } else {
95                         fIsSystemLibrary= false;
96                 }
97         }
98
99         /**
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.
102          * 
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>
107          */
108         public CPUserLibraryElement(String name, boolean isSystemLibrary, CPListElement[] children) {
109                 fName= name;
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]);
115                         }
116                 }
117                 fIsSystemLibrary= isSystemLibrary;
118         }
119
120         public CPListElement[] getChildren() {
121                 return fChildren.toArray(new CPListElement[fChildren.size()]);
122         }
123
124         public String getName() {
125                 return fName;
126         }
127
128         public IPath getPath() {
129                 return new Path(JavaCore.USER_LIBRARY_CONTAINER_ID).append(fName);
130         }
131
132         public boolean isSystemLibrary() {
133                 return fIsSystemLibrary;
134         }
135
136         public void add(CPListElement element) {
137                 element.generated_5439650126968803820(this);
138         }
139
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);
147                 }
148                 if (floating != null) {
149                         res.add(floating);
150                 }
151                 return res;
152         }
153
154         public void moveUp(List<CPListElement> toMoveUp) {
155                 if (toMoveUp.size() > 0) {
156                         fChildren= moveUp(fChildren, toMoveUp);
157                 }
158         }
159
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);
165                 }
166         }
167
168
169         public void remove(CPListElement element) {
170                 fChildren.remove(element);
171         }
172
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);
178                 } else {
179                         int index= fChildren.indexOf(existingElement);
180                         element.generated_503674186899919308(existingElement, this, index);
181                 }
182         }
183
184         public IClasspathContainer getUpdatedContainer() {
185                 return new UpdatedClasspathContainer();
186         }
187
188         public boolean hasChanges(IClasspathContainer oldContainer) {
189                 if (oldContainer == null || (oldContainer.getKind() == IClasspathContainer.K_SYSTEM) != fIsSystemLibrary) {
190                         return true;
191                 }
192                 IClasspathEntry[] oldEntries= oldContainer.getClasspathEntries();
193                 if (fChildren.size() != oldEntries.length) {
194                         return true;
195                 }
196                 for (int i= 0; i < oldEntries.length; i++) {
197                         CPListElement child= fChildren.get(i);
198                         if (!child.getClasspathEntry().equals(oldEntries[i])) {
199                                 return true;
200                         }
201                 }
202                 return false;
203         }
204
205         /**
206          * Returns if a entry has children that are missing
207          * @return Returns a boolean
208          */
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()) {
213                                 return true;
214                         }
215                 }
216                 return false;
217         }
218
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();
223                         try {
224                                 initializer.requestClasspathContainerUpdate(path, jproject, updatedContainer);
225                         } catch (CoreException e) {
226                                 multiStatus.add(e.getStatus());
227                         }
228                 }
229         }
230
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]);
235                         } else {
236                                 add(elements[i]);
237                         }
238                 }
239                 userlibrarypreferencepage.fLibraryList.refresh(this);
240         }
241
242         public void generated_8458246444911177306(List<CPUserLibraryElement> existing, HashMap<String, CPUserLibraryElement> map) {
243                 CPUserLibraryElement found= map.get(getName());
244                 if (found == null) {
245                         existing.add(this);
246                         map.put(getName(), this);
247                 } else {
248                         existing.set(existing.indexOf(found), this); // replace
249                 }
250         }
251
252         public void generated_3290808306039919345(UserLibraryPreferencePage userlibrarypreferencepage, List<CPListElement> cpElementList) {
253                 moveUp(cpElementList);
254                 userlibrarypreferencepage.fLibraryList.refresh(this);
255         }
256
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);
262         }
263
264         public void generated_1556309657071644884(LibraryNameDialog librarynamedialog) {
265                 librarynamedialog.fNameField.setText(getName());
266                 librarynamedialog.fIsSystemField.setSelection(isSystemLibrary());
267         }
268
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());
272                 } else {
273                         return JavaCore.newContainerEntry(getPath(), false);
274                 }
275         }
276
277         public String generated_6170624604143659829() {
278                 String name= getName();
279                 if (isSystemLibrary()) {
280                         name= Messages.format(NewWizardMessages.CPListLabelProvider_systemlibrary, name);
281                 }
282                 return name;
283         }
284
285 }