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  import java.util.EventObject;
23  
24  /**
25   * This is the event object holding the information about the change in
26   * <code>RangedValue</code>. This implementation notifies about changes in
27   * minimum, maximum, value or policy changes. More than one change can be
28   * carried by this event.
29   * 
30   * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
31   * @version $id$
32   */
33  public class RangedValueEvent extends EventObject {
34  
35  	private static final long serialVersionUID = 1L;
36  
37  	// Bit indicating the minimum has changed
38  	public static final int MINIMUM_CHANGED = 1;
39  
40  	// Bit indicating the maximum has changed
41  	public static final int MAXIMUM_CHANGED = 2;
42  
43  	// Bit indicating the value has changed
44  	public static final int VALUE_CHANGED = 4;
45  	
46  	// Bit indicating the value has changed
47  	public static final int RAW_VALUE_CHANGED = 8;
48  
49  	// Bits indicating policies have changed
50  	public static final int POLICY_CHANGED = 16;
51  
52  	private int change;
53  
54  	/**
55  	 * Constructor for RangedValueEvent. This constructor initializes the 
56  	 * fields to notify of change in minimum, maximum and/or value.
57  	 * 
58  	 * @param source
59  	 * @param min boolean Has minimum changed.
60  	 * @param max boolean Has maximum changed.
61  	 * @param val boolean Has value changed.
62  	 */
63  	public RangedValueEvent(
64  		Object source,
65  		boolean min,
66  		boolean max,
67  		boolean val,
68  		boolean raw) {
69  			
70  		super(source);
71  		change = 0;
72  		
73  		setBit(MINIMUM_CHANGED, min);
74  		setBit(MAXIMUM_CHANGED, max);
75  		setBit(VALUE_CHANGED, val);
76  		setBit(RAW_VALUE_CHANGED, raw);
77  	}
78  
79  	/**
80  	 * Constructor for RangedValueEvent. This constructor initializes the fields
81  	 * to notify of change in policies. 
82  	 * 
83  	 * @param source Object
84  	 * @param policyAdded boolean True if policy added, false if removed.
85  	 */
86  	public RangedValueEvent(
87  		Object source,
88  		boolean policyChanged) {
89  		super(source);
90  		setBit(POLICY_CHANGED, policyChanged);
91  	}
92  
93  	/**
94  	 * @param bit int
95  	 * @param state boolean
96  	 */
97  	protected final void setBit(final int bit, final boolean state) {
98  		if (state) {
99  			change |= bit;
100 		} else {
101 			change &= ~bit;
102 		}
103 	}
104 
105 	/**
106 	 * @param bit int
107 	 * @return boolean
108 	 */
109 	private final boolean bitSet(int bit) {
110 		return (change & bit) > 0;
111 	}
112 
113 	/**
114 	 * Reuturns whether minimum has changed.
115 	 * 
116 	 * @return boolean
117 	 */
118 	public final boolean isMinimumChanged() {
119 		return bitSet(MINIMUM_CHANGED);
120 	}
121 
122 	/**
123 	 * Returns whether value has changed.
124 	 * 
125 	 * @return boolean
126 	 */
127 	public final boolean isMaximumChanged() {
128 		return bitSet(MAXIMUM_CHANGED);
129 	}
130 
131 	/**
132 	 * Returns whether value has changed.
133 	 * 
134 	 * @return boolean
135 	 */
136 	public final boolean isValueChanged() {
137 		return bitSet(VALUE_CHANGED);
138 	}
139 	
140 	/**
141 	 * Returns whether value has changed.
142 	 * 
143 	 * @return boolean
144 	 */
145 	public final boolean isRawValueChanged() {
146 		return bitSet(RAW_VALUE_CHANGED);
147 	}
148 
149 	/**
150 	 * Returns whether minimum and maximum have changed.
151 	 * 
152 	 * @return boolean
153 	 */
154 	public final boolean isMinMaxChanged() {
155 		return isMaximumChanged() && isMinimumChanged();
156 	}
157 
158 	/**
159 	 * Returns whether minimum or maximum have changed.
160 	 * 
161 	 * @return boolean
162 	 */
163 	public final boolean isMinOrMaxChanged() {
164 		return isMinimumChanged() || isMaximumChanged();
165 	}
166 	
167 	/**
168 	 * Returns whether the policies have changed.
169 	 * 
170 	 * @return boolean
171 	 */
172 	public final boolean isPolicyChanged() {
173 		return bitSet(POLICY_CHANGED);
174 	}
175 
176 	/**
177 	 * Returns the change mask. This value can be then checked agains the
178 	 * constants defined by this event to determine which changes have occured.
179 	 * 
180 	 * @return int
181 	 */
182 	public final int getChange() {
183 		return change;
184 	}
185 
186 }