001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: aschultz $'
006 * '$Date: 2011-03-19 02:24:12 +0000 (Sat, 19 Mar 2011) $' 
007 * '$Revision: 27324 $'
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.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037import org.kepler.configuration.ConfigurationManager;
038import org.kepler.configuration.ConfigurationProperty;
039
040import ptolemy.actor.gui.TableauFrame;
041import ptolemy.kernel.util.Attribute;
042import ptolemy.kernel.util.IllegalActionException;
043import ptolemy.kernel.util.NameDuplicationException;
044import ptolemy.kernel.util.NamedObj;
045import ptolemy.util.MessageHandler;
046
047public class ViewPaneFactory extends Attribute {
048
049        private static final Log log = LogFactory.getLog(ViewPaneFactory.class
050                        .getName());
051        private static final boolean isDebugging = log.isDebugEnabled();
052
053        public ViewPaneFactory(NamedObj container, String name)
054                        throws IllegalActionException, NameDuplicationException {
055                super(container, name);
056        }
057
058        // /////////////////////////////////////////////////////////////////
059        // // public methods ////
060
061        /**
062         * Create and initialize the ViewPane for each known ViewPaneFactory.
063         * 
064         * @return True if successfully initialized, false otherwise.
065         */
066        public boolean createViewPanes(TableauFrame parent) {
067                boolean success = false;
068                try {
069                        ConfigurationProperty guiProp = ConfigurationManager.getInstance()
070                                        .getProperty(ConfigurationManager.getModule("gui"));
071                        ConfigurationProperty viewPaneProp = guiProp
072                                        .getProperty("viewPaneFactory");
073                        Iterator<ConfigurationProperty> factories = viewPaneProp
074                                        .getProperties().iterator();
075
076                        ViewManager m = ViewManager.getInstance();
077                        while (factories.hasNext()) {
078                                ConfigurationProperty prop = (ConfigurationProperty) factories
079                                                .next();
080                                String classname = prop.getProperty("class").getValue();
081                                String panename = prop.getProperty("name").getValue();
082                                try {
083                                        String tableau = prop.getProperty("tableau").getValue();
084                                        if (!parent.getTableau().getClass().getName()
085                                                        .endsWith(tableau)) {
086                                                continue;
087                                        }
088                                } catch (NullPointerException ignored) {
089                                }
090
091                                if (isDebugging)
092                                        log.debug("classname: " + classname);
093                                if (isDebugging)
094                                        log.debug("panename: " + panename);
095
096                                ViewPaneFactory vpf = (ViewPaneFactory) parent
097                                                .getConfiguration().getAttribute(this.getName());
098                                if (vpf != null) {
099                                        vpf = (ViewPaneFactory) vpf.getAttribute(panename);
100                                }
101
102                                if (vpf == null) {
103                                        Class factoryClass = Class.forName(classname);
104                                        Class[] args = new Class[] { NamedObj.class, String.class };
105                                        Constructor constructor = factoryClass.getConstructor(args);
106                                        Object[] argsImpl = new Object[] { this, panename };
107                                        vpf = (ViewPaneFactory) constructor.newInstance(argsImpl);
108                                }
109
110                                ViewPane vp = vpf.createViewPane(parent);
111                                vp.setParentFrame(parent);
112                                vp.initializeView();
113                                m.addViewPane(vp);
114                        }
115                        success = true;
116                } catch (Exception e) {
117                        try {
118                                e.printStackTrace();
119                                MessageHandler.warning("Could not create tab pane.", e);
120                        } catch (Exception ce) {
121                                // Do nothing
122                        }
123                        success = false;
124                }
125                return success;
126        }
127
128        /**
129         * Always returns null, this method should be overridden by the factory
130         * instance that extends this factory.
131         * 
132         * */
133        public ViewPane createViewPane(TableauFrame parent) {
134                return null;
135        }
136}