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