OrLinearFader.java :  » Music » orDrumbox-0.9.0 » com » ordrumbox » gui » widgets » Java Open Source

Java Open Source » Music » orDrumbox 0.9.0 
orDrumbox 0.9.0 » com » ordrumbox » gui » widgets » OrLinearFader.java
package com.ordrumbox.gui.widgets;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.List;

import com.ordrumbox.core.description.Common;

public class OrLinearFader  extends OrLevel implements MouseListener, MouseMotionListener {
  private static final long  serialVersionUID  = 1L;
  static final int        _WIDTH_MINI       = NoteView._W;
  static final int        _HEIGHT_MINI       = 128;
  static final int        _WIDTH           = OrWidget.W_RACK_ELEMENT ;
  static final int        _HEIGHT           = OrWidget.H_RACK_ELEMENT - 20;
  static final int        _WIDTH_EXTENDED   = 56;
  static final int        _HEIGHT_EXTENDED   = 48;
  public static final int    NONE               = 0000;
  public static final int    _DISPLAY_MINI       = 10;
  public static final int    _DISPLAY_EXTENDED   = 20;
  public static final int    _DISPLAY_NORMAL   = 30;
  private static final int w2 =16;

  int                        maxLevel;
  int                        minLevel;
  String                    unit               = "%";

  int                        displayType       = _DISPLAY_NORMAL;
  int                        w;
  int                        h;
  private List<Common>    list;
  private String            labelToolTip;
  private String            toolTipTxt;

  private int                levelAmplitude;

  /**
   * 
   * @param id
   * @param displayType
   * @param label
   * @param unit
   * @param list2
   * @param numItemSelected
   */
  public OrLinearFader(int displayType, String label, String unit, List<Common> list2, int numItemSelected) {
    super();
    this.list = list2;
    initComponents(displayType, label, unit, 0, list2.size() - 1, numItemSelected);
  }

  /**
   * 
   * @param label
   * @param unit
   * @param minLevel
   * @param maxLevel
   * @param level
   */
  public OrLinearFader( int displayType, String label, String unit, int minLevel, int maxLevel, int level) {
    super();
    initComponents( displayType, label, unit, minLevel, maxLevel, level);
  }

  private void initComponents( int displayType, String labelToolTip, String unit, int minLevel, int maxLevel, int level) {
    this.unit = unit;
    this.maxLevel = maxLevel;
    this.minLevel = minLevel;
    this.level = level;
    this.labelToolTip = labelToolTip;

    switch (displayType) {
    case _DISPLAY_MINI:
      w = _WIDTH_MINI;
      h = _HEIGHT_MINI;
      break;
    case _DISPLAY_EXTENDED:
      w = _WIDTH_EXTENDED;
      h = _HEIGHT_EXTENDED;
      break;
    case _DISPLAY_NORMAL:
      w = _WIDTH;
      h = _HEIGHT;
      break;
    }

    setLevelAmplitude(computeLevelAmplitude());
    addMouseListener(this);
    addMouseMotionListener(this);
    setPreferredSize(new Dimension(w, h));
    setLevel(level);
    setToolTipText(labelToolTip);
    setBackground(Color.black);
    setOpaque(true);
    setCursor(new Cursor(Cursor.HAND_CURSOR));
    //setBorder(new LineBorder(Color.white));
    setLayout(null);
  }

  private int computeLevelAmplitude() {
    if (maxLevel > 0 && minLevel > 0)
      return Math.abs(maxLevel - minLevel);
    if (maxLevel < 0 && minLevel < 0)
      return Math.abs(maxLevel - minLevel);
    if (maxLevel > 0 && minLevel < 0)
      return (Math.abs(maxLevel) + Math.abs(minLevel));
    return maxLevel;
  }

  private int levelToY(int level) {
    int ret = (int) (((level - minLevel) * h) / getLevelAmplitude());
    // if (level != 0)
    // System.out.println("levelToY: h:"+h+" level " + level + " y=" + ret + " ampl:"+getLevelAmplitude());
    return ret;
  }

  private int yToLevel(int y) {
    int ret = (int) ((Math.abs(maxLevel) + Math.abs(minLevel)) * (h - y) / h) - Math.abs(minLevel);
    // if (y !=0)
    // System.out.println("yToLevel: y:"+y+ " level="+ret );
    return ret;
  }

  public void setLevel(int l) {
    if (list != null) {
      Common c = list.get(level);
      toolTipTxt = labelToolTip + " " + level + ": " + c.getDisplayName();
    } else {
      toolTipTxt = labelToolTip + " " + level + " " + unit;
    }
    level = l;
    // System.out.println("setlevel="+jLabel.getText()+"="+ l);
    if (level < minLevel) {
      level = minLevel;
    }
    if (level > maxLevel) {
      level = maxLevel;
    }
    // System.out.println(jLabel.getText() + " setlevel="+ l);

    repaint();
  }

  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.white);
    g.fillRect(0, 0, w2, h);
    g.setColor(Color.black);
    g.fillRect(1, 1, w2-2, h-2);
    g.setColor(OrWidget.LIGHTBLUE);
    g.fillRect(1, h - levelToY(level)+1, w2-2, levelToY(level)-2);
    g.setColor(Color.white);
    g.setFont(OrWidget.SMALL_FONT);
    g.drawString(new Integer(this.level).toString() + " " + unit, w / 10, h / 2 - 4);
    g.drawString(labelToolTip , w / 10, h -10);

  }

  public void mouseClicked(MouseEvent e) {

  }

  public void mouseEntered(MouseEvent e) {}

  public void mouseExited(MouseEvent e) {}

  public void mousePressed(MouseEvent e) {}

  public void mouseDragged(MouseEvent e) {
    int y = e.getY();
    // System.out.println("y = "+ y);
    setLevel(yToLevel(y));
    // refresh();
    repaint();
    // PanelControler.getInstance().setInfoPanelText(toolTipTxt);
    // PanelControler.getInstance().showInfoPanel(e.getXOnScreen(), e.getYOnScreen() - 20);
  }

  public void mouseReleased(MouseEvent e) {
    // System.out.println("OrLinearFader::mouseReleased");
    setLevel(yToLevel(e.getY()));
    notifyLevelValueChange();
    // PanelControler.getInstance().hideInfoPanel();
  }

  public void mouseMoved(MouseEvent e) {}

  private int getLevelAmplitude() {
    return levelAmplitude;
  }

  private void setLevelAmplitude(int levelAmplitude) {
    this.levelAmplitude = levelAmplitude;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.