001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.ui.operations;
016:
017: import net.refractions.udig.internal.ui.UiPlugin;
018:
019: import org.eclipse.core.runtime.IConfigurationElement;
020:
021: /**
022: * Utility class for parsing enablement children
023: * @author Jesse
024: * @since 1.1.0
025: */
026: public class EnablementUtil {
027:
028: public static OpFilter parseEnablement(String extensionID,
029: IConfigurationElement[] enablement) {
030:
031: if (!validateChildren(extensionID, enablement)
032: || enablement[0] == null) {
033: if (enablement.length > 0 && enablement[0] == null)
034: UiPlugin.log("EnablementUtil: null enablement", null); //$NON-NLS-1$
035: return OpFilter.TRUE;
036: }
037:
038: IConfigurationElement[] children = enablement[0].getChildren();
039: if (!validateChildren(extensionID, children)) {
040: UiPlugin
041: .log(
042: "EnablementUtil: Expected child of " + extensionID + " but didn't find one...", null); //$NON-NLS-1$ //$NON-NLS-2$
043: return OpFilter.TRUE;
044: }
045: OpFilterParser parser = new OpFilterParser(new FilterParser[] {
046: new AdaptsToParserImpl(), new PropertyParser() });
047: return parser.parseFilter(children[0]);
048:
049: }
050:
051: private static boolean validateChildren(String extensionID,
052: IConfigurationElement[] children) {
053:
054: if (children.length < 1) {
055: return false;
056: }
057: if (children.length > 1) {
058: UiPlugin
059: .log(
060: "EnablementUtil: Error, more than one enablement element " + extensionID, null); //$NON-NLS-1$
061: return false;
062: }
063: return true;
064: }
065:
066: public static EnablesForData parseEnablesFor(String enablesFor,
067: IConfigurationElement configElem) {
068: if (enablesFor == null) {
069: enablesFor = "1"; //$NON-NLS-1$
070: }
071:
072: enablesFor = enablesFor.trim();
073: EnablesForData data = new EnablesForData();
074: if (enablesFor.equals("+")) { //$NON-NLS-1$
075: data.minHits = 1;
076: data.exactMatch = false;
077: } else if (enablesFor.equals("multiple")) { //$NON-NLS-1$
078: data.minHits = 2;
079: data.exactMatch = false;
080: } else if (enablesFor.equals("2+")) { //$NON-NLS-1$
081: data.minHits = 2;
082: data.exactMatch = false;
083: } else {
084: try {
085: data.minHits = Integer.parseInt(enablesFor);
086: data.exactMatch = true;
087: } catch (Exception e) {
088: UiPlugin
089: .log(
090: "Error parsing extension: " + configElem.getNamespace() + "/" + configElem.getName(), e); //$NON-NLS-1$//$NON-NLS-2$
091: data.minHits = 0;
092: data.exactMatch = false;
093: }
094: }
095: return data;
096: }
097:
098: public static class EnablesForData {
099: int minHits = 0;
100: boolean exactMatch = false;
101: }
102:
103: }
|