|
From: James M. <jma...@us...> - 2001-12-05 16:25:42
|
Update of /cvsroot/geotools/geotools/src/uk/ac/leeds/ccg/widgets
In directory usw-pr-cvs1:/tmp/cvs-serv13886/uk/ac/leeds/ccg/widgets
Added Files:
MinMaxSlider.java
Log Message:
First working version
--- NEW FILE: MinMaxSlider.java ---
/*
* MinMaxSlider.java
*
* Created on December 5, 2001, 10:53 AM
*/
package uk.ac.leeds.ccg.widgets;
import java.beans.*;
import java.awt.*;
/**
*
* @author jamesm
* @version
*/
public class MinMaxSlider extends java.awt.Component implements java.io.Serializable,java.awt.event.MouseMotionListener,java.awt.event.MouseListener {
private static final String PROP_MINSELECTED_PROPERTY = "MinSelected";
private static final String PROP_MAXSELECTED_PROPERTY = "MaxSelected";
private PropertyChangeSupport propertySupport;
/** Holds value of property arrowHeadColor. */
private java.awt.Color arrowHeadColor=Color.black;
/** Holds value of property minValue. */
private int minValue=0;
/** Holds value of property selectionColor. */
private java.awt.Color selectionColor=Color.pink;
/** Holds value of property maxValue. */
private int maxValue=100;
/** Holds value of property barWidth. */
private int barWidth=10;
/** Holds value of property minSelected. */
private int minSelected=20;
/** Holds value of property maxSelected. */
private int maxSelected=40;
/** Holds value of property barBackgroundColor. */
private java.awt.Color barBackgroundColor = Color.white;
/** Creates new MinMaxSlider */
public MinMaxSlider() {
propertySupport = new PropertyChangeSupport( this );
addMouseMotionListener(this);
addMouseListener(this);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertySupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertySupport.removePropertyChangeListener(listener);
}
/** Getter for property arrowHeadColor.
* @return Value of property arrowHeadColor.
*/
public java.awt.Color getArrowHeadColor() {
return arrowHeadColor;
}
/** Setter for property arrowHeadColor.
* @param arrowHeadColor New value of property arrowHeadColor.
*/
public void setArrowHeadColor(java.awt.Color arrowHeadColor) {
this.arrowHeadColor = arrowHeadColor;
}
/** Getter for property minValue.
* @return Value of property minValue.
*/
public int getMinValue() {
return minValue;
}
/** Setter for property minValue.
* @param minValue New value of property minValue.
*/
public void setMinValue(int minValue) {
this.minValue = minValue;
}
/** Getter for property selectionColor.
* @return Value of property selectionColor.
*/
public java.awt.Color getSelectionColor() {
return selectionColor;
}
/** Setter for property selectionColor.
* @param selectionColor New value of property selectionColor.
*/
public void setSelectionColor(java.awt.Color selectionColor) {
this.selectionColor = selectionColor;
}
Image screenBuffer;
public void update(Graphics g){
paint(g);
}
public void paint(java.awt.Graphics g) {
int width = getSize().width;
int height = getSize().height;
Graphics bg;
if(screenBuffer == null){
screenBuffer = this.createImage(width,height);
}
if(screenBuffer == null)bg=g;
else bg = screenBuffer.getGraphics();
bg.setColor(getBackground());
bg.fillRect(0,0,width,height);
bg.setColor(getForeground());
bg.fillRect(width-barWidth,0,barWidth,height);
bg.setColor(getBarBackgroundColor());
bg.fillRect(width-barWidth+2,2,barWidth-4,height-4);
int [] p = getLimits();
bg.setColor(getSelectionColor());
bg.fillRect(width-barWidth+2,p[0],barWidth-4,p[1]-p[0]);
bg.setColor(getArrowHeadColor());
System.out.println(""+p[0]+","+p[1]);
bg.fillPolygon(new int[]{width-barWidth+1,width-1,width-(barWidth/2),width-barWidth+1},new int[]{p[0],p[0],p[0]+(barWidth/2),p[0]},4);
bg.fillPolygon(new int[]{width-barWidth+1,width-1,width-(barWidth/2),width-barWidth+1},new int[]{p[1],p[1],p[1]-(barWidth/2),p[1]},4);
bg.setColor(getForeground());
bg.drawString(""+getMinSelected(),width-barWidth-20,p[0]);
bg.drawString(""+getMaxSelected(),width-barWidth-20,p[1]);
if(screenBuffer!=null){
g.drawImage(screenBuffer, 0,0,this);
}
}
protected int[] getLimits(){
double valRange = maxValue-minValue;
double pixRange = getSize().height;
double scale = pixRange/valRange;
int top = valToPix(minSelected);
int bottom = valToPix(maxSelected);
return new int[]{top,bottom};
}
protected int pixToVal(int pix){
double valRange = maxValue-minValue;
double pixRange = getSize().height;
double scale = pixRange/valRange;
return (int)(pix*scale)+minValue;
}
protected int valToPix(int val){
double valRange = maxValue-minValue;
double pixRange = getSize().height;
double scale = pixRange/valRange;
return (int)((val/scale)-minValue);
}
/** Getter for property maxValue.
* @return Value of property maxValue.
*/
public int getMaxValue() {
return maxValue;
}
/** Setter for property maxValue.
* @param maxValue New value of property maxValue.
*/
public void setMaxValue(int maxValue) {
this.maxValue = maxValue;
}
/** Getter for property barWidth.
* @return Value of property barWidth.
*/
public int getBarWidth() {
return barWidth;
}
/** Setter for property barWidth.
* @param barWidth New value of property barWidth.
*/
public void setBarWidth(int barWidth) {
this.barWidth = barWidth;
}
/** Getter for property minSelected.
* @return Value of property minSelected.
*/
public int getMinSelected() {
return minSelected;
}
/** Setter for property minSelected.
* @param minSelected New value of property minSelected.
*/
public void setMinSelected(int minSelected) {
if(minSelected>getMaxSelected())minSelected = getMaxSelected()-1;
int old = this.minSelected;
this.minSelected = minSelected;
repaint();
this.propertySupport.firePropertyChange(this.PROP_MINSELECTED_PROPERTY,new Integer(old),new Integer(minSelected));
}
/** Getter for property maxSelected.
* @return Value of property maxSelected.
*/
public int getMaxSelected() {
return maxSelected;
}
/** Setter for property maxSelected.
* @param maxSelected New value of property maxSelected.
*/
public void setMaxSelected(int maxSelected) {
if(maxSelected<=getMinSelected())maxSelected = getMinSelected()+1;
int old = maxSelected;
this.maxSelected = maxSelected;
this.propertySupport.firePropertyChange(this.PROP_MAXSELECTED_PROPERTY,new Integer(old),new Integer(maxSelected));
repaint();
}
public void mouseDragged(java.awt.event.MouseEvent mouseEvent) {
//System.out.println("start "+startValue+" now "+value+" "+(value-startValue));
int value = pixToVal(mouseEvent.getY());
switch(whatToMove){
case TOP:
setMinSelected(value);
break;
case BOTTOM:
setMaxSelected(value);
break;
case MID:
setMinSelected(getMinSelected()+value-startValue);
setMaxSelected(getMaxSelected()+value-startValue);
startValue=value;
break;
}
}
public void mouseMoved(java.awt.event.MouseEvent mouseEvent) {
}
public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
}
public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
}
static final int NOTHING=1,TOP=2,MID=3,BOTTOM=4;
int whatToMove;
int startValue;
public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
int y = mouseEvent.getY();
int p[] = getLimits();
if (y<p[0]) whatToMove = NOTHING;
else if (y<p[0]+5) whatToMove = TOP;
else if(y<p[1]-5)whatToMove = MID;
else if(y<p[1]) whatToMove = BOTTOM;
else whatToMove = NOTHING;
startValue = pixToVal(y);
System.out.println("Presed "+whatToMove);
}
public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
}
public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
}
/** Getter for property barBackgroundColor.
* @return Value of property barBackgroundColor.
*/
public java.awt.Color getBarBackgroundColor() {
return barBackgroundColor;
}
/** Setter for property barBackgroundColor.
* @param barBackgroundColor New value of property barBackgroundColor.
*/
public void setBarBackgroundColor(java.awt.Color barBackgroundColor) {
this.barBackgroundColor = barBackgroundColor;
}
}
|