View Javadoc

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 <code>RangedValuePolicy</code> that resizes the range
24   * based on the value parameter.
25   * 
26   * <p>
27   * Whenever the value is set below minimum, minimum will be changed to the the
28   * value, decreased by factor  (maximum - minimum). Same rule applies for
29   * maximum.
30   * </p>
31   * This policy is value dominant i.e., bounds will be adjusted to ensure that
32   * value is always valid. If either bound is set incorrectly, it will be 
33   * adjusted to conform with this condition.
34   * <p>
35   * Rule for adjusting bounds is as follows. Whenever value is either below
36   * minimum ore above maximum, the corresponding bound will be adjusted using
37   * value - range * factor or value + range * factor for minimum and maximum
38   * respectively, where range is maximum - minimum.
39   * 
40   * 
41   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
42   * @version $id$
43   */
44  public class RescalingValuePolicy extends RangedValuePolicy
45  {
46  	private double rescaleFactor;
47  
48  	/**
49  	 * Default constructor for RescalingValuePolicy. Initializes rescaling
50  	 * factor to 0.25.
51  	 */
52  	public RescalingValuePolicy()
53  	{
54  		this(0.25);
55  	}
56  
57  	/**
58  	 * Constructor for RescalingValuePolicy. Allows custom rescaling factor.
59  	 *
60  	 * @param factor by which the range will be resized.
61  	 */
62  	public RescalingValuePolicy(double factor)
63  	{
64  		super();
65  		setFactor(factor);
66  	}
67  
68  	/**
69  	 * Adjusts bounds as specified in class JavaDoc.
70  	 *
71  	 * @param params RangedValueHolder
72  	 *
73  	 * @see com.cosylab.gui.components.range.RangedValuePolicy#validate(RangedValueHolder)
74  	 */
75  	public RangedValue validate(RangedValue params)
76  	{
77  		double value = params.getValue();
78  		double range = params.getMaximum() - params.getMinimum();
79  
80  		if(value < params.getMinimum()) {
81  			params = params.deriveWithMin(value - range * getFactor());
82  
83  			return validatePeerPolicy(params);
84  		}
85  
86  		if(value > params.getMaximum()) {
87  			params = params.deriveWithMax(value + range * getFactor());
88  
89  			return validatePeerPolicy(params);
90  		}
91  		
92  		return validatePeerPolicy(params);
93  	}
94  
95  	/**
96  	 * Returns rescaling factor.
97  	 *
98  	 * @return factor.
99  	 */
100 	public double getFactor()
101 	{
102 		return rescaleFactor;
103 	}
104 
105 	/**
106 	 * Sets new rescaling factor. Factor set will not be less that 0.
107 	 *
108 	 * @param factor new rescaling factor.
109 	 */
110 	public void setFactor(double factor)
111 	{
112 		rescaleFactor = Math.max(0.0, factor);
113 	}
114 }
115 
116 /* __oOo__ */