001/* An applet demonstrating EditablePlot. 002 003 Copyright (c) 1999-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.domains.sdf.demo.Sketch; 029 030import ptolemy.actor.CompositeActor; 031import ptolemy.actor.Manager; 032import ptolemy.actor.gui.AWTContainer; 033import ptolemy.actor.gui.MoMLApplet; 034import ptolemy.actor.lib.gui.SketchedSource; 035import ptolemy.data.IntToken; 036import ptolemy.domains.sdf.kernel.SDFDirector; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.plot.EditListener; 039import ptolemy.plot.EditablePlot; 040 041/////////////////////////////////////////////////////////////////// 042//// SketchApplet 043 044/** 045 This applet demonstrates the use of the SketchSource actor, 046 and in particular, how to share the same plot display between 047 an instance of SketchedSource and an instance of SequencePlotter. 048 049 @see SketchedSource 050 @author Edward A. Lee 051 @version $Id$ 052 @since Ptolemy II 0.4 053 @Pt.ProposedRating Red (eal) 054 @Pt.AcceptedRating Red (reviewmoderator) 055 */ 056@SuppressWarnings("serial") 057public class SketchApplet extends MoMLApplet implements EditListener { 058 /////////////////////////////////////////////////////////////////// 059 //// public methods //// 060 061 /** Execute the model. This is called by the editable plot widget 062 * when the user edits the data in the plot. The execution is 063 * carried out only if the model is idle (not currently executing). 064 * @param source The plot containing the modified data. 065 * @param dataset The data set that has been modified. 066 */ 067 @Override 068 public void editDataModified(EditablePlot source, int dataset) { 069 try { 070 if (_manager.getState() == Manager.IDLE) { 071 _sketchedSource.editDataModified(source, dataset); 072 _go(); 073 } 074 } catch (IllegalActionException ex) { 075 report(ex); 076 } 077 } 078 079 /** Create the shared plot and set it up based on the director parameters. 080 */ 081 @Override 082 public void _createView() { 083 super._createView(); 084 085 try { 086 // Find out how many iterations the director expects to run for. 087 CompositeActor toplevel = (CompositeActor) _toplevel; 088 SDFDirector director = (SDFDirector) toplevel.getDirector(); 089 int iterations = ((IntToken) director.iterations.getToken()) 090 .intValue(); 091 092 _sketchedSource = (SketchedSource) toplevel 093 .getEntity("SketchedSource"); 094 095 // Note: The order of the following is important. 096 // First, specify how long the sketched plot should be. 097 _sketchedSource.length.setToken(new IntToken(iterations)); 098 099 // Then, create the plot and place it in this applet, 100 // and specify to both the source and destination actors 101 // to use the same plot widget. 102 EditablePlot plot = new EditablePlot(); 103 plot.setSize(700, 300); 104 plot.setTitle("Editable envelope"); 105 plot.setXRange(0, iterations); 106 plot.setButtons(true); 107 getContentPane().add(plot); 108 _sketchedSource.place(new AWTContainer(plot)); 109 plot.setBackground(null); 110 plot.addEditListener(this); 111 } catch (Exception ex) { 112 report("Error constructing model.", ex); 113 } 114 } 115 116 /** The SketchedSource actor. */ 117 public SketchedSource _sketchedSource; 118}