001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-10-28 21:00:47 +0000 (Wed, 28 Oct 2015) $' 
007 * '$Revision: 34133 $'
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
030/**
031 * 
032 */
033package org.kepler.gui;
034
035import java.awt.BorderLayout;
036import java.awt.Component;
037import java.awt.Container;
038import java.awt.Dimension;
039import java.util.ArrayList;
040import java.util.List;
041import java.util.Vector;
042
043import javax.swing.JPanel;
044import javax.swing.JSplitPane;
045import javax.swing.JTabbedPane;
046import javax.swing.plaf.basic.BasicTabbedPaneUI;
047
048import diva.gui.toolbox.JCanvasPanner;
049import ptolemy.actor.gui.TableauFrame;
050import ptolemy.kernel.util.IllegalActionException;
051import ptolemy.kernel.util.NameDuplicationException;
052import ptolemy.kernel.util.NamedObj;
053import ptolemy.vergil.basic.BasicGraphFrame;
054
055/**
056 * A ViewPane consisting of 4 ViewPaneLocations.
057 * 
058 * @author Aaron Schultz
059 * 
060 */
061public class ClassicViewPane extends JPanel implements ViewPane {
062
063        private TableauFrame _frame;
064        private String _viewName;
065        private Vector<ViewPaneLocation> _locations;
066
067        /*
068         * The top level split pane that divides the JPanel into left and right
069         * halves.
070         */
071        private JSplitPane _westEastSplitPane;
072
073        private JSplitPane _westSplitPane;
074
075        private JTabbedPane _wtp;
076        private JTabbedPane _etp;
077
078        /**
079         * Constructor. Initializes available locations.
080         */
081        public ClassicViewPane() {
082                _locations = new Vector<ViewPaneLocation>();
083                _locations.add(new ViewPaneLocation("W"));
084                _locations.add(new ViewPaneLocation("E"));
085        }
086
087        /*
088         * (non-Javadoc)
089         * 
090         * @see org.kepler.gui.ViewPane#getParentFrame()
091         */
092        @Override
093    public TableauFrame getParentFrame() {
094                return _frame;
095        }
096
097        /*
098         * (non-Javadoc)
099         * 
100         * @see
101         * org.kepler.gui.ViewPane#setParentFrame(ptolemy.actor.gui.TableauFrame)
102         */
103        @Override
104    public void setParentFrame(TableauFrame parent) {
105                _frame = parent;
106        }
107
108        /*
109         * (non-Javadoc)
110         * 
111         * @see org.kepler.gui.ViewPane#getViewName()
112         */
113        @Override
114    public String getViewName() {
115                return _viewName;
116        }
117
118        public void setViewName(String viewName) {
119                _viewName = viewName;
120        }
121
122        /*
123         * (non-Javadoc)
124         * 
125         * @see org.kepler.gui.ViewPane#initializeView()
126         */
127        @Override
128    public void initializeView() throws Exception {
129
130                _wtp = new JTabbedPane();
131                _etp = new JTabbedPane();
132                
133                // hide the tab if only one pane is present. for the workflow
134                // tab displaying the canvas, this saves some vertical space since
135                // there is only one tabbed in this pane.
136        _etp.setUI(new BasicTabbedPaneUI() {
137            @Override
138            protected int calculateTabAreaHeight(int tabPlacement,
139                    int horizRunCount, int maxTabHeight) {
140                if (_etp.getTabCount() > 1) {
141                    return super.calculateTabAreaHeight(tabPlacement,
142                            horizRunCount, maxTabHeight);
143                } else {
144                    return -10;
145                }
146            }
147        });
148
149                _westSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true);
150                _westSplitPane.setOneTouchExpandable(true);
151                _westSplitPane.setResizeWeight(1);
152                _westSplitPane.setBorder( null );
153                _westSplitPane.setTopComponent(_wtp);
154                
155                // set the color
156                //_westSplitPane.setBackground(new Color(0, 0, 255));
157
158                JCanvasPanner jcp = ((BasicGraphFrame) _frame).getGraphPanner();
159                jcp.setMinimumSize(new Dimension(200, 200));
160                _westSplitPane.setBottomComponent(jcp);
161
162                _westEastSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true);
163                _westEastSplitPane.setOneTouchExpandable(true);
164                _westEastSplitPane.setRightComponent(_etp);
165                _westEastSplitPane.setLeftComponent(_westSplitPane);
166       
167                // set the color
168                //_westEastSplitPane.setBackground(new Color(0, 0, 255));
169
170                this.setLayout(new BorderLayout());
171                this.add(_westEastSplitPane, BorderLayout.CENTER);
172
173        }
174
175        /*
176         * (non-Javadoc)
177         * 
178         * @see org.kepler.gui.ViewPane#getAvailableLocations()
179         */
180        public Vector<ViewPaneLocation> getAvailableLocations() {
181                return _locations;
182        }
183
184        @Override
185    public boolean hasLocation(String locationName) {
186                for (int i = 0; i < _locations.size(); i++) {
187                        if (_locations.elementAt(i).getName().equals(locationName)) {
188                                return true;
189                        }
190                }
191                return false;
192        }
193
194        /*
195         * (non-Javadoc)
196         * 
197         * @see org.kepler.gui.ViewPane#addTabPane(org.kepler.gui.TabPane,
198         * org.kepler.gui.ViewPaneLocation)
199         */
200        @Override
201    public void addTabPane(TabPane tabPane, ViewPaneLocation location)
202                        throws Exception {
203
204                if (location.getName() == "W") {
205                        _wtp.add(tabPane.getTabName(), (Component) tabPane);
206                } else if (location.getName() == "E") {
207                        _etp.add(tabPane.getTabName(), (Component) tabPane);
208                } else {
209                        throw new Exception(
210                                        "Unable to add "
211                                                        + tabPane.getTabName()
212                                                        + " TabPane to "
213                                                        + getViewName()
214                                                        + " ViewPane. "
215                                                        + " The supplied ViewPaneLocation does not exist for this ViewPane.");
216                }
217
218        }
219
220        @Override
221    public Container getLocationContainer(String locationName) throws Exception {
222
223                if (locationName.equals("W")) {
224                        return _wtp;
225                } else if (locationName.equals("E")) {
226                        return _etp;
227                } else {
228                        throw new Exception(
229                                        "Unable to find "
230                                                        + locationName
231                                                        + " ViewPaneLocation in the "
232                                                        + getViewName()
233                                                        + " ViewPane. "
234                                                        + " The supplied ViewPaneLocation does not exist for this ViewPane.");
235                }
236
237        }
238
239        /**
240         * A factory that creates the library panel for the editors.
241         * 
242         *@author Aaron Schultz
243         */
244        public static class Factory extends ViewPaneFactory {
245                /**
246                 * Create a factory with the given name and container.
247                 * 
248                 *@param container
249                 *            The container.
250                 *@param name
251                 *            The name of the entity.
252                 *@exception IllegalActionException
253                 *                If the container is incompatible with this attribute.
254                 *@exception NameDuplicationException
255                 *                If the name coincides with an attribute already in the
256                 *                container.
257                 */
258                public Factory(NamedObj container, String name)
259                                throws IllegalActionException, NameDuplicationException {
260                        super(container, name);
261                }
262
263                /**
264                 * Create a library pane that displays the given library of actors.
265                 * 
266                 * @return A new LibraryPaneTab that displays the library
267                 */
268                @Override
269        public ViewPane createViewPane(TableauFrame parent) {
270
271                        ClassicViewPane vp = new ClassicViewPane();
272                        vp.setParentFrame(parent);
273
274                        // use the name specified in the configuration to name the view
275                        vp.setViewName(this.getName());
276
277                        return vp;
278                }
279        }
280
281        @Override
282    public List<TabPane> getTabPanes(String tabName) throws Exception {
283                List<TabPane> panes = new ArrayList<TabPane>();
284                int index = -1;
285                index = _etp.indexOfTab(tabName);
286                if (index > -1) {
287                        panes.add((TabPane) _etp.getComponentAt(index));
288                }
289                index = _wtp.indexOfTab(tabName);
290                if (index > -1) {
291                        panes.add((TabPane) _wtp.getComponentAt(index));
292                }
293                return panes;
294        }
295}