001/* An attribute that creates an editor to open a doc viewer on its container's container.
002
003 Copyright (c) 2006-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.vergil.basic;
029
030import java.awt.Frame;
031import java.util.List;
032
033import ptolemy.actor.gui.EditorFactory;
034import ptolemy.actor.gui.Effigy;
035import ptolemy.actor.gui.TableauFrame;
036import ptolemy.kernel.CompositeEntity;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.KernelException;
039import ptolemy.kernel.util.NameDuplicationException;
040import ptolemy.kernel.util.NamedObj;
041import ptolemy.util.MessageHandler;
042import ptolemy.vergil.actor.DocEffigy;
043import ptolemy.vergil.actor.DocTableau;
044
045///////////////////////////////////////////////////////////////////
046//// DocViewerFactory
047
048/**
049 An attribute that creates an editor to open a doc viewer for
050 its container's container. The usage for this is to put it in
051 a visible attribute. When the user double clicks on the visible
052 attribute or selects Configure from the context menu, this class
053 will open documentation for the container's container. If the
054 container's container doesn't have any documentation, then
055 the user sees a dialog with instructions on how to create the
056 documentation.
057
058 @author Edward A. Lee
059 @version $Id$
060 @since Ptolemy II 5.2
061 @Pt.ProposedRating Red (eal)
062 @Pt.AcceptedRating Red (johnr)
063 */
064public class DocViewerFactory extends EditorFactory {
065
066    /** Construct a factory with the specified container and name.
067     *  @param container The container.
068     *  @param name The name of the factory.
069     *  @exception IllegalActionException If the factory is not of an
070     *   acceptable attribute for the container.
071     *  @exception NameDuplicationException If the name coincides with
072     *   an attribute already in the container.
073     */
074    public DocViewerFactory(NamedObj container, String name)
075            throws IllegalActionException, NameDuplicationException {
076        super(container, name);
077    }
078
079    ///////////////////////////////////////////////////////////////////
080    ////                         public methods                    ////
081
082    /** Create a doc viewer for the specified object with the
083     *  specified parent window.
084     *  @param object The object to configure, which is required to
085     *   an instance of DocAttribute.
086     *  @param parent The parent window, which is required to be an
087     *   instance of TableauFrame.
088     */
089    @Override
090    public void createEditor(NamedObj object, Frame parent) {
091        NamedObj container = object.getContainer();
092        if (container != null) {
093            List docAttributes = container.attributeList(DocAttribute.class);
094            // DocAttribute is singleton, so there should be at most one.
095            if (docAttributes.size() < 1) {
096                MessageHandler.message(
097                        "To create documentation, right click on the background "
098                                + "and select 'Documentation->Customize Documentation'");
099                return;
100            }
101            DocAttribute doc = (DocAttribute) docAttributes.get(0);
102            if (!(parent instanceof TableauFrame)) {
103                MessageHandler.error("Cannot display documentation!");
104            } else {
105                Effigy effigy = ((TableauFrame) parent).getEffigy();
106                try {
107                    DocEffigy newEffigy = new DocEffigy(
108                            (CompositeEntity) effigy.getContainer(),
109                            effigy.getContainer().uniqueName("parentClass"));
110                    newEffigy.setDocAttribute(doc);
111                    DocTableau tableau = new DocTableau(newEffigy,
112                            "docTableau");
113                    tableau.show();
114                } catch (KernelException e) {
115                    MessageHandler.error("Error opening documentation", e);
116                }
117            }
118            return;
119        }
120        MessageHandler.error("Need a container to document.");
121    }
122}