001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: tao $'
006 * '$Date: 2011-08-05 20:57:09 +0000 (Fri, 05 Aug 2011) $' 
007 * '$Revision: 28219 $'
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.Color;
033import java.awt.Component;
034import java.awt.SystemColor;
035import java.awt.event.ActionEvent;
036import java.util.Vector;
037
038import org.apache.commons.logging.Log;
039import org.apache.commons.logging.LogFactory;
040
041import ptolemy.actor.gui.TableauFrame;
042
043/**
044 * This singleton class can be used to hold references to java components that
045 * implement the TabPane interface so they can be accessed and controlled from
046 * anywhere. Add methods to this class as you need them.
047 */
048public class TabManager {
049
050        private Vector<TabPane> _tabPanes;
051
052        private Vector<TabPaneActionListener> _tabPaneListeners;
053
054        private static final Log log = LogFactory
055                        .getLog(TabManager.class.getName());
056        private static final boolean isDebugging = log.isDebugEnabled();
057
058        /**
059         * Due to slight color change in the background area of a tab this color
060         * should be used to set the background color of components inside the tab.
061         */
062        //public static final Color BGCOLOR = new Color(221, 221, 221);
063        public static final Color BGCOLOR = SystemColor.window;
064
065        // public static final Color BGCOLOR = new Color(255, 255, 255);
066
067        /**
068         * Constructor.
069         */
070        protected TabManager() {
071                _tabPanes = new Vector<TabPane>();
072                _tabPaneListeners = new Vector<TabPaneActionListener>();
073        }
074
075        /**
076         * Instantiate all of the TabPanes that are specified in configuration.xml
077         * 
078         * @param parent
079         */
080        public void initializeTabs(TableauFrame parent) {
081                try {
082                        TabPaneFactory TPfactory = (TabPaneFactory) parent
083                                        .getConfiguration().getAttribute("tabPaneFactory");
084                        if (TPfactory == null) {
085                                TPfactory = new TabPaneFactory(parent.getConfiguration(),
086                                                "tabPaneFactory");
087                        }
088                        if (TPfactory != null) {
089                                boolean success = TPfactory.createTabPaneTabs(parent);
090                                if (!success) {
091                                        System.out
092                                                        .println("error: TabPane is null.  "
093                                                                        + "This "
094                                                                        + "problem can be fixed by adding a tabPaneFactory "
095                                                                        + "property in the configuration.xml file.");
096                                }
097                        } else {
098                                System.out.println("error: TabPane is " + "null.  This "
099                                                + "problem can be fixed by adding a tabPaneFactory "
100                                                + "property in the configuration.xml file.");
101                        }
102                } catch (Exception e) {
103                        System.out.println("Error creating the tabpanefactory: "
104                                        + e.getMessage());
105                        e.printStackTrace();
106                }
107
108        }
109
110        public void removeAllFrameTabs(TableauFrame parent) {
111                for (int i = 0; i < _tabPanes.size(); i++) {
112                        TabPane tabPane = _tabPanes.elementAt(i);
113                        TableauFrame tableauFrame = tabPane.getParentFrame();
114                        if (tableauFrame == parent) {
115                                if (isDebugging)
116                                        log.debug("tabmanager removing at " + i);
117                                tabPane.setParentFrame(null);
118                                _tabPanes.removeElementAt(i);
119                                i--;
120                        }
121                }
122                for (int i = 0; i < _tabPaneListeners.size(); i++) {
123                        if (_tabPaneListeners.get(i).getParentFrame() == parent) {
124                                _tabPaneListeners.removeElementAt(i);
125                                i--;
126                        }
127                }
128        }
129
130        public void removeFrameTab(TableauFrame parent, String name) {
131                for (int i = 0; i < _tabPanes.size(); i++) {
132                        TabPane tabPane = _tabPanes.elementAt(i);
133                        TableauFrame tableauFrame = tabPane.getParentFrame();
134                        if (tableauFrame == parent) {
135                                String tabName = tabPane.getTabName();
136                                if (tabName.equals(name)) {
137                                        tabPane.setParentFrame(null);
138                                        _tabPanes.removeElementAt(i);
139                                        i--;
140                                }
141                        }
142                }
143
144        }
145
146        /**
147         * Return a vector of tab pane objects for the specified TableauFrame.
148         * 
149         * @param parent
150         * */
151        public Vector<TabPane> getFrameTabs(TableauFrame parent) {
152                Vector<TabPane> frameTabPanes = new Vector<TabPane>();
153                for (int i = 0; i < _tabPanes.size(); i++) {
154                        if (_tabPanes.elementAt(i).getParentFrame() == parent) {
155                                frameTabPanes.add(_tabPanes.elementAt(i));
156                        }
157                }
158                return frameTabPanes;
159        }
160
161        /**
162         * Register a TabPane with the TabManager. TabPanes must be subclasses of
163         * java.awt.Component
164         * 
165         * @param tp
166         * @throws ClassCastException
167         */
168        public void addTabPane(TabPane tp) throws ClassCastException {
169                if (tp instanceof Component) {
170                        _tabPanes.add(tp);
171                } else {
172                        throw new ClassCastException(tp.getTabName()
173                                        + " TabPane must be a subclass of java.awt.Component");
174                }
175        }
176
177        public boolean tabExists(TableauFrame parent, String tabName) {
178                boolean exists = false;
179                tabName = tabName.trim();
180                for (int i = 0; i < _tabPanes.size(); i++) {
181                        if (_tabPanes.elementAt(i).getParentFrame() == parent) {
182                                if (_tabPanes.elementAt(i).getTabName().equals(tabName)) {
183                                        exists = true;
184                                }
185                        }
186                }
187                return exists;
188        }
189
190        public TabPane getTab(TableauFrame parent, String tabName) {
191                tabName = tabName.trim();
192                for (int i = 0; i < _tabPanes.size(); i++) {
193                        if (_tabPanes.elementAt(i).getParentFrame() == parent) {
194                                if (_tabPanes.elementAt(i).getTabName().equals(tabName)) {
195                                        return _tabPanes.elementAt(i);
196                                }
197                        }
198                }
199                return null;
200        }
201
202        /**
203         * returns the FIRST tab matching the given class param
204         * 
205         * @param parent
206         * @param tabClass
207         * @return
208         */
209        public TabPane getTab(TableauFrame parent, Class tabClass) {
210                for (int i = 0; i < _tabPanes.size(); i++) {
211                        if (_tabPanes.elementAt(i).getParentFrame() == parent) {
212                                if (tabClass.isInstance(_tabPanes.elementAt(i))) {
213                                        return _tabPanes.elementAt(i);
214                                }
215                        }
216                }
217                return null;
218        }
219
220        public void addTabPaneListener(TableauFrame parent,
221                        TabPaneActionListener tpl) {
222                _tabPaneListeners.add(tpl);
223        }
224
225        public void removeTabPaneListener(TabPaneActionListener tpl) {
226                _tabPaneListeners.remove(tpl);
227        }
228
229        public void tabEvent(TableauFrame parent, ActionEvent ae) {
230                for (int i = 0; i < _tabPaneListeners.size(); i++) {
231                        if (_tabPaneListeners.get(i).getParentFrame() == parent) {
232                                _tabPaneListeners.get(i).actionPerformed(ae);
233                        }
234                }
235        }
236
237        /**
238         * Method for getting an instance of this singleton class.
239         */
240        public static TabManager getInstance() {
241                return TabManagerHolder.INSTANCE;
242        }
243
244        private static class TabManagerHolder {
245                private static final TabManager INSTANCE = new TabManager();
246        }
247
248}