001: /*
002: * @(#) $Header: /cvs/jai-operators/src/main/ca/forklabs/media/jai/DescriptorUtil.java,v 1.3 2007/07/03 18:57:19 forklabs Exp $
003: *
004: * Copyright (C) 2007 Forklabs Daniel Léonard
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: package ca.forklabs.media.jai;
022:
023: import java.awt.image.RenderedImage;
024: import java.awt.image.renderable.ParameterBlock;
025: import java.awt.image.renderable.RenderableImage;
026: import java.lang.reflect.Array;
027: import java.util.Collection;
028: import javax.media.jai.CollectionImage;
029: import javax.media.jai.OperationDescriptor;
030: import javax.media.jai.registry.CollectionRegistryMode;
031: import javax.media.jai.registry.RenderableCollectionRegistryMode;
032: import javax.media.jai.registry.RenderableRegistryMode;
033: import javax.media.jai.registry.RenderedRegistryMode;
034:
035: /**
036: * Class {@code DescriptorUtil} offers methods to help in the validation process
037: * of {@link OperationDescriptor} objects.
038: *
039: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.DescriptorUtil">Daniel Léonard</a>
040: * @version $Revision: 1.3 $
041: */
042: public class DescriptorUtil {
043:
044: //---------------------------
045: // Constructors
046: //---------------------------
047:
048: /**
049: * Only allow subclasses.
050: */
051: protected DescriptorUtil() {
052: // nothing
053: }
054:
055: //---------------------------
056: // Class methods
057: //---------------------------
058:
059: /**
060: * Determines if the mode represents the rendered mode.
061: * @param mode the mode.
062: * @return {@code true} if the mode is the same as
063: * {@link RenderedRegistryMode#MODE_NAME}, {@code false} otherwise.
064: */
065: public static boolean isRenderedMode(String mode) {
066: String rendered_mode = RenderedRegistryMode.MODE_NAME;
067: boolean is_rendered_mode = rendered_mode.equalsIgnoreCase(mode);
068: return is_rendered_mode;
069: }
070:
071: /**
072: * Determines if the mode represents the renderable mode.
073: * @param mode the mode.
074: * @return {@code true} if the mode is the same as
075: * {@link RenderableRegistryMode#MODE_NAME}, {@code false}
076: * otherwise.
077: */
078: public static boolean isRenderableMode(String mode) {
079: String renderable_mode = RenderableRegistryMode.MODE_NAME;
080: boolean is_renderable_mode = renderable_mode
081: .equalsIgnoreCase(mode);
082: return is_renderable_mode;
083: }
084:
085: /**
086: * Determines if the mode represents the collection mode.
087: * @param mode the mode.
088: * @return {@code true} if the mode is the same as
089: * {@link CollectionRegistryMode#MODE_NAME}, {@code false}
090: * otherwise.
091: */
092: public static boolean isCollectionMode(String mode) {
093: String collection_mode = CollectionRegistryMode.MODE_NAME;
094: boolean is_collection_mode = collection_mode
095: .equalsIgnoreCase(mode);
096: return is_collection_mode;
097: }
098:
099: /**
100: * Determines if the mode represents the renderable collection mode.
101: * @param mode the mode.
102: * @return {@code true} if the mode is the same as
103: * {@link RenderableCollectionRegistryMode#MODE_NAME},
104: * {@code false} otherwise.
105: */
106: public static boolean isRenderableCollectionMode(String mode) {
107: String renderable_collection_mode = RenderableCollectionRegistryMode.MODE_NAME;
108: boolean is_renderable_collection_mode = renderable_collection_mode
109: .equalsIgnoreCase(mode);
110: return is_renderable_collection_mode;
111: }
112:
113: /**
114: * Determines if the source is a rendered image.
115: * @param source the source.
116: * @return {@code true} if the source is of type {@link RenderedImage},
117: * {@code false} otherwise.
118: */
119: public static boolean isSourceRendered(Object source) {
120: boolean is_source_rendered = (source instanceof RenderedImage);
121: return is_source_rendered;
122: }
123:
124: /**
125: * Determines if the source is a renderable image.
126: * @param source the source.
127: * @return {@code true} if the source is of type {@link RenderableImage},
128: * {@code false} otherwise.
129: */
130: public static boolean isSourceRenderable(Object source) {
131: boolean is_source_renderable = (source instanceof RenderableImage);
132: return is_source_renderable;
133: }
134:
135: /**
136: * Determines if the source is a collection.
137: * @param source the source.
138: * @return {@code true} if the source is of type {@link Collection},
139: * {@code false} otherwise.
140: */
141: public static boolean isSourceCollection(Object source) {
142: boolean is_source_collection = (source instanceof Collection);
143: return is_source_collection;
144: }
145:
146: /**
147: * Determines if all the sources are rendered image.
148: * @param sources the sources.
149: * @return {@code true} if all the sources are of type
150: * {@link RenderedImage}, {@code false} otherwise.
151: */
152: public static boolean areAllSourceRendered(Iterable<?> sources) {
153: boolean are_rendered = true;
154: for (Object source : sources) {
155: are_rendered = DescriptorUtil.isSourceRendered(source);
156: if (false == are_rendered) {
157: break;
158: }
159: }
160: return are_rendered;
161: }
162:
163: /**
164: * Determines if all the sources are renderable image.
165: * @param sources the sources.
166: * @return {@code true} if all the sources are of type
167: * {@link RenderableImage}, {@code false} otherwise.
168: */
169: public static boolean areAllSourceRenderable(Iterable<?> sources) {
170: boolean are_renderable = true;
171: for (Object source : sources) {
172: are_renderable = DescriptorUtil.isSourceRenderable(source);
173: if (false == are_renderable) {
174: break;
175: }
176: }
177: return are_renderable;
178: }
179:
180: /**
181: * Collates all the sources into a single collection. All the original
182: * sources are removed from the parameter block and the new
183: * {@link CollectionImage} is put as <em>source0</em>. If a source is a
184: * collection, all its elements are put in the order they are returned by the
185: * iterator. If the source is an array, all the elements are put in
186: * index-order.
187: * @param pb the parameter block.
188: */
189: @SuppressWarnings("unchecked")
190: public static void collateSources(ParameterBlock pb) {
191: CollectionImage source0 = new SimpleCollectionImage();
192:
193: for (Object source : pb.getSources()) {
194: boolean is_collection = DescriptorUtil
195: .isSourceCollection(source);
196: boolean is_array = source.getClass().isArray();
197: if (is_collection) {
198: source0.addAll((Collection) source);
199: } else if (is_array) {
200: Object array = source;
201: int len = Array.getLength(array);
202: for (int i = 0; i < len; i++) {
203: source = Array.get(array, i);
204: source0.add(source);
205: }
206: } else {
207: source0.add(source);
208: }
209: }
210:
211: pb.removeSources();
212:
213: pb.addSource(source0);
214: }
215:
216: }
217:
218: /*
219: * $Log: DescriptorUtil.java,v $
220: * Revision 1.3 2007/07/03 18:57:19 forklabs
221: * Made the collection of collated sources a CollectionImage.
222: *
223: * Revision 1.2 2007/06/11 22:00:54 forklabs
224: * Source collation now takes array and treats them as collection.
225: *
226: * Revision 1.1 2007/06/07 21:44:32 forklabs
227: * Utilities for help descriptor validation.
228: *
229: */
|