001/* 002 * $RCSfile$ 003 * 004 * $Author: crawl $ 005 * $Date: 2012-11-26 22:22:25 +0000 (Mon, 26 Nov 2012) $ 006 * $Revision: 31122 $ 007 * 008 * For Details: http://kepler-project.org 009 * 010 * Copyright (c) 2007 The Regents of the University of California. 011 * All rights reserved. 012 * 013 * Permission is hereby granted, without written agreement and without 014 * license or royalty fees, to use, copy, modify, and distribute this 015 * software and its documentation for any purpose, provided that the 016 * above copyright notice and the following two paragraphs appear in 017 * all copies of this software. 018 * 019 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 020 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 021 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 022 * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY 023 * OF SUCH DAMAGE. 024 * 025 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 026 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 027 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 028 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY 029 * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 030 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 031 */ 032package org.kepler.monitor.test; 033 034import java.awt.geom.RectangularShape; 035import java.awt.geom.RoundRectangle2D; 036 037import org.kepler.monitor.MonitorIcon; 038import org.kepler.monitor.figure.BaseFigure.Orientation; 039import org.kepler.monitor.figure.ProgressBarFigure; 040 041import ptolemy.actor.lib.Transformer; 042import ptolemy.data.expr.StringParameter; 043import ptolemy.kernel.CompositeEntity; 044import ptolemy.kernel.util.Attribute; 045import ptolemy.kernel.util.IllegalActionException; 046import ptolemy.kernel.util.NameDuplicationException; 047 048/** 049 * This actor demonstrates the ProgressBarFigure by simulating some task that 050 * depends on the arrival of tokens. 051 * 052 * In this demo, the figure is always displayed. A parameter allows to set the 053 * maximum value for the range of the progress bar. Upon each firing, the 054 * current value of the progress bar is incremented. If it reaches the maximum, 055 * then it's set to the minimum again. If the maximum parameter is set to 0, the 056 * progress bar takes the indeterminate style. By default, the progress bar 057 * takes a horizontal layout. 058 * 059 * @see org.kepler.monitor.figure.ProgressBarFigure 060 * 061 * @author Carlos Rueda 062 * @version $Id: ActorWithProgressBar.java 31122 2012-11-26 22:22:25Z crawl $ 063 */ 064public class ActorWithProgressBar extends Transformer { 065 public StringParameter maxParam; 066 public StringParameter layoutParam; 067 068 public ActorWithProgressBar(CompositeEntity container, String name) 069 throws IllegalActionException, NameDuplicationException { 070 super(container, name); 071 072 maxParam = new StringParameter(this, "maxParam"); 073 maxParam.setExpression("" + _max); 074 maxParam.setDisplayName("Maximum (0 for indeterminate)"); 075 076 layoutParam = new StringParameter(this, "layoutParam"); 077 layoutParam.setExpression("horizontal"); 078 layoutParam.addChoice("horizontal"); 079 layoutParam.addChoice("vertical"); 080 layoutParam.setDisplayName("Layout"); 081 082 _icon = new MonitorIcon(this, "_icon"); 083 _icon.setText("ActorWithProgressBar"); 084 085 _figure = _createFigure(); 086 _icon.setFigure(_figure); 087 } 088 089 /** 090 * Starts the progress bar. 091 */ 092 public void initialize() throws IllegalActionException { 093 super.initialize(); 094 _figure.setStarted(true); 095 } 096 097 /** 098 * Updates the progress bar according to the new parameters. 099 */ 100 public void attributeChanged(Attribute attribute) 101 throws IllegalActionException { 102 if (attribute == maxParam) { 103 try { 104 int max = Integer.parseInt(maxParam.stringValue()); 105 if (max <= 0) { 106 _figure.setIndeterminate(true); 107 } else { 108 _figure.setIndeterminate(false); 109 _figure.setMaximum(max); 110 _figure.setValue(_figure.getMinimum() - 1); 111 } 112 } catch (NumberFormatException e) { 113 throw new IllegalActionException("NumberFormatException: " + e); 114 } 115 } else if (attribute == layoutParam) { 116 if ("horizontal".equalsIgnoreCase(layoutParam.stringValue())) { 117 _figure.setShape(_horizontalBounds); 118 _figure.setOrientation(Orientation.HORIZONTAL); 119 } else { 120 _figure.setShape(_verticalBounds); 121 _figure.setOrientation(Orientation.VERTICAL); 122 } 123 } else { 124 super.attributeChanged(attribute); 125 } 126 } 127 128 /** 129 * Broadcasts the input token. If the progress bar is determinate, it 130 * increments the current value, re-starting from the minimum if the current 131 * value has already reached the maximum. 132 */ 133 public void fire() throws IllegalActionException { 134 super.fire(); 135 if (input.hasToken(0)) { 136 137 if (!_figure.isIndeterminate()) { 138 int next = 1 + _figure.getCurrentValue(); 139 if (next > _figure.getMaximum()) { 140 // re-start progress: 141 next = _figure.getMinimum() - 1; 142 } 143 _figure.setValue(next); 144 } 145 146 output.broadcast(input.get(0)); 147 } 148 } 149 150 /** Stops the progress bar. */ 151 public void wrapup() throws IllegalActionException { 152 super.wrapup(); 153 _figure.setStarted(false); 154 } 155 156 // ///////////////////////////////////////////////////////////////// 157 // // private members //// 158 159 /** Creates a horizontal, determinate progress bar */ 160 private static ProgressBarFigure _createFigure() { 161 ProgressBarFigure progFig = new ProgressBarFigure(_horizontalBounds); 162 progFig.setIndeterminate(false); 163 progFig.setMaximum(_max); 164 progFig.setValue(progFig.getMinimum() - 1); 165 return progFig; 166 } 167 168 private static int _max = 50; 169 private static RectangularShape _horizontalBounds = new RoundRectangle2D.Double( 170 8, 20, 60, 6, 6, 6); 171 private static RectangularShape _verticalBounds = new RoundRectangle2D.Double( 172 4, 3, 6, 18, 6, 6); 173 private ProgressBarFigure _figure; 174 private MonitorIcon _icon; 175 176 private static final long serialVersionUID = 1L; 177}