001/*
002 * Copyright (c) 2003-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.objectmanager.data.db;
031
032import java.lang.reflect.Method;
033import java.net.URL;
034
035import ptolemy.actor.gui.Effigy;
036import ptolemy.actor.gui.EffigyFactory;
037import ptolemy.kernel.CompositeEntity;
038import ptolemy.kernel.util.IllegalActionException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.kernel.util.Workspace;
041
042//////////////////////////////////////////////////////////////////////////
043//// QBEffigy
044/**
045 * An effigy for a SQL XML definition.
046 */
047public class QBEffigy extends Effigy {
048
049        /**
050         * Create a new effigy in the specified workspace with an empty string for
051         * its name.
052         * 
053         * @param workspace
054         *            The workspace for this effigy.
055         */
056        public QBEffigy(Workspace workspace) {
057                super(workspace);
058        }
059
060        /**
061         * Create a new effigy in the given directory with the given name.
062         * 
063         * @param container
064         *            The directory that contains this effigy.
065         * @param name
066         *            The name of this effigy.
067         */
068        public QBEffigy(CompositeEntity container, String name)
069                        throws IllegalActionException, NameDuplicationException {
070                super(container, name);
071        }
072
073        // /////////////////////////////////////////////////////////////////
074        // // public methods ////
075
076        /**
077         * Create a new effigy in the given container containing the specified text.
078         * The new effigy will have a new instance of DefaultStyledDocument
079         * associated with it.
080         * 
081         * @param container
082         *            The container for the effigy.
083         * @return A new instance of QBEffigy.
084         * @exception Exception
085         *                If the text effigy cannot be contained by the specified
086         *                container, or if the specified text cannot be inserted
087         *                into the document.
088         */
089        public static QBEffigy newQBEffigy(CompositeEntity container)
090                        throws Exception {
091                // Create a new effigy.
092                QBEffigy effigy = new QBEffigy(container, container
093                                .uniqueName("effigy"));
094                return effigy;
095        }
096
097        // /////////////////////////////////////////////////////////////////
098        // // inner classes ////
099
100        /**
101         * A factory for creating new effigies.
102         */
103        public static class Factory extends EffigyFactory {
104
105                /**
106                 * Create a factory with the given name and container.
107                 * 
108                 * @param container
109                 *            The container.
110                 * @param name
111                 *            The name.
112                 * @exception IllegalActionException
113                 *                If the container is incompatible with this entity.
114                 * @exception NameDuplicationException
115                 *                If the name coincides with an entity already in the
116                 *                container.
117                 */
118                public Factory(CompositeEntity container, String name)
119                                throws IllegalActionException, NameDuplicationException {
120                        super(container, name);
121                        try {
122                                Class effigyClass = Class
123                                                .forName("org.kepler.objectmanager.data.db.QBEffigy");
124                                _newQBEffigyURL = effigyClass.getMethod("newQBEffigy",
125                                                new Class[] { CompositeEntity.class, URL.class,
126                                                                URL.class });
127                        } catch (ClassNotFoundException ex) {
128                                throw new IllegalActionException(ex.toString());
129                        } catch (NoSuchMethodException ex) {
130                                throw new IllegalActionException(ex.toString());
131                        }
132                }
133
134                // /////////////////////////////////////////////////////////////
135                // // public methods ////
136
137                /**
138                 * Return true, indicating that this effigy factory is capable of
139                 * creating an effigy without a URL being specified.
140                 * 
141                 * @return True.
142                 */
143                public boolean canCreateBlankEffigy() {
144                        return true;
145                }
146
147                /**
148                 * Create a new effigy in the given container by reading the specified
149                 * URL. If the specified URL is null, then create a blank effigy. The
150                 * extension of the URL is not checked, so this will open any file.
151                 * Thus, this factory should be last on the list of effigy factories in
152                 * the configuration. The new effigy will have a new instance of
153                 * DefaultStyledDocument associated with it.
154                 * 
155                 * @param container
156                 *            The container for the effigy.
157                 * @param base
158                 *            The base for relative file references, or null if there
159                 *            are no relative file references. This is ignored in this
160                 *            class.
161                 * @param in
162                 *            The input URL.
163                 * @return A new instance of QBEffigy.
164                 * @exception Exception
165                 *                If the URL cannot be read, or if the data is malformed
166                 *                in some way.
167                 */
168                public Effigy createEffigy(CompositeEntity container, URL base, URL in)
169                                throws Exception {
170                        // Create a new effigy.
171                        try {
172                                return (Effigy) _newQBEffigyURL.invoke(null, new Object[] {
173                                                container, base, in });
174                        } catch (java.lang.reflect.InvocationTargetException ex) {
175                                if (ex instanceof Exception) {
176                                        // Rethrow the initial cause
177                                        throw (Exception) (ex.getCause());
178                                } else {
179                                        throw new Exception(ex.getCause());
180                                }
181                                // Uncomment this for debugging
182                                // throw new java.lang.reflect.InvocationTargetException(ex,
183                                // " Invocation of method failed!. Method was: "
184                                // + _newQBEffigyURL
185                                // + "\nwith arguments( container = " + container
186                                // + " base = " + base + " in = " + in + ")");
187                        }
188                }
189
190                private Method _newQBEffigyURL;
191        }
192}