Package jsynoptic.builtin

Source Code of jsynoptic.builtin.TimePlot

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2006, by :
*     Corporate:
*         EADS Astrium SAS
*         EADS CRC
*     Individual:
*         Claude Cazenave
*
* $Id$
*
* Changes
* -------
* 1 avr. 2006  : Initial public release (CC);
*
*/
package jsynoptic.builtin;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JDialog;
import javax.swing.undo.CompoundEdit;

import jsynoptic.builtin.ui.DateTimePanel;
import jsynoptic.builtin.ui.TimePlotPropertiesPanel;
import jsynoptic.ui.JSynoptic;

import simtools.shapes.AxisLabelFormatter;
import simtools.shapes.LabelShape;
import simtools.shapes.CurveShape.CurvePoint;
import simtools.ui.JPropertiesPanel;
import simtools.ui.PlotInformationDialog;
import simtools.ui.TimePlotStatisticsDialog;

public class TimePlot extends Plot {
  static final long serialVersionUID = 1523083151199597764L;
 
  protected LabelShape _dateTime;
  protected LabelShape _sdateTime;
  protected Font _dateTimeFont;
 
  protected transient SimpleDateFormat datePart1;
  protected transient SimpleDateFormat datePart2;

  public TimePlot(int ox, int oy, int width, int height) {
    super(ox, oy, width, height);
    setTimeFormat(_asx1.getMin(), _asx1.getMax(), _asx1.getStep(), false);
    updateBounds();
  }
 
  /* (non-Javadoc)
   * @see simtools.shapes.PlotShape#set(double, double, double, boolean, boolean)
   */
  public void set(double min, double max, double step, boolean onX, boolean secondary){
    super.set(min, max, step, onX, secondary);
    if(onX){
      setTimeFormat(min, max, step, secondary);
    }
  }
 
  protected void setTimeFormat(double min, double max, double step, boolean secondary) {

    if(secondary && _asx2!=null) {
      SimpleDateFormat dateRef=computeFormat(min,max,secondary);
      _asx2.setFormat(datePart2);
      _asx2.set(min,max,step);
      setDateTime(dateRef.format(new Double(min)),secondary);
    } else if(!secondary && _asx1!=null){
      SimpleDateFormat dateRef=computeFormat(min,max,secondary);
      _asx1.setFormat(datePart1);
      _asx1.set(min,max,step);
      setDateTime(dateRef.format(new Double(min)),secondary);
    }
  }
 
  /* (non-Javadoc)
   * @see jsynoptic.builtin.Plot#createPanel()
   */
  public JPropertiesPanel createPanel() {
    int curvesSize =  ((primaryCurves!=null ? primaryCurves.size() : 0) (secondaryCurves!=null ? secondaryCurves.size() : 0));
    return new TimePlotPropertiesPanel(curvesSize, Builtin.resources.getString("TimePlot"));
  }
 
    /* (non-Javadoc)
     * @see simtools.shapes.AbstractShape#getShapeName()
     */
    public String getShapeName(){
        return Builtin.resources.getString("TimePlot");
    }

  /**
   * Take into account additional elements in bounds
   * computation for the secondary x axis
   * @param ret the rectangle with bounds to update
   */
  protected void secondaryAxisBoundComputation(Rectangle ret){
    // nothing to do, this a hook for derived plots
  }

  /* (non-Javadoc)
   * @see simtools.shapes.PlotShape#drawLegendAndGetBounds(java.awt.Graphics2D, java.awt.Rectangle)
   */
  protected Rectangle drawLegendAndGetBounds(Graphics2D g2,
      Rectangle reference) {
    int xpos = reference.x;
    int ypos = reference.y+reference.height;
    Rectangle ret=new Rectangle(xpos,ypos,0,0);
    Rectangle r;
    Rectangle ref2 = new Rectangle(reference);

    if(_dateTime != null) {
      _dateTime.setAnchor(xpos, ypos);
      _dateTime.setBounds(_font);
      if(g2!=null) _dateTime.draw(g2);
      r=_dateTime.getBounds();
      ret.add(r);
    }
    if(_sdateTime != null) {
      // take into account specific layout/bound computation
      // from derived classes
      secondaryAxisBoundComputation(ret);
     
      _sdateTime.setAnchor(xpos,ypos-(ret.height));
      _sdateTime.setBounds(_font);
      if(g2!=null) _sdateTime.draw(g2);
      r=_sdateTime.getBounds();
      ret.add(r);
    }
    ref2.add(ret);
    Rectangle b2 = super.drawLegendAndGetBounds(g2, ref2);
    b2.add(ret);

    return b2;
  }
 
  public boolean doAction(double x, double y, Object o, String action, CompoundEdit undoableEdit) {

    if (action.equals(resources.getStringValue("information"))){
      // Display plot information
      PlotInformationDialog di = new TimePlotStatisticsDialog(this);
      JDialog jd = di.createDialog(JSynoptic.gui.getOwner());
      if (jd!=null) jd.show();
      return true;
   
    } else {
        return super.doAction(x,y,o,action, undoableEdit);
    }
  }
 
  /**
   * Set date and time on the plot
   * @param s : the string of the DateTime
   * @param secondary : true -> secondary date time
   *             false-> primary date time
   */
  public void setDateTime(String s, boolean secondary) {
    if ((s==null) || s.trim().equals("")) {
      if(!secondary){
        if (_dateTime!=null) {
          _dateTime=null;
          computeBounds();
        }
      }
      else{
        if (_sdateTime!=null) {
          _sdateTime=null;
          computeBounds();
        }       
      }
    } else {
      if(!secondary){
        if (_dateTime==null) {
          _dateTime=new LabelShape(s,0,0,LabelShape.RIGHTDOWN,false);
          computeBounds();
        }
        else {
          // centered, update pos
          _dateTime.setString(s);
          computeBounds();
        }
      }
      else{
        if (_sdateTime==null) {
          _sdateTime=new LabelShape(s,0,0,LabelShape.RIGHTDOWN,false);
          computeBounds();
        }
        else {
          // centered, update pos
          _sdateTime.setString(s);
          computeBounds();
        }
      }
    }
  }

  public void setDateTimeFont(Font f){
    if (f!=_dateTimeFont) {
      _dateTimeFont=f;
      computeLegendBounds();
      updateBounds();
    }
  }

  public SimpleDateFormat computeFormat(double min, double max, boolean secondary){
    // Compute the grain
    Date tsMin = new Date(new Double(min).longValue());
    Date tsMax = new Date(new Double(max).longValue());
   
    int posEndRef[] = new int[1];
    posEndRef[0] = 0;
    SimpleDateFormat dateRef = DateTimePanel.getDateRefFormat(tsMin, tsMax, posEndRef);
    if(secondary){
      datePart2 = DateTimePanel.getDatePartFormat(tsMin, tsMax, axesLimitsArray[SX].step, posEndRef[0]);
    }
    else{
      datePart1 = DateTimePanel.getDatePartFormat(tsMin, tsMax, axesLimitsArray[PX].step, posEndRef[0]);
    }
    return dateRef;
  }
 
  /* (non-Javadoc)
   * @see jsynoptic.builtin.Plot#displayCursorLocation(double, double)
   */
  public void displayCursorLocation(double x, double y) {
    String msg = "";
   
    if (_magnetizedCurve!=null){
      msg += _magnetizedCurve.getLabel();
      double pos_x = _magnetizedCurve.shape.getCurrentPoint().x;
      // TODO r�gler le pb d'afficher les fractions de milliseondes si besoin
      //        ex : + "." + (new Double(pos_x-(long)pos_x).toString()).substring(2).substring(0,3)
     
      msg += " x = " + datePart1.format(new Date((long)pos_x));
      msg += " y = " + _magnetizedCurve.shape.getCurrentPoint().y;
   
      // Display the slope
      msg += "--- Slope : ";
      msg += " x = " +  _magnetizedCurve.shape.getCurrentPoint().slop_x + " ms";
      msg += " y = " + _magnetizedCurve.shape.getCurrentPoint().slop_y;
   
      // Display the delta
        CurvePoint refPoint = _magnetizedCurve.shape.getReferencePoint();
            if (refPoint != null){
                msg += "  ---  Delta from tagged point : ";
                msg += " x = " + (pos_x  - refPoint.x) + " ms";
                msg += ",  y = " + (_magnetizedCurve.shape.getCurrentPoint().y - refPoint.y);
            }
           
    }else{
      if (_asx1!=null) {
        if (axesLimitsArray[PX].label!=null) msg += axesLimitsArray[PX].label;
        else msg+= "x";
        if (datePart1 == null) {
          computeFormat(axesLimitsArray[PX].min, axesLimitsArray[PX].max,
              false);
        }
        msg += "="+ datePart1.format(new Date(
                (long) (_asx1.getMin() + (x - _ox)
                    / _asx1.getScale())));
      }
      if (_asy1!=null) {
        if (!msg.equals("")) msg+="   ";
        if (axesLimitsArray[PY].label!=null) msg += axesLimitsArray[PY].label;
        else msg+= "y";
        msg += "=" + AxisLabelFormatter.labelFormat(_asy1.getMin() + (_oy - y) / _asy1.getScale());
      }
      if (_asx2!=null) {
        if (!msg.equals("")) msg+="   ";
        if (axesLimitsArray[SX].label!=null) msg += axesLimitsArray[SX].label;
        else msg+= "x'";
        if(datePart2==null){
          computeFormat(axesLimitsArray[SX].min, axesLimitsArray[SX].max,
              true);
        }
        msg += "="+ datePart2.format(new Date(
            (long) (_asx2.getMin() + (x - _ox)
                / _asx2.getScale())));
      }
      if (_asy2!=null) {
        if (!msg.equals("")) msg+="   ";
        if (axesLimitsArray[SY].label!=null) msg += axesLimitsArray[SY].label;
        else msg+= "y'";
        msg += "=" + AxisLabelFormatter.labelFormat(_asy2.getMin() + (_oy - y) / _asy2.getScale());
      }
    }
    if (!msg.equals("")) JSynoptic.setStatus(msg);
  }
}
TOP

Related Classes of jsynoptic.builtin.TimePlot

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.