001/* A tableau for evaluating Tcl expression interactively.
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 */
027package ptolemy.actor.gui.ptjacl;
028
029import java.awt.BorderLayout;
030import java.net.URL;
031
032import javax.swing.BoxLayout;
033import javax.swing.JPanel;
034
035import ptolemy.actor.gui.Effigy;
036import ptolemy.actor.gui.Tableau;
037import ptolemy.actor.gui.TableauFactory;
038import ptolemy.actor.gui.TableauFrame;
039import ptolemy.gui.ShellInterpreter;
040import ptolemy.gui.ShellTextArea;
041import ptolemy.kernel.util.IllegalActionException;
042import ptolemy.kernel.util.NameDuplicationException;
043import ptolemy.kernel.util.NamedObj;
044import tcl.lang.Interp;
045import tcl.lang.ReflectObject;
046import tcl.lang.TclException;
047
048///////////////////////////////////////////////////////////////////
049//// TclShellTableau
050
051/**
052 A tableau that provides a Tcl Shell for interacting with Ptjacl,
053 a 100% Java implementation of Tcl.
054
055 @author Christopher Hylands and Edward A. Lee
056 @version $Id$
057 @since Ptolemy II 3.0
058 @Pt.ProposedRating Red (cxh)
059 @Pt.AcceptedRating Red (cxh)
060 */
061public class TclShellTableau extends Tableau implements ShellInterpreter {
062    /** Create a new tableau.
063     *  The tableau is itself an entity contained by the effigy
064     *  and having the specified name.  The frame is not made visible
065     *  automatically.  You must call show() to make it visible.
066     *  @param container The containing effigy.
067     *  @param name The name of this tableau within the specified effigy.
068     *  @exception IllegalActionException If the tableau is not acceptable
069     *   to the specified container.
070     *  @exception NameDuplicationException If the container already contains
071     *   an entity with the specified name.
072     */
073    public TclShellTableau(TclShellEffigy container, String name)
074            throws IllegalActionException, NameDuplicationException {
075        super(container, name);
076
077        TclShellFrame frame = new TclShellFrame(this);
078        setFrame(frame);
079
080        try {
081            //
082            _tclInterp.setVar("panelShell", ReflectObject.newInstance(
083                    _tclInterp, ShellTextArea.class, frame.shellTextArea), 0);
084            _tclInterp.eval("proc puts {s} {" + "global panelShell; "
085                    + "$panelShell appendJTextArea $s\\n}");
086
087            // FIXME: what about user initializations in ~/.tclrc?
088            // Source Ptolemy specific initializations.
089            _tclInterp.eval(
090                    "if [catch {source [java::call ptolemy.data.expr.UtilityFunctions findFile \"ptolemy/actor/gui/ptjacl/init.tcl\"]} errMsg ] { puts $errorInfo};");
091        } catch (TclException ex) {
092            throw new IllegalActionException(this, ex,
093                    "Could not initialize the " + "tcl interpreter:\n"
094                            + _tclInterp.getResult().toString());
095        }
096    }
097
098    ///////////////////////////////////////////////////////////////////
099    ////                         public methods                    ////
100
101    /** Evaluate the specified command.
102     *  @param command The command.
103     *  @return The return value of the command, or null if there is none.
104     *  @exception Exception If something goes wrong processing the command.
105     */
106    @Override
107    public String evaluateCommand(String command) throws Exception {
108        try {
109            _tclInterp.eval(command);
110            return _tclInterp.getResult().toString();
111        } catch (TclException ex) {
112            return _tclInterp.getVar("errorInfo", null, 0).toString();
113        }
114    }
115
116    /** Return true if the specified command is complete (ready
117     *  to be interpreted).
118     *  @param command The command.
119     *  @return True if the command is complete.
120     */
121    @Override
122    public boolean isCommandComplete(String command) {
123        return Interp.commandComplete(command);
124    }
125
126    ///////////////////////////////////////////////////////////////////
127    ////                         private variables                 ////
128    // The Tcl interpreter
129    // FIXME: Perhaps the interpreter should be in its own thread?
130    private Interp _tclInterp = new Interp();
131
132    ///////////////////////////////////////////////////////////////////
133    ////                         inner classes                     ////
134
135    /** The frame that is created by an instance of TclShellTableau.
136     */
137    @SuppressWarnings("serial")
138    public static class TclShellFrame extends TableauFrame {
139        // FindBugs suggested refactoring this into a static class.
140
141        /** Construct a frame to display the TclShell window.
142         *  After constructing this, it is necessary
143         *  to call setVisible(true) to make the frame appear.
144         *  This is typically accomplished by calling show() on
145         *  enclosing tableau.
146         *  @param tclShellTableau The tableau responsible for this frame.
147         *  @exception IllegalActionException If the model rejects the
148         *   configuration attribute.
149         *  @exception NameDuplicationException If a name collision occurs.
150         */
151        public TclShellFrame(TclShellTableau tclShellTableau)
152                throws IllegalActionException, NameDuplicationException {
153            super(tclShellTableau);
154
155            JPanel component = new JPanel();
156            component.setLayout(new BoxLayout(component, BoxLayout.Y_AXIS));
157
158            shellTextArea = new ShellTextArea();
159            shellTextArea.setInterpreter(tclShellTableau);
160            shellTextArea.mainPrompt = "% ";
161            component.add(shellTextArea);
162            getContentPane().add(component, BorderLayout.CENTER);
163        }
164
165        ///////////////////////////////////////////////////////////////////
166        ////                         public variables                  ////
167
168        /** The text area tableau used for input and output. */
169        public ShellTextArea shellTextArea;
170
171        ///////////////////////////////////////////////////////////////////
172        ////                         protected methods                 ////
173        @Override
174        protected void _help() {
175            try {
176                URL doc = getClass().getClassLoader()
177                        .getResource("ptolemy/actor/gui/ptjacl/help.htm");
178                getConfiguration().openModel(null, doc, doc.toExternalForm());
179            } catch (Exception ex) {
180                System.out.println("TclShellTableau._help(): " + ex);
181                _about();
182            }
183        }
184    }
185
186    /** A factory that creates a control panel to display a Tcl Shell.
187     */
188    public static class Factory extends TableauFactory {
189        /** Create a factory with the given name and container.
190         *  @param container The container.
191         *  @param name The name.
192         *  @exception IllegalActionException If the container is incompatible
193         *   with this attribute.
194         *  @exception NameDuplicationException If the name coincides with
195         *   an attribute already in the container.
196         */
197        public Factory(NamedObj container, String name)
198                throws IllegalActionException, NameDuplicationException {
199            super(container, name);
200        }
201
202        ///////////////////////////////////////////////////////////////////
203        ////                         public methods                    ////
204
205        /** Create a new instance of TclShellTableau in the specified
206         *  effigy. It is the responsibility of callers of
207         *  this method to check the return value and call show().
208         *  @param effigy The model effigy.
209         *  @return A new control panel tableau if the effigy is
210         *    a PtolemyEffigy, or null otherwise.
211         *  @exception Exception If the factory should be able to create a
212         *   tableau for the effigy, but something goes wrong.
213         */
214        @Override
215        public Tableau createTableau(Effigy effigy) throws Exception {
216            // NOTE: Can create any number of tableaux within the same
217            // effigy.  Is this what we want?
218            if (effigy instanceof TclShellEffigy) {
219                return new TclShellTableau((TclShellEffigy) effigy,
220                        "TclShellTableau");
221            } else {
222                return null;
223            }
224        }
225    }
226}