001/* 002 * $RCSfile$ 003 * 004 * $Author: barseghian $ 005 * $Date: 2012-08-09 23:50:32 +0000 (Thu, 09 Aug 2012) $ 006 * $Revision: 30396 $ 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.beans.PropertyChangeEvent; 035import java.beans.PropertyChangeListener; 036 037import org.apache.commons.logging.Log; 038import org.apache.commons.logging.LogFactory; 039import org.kepler.monitor.MonitoredStatus.State; 040import org.kepler.monitor.figure.BaseFigure; 041import org.kepler.monitor.figure.ProgressBarFigure; 042import org.kepler.monitor.figure.QualityFigure; 043import org.kepler.monitor.figure.StateFigure; 044 045import diva.canvas.Figure; 046import diva.canvas.toolbox.LabelFigure; 047 048/** 049 * Updates a figure used for monitoring. The figure is updated as reaction to 050 * property changes occuring in some monitored object. 051 * 052 * @author Carlos Rueda 053 * @version $Id: FigureUpdater.java 30396 2012-08-09 23:50:32Z barseghian $ 054 */ 055public class FigureUpdater implements PropertyChangeListener { 056 057 /** 058 * Creates a figure updater. Use {@link #setFigure(Figure)} to associate a 059 * figure. 060 */ 061 public FigureUpdater() { 062 } 063 064 /** 065 * Sets the figure. Updates on the previous figure, if any, are stopped. If 066 * this updater was already started, then it continues updating the new 067 * figure. 068 * 069 * @param fig 070 */ 071 public void setFigure(Figure fig) { 072 boolean wasStarted = _started; 073 stop(); 074 _figure = fig; 075 if (wasStarted) { 076 start(); 077 } 078 } 079 080 /** 081 * Gets the associated figure. 082 * 083 * @return the figure 084 */ 085 public Figure getFigure() { 086 return _figure; 087 } 088 089 /** 090 * 091 * Sets the property name for the case of LabelFigure 092 * 093 * @param propName 094 * Name of property 095 */ 096 public void setPropertyNameForLabel(String propName) { 097 _propNameForLabel = propName; 098 } 099 100 public void propertyChange(PropertyChangeEvent evt) { 101 String propName = evt.getPropertyName(); 102 Object newValue = evt.getNewValue(); 103 if (isDebugging) { 104 log.debug(propName + " -> " + newValue); 105 } 106 107 if (_figure instanceof StateFigure) { 108 StateFigure fig = (StateFigure) _figure; 109 if (State.STATE.equals(propName)) { 110 Object state = newValue; 111 fig.setState(state); 112 fig.update(); 113 } 114 } 115 else if (_figure instanceof QualityFigure) { 116 QualityFigure fig = (QualityFigure) _figure; 117 118 /** Check states and assign appropriate quality values*/ 119 if (State.HIGH_QUALITY.equals(propName)) { 120 fig.setHighQualityThreshold(newValue); 121 } 122 else if (State.LOW_QUALITY.equals(propName)) { 123 fig.setLowQualityThreshold(newValue); 124 } 125 126 /** If state being passed is quality score, then update quality figure*/ 127 else if (State.QUALITY_SCORE.equals(propName)) { 128 fig.update2(newValue); 129 } 130 } 131 else if (_figure instanceof ProgressBarFigure) { 132 ProgressBarFigure fig = (ProgressBarFigure) _figure; 133 134 // TODO: the current value for the bar should be taken from the 135 // property? 136 // .. 137 138 fig.update(); 139 } else if (_figure instanceof LabelFigure) { 140 if (isDebugging) { 141 log.debug("updating LabelFigure: _propNameForLabel=" 142 + _propNameForLabel); 143 } 144 LabelFigure fig = (LabelFigure) _figure; 145 if (propName.equals(_propNameForLabel)) { 146 fig.setString(String.valueOf(newValue)); 147 } 148 } 149 } 150 151 /** 152 * Starts the dynamic behavior of the figure. 153 */ 154 public void start() { 155 _started = true; 156 if (_figure instanceof ProgressBarFigure) { 157 ((ProgressBarFigure) _figure).setStarted(_started); 158 } 159 } 160 161 /** 162 * Stops the dynamic behavior of the figure. 163 */ 164 public void stop() { 165 _started = false; 166 if (_figure instanceof ProgressBarFigure) { 167 ((ProgressBarFigure) _figure).setStarted(_started); 168 } 169 } 170 171 /** 172 * Tells if the figure is started. 173 */ 174 public boolean isStarted() { 175 return _started; 176 } 177 178 /** 179 * Updates the figure. 180 */ 181 public void update() { 182 if (_figure instanceof BaseFigure) { 183 ((BaseFigure) _figure).update(); 184 } 185 186 } 187 188 // ///////////////////////////////////////////////////////////////// 189 // // private members //// 190 191 /** The figure under control */ 192 private Figure _figure; 193 194 /** True iff the updater is started. */ 195 private volatile boolean _started; 196 197 /** property name for the case of LabelFigure */ 198 private String _propNameForLabel; 199 200 private static final Log log = LogFactory.getLog(FigureUpdater.class); 201 202 private static final boolean isDebugging = log.isDebugEnabled(); 203}