Package bandwidth.ui

Source Code of bandwidth.ui.GraphicManager

package bandwidth.ui;


import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import misc.Rect;

import bandwidth.AbstractBandwidthController;
import bandwidth.history.History;
import bandwidth.history.HistoryListenerInterface;
import bandwidth.listener.NullUsageListener;


public class GraphicManager extends JPanel implements GraphicManagerInterface,
    FunctionInterface, GripperClientInterface, HistoryListenerInterface
{
  private static final long serialVersionUID = -5898924537072196910L;

  private static final double VALUE_DISTRIBUTION = 0.4;

  private AbstractBandwidthController controller;

  private GraphicCanvas canvas;

  private GraphicBackground background;
  private History history;
  private Gripper gripper;

  private Rect gripperArea;
  private Rect backgroundArea;
  private Rect graphicArea;

  private double scalingFactor;

  public GraphicManager(AbstractBandwidthController controller)
  {
    super();
    setBorder(new BevelBorder(BevelBorder.LOWERED));
    setLayout(new BorderLayout());

    this.controller = controller;

    gripperArea = Rect.createWithMeasures(0, 0, 0, 0);
    backgroundArea = Rect.createWithMeasures(0, 0, 0, 0);
    graphicArea = Rect.createWithMeasures(0, 0, 0, 0);

    canvas = new GraphicCanvas(this);

    background = new GraphicBackground(this);

    gripper = new Gripper(this, this);
    canvas.addMouseListener(gripper);
    canvas.addMouseMotionListener(gripper);

    history = new History(this, this);

    add(canvas, BorderLayout.CENTER);
  }

  public Gripper getGripper()
  {
    return gripper;
  }

  public void updateRegions(Rect gripperArea, Rect backgroundArea, Rect graphicArea)
  {
    this.gripperArea = gripperArea;
    this.backgroundArea = backgroundArea;
    this.graphicArea = graphicArea;

    resetFactor();
    repositionGripper();
  }

  private void repositionGripper()
  {
    gripper.setPosition(getYFromValue(history.getLimitValue()));
    canvas.repaint();
  }

  public void drawElements(Graphics g)
  {
    background.draw(g, backgroundArea, graphicArea);
    history.draw(g, graphicArea);
    gripper.draw(g, gripperArea, backgroundArea);
  }

  private void resetFactor()
  {
    scalingFactor = graphicArea.getHeight()
        / Math.pow(history.getMaximumValue(), VALUE_DISTRIBUTION);
  }

  public int getMaximumValue()
  {
    return history.getMaximumValue();
  }

  public int getYFromValue(int value)
  {
    return graphicArea.getBottom()
        - (int) Math.round(Math.pow(value, VALUE_DISTRIBUTION) * scalingFactor);
  }

  public int getValueFromY(int y)
  {
    return (int) Math.round(Math.pow((graphicArea.getBottom() - y) / scalingFactor,
        1 / VALUE_DISTRIBUTION));
  }

  public void onChange()
  {
    resetFactor();
    repositionGripper();
  }

  public void onProgress()
  {
    canvas.repaint();
  }

  public void update()
  {
    canvas.repaint();
  }

  public void setValue(int value)
  {
    controller.setAvailableBandwidth(value);
  }

  public void activate()
  {
    controller.setUsageListener(history);
  }

  public void deactivate()
  {
    controller.setUsageListener(new NullUsageListener());
    history.mark();
  }

}
TOP

Related Classes of bandwidth.ui.GraphicManager

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.