001/* A tableau that creates a new run control panel for a ptolemy model. 002 003 Copyright (c) 1998-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; 028 029import java.awt.Color; 030import java.awt.Graphics; 031import java.awt.event.ActionEvent; 032import java.awt.event.ActionListener; 033import java.awt.event.KeyEvent; 034import java.awt.print.PageFormat; 035import java.awt.print.Printable; 036import java.awt.print.PrinterException; 037 038import javax.swing.JMenu; 039import javax.swing.JMenuItem; 040 041import ptolemy.actor.CompositeActor; 042import ptolemy.actor.Manager; 043import ptolemy.kernel.util.Debuggable; 044import ptolemy.kernel.util.IllegalActionException; 045import ptolemy.kernel.util.KernelException; 046import ptolemy.kernel.util.NameDuplicationException; 047import ptolemy.kernel.util.NamedObj; 048import ptolemy.util.CancelException; 049import ptolemy.util.MessageHandler; 050 051/////////////////////////////////////////////////////////////////// 052//// RunTableau 053 054/** 055 A tableau that creates a new run control panel for a ptolemy model. 056 This panel has controls for parameters of the top-level entity 057 and its director, if any, a set of buttons to control execution 058 of the model, and a panel displaying the placeable entities within 059 the model. 060 061 @author Steve Neuendorffer and Edward A. Lee 062 @version $Id$ 063 @since Ptolemy II 1.0 064 @Pt.ProposedRating Red (neuendor) 065 @Pt.AcceptedRating Red (neuendor) 066 */ 067public class RunTableau extends Tableau { 068 /** Create a new run control panel for the model with the given 069 * effigy. The tableau is itself an entity contained by the effigy 070 * and having the specified name. The frame is not made visible 071 * automatically. You must call show() to make it visible. 072 * @param container The containing effigy. 073 * @param name The name of this tableau within the specified effigy. 074 * @exception IllegalActionException If the tableau is not acceptable 075 * to the specified container. 076 * @exception NameDuplicationException If the container already contains 077 * an entity with the specified name. 078 */ 079 public RunTableau(PtolemyEffigy container, String name) 080 throws IllegalActionException, NameDuplicationException { 081 super(container, name); 082 083 NamedObj model = container.getModel(); 084 085 if (!(model instanceof CompositeActor)) { 086 throw new IllegalActionException(this, 087 "Cannot run a model that is not a CompositeActor." 088 + " It is: " + model); 089 } 090 091 CompositeActor actor = (CompositeActor) model; 092 093 // Create a manager. 094 Manager manager = actor.getManager(); 095 096 if (manager == null) { 097 try { 098 actor.setManager(new Manager(actor.workspace(), "manager")); 099 } catch (IllegalActionException ex) { 100 throw new IllegalActionException(this, ex, 101 "Failed to set manager. This can occur if " 102 + "you try to run a non-toplevel model that " 103 + "is a component of a toplevel model. " 104 + "The solution is invoke View -> Run while in a " 105 + "toplevel window."); 106 } 107 108 //manager = actor.getManager(); 109 } 110 111 ModelFrame frame = new RunFrame(actor, this); 112 setFrame(frame); 113 frame.setBackground(BACKGROUND_COLOR); 114 } 115 116 /////////////////////////////////////////////////////////////////// 117 //// private variables //// 118 // FIXME: should be somewhere else? 119 // Default background color is a light grey. 120 private static Color BACKGROUND_COLOR = new Color(0xe5e5e5); 121 122 /////////////////////////////////////////////////////////////////// 123 //// inner classes //// 124 125 /** The frame that is created by an instance of RunTableau. 126 */ 127 @SuppressWarnings("serial") 128 public class RunFrame extends ModelFrame implements Printable { 129 /** Construct a frame to control the specified Ptolemy II model. 130 * After constructing this, it is necessary 131 * to call setVisible(true) to make the frame appear. 132 * This is typically accomplished by calling show() on 133 * enclosing tableau. 134 * @param model The model to put in this frame, or null if none. 135 * @param tableau The tableau responsible for this frame. 136 */ 137 public RunFrame(CompositeActor model, Tableau tableau) { 138 super(model, tableau); 139 } 140 141 /** Print the plot to a printer, represented by the specified graphics 142 * object. 143 * @param graphics The context into which the page is drawn. 144 * @param format The size and orientation of the page being drawn. 145 * @param index The zero based index of the page to be drawn. 146 * @return PAGE_EXISTS if the page is rendered successfully, or 147 * NO_SUCH_PAGE if pageIndex specifies a non-existent page. 148 * @exception PrinterException If the print job is terminated. 149 */ 150 @Override 151 public synchronized int print(Graphics graphics, PageFormat format, 152 int index) throws PrinterException { 153 if (graphics == null) { 154 return Printable.NO_SUCH_PAGE; 155 } 156 157 // We only print on one page. 158 // FIXME: we should allow printing to multiple pages 159 if (index >= 1) { 160 return Printable.NO_SUCH_PAGE; 161 } 162 163 paint(graphics); 164 return Printable.PAGE_EXISTS; 165 } 166 167 /////////////////////////////////////////////////////////////////// 168 //// protected methods //// 169 170 /** Add a Debug menu. 171 */ 172 @Override 173 protected void _addMenus() { 174 super._addMenus(); 175 176 JMenuItem[] debugMenuItems = { 177 new JMenuItem("Listen to Manager", KeyEvent.VK_M), 178 new JMenuItem("Listen to Director", KeyEvent.VK_D), }; 179 180 // NOTE: This has to be initialized here rather than 181 // statically because this method is called by the constructor 182 // of the base class, and static initializers have not yet 183 // been run. 184 _debugMenu = new JMenu("Debug"); 185 _debugMenu.setMnemonic(KeyEvent.VK_D); 186 187 DebugMenuListener debugMenuListener = new DebugMenuListener(); 188 189 // Set the action command and listener for each menu item. 190 for (JMenuItem debugMenuItem : debugMenuItems) { 191 debugMenuItem.setActionCommand(debugMenuItem.getText()); 192 debugMenuItem.addActionListener(debugMenuListener); 193 _debugMenu.add(debugMenuItem); 194 } 195 196 _menubar.add(_debugMenu); 197 } 198 199 /////////////////////////////////////////////////////////////////// 200 //// protected variables //// 201 202 /** Debug menu for this frame. */ 203 protected JMenu _debugMenu; 204 205 /////////////////////////////////////////////////////////////////// 206 //// inner classes //// 207 208 /** Listener for debug menu commands. */ 209 public class DebugMenuListener implements ActionListener { 210 @Override 211 public void actionPerformed(ActionEvent e) { 212 JMenuItem target = (JMenuItem) e.getSource(); 213 String actionCommand = target.getActionCommand(); 214 NamedObj model = getModel(); 215 216 if (model instanceof CompositeActor) { 217 try { 218 Debuggable debug; 219 220 if (actionCommand.equals("Listen to Manager")) { 221 debug = ((CompositeActor) model).getManager(); 222 } else if (actionCommand.equals("Listen to Director")) { 223 debug = ((CompositeActor) model).getDirector(); 224 } else { 225 debug = null; 226 } 227 228 if (debug != null) { 229 Effigy effigy = (Effigy) RunTableau.this 230 .getContainer(); 231 232 // Create a new text effigy inside this one. 233 Effigy textEffigy = new TextEffigy(effigy, 234 effigy.uniqueName("debug listener")); 235 DebugListenerTableau tableau = new DebugListenerTableau( 236 textEffigy, 237 textEffigy.uniqueName("debugListener")); 238 tableau.setDebuggable(debug); 239 } 240 } catch (KernelException ex) { 241 try { 242 MessageHandler.warning( 243 "Failed to create debug listener: " + ex); 244 } catch (CancelException exception) { 245 } 246 } 247 } 248 } 249 } 250 } 251 252 /** A factory that creates run control panel tableaux for Ptolemy models. 253 */ 254 public static class Factory extends TableauFactory { 255 /** Create a factory with the given name and container. 256 * @param container The container. 257 * @param name The name. 258 * @exception IllegalActionException If the container is incompatible 259 * with this attribute. 260 * @exception NameDuplicationException If the name coincides with 261 * an attribute already in the container. 262 */ 263 public Factory(NamedObj container, String name) 264 throws IllegalActionException, NameDuplicationException { 265 super(container, name); 266 } 267 268 /////////////////////////////////////////////////////////////////// 269 //// public methods //// 270 271 /** If the specified effigy already contains a tableau named 272 * "runTableau", then return that tableau; otherwise, create 273 * a new instance of RunTableau for the effigy, and 274 * name it "runTableau". If the specified effigy is not an 275 * instance of PtolemyEffigy, then do not create a tableau 276 * and return null. It is the responsibility of callers of 277 * this method to check the return value and call show(). 278 * 279 * @param effigy The model effigy. 280 * @return A new run tableau if the effigy is a PtolemyEffigy, 281 * or null otherwise. 282 * @exception Exception If the factory should be able to create a 283 * tableau for the effigy, but something goes wrong. 284 */ 285 @Override 286 public Tableau createTableau(Effigy effigy) throws Exception { 287 if (effigy instanceof PtolemyEffigy) { 288 // First see whether the effigy already contains a RunTableau. 289 RunTableau tableau = (RunTableau) effigy 290 .getEntity("runTableau"); 291 292 if (tableau == null) { 293 tableau = new RunTableau((PtolemyEffigy) effigy, 294 "runTableau"); 295 } 296 297 // Don't call show() here, it is called for us in 298 // TableauFrame.ViewMenuListener.actionPerformed() 299 return tableau; 300 } else { 301 return null; 302 } 303 } 304 } 305 306 /** A factory that creates run control panel tableaux for the model 307 * associated with a top-level effigy (one that has a file 308 * representation). 309 */ 310 public static class TopFactory extends Factory { 311 /** Create a factory with the given name and container. 312 * @param container The container. 313 * @param name The name. 314 * @exception IllegalActionException If the container is incompatible 315 * with this attribute. 316 * @exception NameDuplicationException If the name coincides with 317 * an attribute already in the container. 318 */ 319 public TopFactory(NamedObj container, String name) 320 throws IllegalActionException, NameDuplicationException { 321 super(container, name); 322 } 323 324 /////////////////////////////////////////////////////////////////// 325 //// public methods //// 326 327 /** Create a tableau to run the model associated with the specified 328 * effigy. The top-level effigy, as returned by 329 * {@link Effigy#masterEffigy()}, is the one that is run. 330 * If that effigy already contains a tableau named 331 * "runTableau", then return that tableau; otherwise, create 332 * a new instance of RunTableau for the top effigy, and 333 * name it "runTableau". If the specified effigy is not an 334 * instance of PtolemyEffigy, then do not create a tableau 335 * and return null. It is the responsibility of callers of 336 * this method to check the return value and call show(). 337 * 338 * @param effigy The model effigy. 339 * @return A new run tableau if the effigy is a PtolemyEffigy, 340 * or null otherwise. 341 * @exception Exception If the factory should be able to create a 342 * tableau for the effigy, but something goes wrong. 343 */ 344 @Override 345 public Tableau createTableau(Effigy effigy) throws Exception { 346 return super.createTableau(effigy.masterEffigy()); 347 } 348 } 349}