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;
21  
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.beans.Customizer;
25  import java.beans.PropertyChangeEvent;
26  import java.beans.PropertyChangeListener;
27  
28  import javax.swing.BoxLayout;
29  import javax.swing.ButtonGroup;
30  import javax.swing.ButtonModel;
31  import javax.swing.JPanel;
32  import javax.swing.JRadioButton;
33  
34  import com.cosylab.gui.components.range2.RangedValuePolicy;
35  import com.cosylab.gui.components.range2.RescalingValuePolicy;
36  import com.cosylab.gui.components.range2.ShiftValuePolicy;
37  import com.cosylab.gui.components.range2.TrimValuePolicy;
38  
39  /**
40   * <code>ValuePolicyCustomizer</code> is a java bean customizer for
41   * setting the RangedValuePolicy property.
42   * 
43   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
44   * @version $Id: ValuePolicyCustomizer.java,v 1.6 2008-04-22 12:28:40 jbobnar Exp $
45   *
46   */
47  public class ValuePolicyCustomizer extends JPanel implements Customizer
48  {
49  	private static final long serialVersionUID = 1L;
50  	private ButtonGroup bg;
51  	private DialKnob displayer;
52  	private JRadioButton rescalingButton;
53  	private JRadioButton shiftButton;
54  	private JRadioButton trimButton;
55  
56  	private RangedValuePolicy policy;
57  	/**
58  	 * Creates a new WPanel object.
59  	 */
60  	public ValuePolicyCustomizer()
61  	{
62  		bg = new ButtonGroup();
63  
64  		trimButton = new JRadioButton("Trim Value Range");
65  		rescalingButton = new JRadioButton("Rescale Value Range");
66  		shiftButton = new JRadioButton("Shift Value Range");
67  
68  		bg.add(trimButton);
69  		bg.add(rescalingButton);
70  		bg.add(shiftButton);
71  
72  		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
73  		add(trimButton);
74  		add(rescalingButton);
75  		add(shiftButton);
76  
77  		ActionListener l= new ActionListener() {
78  		
79  			public void actionPerformed(ActionEvent e) {
80  				applySettings();
81  			}
82  		};
83  		
84  		trimButton.addActionListener(l);
85  		rescalingButton.addActionListener(l);
86  		shiftButton.addActionListener(l);
87  		
88  	}
89  	
90  	/* (non-Javadoc)
91  	 * @see java.beans.Customizer#setObject(java.lang.Object)
92  	 */
93  	public void setObject(Object bean) {
94  		displayer= (DialKnob)bean;
95  		displayer.addPropertyChangeListener("valuePolicy",new PropertyChangeListener() {
96  			public void propertyChange(PropertyChangeEvent evt) {
97  				if (!"valuePolicy".equals(evt.getPropertyName())) {
98  					return;
99  				}
100 				setValuePolicy(displayer.getValuePolicy());
101 			}
102 		
103 		});
104 		setValuePolicy(displayer.getValuePolicy());
105 	}
106 
107 
108 	/**
109 	 * Applyes policy to the displayer.
110 	 *
111 	 * @throws AssertionError if customizer buttons appear in a wrong state 
112 	 * (none is selected)
113 	 */
114 	public void applySettings()
115 	{
116 		ButtonModel bm = bg.getSelection();
117 
118 		if (bm == null) {
119 			return;
120 		}
121 		RangedValuePolicy policy = null;
122 		if (bm == shiftButton.getModel()) {
123 			policy = new ShiftValuePolicy();
124 		} else if (bm == rescalingButton.getModel()) {
125 			policy = new RescalingValuePolicy();
126 		} else if (bm == trimButton.getModel()) {
127 			policy = new TrimValuePolicy();
128 		} else {
129 			throw new AssertionError();
130 		}
131 		
132 		displayer.setValuePolicy(policy);
133 		
134 		firePropertyChange("valuePolicy", null, policy);
135 
136 	}
137 	
138 	/**
139 	 * Sets value policy to the customizer.
140 	 * 
141 	 * @param policy new value policy
142 	 */
143 	public void setValuePolicy(RangedValuePolicy policy) {
144 		if (displayer != null) {
145 			if (policy instanceof RescalingValuePolicy && !rescalingButton.isSelected()) {
146 				rescalingButton.setSelected(true);
147 			} else if (policy instanceof TrimValuePolicy && !trimButton.isSelected()) {
148 				trimButton.setSelected(true);
149 			} else if (policy instanceof ShiftValuePolicy && !shiftButton.isSelected()) {
150 				shiftButton.setSelected(true);
151 			}
152 		}
153 		this.policy = policy;
154 		firePropertyChange("valuePolicy", null, policy);		
155 	}
156 	
157 	/**
158 	 * Returns the value policy.
159 	 * 
160 	 * @return
161 	 */
162 	public RangedValuePolicy getValuePolicy() {
163 		return policy;
164 	}
165 }