001/*
002 * Copyright (c) 2005-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.sms.gui;
031
032import java.awt.datatransfer.DataFlavor;
033import java.awt.datatransfer.Transferable;
034import java.awt.datatransfer.UnsupportedFlavorException;
035import java.io.IOException;
036import java.io.Serializable;
037import java.util.LinkedList;
038import java.util.List;
039
040import org.kepler.sms.NamedOntClass;
041
042import ptolemy.vergil.kernel.VergilUtilities;
043
044/**
045 * A transferable object that contains a local JVM reference to a number of
046 * named objects. To get a reference to an iterator on the objects, request data
047 * with the data flavor given in the static namedObjFlavor variable.
048 * 
049 * Note that this class was copied/modified from
050 * ptolemy.vergil.toolbox.PtolemyTransferable
051 */
052
053public class NamedOntClassTransferable implements Transferable, Serializable {
054
055        /**
056         * Create a new transferable object that contains no objects.
057         */
058        public NamedOntClassTransferable() {
059                _objectList = new LinkedList();
060        }
061
062        /**
063         * The only flavor associated with this transferable
064         */
065        public static DataFlavor getNamedOntClassFlavor() {
066                return namedOntClassFlavor;
067        }
068
069        /**
070         * Add the given named object to the objects contained in this transferable.
071         * If the object already exists in this transferable, then do not add it
072         * again.
073         * 
074         * @param object
075         *            The object to be added to this transferable.
076         */
077        public void addObject(NamedOntClass object) {
078                if (!_objectList.contains(object))
079                        _objectList.add(object);
080        }
081
082        /**
083         * Return the data flavors that this transferable supports.
084         * 
085         * @return The data flavors.
086         */
087        public synchronized DataFlavor[] getTransferDataFlavors() {
088                return _flavors;
089        }
090
091        /**
092         * Return true if the given data flavor is supported.
093         * 
094         * @param flavor
095         *            The data flavor that is searched for.
096         * @return true if the given data flavor is supported.
097         */
098        public boolean isDataFlavorSupported(DataFlavor flavor) {
099                int i;
100
101                for (i = 0; i < _flavors.length; i++) {
102                        if (_flavors[i].equals(flavor)) {
103                                return true;
104                        }
105                }
106
107                return false;
108        }
109
110        /**
111         * Return an object that represents the data contained within this
112         * transferable with the given flavor. If the flavor is namedOntClassFlavor,
113         * return an iterator of the objects that this transferable refers to.
114         * Otherwise, an exception is thrown.
115         * 
116         * @param flavor
117         *            The data flavor.
118         * @return An object with the given flavor.
119         * @exception UnsupportedFlavorException
120         *                If the given flavor is not supported.
121         */
122        public Object getTransferData(DataFlavor flavor)
123                        throws UnsupportedFlavorException, IOException {
124                if (flavor.equals(namedOntClassFlavor)) {
125                        return _objectList.iterator();
126                }
127
128                throw new UnsupportedFlavorException(flavor);
129        }
130
131        /**
132         * Remove the given object from this transferable. If the object does not
133         * exist in the transferable, then do nothing.
134         * 
135         * @param object
136         *            The object to be removed.
137         */
138        public void removeObject(NamedOntClass object) {
139                if (_objectList.contains(object))
140                        _objectList.remove(object);
141        }
142
143        // Under MacOS X 10.2, Java 1.4.1_01 we get a stack trace
144        // when ever we drag and drop. For details See
145        // http://lists.apple.com/archives/java-dev/2003/Apr/16/classcastexceptionindrag.txt
146        // FIXME: This change happened just before the release of 3.0.2,
147        // so we only make the change under Mac OS.
148        static {
149                if (VergilUtilities.macOSLookAndFeel()) {
150                        namedOntClassFlavor = new DataFlavor(
151                                        DataFlavor.javaJVMLocalObjectMimeType
152                                                        + ";class=org.kepler.sms.NamedOntClass",
153                                        "Named Ont Class");
154                } else {
155                        namedOntClassFlavor = new DataFlavor(
156                                        DataFlavor.javaJVMLocalObjectMimeType
157                                                        + "org.kepler.sms.NamedOntClass", "Named Ont Class");
158                }
159        }
160
161        private final DataFlavor[] _flavors = { namedOntClassFlavor };
162        private List _objectList; // the object contained by this transferable.
163        private static final DataFlavor namedOntClassFlavor;
164
165}