KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > BuildPathBasePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.IPath;
18
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jdt.core.IClasspathAttribute;
24 import org.eclipse.jdt.core.IClasspathEntry;
25 import org.eclipse.jdt.core.IJavaProject;
26
27 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
28
29 import org.eclipse.jdt.ui.wizards.ClasspathAttributeConfiguration;
30
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32
33 public abstract class BuildPathBasePage {
34     
35     private ClasspathAttributeConfigurationDescriptors fAttributeDescriptors;
36     
37     public BuildPathBasePage() {
38         fAttributeDescriptors= JavaPlugin.getDefault().getClasspathAttributeConfigurationDescriptors();
39     }
40         
41     protected boolean editCustomAttribute(Shell shell, CPListElementAttribute elem) {
42         ClasspathAttributeConfiguration config= fAttributeDescriptors.get(elem.getKey());
43         if (config != null) {
44             IClasspathAttribute result= config.performEdit(shell, elem.getClasspathAttributeAccess());
45             if (result != null) {
46                 elem.setValue(result.getValue());
47                 return true;
48             }
49         }
50         return false;
51     }
52     
53     protected boolean removeCustomAttribute(CPListElementAttribute elem) {
54         ClasspathAttributeConfiguration config= fAttributeDescriptors.get(elem.getKey());
55         if (config != null) {
56             IClasspathAttribute result= config.performRemove(elem.getClasspathAttributeAccess());
57             if (result != null) {
58                 elem.setValue(result.getValue());
59                 return true;
60             }
61         }
62         return false;
63     }
64     
65     protected boolean canEditCustomAttribute(CPListElementAttribute elem) {
66         ClasspathAttributeConfiguration config= fAttributeDescriptors.get(elem.getKey());
67         if (config != null) {
68             return config.canEdit(elem.getClasspathAttributeAccess());
69         }
70         return false;
71     }
72     
73     protected boolean canRemoveCustomAttribute(CPListElementAttribute elem) {
74         ClasspathAttributeConfiguration config= fAttributeDescriptors.get(elem.getKey());
75         if (config != null) {
76             return config.canRemove(elem.getClasspathAttributeAccess());
77         }
78         return false;
79     }
80     
81     
82     public abstract List JavaDoc getSelection();
83     public abstract void setSelection(List JavaDoc selection, boolean expand);
84     
85     
86     public void addElement(CPListElement element) {
87         
88     }
89     
90     public abstract boolean isEntryKind(int kind);
91             
92     protected void filterAndSetSelection(List JavaDoc list) {
93         ArrayList JavaDoc res= new ArrayList JavaDoc(list.size());
94         for (int i= list.size()-1; i >= 0; i--) {
95             Object JavaDoc curr= list.get(i);
96             if (curr instanceof CPListElement) {
97                 CPListElement elem= (CPListElement) curr;
98                 if (elem.getParentContainer() == null && isEntryKind(elem.getEntryKind())) {
99                     res.add(curr);
100                 }
101             }
102         }
103         setSelection(res, false);
104     }
105     
106     public static void fixNestingConflicts(CPListElement[] newEntries, CPListElement[] existing, Set JavaDoc modifiedSourceEntries) {
107         for (int i= 0; i < newEntries.length; i++) {
108             addExclusionPatterns(newEntries[i], existing, modifiedSourceEntries);
109         }
110     }
111     
112     private static void addExclusionPatterns(CPListElement newEntry, CPListElement[] existing, Set JavaDoc modifiedEntries) {
113         IPath entryPath= newEntry.getPath();
114         for (int i= 0; i < existing.length; i++) {
115             CPListElement curr= existing[i];
116             if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
117                 IPath currPath= curr.getPath();
118                 if (!currPath.equals(entryPath)) {
119                     if (currPath.isPrefixOf(entryPath)) {
120                         if (addToExclusions(entryPath, curr)) {
121                             modifiedEntries.add(curr);
122                         }
123                     } else if (entryPath.isPrefixOf(currPath) && newEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
124                         if (addToExclusions(currPath, newEntry)) {
125                             modifiedEntries.add(curr);
126                         }
127                     }
128                 }
129             }
130         }
131     }
132     
133     private static boolean addToExclusions(IPath entryPath, CPListElement curr) {
134         IPath[] exclusionFilters= (IPath[]) curr.getAttribute(CPListElement.EXCLUSION);
135         if (!JavaModelUtil.isExcludedPath(entryPath, exclusionFilters)) {
136             IPath pathToExclude= entryPath.removeFirstSegments(curr.getPath().segmentCount()).addTrailingSeparator();
137             IPath[] newExclusionFilters= new IPath[exclusionFilters.length + 1];
138             System.arraycopy(exclusionFilters, 0, newExclusionFilters, 0, exclusionFilters.length);
139             newExclusionFilters[exclusionFilters.length]= pathToExclude;
140             curr.setAttribute(CPListElement.EXCLUSION, newExclusionFilters);
141             return true;
142         }
143         return false;
144     }
145     
146     protected boolean containsOnlyTopLevelEntries(List JavaDoc selElements) {
147         if (selElements.size() == 0) {
148             return true;
149         }
150         for (int i= 0; i < selElements.size(); i++) {
151             Object JavaDoc elem= selElements.get(i);
152             if (elem instanceof CPListElement) {
153                 if (((CPListElement) elem).getParentContainer() != null) {
154                     return false;
155                 }
156             } else {
157                 return false;
158             }
159         }
160         return true;
161     }
162
163     public abstract void init(IJavaProject javaProject);
164
165     public abstract Control getControl(Composite parent);
166     
167     public abstract void setFocus();
168     
169 }
170
Popular Tags