001/*
002 *    $RCSfile$
003 *
004 *     $Author: crawl $
005 *       $Date: 2015-08-24 22:44:14 +0000 (Mon, 24 Aug 2015) $
006 *   $Revision: 33630 $
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;
033
034import java.awt.Color;
035import java.awt.Font;
036import java.awt.geom.Rectangle2D;
037import java.awt.geom.RoundRectangle2D;
038
039import javax.swing.SwingConstants;
040
041import diva.canvas.CompositeFigure;
042import diva.canvas.Figure;
043import diva.canvas.toolbox.BasicFigure;
044import diva.canvas.toolbox.LabelFigure;
045import ptolemy.kernel.util.IllegalActionException;
046import ptolemy.kernel.util.NameDuplicationException;
047import ptolemy.kernel.util.NamedObj;
048import ptolemy.vergil.icon.DynamicEditorIcon;
049
050/**
051 * The actual icon that displays a monitoring figure. This figure is set with
052 * {@link #setFigure(Figure)}.
053 * 
054 * @author Carlos Rueda
055 * @version $Id: MonitorIcon.java 33630 2015-08-24 22:44:14Z crawl $
056 */
057public class MonitorIcon extends DynamicEditorIcon {
058        public MonitorIcon(NamedObj container, String name)
059                        throws IllegalActionException, NameDuplicationException {
060                super(container, name);
061                _text = null;
062                setPersistent(false);
063        }
064
065        /**
066         * Sets the foreground figure for this icon.
067         * 
068         * @param figure
069         */
070        public void setFigure(Figure figure) {
071                _figure = figure;
072        }
073
074        public Figure getFigure() {
075                return _figure;
076        }
077
078        /**
079         * Create a new default background figure.
080         * 
081         * @return A figure representing the specified shape.
082         */
083        public Figure createBackgroundFigure() {
084                double x = 0;
085                double y = 0;
086                if (_text != null) {
087                        _createLabelFigure();
088                } else if (_figure != null) {
089                        Rectangle2D bounds = _figure.getBounds();
090                        x = bounds.getMinX();
091                        y = bounds.getMinY();
092                        _width = bounds.getWidth();
093                        _heigth = bounds.getHeight();
094                }
095                BasicFigure fig = new BasicFigure(new RoundRectangle2D.Double(x, y,
096                                _width, _heigth, 8, 8));
097                fig.setFillPaint(_bgColor);
098                fig.setStrokePaint(_bgColor);
099                return fig;
100        }
101
102        public Figure createFigure() {
103                CompositeFigure result = (CompositeFigure) super.createFigure();
104
105                if (_text != null) {
106                        LabelFigure label = _createLabelFigure();
107                        Rectangle2D backBounds = result.getBackgroundFigure().getBounds();
108                        label.translateTo(backBounds.getMinX() + 11, backBounds.getMinY()
109                                        + _heigth / 4);
110                        result.add(label);
111                }
112
113                if (_figure != null) {
114                        _addLiveFigure(_figure);
115                        result.add(_figure);
116                }
117
118                return result;
119        }
120
121        
122        public void setBackgroundColor(Color color) {
123                _bgColor = color;
124        }
125
126        /** Set the color of the text for this icon. */
127        public void setForegroundColor(Color color) {
128            _labelColor = color;
129        }
130
131        /** Sets the text for this icon */
132        public void setText(String _text) {
133                this._text = _text;
134        }
135
136        /** Gets the text of this icon */
137        public String getText() {
138                return _text;
139        }
140
141        /** Sets the text font for this icon */
142        public void setFont(Font font) {
143                this._font = font;
144        }
145        
146        // /////////////////////////////////////////////////////////////////
147        // // private members ////
148
149        private String _displayString() {
150                if (_text != null) {
151                        LabelFigure label = new LabelFigure(_text, _font, 1.0,
152                                        SwingConstants.LEFT);
153                        Rectangle2D stringBounds = label.getBounds();
154                        _width = stringBounds.getWidth() + 20;
155                        _heigth = stringBounds.getHeight() + 12;
156                }
157                return _text;
158        }
159
160        private LabelFigure _createLabelFigure() {
161                LabelFigure label = new LabelFigure(_displayString(), _font);
162
163                // By default, the origin should be the upper left.
164                label.setAnchor(SwingConstants.NORTH_WEST);
165                label.setFillPaint(_labelColor);
166
167                return label;
168        }
169
170        private double _width = 1;
171
172        private double _heigth = 1;
173
174        /** The foreground figure for this icon */
175        private Figure _figure;
176
177        private Font _font = new Font("SansSerif", Font.PLAIN, 12);
178
179        private Color _bgColor = Color.LIGHT_GRAY;
180
181        private Color _labelColor = Color.WHITE;// BLACK;
182
183        private String _text = null;
184
185        private static final long serialVersionUID = 1L;
186}