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 a selection in a roll chart. */ public class Selection { /** No selection. */ public static final int NONE = 0; /** A selection is about to be made (maybe). */ public static final int BEGIN = 1; /** A selection is being made or has been made. */ public static final int SELECT = 2; /** The selection is zoomed in. */ public static final int ZOOM = 3; private int left; private int right; private int state = NONE; private Buffer buffer; /** Constructor. */ public Selection(Buffer buffer) {this.buffer = buffer;} /** Returns one endpoint of the selection. */ public int getLeft() { if (state < SELECT) throw new UnsupportedOperationException( "No selection exists"); return left; } /** Returns the left endpoint of the selection. */ public int getLeftmost() { return left < right ? getLeft() : getRight(); } /** Sets one endpoint of the selection. */ public void setLeft(int left) {setRange(left, right);} /** Returns the other endpoint of the selection. */ public int getRight() { if (state < SELECT) throw new UnsupportedOperationException( "No selection exists"); return right; } /** Returns the right endpoint of the selection. */ public int getRightmost() { return right > left ? getRight() : getLeft(); } /** Sets the other endpoint of the selection. */ public void setRight(int right) {setRange(left, right);} /** Returns the minimal endpoint. */ public int getMin() {return buffer.getStart();} public int getMax() {return buffer.getSize() - 1;} /** Sets both endpoints of the selection. */ public void setRange(int left, int right) { if (state < SELECT) throw new UnsupportedOperationException( "No selection exists"); if (state == ZOOM) throw new UnsupportedOperationException( "Cannot resize a zoomed selection"); int min = getMin(); int max = getMax(); if (left < min) left = min; if (left > max) left = max; if (right < min) right = min; if (right > max) right = max; this.left = left; this.right = right; } /** Grows the selection to include pos. */ public void extend(int pos) { if (contains(pos)) return; int left = getLeftmost(); int right = getRightmost(); if (pos < getLeftmost()) left = pos; else /* if (pos > getRightmost()) */ right = pos; setRange(left, right); } /** Returns true if pos is within the selection. */ public boolean contains(int pos) { if (state < SELECT) return false; if (pos >= left && pos <= right) return true; if (pos >= right && pos <= left) return true; return false; } /** Returns the current state of the selection. */ public int getState() {return state;} /** Sets the state of the selection. */ public void setState(int state) { switch (state) { case NONE: case BEGIN: case SELECT: case ZOOM: if (state == this.state) return; this.state = state; break; default: throw new IllegalArgumentException("Invalid state"); } } }