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.awt.Component;
033import java.lang.reflect.Constructor;
034import java.util.Iterator;
035
036import javax.swing.JTabbedPane;
037
038import org.kepler.configuration.ConfigurationManager;
039import org.kepler.configuration.ConfigurationProperty;
040
041import ptolemy.actor.gui.TableauFrame;
042import ptolemy.kernel.util.Attribute;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.NameDuplicationException;
045import ptolemy.kernel.util.NamedObj;
046import ptolemy.util.MessageHandler;
047
048//////////////////////////////////////////////////////////////////////////
049//// TableauFactory
050/**
051 * This class is an attribute that creates a LibraryPaneTab for the
052 * TabbedLibraryPane.
053 * 
054 *@author Aaron Schultz
055 */
056public class PreferencesTabFactory extends Attribute {
057
058        /**
059         * Create a factory with the given name and container.
060         * 
061         *@param container
062         *            The container.
063         *@param name
064         *            The name.
065         *@exception IllegalActionException
066         *                If the container is incompatible with this attribute.
067         *@exception NameDuplicationException
068         *                If the name coincides with an attribute already in the
069         *                container.
070         */
071        public PreferencesTabFactory(NamedObj container, String name)
072                        throws IllegalActionException, NameDuplicationException {
073                super(container, name);
074        }
075
076        // /////////////////////////////////////////////////////////////////
077        // // public methods ////
078
079        /**
080         *@return A LibraryPaneTab for the TabbedLibraryPane, or null if one cannot
081         *         be created.
082         */
083        public boolean createPreferencesTabs(JTabbedPane parent,
084                        TableauFrame frame) {
085                boolean success = false;
086                try {
087      //get the factory classes from the config
088      ConfigurationProperty guiProp = ConfigurationManager.getInstance()
089        .getProperty(ConfigurationManager.getModule("gui"));
090      ConfigurationProperty tabProp = guiProp.getProperty("preferencesTabFactory");
091      Iterator factories = tabProp.getProperties().iterator();
092      
093                        while (factories.hasNext()) {
094        ConfigurationProperty prop = (ConfigurationProperty)factories.next();
095        String classname = prop.getProperty("class").getValue();
096        String tabname = prop.getProperty("name").getValue();
097        PreferencesTabFactory ptf = (PreferencesTabFactory)frame.getConfiguration().getAttribute(this.getName());
098        if(ptf != null)
099        {
100          ptf = (PreferencesTabFactory)ptf.getAttribute(tabname);
101        }
102        
103        if(ptf == null)
104        {
105          //get the factory with reflections
106          Class factoryClass = Class.forName(classname);
107          Class[] args = new Class[] {NamedObj.class, String.class};
108          Constructor constructor = factoryClass.getConstructor(args);
109          Object[] argsImpl = new Object[] {this, tabname};
110          ptf = (PreferencesTabFactory)constructor.newInstance(argsImpl);
111        }
112        //create the pref tab
113        PreferencesTab pt = ptf.createPreferencesTab();
114                                pt.setParent(frame);
115                                pt.initializeTab();
116                                parent.add(pt.getTabName(), (Component) pt);
117                        }
118                        success = true;
119                } catch (Exception e) {
120                        try {
121                                MessageHandler.warning("Could not create data pane.", e);
122                        } catch (Exception ce) {
123                                // Do nothing
124                        }
125                        success = false;
126                }
127                return success;
128        }
129
130        /**
131         * Always returns null, this method should be overridden by the factory
132         * instance that extends this factory.
133         * 
134         *       */
135        public PreferencesTab createPreferencesTab() {
136                return null;
137        }
138}