001/* An attribute that causes look inside to open a text editor to
002 edit a file or URL specified by an attribute in the container.
003
004 Copyright (c) 2003-2014 The Regents of the University of California.
005 All rights reserved.
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.toolbox;
031
032import java.io.File;
033import java.net.URL;
034
035import javax.swing.JOptionPane;
036
037import ptolemy.actor.gui.Configuration;
038import ptolemy.actor.gui.Effigy;
039import ptolemy.actor.gui.PtolemyEffigy;
040import ptolemy.actor.gui.Tableau;
041import ptolemy.actor.gui.TableauFactory;
042import ptolemy.data.expr.FileParameter;
043import ptolemy.kernel.util.Attribute;
044import ptolemy.kernel.util.IllegalActionException;
045import ptolemy.kernel.util.NameDuplicationException;
046import ptolemy.kernel.util.NamedObj;
047import ptolemy.kernel.util.StringAttribute;
048
049///////////////////////////////////////////////////////////////////
050//// FileEditorTableauFactory
051
052/**
053 This class is an attribute that creates an editor to edit a specified
054 file or URL given by an attribute in the container of this attribute.
055 It is similar to TextEditorTableauFactory, but instead of editing an
056 attribute in the container, it edits a file or URL referenced by that
057 attribute.  The file or URL must be given in the container by an
058 instance of FileParameter. If the file or URL does not exist, then
059 it attempts to create the file and open it.
060
061 @author Edward A. Lee
062 @version $Id$
063 @since Ptolemy II 4.0
064 @Pt.ProposedRating Yellow (eal)
065 @Pt.AcceptedRating Red (ptolemy)
066 @see TextEditorTableauFactory
067 @see FileParameter
068 */
069public class FileEditorTableauFactory extends TableauFactory {
070    /** Create a factory with the given name and container.
071     *  @param container The container.
072     *  @param name The name.
073     *  @exception IllegalActionException If the container is incompatible
074     *   with this attribute.
075     *  @exception NameDuplicationException If the name coincides with
076     *   an attribute already in the container.
077     */
078    public FileEditorTableauFactory(NamedObj container, String name)
079            throws IllegalActionException, NameDuplicationException {
080        super(container, name);
081
082        attributeName = new StringAttribute(this, "attributeName");
083    }
084
085    ///////////////////////////////////////////////////////////////////
086    ////                         parameters                        ////
087
088    /** The name of the file attribute giving the file name or URL. */
089    public StringAttribute attributeName;
090
091    ///////////////////////////////////////////////////////////////////
092    ////                         public methods                    ////
093
094    /** Create a tableau for the specified effigy. The tableau will be
095     *  created with a new unique name with the specified effigy as its
096     *  container.  If this factory cannot create a tableau
097     *  for the given effigy (it is not an instance of PtolemyEffigy),
098     *  then return null.
099     *  @param effigy The component effigy.
100     *  @return A tableau for the effigy, or null if one cannot be created.
101     *  @exception Exception If the factory should be able to create a
102     *   Tableau for the effigy, but something goes wrong.
103     */
104    @Override
105    public Tableau createTableau(Effigy effigy) throws Exception {
106        // FIXME: Exceptions thrown here are ignored by the caller,
107        // who then just goes to the next tableau factory...
108        if (!(effigy instanceof PtolemyEffigy)) {
109            return null;
110        }
111
112        NamedObj object = ((PtolemyEffigy) effigy).getModel();
113        Attribute attribute = object
114                .getAttribute(attributeName.getExpression());
115
116        if (!(attribute instanceof FileParameter)) {
117            throw new IllegalActionException(object,
118                    "Expected " + object.getFullName()
119                            + " to contain a FileParameter named "
120                            + attributeName.getExpression()
121                            + ", but it does not.");
122        }
123
124        URL url = ((FileParameter) attribute).asURL();
125        Configuration configuration = (Configuration) effigy.toplevel();
126        try {
127            return configuration.openModel(null, url, url.toExternalForm());
128        } catch (Exception ex) {
129            // Attempt to create a blank file with the specified name.
130            // If there is no specified name, first prompt the user for one.
131            FileParameter parameter = (FileParameter) attribute;
132            if (parameter.getExpression().trim().equals("")) {
133                // Prompt for a file name.
134                // Then set the parameter to match the file name.
135                String inputValue = JOptionPane
136                        .showInputDialog("Please specify a file name");
137                parameter.setExpression(inputValue);
138                url = parameter.asURL();
139            }
140            File file = parameter.asFile();
141            if (!file.exists()) {
142                if (!file.createNewFile()) {
143                    throw new Exception(
144                            "Failed to create \"" + file.getName() + "\"");
145                }
146            } else {
147                // FIXME: Why did this fail? Prompt for new file name?
148            }
149            // Try again to open the model.
150            return configuration.openModel(null, url, url.toExternalForm());
151        }
152    }
153}