001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2005, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.parameter;
018:
019: // J2SE dependencies
020: import java.util.Locale;
021: import java.util.ResourceBundle;
022: import java.util.MissingResourceException;
023: import java.io.Serializable;
024:
025: // JAI dependencies
026: import javax.media.jai.OperationDescriptor;
027:
028: // Geotools dependencies
029: import org.geotools.resources.Utilities;
030: import org.geotools.util.AbstractInternationalString;
031:
032: /**
033: * A localized string for a JAI's operation parameter.
034: * This is used by {@link ImagingParameterDescriptors}.
035: *
036: * @since 2.2
037: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/parameter/ImagingParameterDescription.java $
038: * @version $Id: ImagingParameterDescription.java 20874 2006-08-07 10:00:01Z jgarnett $
039: * @author Martin Desruisseaux
040: */
041: final class ImagingParameterDescription extends
042: AbstractInternationalString implements Serializable {
043: /**
044: * Serial number for interoperability with different versions.
045: */
046: private static final long serialVersionUID = -325584046563057577L;
047:
048: /**
049: * Shared keys for arguments. Will be completed only as needed.
050: */
051: private static final String[] argumentKeys = new String[12];
052:
053: /**
054: * The operation to fetch localized resource from.
055: */
056: private final OperationDescriptor operation;
057:
058: /**
059: * The key for the resource to fetch.
060: */
061: private final String key;
062:
063: /**
064: * Prefix to removes from the value associated to the {@code key}, or {@code null} if none.
065: * This is usually the vendor key.
066: */
067: private final String prefixKey;
068:
069: /**
070: * Creates a new international string from the specified operation and argument number.
071: *
072: * @param operation The operation to fetch localized resource from.
073: * @param arg The argument number.
074: */
075: public ImagingParameterDescription(
076: final OperationDescriptor operation, final int arg) {
077: this .operation = operation;
078: this .prefixKey = null;
079: if (arg < argumentKeys.length) {
080: String candidate = argumentKeys[arg];
081: if (candidate != null) {
082: key = candidate;
083: return;
084: }
085: }
086: key = "arg" + arg + "Desc";
087: if (arg < argumentKeys.length) {
088: argumentKeys[arg] = key;
089: }
090: }
091:
092: /**
093: * Creates a new international string from the specified operation and key.
094: *
095: * @param operation The operation to fetch localized resource from.
096: * @param key The key for the resource to fetch.
097: */
098: public ImagingParameterDescription(
099: final OperationDescriptor operation, final String key,
100: final String prefixKey) {
101: this .operation = operation;
102: this .key = key;
103: this .prefixKey = prefixKey;
104: }
105:
106: /**
107: * Tests if the resource exists.
108: */
109: public boolean exists() {
110: try {
111: // AbstractInternationalString.toString() never returns null.
112: return toString().length() != 0;
113: } catch (MissingResourceException exception) {
114: return false;
115: }
116: }
117:
118: /**
119: * Returns a string in the specified locale.
120: *
121: * @param locale The locale to look for, or {@code null} for the default locale.
122: * @return The string in the specified locale, or in a default locale.
123: * @throws MissingResourceException is the key given to the constructor is invalid.
124: */
125: public String toString(Locale locale)
126: throws MissingResourceException {
127: if (locale == null) {
128: locale = Locale.getDefault();
129: }
130: final ResourceBundle resources = operation
131: .getResourceBundle(locale);
132: String name = resources.getString(key);
133: if (prefixKey != null) {
134: name = trimPrefix(name, resources.getString(prefixKey));
135: }
136: return name;
137: }
138:
139: /**
140: * If the specified name starts with the specified prefix, removes the prefix from
141: * the name. This is used for removing the "org.geotools" part in operation name
142: * like "org.geotools.NodataFilter" for example.
143: */
144: static String trimPrefix(String name, String prefix) {
145: name = name.trim();
146: if (prefix != null) {
147: prefix = prefix.trim();
148: final int offset = prefix.length();
149: if (offset != 0) {
150: if (name.startsWith(prefix)) {
151: final int length = name.length();
152: if (offset < length && name.charAt(offset) == '.') {
153: name = name.substring(offset + 1);
154: }
155: }
156: }
157: }
158: return name;
159: }
160:
161: /**
162: * Compares this international string with the specified object for equality.
163: */
164: public boolean equals(final Object object) {
165: if (object != null && object.getClass().equals(getClass())) {
166: final ImagingParameterDescription that = (ImagingParameterDescription) object;
167: return Utilities.equals(this .key, that.key)
168: && Utilities.equals(this .prefixKey, that.prefixKey)
169: && Utilities.equals(this .operation, that.operation);
170: }
171: return false;
172: }
173:
174: /**
175: * Returns a hash code value for this international text.
176: */
177: public int hashCode() {
178: return (int) serialVersionUID ^ key.hashCode()
179: ^ operation.hashCode();
180: }
181: }
|