001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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 org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.IStatus;
014:
015: import org.eclipse.jdt.core.ClasspathContainerInitializer;
016: import org.eclipse.jdt.core.IClasspathAttribute;
017: import org.eclipse.jdt.core.IClasspathEntry;
018: import org.eclipse.jdt.core.IJavaProject;
019: import org.eclipse.jdt.core.JavaCore;
020:
021: import org.eclipse.jdt.ui.wizards.ClasspathAttributeConfiguration.ClasspathAttributeAccess;
022:
023: /**
024: */
025: public class CPListElementAttribute {
026:
027: private CPListElement fParent;
028: private String fKey;
029: private Object fValue;
030: private final boolean fBuiltIn;
031: private IStatus fStatus;
032:
033: private ClasspathAttributeAccess fCachedAccess;
034:
035: public CPListElementAttribute(CPListElement parent, String key,
036: Object value, boolean builtIn) {
037: fKey = key;
038: fValue = value;
039: fParent = parent;
040: fBuiltIn = builtIn;
041: if (!builtIn) {
042: Assert.isTrue(value instanceof String || value == null);
043: }
044: fStatus = getContainerChildStatus();
045: }
046:
047: private CPListElementAttribute(boolean buildIn) {
048: fBuiltIn = buildIn;
049: }
050:
051: public IClasspathAttribute getClasspathAttribute() {
052: Assert.isTrue(!fBuiltIn);
053: return JavaCore.newClasspathAttribute(fKey, (String) fValue);
054: }
055:
056: public CPListElement getParent() {
057: return fParent;
058: }
059:
060: /**
061: * @return Returns <code>true</code> if the attribute is a built in attribute.
062: */
063: public boolean isBuiltIn() {
064: return fBuiltIn;
065: }
066:
067: /**
068: * @return Returns <code>true</code> if the attribute a on a container child and is read-only
069: */
070: public boolean isNonModifiable() {
071: return fStatus != null && !fStatus.isOK();
072: }
073:
074: /**
075: * @return Returns <code>true</code> if the attribute a on a container child and is not supported
076: */
077: public boolean isNotSupported() {
078: return fStatus != null
079: && fStatus.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED;
080: }
081:
082: /**
083: * @return Returns the container child status or <code>null</code> if the attribute is not in a container child
084: */
085: private IStatus getContainerChildStatus() {
086: return fParent.getContainerChildStatus(this );
087: }
088:
089: /**
090: * Returns the key.
091: * @return String
092: */
093: public String getKey() {
094: return fKey;
095: }
096:
097: /**
098: * Returns the value.
099: * @return Object
100: */
101: public Object getValue() {
102: return fValue;
103: }
104:
105: /**
106: * Returns the value.
107: * @param value value to set
108: */
109: public void setValue(Object value) {
110: fValue = value;
111: fCachedAccess = null;
112: getParent().attributeChanged(fKey);
113: }
114:
115: public boolean equals(Object obj) {
116: if (!(obj instanceof CPListElementAttribute))
117: return false;
118: CPListElementAttribute attrib = (CPListElementAttribute) obj;
119: return attrib.fKey == this .fKey
120: && attrib.getParent().getPath().equals(
121: fParent.getPath());
122: }
123:
124: public CPListElementAttribute copy() {
125: CPListElementAttribute result = new CPListElementAttribute(
126: fBuiltIn);
127: result.fParent = fParent;
128: result.fKey = fKey;
129: result.fValue = fValue;
130: result.fStatus = fStatus;
131: return result;
132: }
133:
134: public ClasspathAttributeAccess getClasspathAttributeAccess() {
135: if (fCachedAccess == null) {
136: fCachedAccess = new ClasspathAttributeAccess() {
137: public IClasspathAttribute getClasspathAttribute() {
138: return CPListElementAttribute.this
139: .getClasspathAttribute();
140: }
141:
142: public IJavaProject getJavaProject() {
143: return getParent().getJavaProject();
144: }
145:
146: public IClasspathEntry getParentClasspassEntry() {
147: return getParent().getClasspathEntry();
148: }
149: };
150: }
151: return fCachedAccess;
152: }
153:
154: }
|