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 * This is the implementation of <code>RangedValuePolicy</code> that allows 24 * values of <code>RangedValue</code> to be only within the specified range. 25 * This range is defined by allowedMinimum and allowedMaximum. These values 26 * are independent from minimum and maximum defined in <code>RangedValue</code> 27 * in that no value can be outside these bounds, including minimum and maximum, 28 * but will be cropped to these values. 29 * 30 * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a> 31 * @version $id$ 32 */ 33 public class LimitedRangePolicy extends RangedValuePolicy { 34 35 // Absolute allowed minimum for all three parameter in RangedValue. 36 private double allowedMin = -Double.MAX_VALUE; 37 38 // Absolute allowed maximum for all three parameter in RangedValue. 39 private double allowedMax = Double.MAX_VALUE; 40 41 /** 42 * Constructor for LimitedRangedValuePolicy. 43 * @param rangedValue 44 */ 45 public LimitedRangePolicy(double allowedMinimum, double allowedMaximum) { 46 if (allowedMinimum > allowedMaximum) { 47 double temp = allowedMinimum; 48 allowedMinimum = allowedMaximum; 49 allowedMaximum = temp; 50 } 51 allowedMin = allowedMinimum; 52 allowedMax = allowedMaximum; 53 } 54 55 /** 56 * Convenience method to check for bounds acceptability. 57 * 58 * @param value to check against bounds. 59 * @return adjusted value. 60 */ 61 private final double checkBounds(double value) { 62 if (value < allowedMin) { 63 value = allowedMin; 64 } 65 if (value > allowedMax) { 66 value = allowedMax; 67 } 68 return value; 69 } 70 71 /** 72 * @see com.cosylab.gui.components.range.RangedValuePolicy#validate(RangedValueHolder) 73 */ 74 public RangedValue validate(RangedValue params) { 75 params = new RangedValue(checkBounds(params.getMinimum()),checkBounds(params.getMaximum()), 76 checkBounds(params.getValue())); 77 return validatePeerPolicy(params); 78 } 79 }