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 * Defines value policy that maintains constant span. Whenever value moves 24 * outside the bounds of the range, min and max will be adjusted to follow the 25 * value or will be set to limit of the range. 26 * 27 * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a> 28 * @version $id$ 29 */ 30 public class ShiftValuePolicy extends RangedValuePolicy 31 { 32 private double shiftFactor; 33 34 /** 35 * Default constructor for ShiftValuePolicy. 36 */ 37 public ShiftValuePolicy() 38 { 39 this(0.25); 40 } 41 42 /** 43 * Default constructor for ShiftValuePolicy. 44 * 45 * @param factor double 46 */ 47 public ShiftValuePolicy(double factor) 48 { 49 super(); 50 setFactor(factor); 51 } 52 53 /** 54 * Calculates new limits for the range. If possible, the scale will only be 55 * shifted by ammount specified by factor. In case that is insufficient, 56 * the limit will be adjusted so the new limit is offset from value by 57 * factor ammount. 58 * 59 * @param params RangedValueHolder 60 * 61 * @see com.cosylab.gui.components.range.RangedValuePolicy#validate(RangedValueHolder) 62 */ 63 public RangedValue validate(RangedValue params) 64 { 65 double minimum = params.getMinimum(); 66 double maximum = params.getMaximum(); 67 double value = params.getValue(); 68 double span = maximum - minimum; 69 double delta = span * getFactor(); 70 71 double newMinimum = minimum; 72 double newMaximum = maximum; 73 74 if (value < minimum) { 75 // move scale down 76 newMinimum = minimum - delta; 77 78 if (newMinimum > value) { 79 newMinimum = value - delta; 80 } 81 82 //newMaximum = maximum + newMinimum - minimum; 83 newMaximum = newMinimum + span; 84 } 85 86 if (value > maximum) { 87 // move scale up 88 newMaximum = maximum + delta; 89 90 if (newMaximum < value) { 91 newMaximum = value + delta; 92 } 93 94 //newMinimum = minimum + newMaximum - maximum; 95 newMinimum = newMaximum - span; 96 } 97 98 params = new RangedValue(newMinimum, newMaximum, params.getValue()); 99 100 return validatePeerPolicy(params); 101 } 102 103 /** 104 * Returns factor ammount. 105 * 106 * @return double current factor ammount. 107 */ 108 public double getFactor() 109 { 110 return shiftFactor; 111 } 112 113 /** 114 * Sets new shift factor. This ammount will be between 0 and 1 and will be 115 * adjusted to fit into this range. 116 * 117 * @param factor double 118 */ 119 public void setFactor(double factor) 120 { 121 factor = Math.min(1.0, factor); 122 factor = Math.max(0.0, factor); 123 124 shiftFactor = factor; 125 } 126 } 127 128 /* __oOo__ */