001/*
002 * Copyright (c) 1997-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: berkley $'
006 * '$Date: 2010-04-28 00:12:36 +0000 (Wed, 28 Apr 2010) $' 
007 * '$Revision: 24000 $'
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.gui;
031
032import java.lang.reflect.Constructor;
033import java.util.Iterator;
034
035import org.kepler.configuration.ConfigurationManager;
036import org.kepler.configuration.ConfigurationProperty;
037
038import ptolemy.actor.gui.TableauFrame;
039import ptolemy.kernel.util.Attribute;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NameDuplicationException;
042import ptolemy.kernel.util.NamedObj;
043import ptolemy.util.MessageHandler;
044
045//////////////////////////////////////////////////////////////////////////
046//// TableauFactory
047/**
048 * This class is an attribute that creates a LibraryPaneTab for the
049 * TabbedLibraryPane.
050 * 
051 *@author Aaron Schultz
052 */
053public class TabPaneFactory extends Attribute {
054
055        /**
056         * Create a factory with the given name and container.
057         * 
058         *@param container
059         *            The container.
060         *@param name
061         *            The name.
062         *@exception IllegalActionException
063         *                If the container is incompatible with this attribute.
064         *@exception NameDuplicationException
065         *                If the name coincides with an attribute already in the
066         *                container.
067         */
068        public TabPaneFactory(NamedObj container, String name)
069                        throws IllegalActionException, NameDuplicationException {
070                super(container, name);
071        }
072
073        /**
074         * 
075         * @param parent
076         **/
077        public boolean createTabPaneTabs(TableauFrame parent) {
078                boolean success = false;
079                try {
080                        ConfigurationProperty guiProp = ConfigurationManager.getInstance()
081                                        .getProperty(ConfigurationManager.getModule("gui"));
082                        ConfigurationProperty tabProp = guiProp
083                                        .getProperty("tabPaneFactory");
084                        Iterator<ConfigurationProperty> factories = tabProp.getProperties()
085                                        .iterator();
086
087                        TabManager m = TabManager.getInstance();
088                        while (factories.hasNext()) {
089                                ConfigurationProperty prop = factories.next();
090                                String classname = prop.getProperty("class").getValue();
091                                String tabname = prop.getProperty("name").getValue();
092
093                                TabPaneFactory tpf = (TabPaneFactory) parent.getConfiguration()
094                                                .getAttribute(this.getName());
095                                if (tpf != null) {
096                                        tpf = (TabPaneFactory) tpf.getAttribute(tabname);
097                                }
098
099                                if (tpf == null) {
100                                        // get the factory with reflections
101                                        Class factoryClass = Class.forName(classname);
102                                        Class[] args = new Class[] { NamedObj.class, String.class };
103                                        Constructor constructor = factoryClass.getConstructor(args);
104                                        Object[] argsImpl = new Object[] { this, tabname };
105                                        tpf = (TabPaneFactory) constructor.newInstance(argsImpl);
106                                }
107
108                                TabPane tp = tpf.createTabPane(parent);
109                                tp.setParentFrame(parent);
110                                tp.initializeTab();
111                                m.addTabPane(tp);
112                        }
113                        success = true;
114                } catch (Exception e) {
115                        try {
116                                e.printStackTrace();
117                                MessageHandler.warning("Could not create tab pane.", e);
118                        } catch (Exception ce) {
119                                // Do nothing
120                        }
121                        success = false;
122                }
123                return success;
124        }
125
126        /**
127         * Always returns null, this method should be overridden by the factory
128         * instance that extends this factory.
129         * 
130         **/
131        public TabPane createTabPane(TableauFrame parent) {
132                System.out.println("WARNING: returning null in createTabPane.");
133                return null;
134        }
135}