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.List;
014: import java.util.Set;
015:
016: import org.eclipse.core.runtime.IPath;
017:
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.swt.widgets.Shell;
021:
022: import org.eclipse.jdt.core.IClasspathAttribute;
023: import org.eclipse.jdt.core.IClasspathEntry;
024: import org.eclipse.jdt.core.IJavaProject;
025:
026: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
027:
028: import org.eclipse.jdt.ui.wizards.ClasspathAttributeConfiguration;
029:
030: import org.eclipse.jdt.internal.ui.JavaPlugin;
031:
032: public abstract class BuildPathBasePage {
033:
034: private ClasspathAttributeConfigurationDescriptors fAttributeDescriptors;
035:
036: public BuildPathBasePage() {
037: fAttributeDescriptors = JavaPlugin.getDefault()
038: .getClasspathAttributeConfigurationDescriptors();
039: }
040:
041: protected boolean editCustomAttribute(Shell shell,
042: CPListElementAttribute elem) {
043: ClasspathAttributeConfiguration config = fAttributeDescriptors
044: .get(elem.getKey());
045: if (config != null) {
046: IClasspathAttribute result = config.performEdit(shell, elem
047: .getClasspathAttributeAccess());
048: if (result != null) {
049: elem.setValue(result.getValue());
050: return true;
051: }
052: }
053: return false;
054: }
055:
056: protected boolean removeCustomAttribute(CPListElementAttribute elem) {
057: ClasspathAttributeConfiguration config = fAttributeDescriptors
058: .get(elem.getKey());
059: if (config != null) {
060: IClasspathAttribute result = config.performRemove(elem
061: .getClasspathAttributeAccess());
062: if (result != null) {
063: elem.setValue(result.getValue());
064: return true;
065: }
066: }
067: return false;
068: }
069:
070: protected boolean canEditCustomAttribute(CPListElementAttribute elem) {
071: ClasspathAttributeConfiguration config = fAttributeDescriptors
072: .get(elem.getKey());
073: if (config != null) {
074: return config.canEdit(elem.getClasspathAttributeAccess());
075: }
076: return false;
077: }
078:
079: protected boolean canRemoveCustomAttribute(
080: CPListElementAttribute elem) {
081: ClasspathAttributeConfiguration config = fAttributeDescriptors
082: .get(elem.getKey());
083: if (config != null) {
084: return config.canRemove(elem.getClasspathAttributeAccess());
085: }
086: return false;
087: }
088:
089: public abstract List getSelection();
090:
091: public abstract void setSelection(List selection, boolean expand);
092:
093: public void addElement(CPListElement element) {
094:
095: }
096:
097: public abstract boolean isEntryKind(int kind);
098:
099: protected void filterAndSetSelection(List list) {
100: ArrayList res = new ArrayList(list.size());
101: for (int i = list.size() - 1; i >= 0; i--) {
102: Object curr = list.get(i);
103: if (curr instanceof CPListElement) {
104: CPListElement elem = (CPListElement) curr;
105: if (elem.getParentContainer() == null
106: && isEntryKind(elem.getEntryKind())) {
107: res.add(curr);
108: }
109: }
110: }
111: setSelection(res, false);
112: }
113:
114: public static void fixNestingConflicts(CPListElement[] newEntries,
115: CPListElement[] existing, Set modifiedSourceEntries) {
116: for (int i = 0; i < newEntries.length; i++) {
117: addExclusionPatterns(newEntries[i], existing,
118: modifiedSourceEntries);
119: }
120: }
121:
122: private static void addExclusionPatterns(CPListElement newEntry,
123: CPListElement[] existing, Set modifiedEntries) {
124: IPath entryPath = newEntry.getPath();
125: for (int i = 0; i < existing.length; i++) {
126: CPListElement curr = existing[i];
127: if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
128: IPath currPath = curr.getPath();
129: if (!currPath.equals(entryPath)) {
130: if (currPath.isPrefixOf(entryPath)) {
131: if (addToExclusions(entryPath, curr)) {
132: modifiedEntries.add(curr);
133: }
134: } else if (entryPath.isPrefixOf(currPath)
135: && newEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
136: if (addToExclusions(currPath, newEntry)) {
137: modifiedEntries.add(curr);
138: }
139: }
140: }
141: }
142: }
143: }
144:
145: private static boolean addToExclusions(IPath entryPath,
146: CPListElement curr) {
147: IPath[] exclusionFilters = (IPath[]) curr
148: .getAttribute(CPListElement.EXCLUSION);
149: if (!JavaModelUtil.isExcludedPath(entryPath, exclusionFilters)) {
150: IPath pathToExclude = entryPath.removeFirstSegments(
151: curr.getPath().segmentCount())
152: .addTrailingSeparator();
153: IPath[] newExclusionFilters = new IPath[exclusionFilters.length + 1];
154: System.arraycopy(exclusionFilters, 0, newExclusionFilters,
155: 0, exclusionFilters.length);
156: newExclusionFilters[exclusionFilters.length] = pathToExclude;
157: curr.setAttribute(CPListElement.EXCLUSION,
158: newExclusionFilters);
159: return true;
160: }
161: return false;
162: }
163:
164: protected boolean containsOnlyTopLevelEntries(List selElements) {
165: if (selElements.size() == 0) {
166: return true;
167: }
168: for (int i = 0; i < selElements.size(); i++) {
169: Object elem = selElements.get(i);
170: if (elem instanceof CPListElement) {
171: if (((CPListElement) elem).getParentContainer() != null) {
172: return false;
173: }
174: } else {
175: return false;
176: }
177: }
178: return true;
179: }
180:
181: public abstract void init(IJavaProject javaProject);
182:
183: public abstract Control getControl(Composite parent);
184:
185: public abstract void setFocus();
186:
187: }
|