1 /* 2 * Copyright (c) 2003-2008 by Cosylab d. d. 3 * 4 * This file is part of CosyBeans-Common. 5 * 6 * CosyBeans-Common is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * CosyBeans-Common is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with CosyBeans-Common. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package com.cosylab.gui.components.range2; 21 22 /** 23 * Implementation of linear range. 24 * 25 * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a> 26 * @version $id$ 27 */ 28 public class LinearRange implements Range 29 { 30 31 private TickCalculator defaultTickCalculator; 32 33 /** 34 * Returns the parameters. 35 * 36 * @see com.cosylab.gui.components.range.Range#checkValue(double) 37 */ 38 public double validate(double value) 39 { 40 return value; 41 } 42 43 /** 44 * Converts relative value in this range to absolute. If span of range is 45 * infinite, return value is 0. Conversion is implemented for all values 46 * even outside the 0.0 - 1.0 range. 47 * 48 * @param relative value in range 0.0 to 1.0 inclusively. 49 * 50 * @return absolute value. 51 * 52 * @see com.cosylab.gui.components.range.Range#toAbsolute(double) 53 */ 54 public double toAbsolute(double relative, RangedValue ranged) 55 { 56 final double min = ranged.getMinimum(); 57 final double max = ranged.getMaximum(); 58 59 if (Double.isInfinite(max) || (Double.isInfinite(min))) { 60 return 0.0; 61 } 62 63 return min + relative * (max - min); 64 } 65 66 /** 67 * Converts absolute value into relative (defined in 0.0 to 1.0 range). If 68 * span of the range is infinite, return value is 0.5. Conversion is 69 * defined for all values of even outside the range. 70 * 71 * @param absolute value. 72 * 73 * @return relative value. 74 * 75 * @see com.cosylab.gui.components.range.Range#toRelative(double) 76 */ 77 public double toRelative(double absolute, RangedValue ranged) 78 { 79 double range = ranged.getMaximum() - ranged.getMinimum(); 80 81 if (range == 0.0) { 82 return 0.5; 83 } 84 85 if (Double.isInfinite(range)) { 86 return 0.5; 87 } 88 89 return (absolute - ranged.getMinimum()) / range; 90 } 91 92 /* 93 * (non-Javadoc) 94 * @see com.cosylab.gui.components.range2.Range#getDefaultTickCalculator() 95 */ 96 public TickCalculator getDefaultTickCalculator() { 97 if (defaultTickCalculator == null) { 98 defaultTickCalculator = new LinearTickCalculator(3, false); 99 } 100 return defaultTickCalculator; 101 } 102 } 103 104 /* __oOo__ */