Clover coverage report - Graph - 1.0.0
Coverage timestamp: ven. déc. 26 2003 02:14:20 CET
file stats: LOC: 369   Methods: 13
NCLOC: 245   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ControlCompositeTrack.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  * EJTools, the Enterprise Java Tools
 3   
  *
 4   
  * Distributable under LGPL license.
 5   
  * See terms of license at www.gnu.org.
 6   
  */
 7   
 package org.ejtools.graph.frame;
 8   
 
 9   
 import java.awt.Color;
 10   
 import java.awt.Graphics;
 11   
 import java.awt.GridBagConstraints;
 12   
 import java.awt.GridBagLayout;
 13   
 import java.awt.GridLayout;
 14   
 import java.awt.Insets;
 15   
 import java.awt.event.ActionEvent;
 16   
 import java.awt.event.ActionListener;
 17   
 import java.awt.geom.Point2D;
 18   
 import java.text.DecimalFormat;
 19   
 import java.text.NumberFormat;
 20   
 import java.util.ArrayList;
 21   
 import java.util.Arrays;
 22   
 import java.util.Collection;
 23   
 import java.util.Hashtable;
 24   
 import java.util.Iterator;
 25   
 import java.util.List;
 26   
 import java.util.Map;
 27   
 import java.util.ResourceBundle;
 28   
 import java.util.TreeMap;
 29   
 import java.util.Vector;
 30   
 
 31   
 import javax.swing.JButton;
 32   
 import javax.swing.JComponent;
 33   
 import javax.swing.JPanel;
 34   
 
 35   
 import org.ejtools.graph.GraphElement;
 36   
 import org.ejtools.graph.LabelElement;
 37   
 import org.ejtools.graph.Range;
 38   
 import org.ejtools.graph.Track;
 39   
 import org.ejtools.graph.service.GraphConsumer;
 40   
 import org.ejtools.graph.service.GraphProducer;
 41   
 
 42   
 /**
 43   
  * @author    Laurent Etiemble
 44   
  * @version   $Revision: 1.5 $
 45   
  */
 46   
 public class ControlCompositeTrack implements GraphElement, LabelElement
 47   
 {
 48   
    /** Description of the Field */
 49   
    protected JPanel component;
 50   
    /** Description of the Field */
 51   
    protected GraphConsumer consumer = null;
 52   
    /** Description of the Field */
 53   
    protected GridLayout layout = null;
 54   
    /** Description of the Field */
 55   
    protected Hashtable producers = new Hashtable();
 56   
    /** Description of the Field */
 57   
    protected List tracks = new Vector();
 58   
    /** Description of the Field */
 59   
    private static Color[] COLORS = new Color[]{
 60   
       Color.blue,
 61   
       Color.cyan,
 62   
       Color.green,
 63   
       Color.yellow,
 64   
       Color.orange,
 65   
       Color.red,
 66   
       Color.pink,
 67   
       Color.magenta
 68   
       };
 69   
    /** Description of the Field */
 70   
    private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.graph.GraphService");
 71   
 
 72   
 
 73   
    /**
 74   
     * Constructor for the Track object
 75   
     *
 76   
     * @param consumer  Description of the Parameter
 77   
     */
 78  0
    public ControlCompositeTrack(GraphConsumer consumer)
 79   
    {
 80  0
       this.consumer = consumer;
 81  0
       this.layout = new GridLayout(1, 1, 1, 1);
 82  0
       this.component = new JPanel(this.layout);
 83   
    }
 84   
 
 85   
 
 86   
    /**
 87   
     * Adds a feature to the Track attribute of the CompositeTrack object
 88   
     *
 89   
     * @param t         The feature to be added to the Track attribute
 90   
     * @param producer  The feature to be added to the Track attribute
 91   
     */
 92  0
    public void addTrack(GraphProducer producer, Track t)
 93   
    {
 94  0
       ControlTrack ct = new ControlTrack(this.consumer, producer, t);
 95   
 
 96  0
       this.tracks.add(t);
 97  0
       this.producers.put(producer, ct);
 98  0
       this.component.add(ct);
 99  0
       this.layout.setRows(this.tracks.size());
 100  0
       this.computeColors();
 101   
    }
 102   
 
 103   
 
 104   
    /** Description of the Method */
 105  0
    public void clear()
 106   
    {
 107  0
       synchronized (this.tracks)
 108   
       {
 109  0
          for (Iterator it = this.tracks.iterator(); it.hasNext(); )
 110   
          {
 111  0
             ((Track) it.next()).clear();
 112   
          }
 113   
       }
 114   
    }
 115   
 
 116   
 
 117   
    /**
 118   
     * @param graphics  Description of the Parameter
 119   
     * @param scaleX    Description of the Parameter
 120   
     * @param offsetX   Description of the Parameter
 121   
     * @param scaleY    Description of the Parameter
 122   
     * @param offsetY   Description of the Parameter
 123   
     */
 124  0
    public void draw(Graphics graphics, double scaleX, double offsetX, double scaleY, double offsetY)
 125   
    {
 126  0
       synchronized (this.tracks)
 127   
       {
 128  0
          for (Iterator it = this.tracks.iterator(); it.hasNext(); )
 129   
          {
 130  0
             ((GraphElement) it.next()).draw(graphics, scaleX, offsetX, scaleY, offsetY);
 131   
          }
 132   
       }
 133   
    }
 134   
 
 135   
 
 136   
    /**
 137   
     * @return   The color value
 138   
     */
 139  0
    public Color getColor()
 140   
    {
 141  0
       return Color.black;
 142   
    }
 143   
 
 144   
 
 145   
    /**
 146   
     * @return   The component value
 147   
     */
 148  0
    public JComponent getComponent()
 149   
    {
 150  0
       return this.component;
 151   
    }
 152   
 
 153   
 
 154   
    /**
 155   
     * Gets the pointsAsText attribute of the ControlCompositeTrack object
 156   
     *
 157   
     * @return   The pointsAsText value
 158   
     */
 159  0
    public StringBuffer getPointsAsText()
 160   
    {
 161  0
       StringBuffer result = new StringBuffer();
 162   
 
 163  0
       List headers = new ArrayList();
 164  0
       List allPoints = new ArrayList();
 165  0
       synchronized (this.tracks)
 166   
       {
 167  0
          for (Iterator it = this.tracks.iterator(); it.hasNext(); )
 168   
          {
 169  0
             Track t = (Track) it.next();
 170  0
             headers.add(t.getComponent().toString());
 171  0
             allPoints.add(t.getPoints());
 172   
          }
 173   
       }
 174   
 
 175  0
       NumberFormat format = new DecimalFormat(resources.getString("export.as.text.column.numeric.format"));
 176  0
       Map timeline = new TreeMap();
 177  0
       for (int i = 0; i < allPoints.size(); i++)
 178   
       {
 179  0
          Collection points = (Collection) allPoints.get(i);
 180  0
          for (Iterator it2 = points.iterator(); it2.hasNext(); )
 181   
          {
 182  0
             Point2D.Double point = (Point2D.Double) it2.next();
 183  0
             String time = format.format(point.getX());
 184  0
             double[] values = (double[]) timeline.get(time);
 185  0
             if (values == null)
 186   
             {
 187  0
                values = new double[allPoints.size()];
 188  0
                Arrays.fill(values, Double.MIN_VALUE);
 189  0
                timeline.put(time, values);
 190   
             }
 191  0
             values[i] = point.getY();
 192   
          }
 193   
       }
 194   
 
 195  0
       result.append(resources.getString("export.as.text.column.absolute.time"));
 196  0
       result.append(",");
 197  0
       result.append(resources.getString("export.as.text.column.relative.time"));
 198  0
       for (int i = 0; i < headers.size(); i++)
 199   
       {
 200  0
          result.append(",");
 201  0
          result.append(headers.get(i));
 202   
       }
 203  0
       result.append("\n");
 204   
 
 205  0
       long delay = 0;
 206  0
       double[] lasts = new double[headers.size()];
 207  0
       Arrays.fill(lasts, 0.0d);
 208   
 
 209  0
       for (Iterator it = timeline.keySet().iterator(); it.hasNext(); )
 210   
       {
 211  0
          String time = (String) it.next();
 212  0
          double[] values = (double[]) timeline.get(time);
 213   
 
 214  0
          long current = Long.parseLong(time);
 215  0
          result.append(current);
 216  0
          result.append(",");
 217  0
          if (delay > 0)
 218   
          {
 219  0
             result.append(current - delay);
 220   
          }
 221   
          else
 222   
          {
 223  0
             result.append(delay);
 224  0
             delay = current;
 225   
          }
 226   
 
 227  0
          for (int i = 0; i < headers.size(); i++)
 228   
          {
 229  0
             result.append(",");
 230  0
             if (values[i] != Double.MIN_VALUE)
 231   
             {
 232  0
                result.append(values[i]);
 233  0
                lasts[i] = values[i];
 234   
             }
 235   
             else
 236   
             {
 237  0
                result.append(lasts[i]);
 238   
             }
 239   
          }
 240  0
          result.append("\n");
 241   
       }
 242  0
       return result;
 243   
    }
 244   
 
 245   
 
 246   
    /**
 247   
     * @return   The xRange value
 248   
     */
 249  0
    public Range getXRange()
 250   
    {
 251  0
       synchronized (this.tracks)
 252   
       {
 253  0
          Range result = new Range(Double.MAX_VALUE, Double.MIN_VALUE);
 254  0
          for (Iterator it = this.tracks.iterator(); it.hasNext(); )
 255   
          {
 256  0
             result = result.compose(((GraphElement) it.next()).getXRange());
 257   
          }
 258  0
          return result;
 259   
       }
 260   
    }
 261   
 
 262   
 
 263   
    /**
 264   
     * @return   The yRange value
 265   
     */
 266  0
    public Range getYRange()
 267   
    {
 268  0
       synchronized (this.tracks)
 269   
       {
 270  0
          Range result = new Range(Double.MAX_VALUE, Double.MIN_VALUE);
 271  0
          for (Iterator it = this.tracks.iterator(); it.hasNext(); )
 272   
          {
 273  0
             result = result.compose(((GraphElement) it.next()).getYRange());
 274   
          }
 275  0
          return result;
 276   
       }
 277   
    }
 278   
 
 279   
 
 280   
    /**
 281   
     * Description of the Method
 282   
     *
 283   
     * @param t         Description of the Parameter
 284   
     * @param producer  Description of the Parameter
 285   
     */
 286  0
    public void removeTrack(GraphProducer producer, Track t)
 287   
    {
 288  0
       if (this.producers.containsKey(producer))
 289   
       {
 290  0
          ControlTrack ct = (ControlTrack) this.producers.get(producer);
 291  0
          this.component.remove(ct);
 292  0
          this.tracks.remove(t);
 293  0
          this.producers.remove(producer);
 294  0
          this.layout.setRows(this.tracks.size());
 295  0
          this.computeColors();
 296   
       }
 297   
    }
 298   
 
 299   
 
 300   
    /** Description of the Method */
 301  0
    private void computeColors()
 302   
    {
 303  0
       synchronized (this.tracks)
 304   
       {
 305  0
          for (int i = 0; i < this.tracks.size(); i++)
 306   
          {
 307  0
             Track t = (Track) this.tracks.get(i);
 308  0
             t.setColor(COLORS[i % COLORS.length]);
 309   
          }
 310   
       }
 311   
    }
 312   
 
 313   
 
 314   
    /**
 315   
     * Description of the Class
 316   
     *
 317   
     * @author    letiembl
 318   
     * @version   $Revision: 1.5 $
 319   
     */
 320   
    private class ControlTrack extends JPanel implements ActionListener
 321   
    {
 322   
       /** Description of the Field */
 323   
       private JButton button = null;
 324   
       /** Description of the Field */
 325   
       private GraphConsumer consumer = null;
 326   
       /** Description of the Field */
 327   
       private GraphProducer producer = null;
 328   
       /** Description of the Field */
 329   
       private Track track = null;
 330   
 
 331   
 
 332   
       /**
 333   
        *Constructor for the ControlTrack object
 334   
        *
 335   
        * @param consumer  Description of the Parameter
 336   
        * @param producer  Description of the Parameter
 337   
        * @param track     Description of the Parameter
 338   
        */
 339  0
       public ControlTrack(GraphConsumer consumer, GraphProducer producer, Track track)
 340   
       {
 341  0
          super(new GridBagLayout());
 342  0
          this.consumer = consumer;
 343  0
          this.producer = producer;
 344  0
          this.track = track;
 345  0
          this.button = new JButton(resources.getString("graph.button.remove"));
 346   
 
 347  0
          GridBagConstraints constraints = new GridBagConstraints();
 348  0
          constraints.insets = new Insets(1, 1, 1, 1);
 349  0
          constraints.fill = GridBagConstraints.HORIZONTAL;
 350   
 
 351  0
          constraints.weightx = 1.0d;
 352  0
          this.add(this.track.getComponent(), constraints);
 353   
 
 354  0
          constraints.weightx = 0.0d;
 355  0
          this.add(this.button, constraints);
 356  0
          this.button.addActionListener(this);
 357   
       }
 358   
 
 359   
 
 360   
       /**
 361   
        * @param e  Description of the Parameter
 362   
        */
 363  0
       public void actionPerformed(ActionEvent e)
 364   
       {
 365  0
          this.consumer.removeGraphProducer(this.producer);
 366   
       }
 367   
    }
 368   
 }
 369