source: ether_statistics/service/implementation/gov/noaa/pmel/sgt/LineCartesianRenderer.java @ 569

Last change on this file since 569 was 569, checked in by vmipsl, 12 years ago

Nouveau projet

File size: 9.8 KB
Line 
1/*
2 * $Id: LineCartesianRenderer.java,v 1.18 2003/08/22 23:02:32 dwd Exp $
3 *
4 * This software is provided by NOAA for full, free and open release.  It is
5 * understood by the recipient/user that NOAA assumes no liability for any
6 * errors contained in the code.  Although this software is released without
7 * conditions or restrictions in its use, it is expected that appropriate
8 * credit be given to its author and to the National Oceanic and Atmospheric
9 * Administration should the software be included by the recipient as an
10 * element in other product development.
11 */
12
13package  gov.noaa.pmel.sgt;
14
15import gov.noaa.pmel.sgt.dm.SGTLine;
16import gov.noaa.pmel.sgt.dm.SGTData;
17import gov.noaa.pmel.sgt.dm.Collection;
18
19//import gov.noaa.pmel.util.GeoDate;
20import gov.noaa.pmel.util.Debug;
21
22import java.util.Vector;
23import java.util.Enumeration;
24import java.awt.Graphics;
25import java.awt.Event;
26import java.awt.Point;
27import java.awt.Color;
28import java.awt.Rectangle;
29import java.beans.PropertyChangeEvent;
30
31/**
32 * Produces a line plot with optional coloring from a second data set. If
33 * a second data set is specified it must have the same shape as the first.
34 *
35 * @author Donald Denbo
36 * @version $Revision: 1.18 $, $Date: 2003/08/22 23:02:32 $
37 * @since 1.0
38 */
39public class LineCartesianRenderer extends CartesianRenderer {
40  /**@shapeType AggregationLink
41   * @label attr
42   * @undirected
43   * @supplierCardinality 1 */
44  private LineAttribute attr_ = null;
45  /**@shapeType AggregationLink
46   * @supplierCardinality 0..1
47   * @label line
48   * @undirected */
49  private SGTLine line_ = null;
50  /**@shapeType AggregationLink
51   * @supplierCardinality 0..1 */
52  private Collection collection_ = null;
53
54  private StrokeDrawer stroke_ = null;
55
56  /**
57   * Get the <code>Attribute</code> associated with the data.
58   */
59  public Attribute getAttribute() {
60    return attr_;
61  }
62  private void drawLine(Graphics g, SGTLine line, LineAttribute attr) {
63    int[] xp, yp;
64    int[] xout, yout;
65    int count, size, nout;
66    double[] xValues, yValues;
67    long[] xTValues, yTValues;
68
69    if(line.isXTime()) {
70      xTValues = line.getGeoDateArray().getTime();
71      size = xTValues.length;
72      xp = new int[size];
73      for(count=0; count < size; count++) {
74        xp[count] = cg_.getXUtoD(xTValues[count]);
75      }
76    } else {
77      xValues = line.getXArray();
78      size = xValues.length;
79      xp = new int[size];
80      for(count=0; count < size; count++) {
81        xp[count] = cg_.getXUtoD(xValues[count]);
82      }
83    }
84    //
85    if(line.isYTime()) {
86      yTValues = line.getGeoDateArray().getTime();
87      size = yTValues.length;
88      yp = new int[size];
89      for(count=0; count < size; count++) {
90        yp[count] = cg_.getYUtoD(yTValues[count]);
91      }
92    } else {
93      yValues = line.getYArray();
94      size = yValues.length;
95      yp = new int[size];
96      for(count=0; count < size; count++) {
97        yp[count] = cg_.getYUtoD(yValues[count]);
98      }
99    }
100    //
101    // check for missing values a Double.NaN is converted to a Integer.MIN_VALUE
102    //
103    int first = 0;
104    int lsize = 0;
105    xout = new int[size];
106    yout = new int[size];
107    while (first < size) {
108      nout=-1;
109    line:
110      for(count=first; count < size; count++) {
111        if(xp[count] != Integer.MIN_VALUE && yp[count] != Integer.MIN_VALUE) {
112          nout++;
113          xout[nout] = xp[count];
114          yout[nout] = yp[count];
115        } else if (nout >= 0) {
116          break line;
117        }
118      }
119      first = count + 1;
120      lsize = nout + 1;
121      if(lsize <= 0) return;
122      //
123      // draw regular line
124      //
125      switch(attr.getStyle()) {
126      case LineAttribute.MARK:
127        drawMark(g, xout, yout, lsize, attr);
128        break;
129      case LineAttribute.HIGHLIGHT:
130        stroke_.drawHighlight(g, xout, yout, lsize, attr);
131        break;
132      case LineAttribute.HEAVY:
133        stroke_.drawHeavy(g, xout, yout, lsize, attr);
134        break;
135      case LineAttribute.DASHED:
136        stroke_.drawDashed(g, xout, yout, lsize, attr);
137        break;
138      case LineAttribute.STROKE:
139        stroke_.drawStroke(g, xout, yout , lsize, attr);
140        break;
141      case LineAttribute.MARK_LINE:
142        drawMark(g, xout, yout, lsize, attr);
143      default:
144      case LineAttribute.SOLID:
145        g.drawPolyline(xout, yout, lsize);
146      }
147    }
148  }
149  /**
150   * Draw a mark at the requested location. This routine is used by LineCartesianGraph and
151   * LineKey.
152   *
153   * @param g Graphics object
154   * @param xp horizontal coordinate
155   * @param yp vertical coordinate
156   * @param attr line attribute
157   * @see LineKey
158   */
159  protected void drawMark(Graphics g, int[] xp, int[] yp,
160                          int npoints, LineAttribute attr) {
161    Layer ly = cg_.getLayer();
162    PlotMark pm = new PlotMark(attr);
163
164    for(int i=0; i < npoints; i++) {
165      pm.paintMark(g, ly, xp[i], yp[i]);
166    }
167  }
168
169  /**
170   * Default constructor.
171   *
172   * @see CartesianGraph
173   * @see Graph
174   **/
175  public LineCartesianRenderer(CartesianGraph cg) {
176    this(cg, (SGTLine)null, null);
177  }
178  /**
179   * Construct a <code>LineCartesianRenderer</code>. The default
180   * <code>LineAttribute</code> will be used.
181   *
182   * @param cg the parent <code>CartesianGraph</code>
183   * @param data an <code>SGTLine</code> object
184   *
185   * @see CartesianGraph
186   * @see Graph
187   **/
188  public LineCartesianRenderer(CartesianGraph cg, SGTLine line) {
189    this(cg, line, null);
190    cg_ = cg;
191    line_ = line;
192  }
193  /**
194   * Construct a <code>LineCartesianRenderer</code>.
195   *
196   * @param cg the parent <code>CartesianGraph</code>
197   * @param data an <code>SGTLine</code>
198   * @param line the <code>LineAttribute</code>
199   * @see CartesianGraph
200   * @see Graph
201   **/
202  public LineCartesianRenderer(CartesianGraph cg, SGTLine line, LineAttribute attr) {
203    cg_ = cg;
204    line_ = line;
205    attr_ = attr;
206    if(attr_ != null) attr_.addPropertyChangeListener(this);
207    stroke_ = PaneProxy.strokeDrawer;
208  }
209  /**
210   * Construct a <code>LineCartesianRenderer</code>.
211   *
212   * @param cg the parent <code>CartesianGraph</code>
213   * @param col a <code>Collection</code> of <code>SGTLine</code> objects
214   * @param line the <code>LineAttribute</code>
215   * @see CartesianGraph
216   * @see Graph
217   **/
218  public LineCartesianRenderer(CartesianGraph cg, Collection col, LineAttribute attr) {
219    cg_ = cg;
220    collection_ = col;
221    attr_ = attr;
222    if(attr_ != null) attr_.addPropertyChangeListener(this);
223    stroke_ = PaneProxy.strokeDrawer;
224  }
225  /**
226   * Render the <code>SGTData</code>. This method should not
227   * be directly called.
228   *
229   * @param g graphics context
230   *
231   * @see Pane#draw
232   */
233  public void draw(Graphics g) {
234    LineAttribute attr;
235    Object line;
236
237    if(cg_.clipping_) {
238      int xmin, xmax, ymin, ymax;
239      int x, y, width, height;
240      if(cg_.xTransform_.isSpace()) {
241        xmin = cg_.getXUtoD(cg_.xClipRange_.start);
242        xmax = cg_.getXUtoD(cg_.xClipRange_.end);
243      } else {
244        xmin = cg_.getXUtoD(cg_.tClipRange_.start);
245        xmax = cg_.getXUtoD(cg_.tClipRange_.end);
246      }
247      if(cg_.yTransform_.isSpace()) {
248        ymin = cg_.getYUtoD(cg_.yClipRange_.start);
249        ymax = cg_.getYUtoD(cg_.yClipRange_.end);
250      } else {
251        ymin = cg_.getYUtoD(cg_.tClipRange_.start);
252        ymax = cg_.getYUtoD(cg_.tClipRange_.end);
253      }
254      if(xmin < xmax) {
255        x = xmin;
256        width = xmax - xmin;
257      } else {
258        x=xmax;
259        width = xmin - xmax;
260      }
261      if(ymin < ymax) {
262        y = ymin;
263        height = ymax - ymin;
264      } else {
265        y = ymax;
266        height = ymin - ymax;
267      }
268      g.setClip(x, y, width, height);
269    }
270    if(attr_ == null) {
271      attr = new LineAttribute(LineAttribute.SOLID,
272                cg_.getPane().getComponent().getForeground());
273    } else {
274      attr = attr_;
275    }
276    g.setColor(attr.getColor());
277    if(collection_ == null) {
278      drawLine(g, line_, attr);
279    } else {
280      for(Enumeration li = collection_.elements(); li.hasMoreElements();) {
281        line = li.nextElement();
282        if(line instanceof SGTLine) {
283          drawLine(g, (SGTLine)line, attr);
284        }
285      }
286    }
287
288    //
289    // reset clip
290    //
291    Rectangle rect = cg_.getLayer().getPane().getBounds();
292    g.setClip(rect);
293  }
294  /**
295   * Set the <code>LineAttribute</code>. The line appearance is controlled by
296   * this object.
297   *
298   * @param l <code>LineAttribute</code>
299   **/
300  public void setLineAttribute(LineAttribute l) {
301    if(attr_ != null) attr_.removePropertyChangeListener(this);
302    attr_ = l;
303    if(attr_ != null) attr_.addPropertyChangeListener(this);
304  }
305  /**
306   * Get the <code>LineAttribute</code>.
307   *
308   * @return <code>LineAttribute</code>
309   **/
310  public LineAttribute getLineAttribute() {
311    return attr_;
312  }
313  /**
314   * Test if a <code>Collection</code> of <code>SGTLine</code>
315   * was using to construct this renderer.
316   *
317   * @return true if <code>Collection</code> was used
318   */
319  public boolean hasCollection() {
320    return (collection_ != null);
321  }
322  /**
323   * Get the <code>Collection</code> of <code>SGTLine</code> objects.
324   *
325   * @return <code>Collection</code>
326   */
327  public Collection getCollection() {
328    return collection_;
329  }
330  /**
331   * Get the <code>SGTLine</code> object.
332   *
333   * @return <code>SGTLine</code>
334   */
335  public SGTLine getLine(){
336    return line_;
337  }
338  /**
339   * Get the associated <code>CartesianGraph</code> object.
340   *
341   * @return <code>CartesianGraph</code>
342   */
343  public CartesianGraph getCartesianGraph() {
344    return cg_;
345  }
346
347  public void propertyChange(PropertyChangeEvent evt) {
348//      if(Debug.EVENT) {
349//        System.out.println("LineCartesianRenderer: " + evt);
350//        System.out.println("                       " + evt.getPropertyName());
351//      }
352    modified("LineCartesianRenderer: propertyChange(" +
353             evt.getSource().toString() + "[" +
354             evt.getPropertyName() + "]" + ")");
355  }
356  /**
357   * @since 3.0
358   */
359  public SGTData getDataAt(Point pt) {
360    return null;
361  }
362}
Note: See TracBrowser for help on using the repository browser.