001/* An attribute that creates a table to edit an array of records.
002
003 Copyright (c) 2003-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.toolbox;
029
030import java.awt.Frame;
031
032import ptolemy.actor.gui.ArrayOfRecordsPane;
033import ptolemy.actor.gui.EditorFactory;
034import ptolemy.data.ArrayToken;
035import ptolemy.data.Token;
036import ptolemy.data.expr.Parameter;
037import ptolemy.data.expr.StringParameter;
038import ptolemy.data.type.ArrayType;
039import ptolemy.data.type.BaseType;
040import ptolemy.gui.ComponentDialog;
041import ptolemy.kernel.util.IllegalActionException;
042import ptolemy.kernel.util.KernelException;
043import ptolemy.kernel.util.NameDuplicationException;
044import ptolemy.kernel.util.NamedObj;
045import ptolemy.util.MessageHandler;
046
047///////////////////////////////////////////////////////////////////
048//// ArrayOfRecordsConfigureFactory
049
050/**
051 If this class is contained by an actor, then double clicking on that
052 actor will display a table that shows the value of an
053 array of tokens contained by a parameter contained by the
054 same container as this factory. The name of the parameter
055 is given by the  <i>parameterName</i> attribute of this factory.
056 It is required that the parameter contain an array of records.
057
058 @author Edward A. Lee
059 @version $Id$
060 @since Ptolemy II 8.0
061 @Pt.ProposedRating Yellow (eal)
062 @Pt.AcceptedRating Red (ptolemy)
063 */
064public class ArrayOfRecordsConfigureFactory extends EditorFactory {
065    /** Construct a factory with the specified container and name.
066     *  @param container The container.
067     *  @param name The name of the factory.
068     *  @exception IllegalActionException If the factory is not of an
069     *   acceptable attribute for the container.
070     *  @exception NameDuplicationException If the name coincides with
071     *   an attribute already in the container.
072     */
073    public ArrayOfRecordsConfigureFactory(NamedObj container, String name)
074            throws IllegalActionException, NameDuplicationException {
075        super(container, name);
076
077        parameterName = new StringParameter(this, "parameterName");
078        columns = new Parameter(this, "columns");
079        columns.setTypeEquals(new ArrayType(BaseType.STRING));
080    }
081
082    ///////////////////////////////////////////////////////////////////
083    ////                         parameters                        ////
084
085    /** The names of the fields to be displayed from the records, in
086     *  the order in which they should be displayed. This is required
087     *  to be an array of string tokens. This defaults to null (no value)
088     *  which results in all fields being displayed in alphabetical
089     *  order.
090     */
091    public Parameter columns;
092
093    /** The name of the attribute that is to be displayed.
094     *  That attribute is required to contain an array of record tokens.
095     */
096    public StringParameter parameterName;
097
098    ///////////////////////////////////////////////////////////////////
099    ////                         public methods                    ////
100
101    /** Create a top-level viewer for the specified object with the
102     *  specified parent window.
103     *  @param object The object to configure, which is required to
104     *   contain a parameter with name matching <i>parameterName</i>
105     *   and value that is an array of records.
106     *  @param parent The parent window, which is required to be an
107     *   instance of TableauFrame.
108     */
109    @Override
110    public void createEditor(NamedObj object, Frame parent) {
111        try {
112            Parameter attributeToEdit = (Parameter) object.getAttribute(
113                    parameterName.getExpression(), Parameter.class);
114            if (attributeToEdit == null) {
115                MessageHandler.error(
116                        "No such parameter: " + parameterName.getExpression());
117                return;
118            }
119            Token value = attributeToEdit.getToken();
120            if (!(value instanceof ArrayToken)) {
121                MessageHandler
122                        .error("Parameter does not contain an array token: "
123                                + attributeToEdit.toString());
124                return;
125            }
126            ArrayOfRecordsPane pane = new ArrayOfRecordsPane();
127            pane.display((ArrayToken) value, (ArrayToken) columns.getToken());
128            new ComponentDialog(parent, object.getFullName(), pane);
129        } catch (KernelException ex) {
130            MessageHandler.error(
131                    "Cannot get specified string attribute to edit.", ex);
132            return;
133        }
134    }
135}