package com.rle.monitor; /* * RLE Surface Crack Detector Graphing Software * * Copyright (C) 2002 RLE Technologies * For more information visit http://www.rletech.com/ * * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ /** * @author Andy Goth * @author Eric Peterson * @author Matt Lane * @version 0.1 */ /** Data structure describing the bounds of a roll chart. */ public class Bounds { /** The minimum distance between left and right. */ public static final int MIN_WIDTH = 64; /** The minimum distance between floor and ceiling. */ public static final int MIN_HEIGHT = 32; private int left; private int ceil; private int right; private int floor; private Buffer buffer; /** Constructor. */ public Bounds(Buffer buffer) { this(buffer, buffer.getSize() - 2048 > 0 ? buffer.getSize() - 2048 : 0, 255, buffer.getSize() - 1,0); } /** Another constructor. */ public Bounds(Buffer buffer, int left, int ceil, int right, int floor) { this.buffer = buffer; this.left = left; this.ceil = ceil; this.right = right; this.floor = floor; if (buffer == null) throw new NullPointerException(); if (right - left < MIN_WIDTH) throw new IllegalArgumentException("Out of bounds"); if (ceil - floor < MIN_HEIGHT) throw new IllegalArgumentException("Out of bounds"); } /** Yet another constructor. */ public Bounds(Bounds bounds) {buffer = bounds.buffer; setRange(bounds);} /** Returns the left edge of the bound. */ public int getLeft() {return left;} /** Returns the minimal left edge of the bound. */ public int getMinLeft() {return 0;} /** Returns the maximal left edge of the bound. */ public int getMaxLeft() {return right - MIN_WIDTH;} /** Sets the left edge of the bound. */ public void setLeft(int left) { if (left < getMinLeft() || left > getMaxLeft()) throw new IllegalArgumentException("Out of bounds"); this.left = left; } /** Returns the ceiling of the bound. */ public int getCeil() {return ceil;} /** Returns the minimal ceiling of the bound. */ public int getMinCeil() {return floor + MIN_HEIGHT;} /** Returns the maximal ceiling of the bound. */ public int getMaxCeil() {return 255;} /** Sets the ceiling of the bound. */ public void setCeil(int ceil) { if (ceil < getMinCeil() || ceil > getMaxCeil()) throw new IllegalArgumentException("Out of bounds"); this.ceil = ceil; } /** Returns the right edge of the bound. */ public int getRight() {return right;} /** Returns the minimal right edge of the bound. */ public int getMinRight() {return left + MIN_WIDTH;} /** Returns the maximal right edge of the bound. */ public int getMaxRight() {return buffer.getSize() - 1;} /** Sets the right edge of the bound. */ public void setRight(int right) { if (right < getMinRight() || right > getMaxRight()) throw new IllegalArgumentException("Out of bounds"); this.right = right; } /** Returns the floor of the bound. */ public int getFloor() {return floor;} /** Returns the minimal floor of the bound. */ public int getMinFloor() {return 0;} /** Returns the maximal floor of the bound. */ public int getMaxFloor() {return ceil - MIN_HEIGHT;} /** Sets the floor of the bound. */ public void setFloor(int floor) { if (floor < getMinFloor() || floor > getMaxFloor()) throw new IllegalArgumentException("Out of bounds"); this.floor = floor; } /** Returns the width of the bound. */ public int getWidth() {return right - left;} /** Returns the minimal width of the bound. */ public int getMinWidth() {return MIN_WIDTH;} /** Returns the maximal width of the bound. */ public int getMaxWidth() {return right;} /** Sets the width of the bound, keeping the right edge constant. */ public void setWidth(int width) { if (width < getMinWidth() || width > getMaxWidth()) throw new IllegalArgumentException("Out of bounds"); left = right - width; } /** Returns the right position of the bound. */ public int getPosition() {return right;} /** Returns the minimal position of the bound. */ public int getMinPosition() {return right - left;} /** Returns the maximal position of the bound. */ public int getMaxPosition() {return buffer.getSize() - 1;} /** Sets the right edge of the bound, keeping the width constant. */ public void setPosition(int position) { if (position < getMinPosition() || position > getMaxPosition()) throw new IllegalArgumentException("Out of bounds"); left = position + left - right; right = position; } /** Sets the left and right edges of the bound. */ public void setRange(int left, int right) { if (left > right) { int t = left; left = right; right = t; } this.left = left; this.right = right; } /** Sets all edges of the bound. */ public void setRange(Bounds bounds) { this.left = bounds.getLeft(); this.ceil = bounds.getCeil(); this.right = bounds.getRight(); this.floor = bounds.getFloor(); } /** Returns the height of the bound. */ public int getHeight() {return ceil - floor;} /** Returns the minimal height of the bound. */ public int getMinHeight() {return MIN_HEIGHT;} /** Returns true if index and value are both within the bound. */ public boolean contains(int index, int value) { return containsIndex(index) && containsValue(value); } /** Returns true if index is within the bound. */ public boolean containsIndex(int index) { return index >= left && index <= right; } /** Returns true if value is within the bound. */ public boolean containsValue(int value) { return value >= floor && value <= ceil; } /** Returns true if the selection is within the bound. */ public boolean contains(Selection selection) { if (selection.getState() == Selection.NONE) return false; if (selection.getLeftmost() > right) return false; if (selection.getRightmost() < left) return false; return true; } /** Returns true if this and the parameter represent the same range. */ public boolean equals(Object obj) { if (!(obj instanceof Bounds)) return false; if (((Bounds)obj).getLeft() != left) return false; if (((Bounds)obj).getCeil() != ceil) return false; if (((Bounds)obj).getRight() != right) return false; if (((Bounds)obj).getFloor() != floor) return false; return true; } }