001: package net.refractions.udig.ui.operations;
002:
003: import java.util.Iterator;
004:
005: import net.refractions.udig.internal.ui.UiPlugin;
006:
007: import org.eclipse.core.runtime.CoreException;
008: import org.eclipse.core.runtime.IConfigurationElement;
009: import org.eclipse.jface.viewers.IStructuredSelection;
010:
011: /**
012: * Assesses if a tools is valid for a given property value.
013: *
014: * @author rgould
015: * @since 1.1.0
016: */
017: class OpFilterPropertyValueCondition implements OpFilter {
018:
019: private static final PropertyValue FAILED_TO_LOAD = new PropertyValue<Object>() {
020:
021: public void addListener(IOpFilterListener listener) {
022: }
023:
024: public boolean canCacheResult() {
025: return true;
026: }
027:
028: public boolean isBlocking() {
029: return false;
030: }
031:
032: public boolean isTrue(Object object, String value) {
033: return true;
034: }
035:
036: public void removeListener(IOpFilterListener listener) {
037: }
038:
039: };
040: private final String equalsValue;
041: private final IConfigurationElement propertyElement;
042: private final String targetObject;
043: private volatile PropertyValue propertyValueInstance;
044: private Class<?> targetClass;
045:
046: public OpFilterPropertyValueCondition(
047: IConfigurationElement propertyElement2,
048: String targetObject2, String equalsValue2) {
049: this .equalsValue = equalsValue2;
050: this .propertyElement = propertyElement2;
051: this .targetObject = targetObject2;
052: }
053:
054: public boolean accept(Object object) {
055: if (object instanceof IStructuredSelection) {
056: IStructuredSelection selection = (IStructuredSelection) object;
057: for (Iterator iter = selection.iterator(); iter.hasNext();) {
058: Object element = iter.next();
059: if (!accepted(element))
060: return false;
061: }
062: return true;
063: } else {
064: return accepted(object);
065: }
066: }
067:
068: @SuppressWarnings("unchecked")
069: private boolean accepted(Object object) {
070: if (object == null)
071: return false;
072: if (getTargetObject(object) != null
073: && !getTargetObject(object).isAssignableFrom(
074: object.getClass()))
075: return false;
076:
077: PropertyValue v = getValue();
078: if (v == null)
079: return false;
080: return v.isTrue(object, this .equalsValue);
081: }
082:
083: private PropertyValue getValue() {
084: if (propertyValueInstance == null) {
085: try {
086: String classAttribute = propertyElement
087: .getAttribute("class"); //$NON-NLS-1$
088: if (classAttribute == null
089: || classAttribute.trim().length() == 0)
090: // fall back case to deprecated tag
091: propertyValueInstance = (PropertyValue) propertyElement
092: .createExecutableExtension("value"); //$NON-NLS-1$
093: else {
094: propertyValueInstance = (PropertyValue) propertyElement
095: .createExecutableExtension("class"); //$NON-NLS-1$
096: }
097: } catch (CoreException e) {
098: propertyValueInstance = FAILED_TO_LOAD;
099: UiPlugin
100: .log(
101: "Error in extension: " + propertyElement.getDeclaringExtension().getUniqueIdentifier(), e); //$NON-NLS-1$
102: return null;
103: }
104: }
105: return propertyValueInstance;
106: }
107:
108: private Class<? extends Object> getTargetObject(Object object) {
109:
110: if (targetClass == null) {
111: try {
112: targetClass = object.getClass().getClassLoader()
113: .loadClass(targetObject);
114: } catch (ClassNotFoundException e) {
115: UiPlugin.log("", e); //$NON-NLS-1$
116: return null;
117: }
118: }
119:
120: return targetClass;
121: }
122:
123: public void addListener(IOpFilterListener listener) {
124: getValue().addListener(listener);
125: }
126:
127: public boolean canCacheResult() {
128: return getValue().canCacheResult();
129: }
130:
131: public boolean isBlocking() {
132: return getValue().isBlocking();
133: }
134:
135: public void removeListener(IOpFilterListener listener) {
136: getValue().removeListener(listener);
137: }
138:
139: }
|