Package jmt.gui.jwat.fitting.charts

Source Code of jmt.gui.jwat.fitting.charts.EnlargeQQPlot$PlotPopup

/**   
* Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
package jmt.gui.jwat.fitting.charts;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

import jmt.engine.jwat.VariableNumber;
import jmt.engine.jwat.fitting.FittingAlgorithm;
import jmt.engine.jwat.fitting.ParetoFitting;
import jmt.engine.jwat.fitting.utils.ModelFitting;
import jmt.engine.jwat.workloadAnalysis.utils.ChangeVariableListener;
import jmt.engine.jwat.workloadAnalysis.utils.ModelWorkloadAnalysis;
import jmt.gui.jwat.JWATConstants;
import jmt.gui.jwat.workloadAnalysis.chart.EnlargePlotDistGraph.PlotImagesFileChooser;
import jmt.gui.jwat.workloadAnalysis.chart.EnlargePlotDistGraph.PlotImagesFileFilter;
import ptolemy.plot.Plot;

public class EnlargeQQPlot extends Plot implements MouseListener {
  /**
   *
   */
  private static final long serialVersionUID = 1L;
  // Zoom factor for zoom-in / zoom-out
  public static final double PLOT_ZOOM_FACTOR = 0.5;
  private ModelFitting model = null;
  private FittingAlgorithm engfitting;
  private int distribution;

  private PlotPopup pop = new PlotPopup();

  public EnlargeQQPlot(ModelFitting model, FittingAlgorithm engfitting, int distr) {
    this.model = model;
    this.engfitting = engfitting;
    this.distribution = distr;
    this.setConnected(false, 1);
    this.setMarksStyle("various", 1);
    this.setConnected(true, 2);
    this.setMarksStyle("point", 2);
    this.setColors(new Color[] { Color.RED, Color.BLUE });
    this.model.addOnChangeVariableValue(new ChangeVariableListener() {
      public void onChangeVariableValues() {
        drawGraph();
      }
    });
    this.addMouseListener(this);
    drawGraph();
  }

  private void drawGraph() {
    this.clear(true);
    this.setConnected(false, 1);
    this.setMarksStyle("various", 1);
    this.setConnected(true, 2);
    this.setMarksStyle("point", 2);
    this.setColors(new Color[] { Color.RED, Color.BLUE });

    double[] x = model.getListObservations();
    double[] y = engfitting.generateQQPlot();
    double xRange = x[x.length-1] / x.length;
    //double yRange = (y[y.length-1]-y[0]) / x.length;
   
    setXRange(0, Math.max(x[x.length-1],y[y.length-1])+1);
    setYRange(0, Math.max(x[x.length-1],y[y.length-1])+1);
   
    for (int i = 0; i < x.length; i++) {
      //System.out.println("x[i]: " + x[i] + " y[i]: " + y[i] + " xRange * i: " + xRange * i);
      addPoint(1, x[i], y[i], true);
      addPoint(2, xRange * i, xRange * i, true);
    }
    addPoint(2, xRange * x.length, xRange * x.length, true);

    fillPlot();
  }

  public void mouseClicked(MouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON3) {
      pop.show(EnlargeQQPlot.this, e.getX(), e.getY());
    }
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public class PlotPopup extends JPopupMenu {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    public JMenuItem restore;
    public JMenuItem zoomIn;
    public JMenuItem zoomOut;
    public JMenuItem saveAs;

    public PlotPopup() {
      restore = new JMenuItem("Original view");
      zoomIn = new JMenuItem("Zoom in");
      zoomOut = new JMenuItem("Zoom out");
      saveAs = new JMenuItem("Save as...");

      this.add(restore);
      this.add(zoomIn);
      this.add(zoomOut);
      this.addSeparator();
      this.add(saveAs);
      addListeners();
    }

    private void addListeners() {
      restore.addActionListener(new AbstractAction() {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
          fillPlot();
        }
      });

      zoomIn.addActionListener(new AbstractAction() {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
          // Gets maximum and minimum values
          double[] XBounds = getXAutoRange();
          double[] YBounds = getYAutoRange();

          double[] xRange = getXRange();
          double[] yRange = getYRange();
          double width = xRange[1] - xRange[0];
          double height = yRange[1] - yRange[0];
          double newWidth = (xRange[1] - xRange[0]) * PLOT_ZOOM_FACTOR;
          double newHeight = (yRange[1] - yRange[0]) * PLOT_ZOOM_FACTOR;
          double newXMin = xRange[0] + (width - newWidth) / 2;
          //The next 7 lines check that the new range is valid
          if (newXMin < XBounds[0]) {
            newXMin = XBounds[0];
          }
          double newXMax = xRange[0] + (width - newWidth) / 2 + newWidth;
          if (newXMax > XBounds[1]) {
            newXMax = XBounds[1];
          }
          double newYMin = yRange[0] + (height - newHeight) / 2;
          if (newYMin < YBounds[0]) {
            newYMin = YBounds[0];
          }
          double newYMax = yRange[0] + (height - newHeight) / 2 + newHeight;
          if (newYMax > YBounds[1]) {
            newYMax = YBounds[1];
          }
          setXRange(newXMin, newXMax);
          setYRange(newYMin, newYMax);
          EnlargeQQPlot.this.repaint();
        }
      });

      zoomOut.addActionListener(new AbstractAction() {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
          // Gets maximum and minimum values
          double[] XBounds = getXAutoRange();
          double[] YBounds = getYAutoRange();

          double[] xRange = getXRange();
          double[] yRange = getYRange();
          double width = xRange[1] - xRange[0];
          double height = yRange[1] - yRange[0];
          double newWidth = (xRange[1] - xRange[0]) / PLOT_ZOOM_FACTOR;
          double newHeight = (yRange[1] - yRange[0]) / PLOT_ZOOM_FACTOR;
          double newXMin = xRange[0] + (width - newWidth) / 2;
          //The next 7 lines check that the new range is valid
          if (newXMin < XBounds[0]) {
            newXMin = XBounds[0];
          }
          double newXMax = xRange[0] + (width - newWidth) / 2 + newWidth;
          if (newXMax > XBounds[1]) {
            newXMax = XBounds[1];
          }
          double newYMin = yRange[0] + (height - newHeight) / 2;
          if (newYMin < YBounds[0]) {
            newYMin = YBounds[0];
          }
          double newYMax = yRange[0] + (height - newHeight) / 2 + newHeight;
          if (newYMax > YBounds[1]) {
            newYMax = YBounds[1];
          }
          setXRange(newXMin, newXMax);
          setYRange(newYMin, newYMax);
          EnlargeQQPlot.this.repaint();
        }
      });

      saveAs.addActionListener(new AbstractAction() {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
          PlotImagesFileFilter PNGfilter = new PlotImagesFileFilter(".png", "Portable Network Graphics images");
          PlotImagesFileFilter EPSfilter = new PlotImagesFileFilter(".eps", "Encapsulated Post Script images");
          PlotImagesFileChooser fileChooser = new PlotImagesFileChooser(PNGfilter);
          fileChooser.setFileFilter(PNGfilter);
          fileChooser.addChoosableFileFilter(EPSfilter);
          int r = fileChooser.showSaveDialog(EnlargeQQPlot.this);
          if (r == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            if (fileChooser.getFileFilter().equals(EPSfilter)) {
              EnlargeQQPlot plot = EnlargeQQPlot.this;
              try {
                FileOutputStream fileStream = new FileOutputStream(file);
                plot.export(fileStream);
                fileStream.close();
              } catch (FileNotFoundException fnf) {
                JOptionPane.showMessageDialog(fileChooser, "File not found", "JMT - Error", JOptionPane.ERROR_MESSAGE);
              } catch (IOException ioe) {
                JOptionPane.showMessageDialog(fileChooser, "I/O exception", "JMT - Error", JOptionPane.ERROR_MESSAGE);
              }
            } else {
              EnlargeQQPlot plot = EnlargeQQPlot.this;
              BufferedImage image = plot.exportImage();
              try {
                BufferedImage originalImage = convertType(image, BufferedImage.TYPE_INT_RGB);
                ImageIO.write(originalImage, "png", file);
                ImageIO.createImageOutputStream(file).close();
              } catch (IOException fnf) {
                JOptionPane.showMessageDialog(null, "File not found");
              }
            }
          }
        }

      });
    }

    BufferedImage convertType(BufferedImage src, int targetType) {
      if (src.getType() == targetType) {
        return src;
      }
      BufferedImage tgt = new BufferedImage(src.getWidth(), src.getHeight(), targetType);
      Graphics2D g = tgt.createGraphics();
      g.drawRenderedImage(src, null);
      g.dispose();
      return tgt;
    }
  }
}
TOP

Related Classes of jmt.gui.jwat.fitting.charts.EnlargeQQPlot$PlotPopup

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.