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.figure;
033
034import java.awt.Color;
035import java.awt.geom.Rectangle2D;
036import java.awt.geom.RectangularShape;
037
038import javax.swing.SwingUtilities;
039
040import org.kepler.monitor.MonitoredStatus.State;
041
042import diva.canvas.toolbox.BasicFigure;
043import ptolemy.data.ScalarToken;
044import ptolemy.kernel.util.IllegalActionException;
045
046/** A quality figure.
047 * 
048 * @author Aisa Na'Im
049 * @version $Id: QualityFigure.java 33630 2015-08-24 22:44:14Z crawl $
050 *
051 */
052public class QualityFigure extends BaseFigure {
053
054        public QualityFigure(RectangularShape shape) {
055                super(shape);
056                
057                this._shape = (RectangularShape) shape.clone();
058                _orientation = Orientation.HORIZONTAL;
059                figs = new BasicFigure[1];
060                _setRectangle(figs, _orientation, _shape);
061                
062                for (BasicFigure fig : figs) {
063                        this.add(fig);
064                }
065        }
066        
067
068        /**
069     * Updates the quality score value 
070     */
071    public void update2(final Object value) {
072        _lastUpdate = System.currentTimeMillis();
073        Runnable doSet = new Runnable() {
074            @Override
075            public void run() {
076                _update2(value);
077            }
078        };
079        SwingUtilities.invokeLater(doSet);
080    }
081
082        /** Set the rectangle figure */
083        private static void _setRectangle(BasicFigure[] figs, Orientation orientation,
084                        RectangularShape shape) {
085        
086                double x = shape.getX();
087                double y = shape.getY();
088                double h = shape.getHeight();
089                double w = shape.getWidth();
090
091                figs[0] = new BasicFigure(new Rectangle2D.Double(x, y, h, w));
092        }
093        
094
095        @Override
096        protected void _update() {
097        }
098        
099
100        /** Update color of rectangle based on quality thresholds and score */
101        protected void _update2(Object value) {
102                
103                if (!(value.equals(null) || value.equals(State.NO_QUALITY_SCORE))) {
104                        //figs[0].setFillPaint(Color.BLACK);
105                //}
106                //else {
107                        setQualityValue(value);
108                        try {
109                                if (quality_value.equals(high_quality) ||
110                                                quality_value.isGreaterThan(high_quality).booleanValue()) {
111                                        figs[0].setFillPaint(Color.GREEN);
112                                }
113                                else if (quality_value.equals(low_quality) || quality_value.isLessThan(low_quality).booleanValue()) {
114                                        figs[0].setFillPaint(Color.RED);
115                                }
116                                else if (quality_value.isLessThan(high_quality).booleanValue() &&
117                                                quality_value.isGreaterThan(low_quality).booleanValue())
118                                {
119                                        figs[0].setFillPaint(Color.YELLOW);
120                                }
121                                        
122                        } catch (IllegalActionException e) {
123                                // TODO Auto-generated catch block
124                                e.printStackTrace();
125                        } catch (NullPointerException e) {
126                                e.printStackTrace();
127                        }
128                }
129        }
130        
131        /** Set the high quality threshold for entity being monitored */
132        public void setHighQualityThreshold(Object high_quality_token){
133                high_quality = (ScalarToken) high_quality_token; 
134        }
135        
136        /** Set the low quality threshold for entity being monitored */
137        public void setLowQualityThreshold(Object low_quality_token) {
138                low_quality = (ScalarToken) low_quality_token; 
139
140        }
141        
142        /** Set the quality score for entity being monitored */
143        public void setQualityValue(Object quality_score_token) {
144                        quality_value = (ScalarToken) quality_score_token; 
145                        //System.out.println("(QF) quality_score is: " + quality_score_token.toString());
146        }
147        
148        /** The rectangle within the rectangle to be filled */
149        private BasicFigure[] figs;
150
151        /** Quality score values and thresholds */ 
152        private ScalarToken quality_value;      
153        private ScalarToken high_quality; 
154        private ScalarToken low_quality; 
155         
156}