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   * This class represents adjustable policies for <code>RangedValue</code>.
24   * Whenever a change any of the values (minimum, maximum or value) is made in
25   * <code>RangedValue</code> validate method will be called giving the
26   * implementor of this interface the chance to adjust the values before they
27   * are set and property change events invoked.<br>
28   * Note: Regardles of the changes proposed by validate method,
29   * <code>RangedValue</code> will always ensure the proper relation between the
30   * values: values must always form increasing order (minimum, value,
31   * maximum).
32   *
33   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
34   * @version $id$
35   */
36  public abstract class RangedValuePolicy
37  {
38  	RangedValuePolicy peer;
39  	
40  	/**
41  	 * Passes the value triplet <code>params</code> that may be modified by
42  	 * validate method. Regardles of the changes made, all three values will
43  	 * still be made to comply with the basic definition of <code>
44  	 * RangedValue</code>.
45  	 * 
46  	 * <p>Before or after this policy has been executed it must call protected method validatePeerPolicy(RangedValue).
47  	 *
48  	 * @param params Triplet of values to validate.
49  	 * 
50  	 * @return if the validation require the change of value or bounds of the RangedValue
51  	 * 		a new RangedValue with appropriate values should be returned
52  	 */
53  	public abstract RangedValue validate(RangedValue params);
54  	
55  	/**
56  	 * Inserts provided polici as first policy in chain, here next peer policy is this policy.
57  	 * @param p new first policy in chain
58  	 * @return returns new first policy in chain, the p parameter.
59  	 */
60  	public RangedValuePolicy insertPeerPolicy(RangedValuePolicy p) {
61  		p.peer=this;
62  		return p;
63  	}
64  
65  	/**
66  	 * Adds provided policy to the end of the chin
67  	 * @param p
68  	 */
69  	public void addPeerPolicy(RangedValuePolicy p) {
70  		if (p == this) return;
71  		if (peer!=null) {
72  			peer.addPeerPolicy(p);
73  		} else {
74  			peer=p;
75  		}
76  	}
77  	
78  	/**
79  	 * Returns the peer policy.
80  	 * 
81  	 * @return
82  	 */
83  	public RangedValuePolicy getPeerPolicy() {
84  		return peer;
85  	}
86  	
87  	/**
88  	 * Removes provided policy from the chain, and returnes first policy in chain.
89  	 * @param p policy to be removed from chain
90  	 * @return returns new first policy in chain, this or peer policy
91  	 */
92  	public RangedValuePolicy removePeerPolicy(RangedValuePolicy p) {
93  		if (p==this) {
94  			p.peer=null;
95  			return peer;
96  		}
97  		if (peer != null) {
98  			peer=peer.removePeerPolicy(p);
99  		}
100 		return this;
101 	}
102 	
103 	/**
104 	 * Removes provided policy from the chain, and returnes first policy in chain.
105 	 * @param p policy to be removed from chain
106 	 * @return returns new first policy in chain, this or peer policy
107 	 */
108 	public RangedValuePolicy removePeerPolicyByType(Class<? extends RangedValuePolicy> c) {
109 		if (c.isAssignableFrom(this.getClass())) {
110 			peer=null;
111 			return peer;
112 		}
113 		if (peer != null) {
114 			peer=peer.removePeerPolicyByType(c);
115 		}
116 		return this;
117 	}
118 
119 	protected RangedValue validatePeerPolicy(RangedValue p) {
120 		if (peer !=null) {
121 			return peer.validate(p);
122 		}
123 		return p;
124 	}
125 	
126 }
127 
128 /* __oOo__ */