001/* A panel that shows the elements of a Ptolemy II model in a JTree.
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005
006 Permission is hereby granted, without written agreement and without
007 license or royalty fees, to use, copy, modify, and distribute this
008 software and its documentation for any purpose, provided that the above
009 copyright notice and the following two paragraphs appear in all copies
010 of this software.
011
012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
016 SUCH DAMAGE.
017
018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 ENHANCEMENTS, OR MODIFICATIONS.
024
025 PT_COPYRIGHT_VERSION_2
026 COPYRIGHTENDKEY
027
028
029 */
030package ptolemy.vergil.tree;
031
032import java.awt.Dimension;
033
034import javax.swing.JScrollPane;
035import javax.swing.JTree;
036import javax.swing.tree.TreeCellRenderer;
037
038import ptolemy.kernel.CompositeEntity;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NamedObj;
041import ptolemy.moml.MoMLParser;
042
043///////////////////////////////////////////////////////////////////
044//// TreeEditorPanel
045
046/**
047 A panel that displays a Ptolemy II model in a JTree.
048
049 @author Edward Lee
050 @version $Id$
051 @since Ptolemy II 1.0
052 @Pt.ProposedRating Red (eal)
053 @Pt.AcceptedRating Red (eal)
054 */
055@SuppressWarnings("serial")
056public class TreeEditorPanel extends JScrollPane {
057    /** Construct a display of the Ptolemy II model given by the
058     *  specified MoML file.
059     *  @param filename The name of a MoML file.
060     *  @exception Exception If the parser cannot parse the file.
061     */
062    public TreeEditorPanel(String filename) throws Exception {
063        this(filename, null);
064    }
065
066    /** Construct a display of the Ptolemy II model given by the
067     *  specified MoML file.
068     *  @param filename The name of a MoML file.
069     *  @param cellRenderer The renderer for nodes of the tree, or null
070     *   to use the default.
071     *  @exception Exception If the parser cannot parse the file.
072     */
073    public TreeEditorPanel(String filename, TreeCellRenderer cellRenderer)
074            throws Exception {
075        _parser = new MoMLParser();
076
077        // FIXME: This should use the Configuration.
078        _toplevel = _parser.parseFile(filename);
079
080        if (_toplevel instanceof CompositeEntity) {
081            FullTreeModel model = new FullTreeModel(
082                    (CompositeEntity) _toplevel);
083            JTree tree = new JTree(model);
084            tree.setPreferredSize(new Dimension(600, 800));
085
086            if (cellRenderer == null) {
087                cellRenderer = new PtolemyTreeCellRenderer();
088            }
089
090            tree.setCellRenderer(cellRenderer);
091            tree.setScrollsOnExpand(true);
092
093            setViewportView(tree);
094        } else {
095            throw new IllegalActionException("Cannot display a tree unless "
096                    + "the top level is a CompositeEntity.");
097        }
098    }
099
100    ///////////////////////////////////////////////////////////////////
101    ////                         private variables                 ////
102
103    /** The parser being used for this model. */
104    private MoMLParser _parser;
105
106    /** The top-level entity of the model. */
107    private NamedObj _toplevel;
108}