001/* An editor factory for an object that has a fileOrURL parameter.
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.actor.gui;
029
030import java.awt.Component;
031import java.awt.Dimension;
032import java.awt.Frame;
033import java.net.URL;
034
035import javax.swing.BorderFactory;
036import javax.swing.JPanel;
037import javax.swing.JScrollPane;
038import javax.swing.JTextArea;
039
040import ptolemy.data.expr.FileParameter;
041import ptolemy.data.expr.StringParameter;
042import ptolemy.gui.ComponentDialog;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.InternalErrorException;
045import ptolemy.kernel.util.NameDuplicationException;
046import ptolemy.kernel.util.NamedObj;
047
048///////////////////////////////////////////////////////////////////
049//// FileOrURLEditorFactory
050
051/**
052 An editor factory for an attribute that has a fileOrURL parameter.
053
054 The editor has two choices: the usual parameter editor and a file editor
055 that edits the fileOrURL parameter.
056
057 @author Christopher Brooks, based on AnnotationEditorFactory by Edward A. Lee
058 @version $Id$
059 @since Ptolemy II 5.2
060 @Pt.ProposedRating Red (cxh)
061 @Pt.AcceptedRating Red (cxh)
062 */
063public class FileOrURLEditorFactory extends EditorFactory {
064    /** Construct a factory with the specified container and name.
065     *  @param container The container.
066     *  @param name The name of the factory.
067     *  @exception IllegalActionException If the factory is not of an
068     *   acceptable attribute for the container.
069     *  @exception NameDuplicationException If the name coincides with
070     *   an attribute already in the container.
071     */
072    public FileOrURLEditorFactory(NamedObj container, String name)
073            throws IllegalActionException, NameDuplicationException {
074        super(container, name);
075    }
076
077    ///////////////////////////////////////////////////////////////////
078    ////                         public methods                    ////
079
080    /** Create an editor for configuring the specified object.
081     */
082    @Override
083    public void createEditor(NamedObj object, Frame parent) {
084
085        ComponentDialog dialog = null;
086
087        FileParameter helpFileParameter = (FileParameter) object
088                .getAttribute("help");
089
090        String[] buttons = _moreButtons;
091        if (helpFileParameter == null) {
092            // Do not include the help button.
093            buttons = _moreButtonsNoHelp;
094        }
095
096        dialog = new ComponentDialog(parent, "Edit Parameters or File",
097                createEditorPane(), buttons);
098
099        String button = dialog.buttonPressed();
100
101        if (button.equals("Cancel")) {
102            return;
103        }
104
105        if (button.equals(_moreButtons[0])) {
106            new EditParametersDialog(parent, object);
107            return;
108        }
109
110        Configuration configuration = (Configuration) Configuration
111                .findEffigy(object.getContainer()).toplevel();
112
113        if (button.equals(_moreButtons[1])) {
114            FileParameter fileOrURLParameter = (FileParameter) object
115                    .getAttribute("fileOrURL");
116            if (fileOrURLParameter == null) {
117                throw new InternalErrorException(object, null,
118                        "No \"fileOrURL\" attribute.");
119            } else {
120                try {
121                    if (fileOrURLParameter.asURL() == null) {
122                        ModelDirectory directory = configuration.getDirectory();
123
124                        StringParameter initialDefaultContentsParameter = (StringParameter) object
125                                .getAttribute("initialDefaultContents");
126                        String defaultText = "";
127                        if (initialDefaultContentsParameter != null) {
128                            defaultText = initialDefaultContentsParameter
129                                    .getExpression();
130                        }
131                        Effigy effigy = TextEffigy.newTextEffigy(directory,
132                                defaultText);
133                        configuration.createPrimaryTableau(effigy);
134
135                        //                         EffigyFactory textEffigyFactory = new TextEffigy.Factory(directory, "effigyFactory");
136                        //                         Tableau tableau = configuration.openModel(
137                        //                                 null, null, "Unnamed",
138                        //                                 textEffigyFactory);
139                        //                         Effigy effigy = (Effigy) tableau.getContainer();
140                        //                         effigy.masterEffigy().setModifiable(true);
141                        //                         System.out.println("FileOrURLEditorFactory: modifiable"
142                        //                                            + effigy.isModifiable() + " " + effigy);
143
144                    } else {
145                        URL fileURL = fileOrURLParameter.asFile().toURI()
146                                .toURL();
147                        configuration.openModel(null, fileURL,
148                                fileURL.toExternalForm());
149                    }
150                } catch (Exception ex) {
151                    throw new InternalErrorException(object, ex,
152                            "Failed to open \"" + fileOrURLParameter + "\"");
153                }
154                return;
155            }
156        }
157
158        if (button.equals(_moreButtons[2])) {
159            try {
160                if (helpFileParameter != null) {
161                    URL fileURL = helpFileParameter.asFile().toURI().toURL();
162                    configuration.openModel(null, fileURL,
163                            fileURL.toExternalForm());
164                } else {
165                    throw new InternalErrorException("No help parameter?");
166                }
167            } catch (Exception ex) {
168                throw new InternalErrorException(object, ex,
169                        "Failed to open \"" + helpFileParameter + "\"");
170            }
171            return;
172        }
173    }
174
175    /** Return a new widget for configuring the container.
176     *  @return A JPanel that is a text editor for editing the annotation text.
177     */
178    public Component createEditorPane() {
179        JPanel panel = new JPanel();
180        JTextArea textArea = new JTextArea(
181                "To specify the name of the file that contains the parameters, select "
182                        + "\"" + _moreButtons[0]
183                        + "\".  To edit the file itself that contains the parameters,"
184                        + " select \"" + _moreButtons[1] + "\".",
185                3, 40);
186        textArea.setEditable(false);
187        textArea.setBorder(BorderFactory.createEtchedBorder());
188        textArea.setLineWrap(true);
189        textArea.setWrapStyleWord(true);
190
191        JScrollPane pane = new JScrollPane(textArea);
192
193        // NOTE: Should the size be hardwired here?
194        pane.setPreferredSize(new Dimension(400, 100));
195        panel.add(pane);
196
197        return panel;
198    }
199
200    ///////////////////////////////////////////////////////////////////
201    ////                         private members                   ////
202
203    // Button labels.
204    private static String[] _moreButtons = { "Configure Parameters",
205            "Edit File", "Help", "Cancel" };
206
207    private static String[] _moreButtonsNoHelp = { "Configure Parameters",
208            "Edit File", "Cancel" };
209
210}