1   /*
2    * Copyright (c) 2006 Stiftung Deutsches Elektronen-Synchroton,
3    * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
4    *
5    * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS.
6    * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
7    * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND
8    * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
9    * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10   * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
11   * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE
12   * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR
13   * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
14   * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
15   * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
16   * OR MODIFICATIONS.
17   * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION,
18   * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS
19   * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY
20   * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
21   */
22  
23  package de.desy.acop.demo.example;
24  
25  import java.awt.Color;
26  import java.beans.PropertyVetoException;
27  
28  import com.cosylab.gui.adapters.Converter;
29  import com.cosylab.gui.adapters.ConverterChain;
30  import com.cosylab.gui.adapters.LinearConverter;
31  import com.cosylab.gui.adapters.LogarithmicConverter;
32  import com.cosylab.gui.components.range2.RangedValuePolicy;
33  import com.cosylab.gui.components.range2.RescalingValuePolicy;
34  import com.cosylab.gui.components.range2.ShiftValuePolicy;
35  import com.cosylab.util.CommonException;
36  
37  import de.desy.acop.displayers.AcopDialKnob;
38  import de.desy.acop.transport.AccessMode;
39  import de.desy.acop.transport.ConnectionParameters;
40  
41  public class DialKnobExample {
42  
43  	public static void main(String[] args) {
44  		
45  		//How to connect dial knob to a remote property?
46  		//First we create an instance of the AcopDialKnob object.
47  		AcopDialKnob knob = new AcopDialKnob();
48  		//We require ConnectionParameters which describe the remote property
49  		ConnectionParameters parameters = new ConnectionParameters("TINE",
50  				"context", "server", "device", "property", AccessMode.POLL, 1000);
51  		//These parameters describe a remote property 'context/server/device/property'
52  		//in the TINE control system. We will request a constant monitoring,
53  		//therefore AccessMode.POLL was used. Polling rate is set to 1000 milliseconds,
54  		//which means that value on the dial knob will be updated every 1000 milliseconds.
55  		//Parameters are then attached to the dial knob. Connection is created automatically.
56  		try {
57  			knob.setConnectionParameters(parameters);
58  		} catch (CommonException e) {
59  			e.printStackTrace();
60  		} catch (PropertyVetoException e) {
61  			e.printStackTrace();
62  		}
63  		//If no exception occured, dial knob should show the remote value.
64  				
65  		//The incomming values are too large (large exponents) and have a large extent.
66  		//We want to show values in more readable form. Let us show the logarithm of
67  		//the value. For this we will use a logarithmic converter with logarithm base 10:
68  		LogarithmicConverter logConverter = new LogarithmicConverter(10);
69  		try {
70  			knob.setConverter(logConverter);
71  		} catch (PropertyVetoException e) {
72  			e.printStackTrace();
73  		}
74  		//Instead of real value 'x' dial knob will now show log_10(x).
75  		//Similarly we can shift the values to be comparable with some other property.
76  		//We will use linear converter:
77  		LinearConverter linConverter = new LinearConverter(5,4);
78  		try {
79  			knob.setConverter(linConverter);
80  		} catch (PropertyVetoException e) {
81  			e.printStackTrace();
82  		}
83  		//Instead of value 'x' dial knob will now show 5 x + 4. We can even combine both 
84  		//converters into a single one.
85  		try {
86  			ConverterChain chain = new ConverterChain(new Converter[]{logConverter, linConverter});
87  			knob.setConverter(chain);
88  		} catch (PropertyVetoException e) {
89  			e.printStackTrace();
90  		}
91  		//Dial knob will now show 5 (log_10(x)) + 4.
92  		
93  		//If dial knob is used to show a value that changes because of a 3rd party, 
94  		//we don't want the dial knob needle to be out of sync each time the value
95  		//changes, but want the needle to move together with the shadow. We will
96  		//set the automatic synchronization with 0 sync time
97  		knob.setAutoSynchronize(true);
98  		knob.setAutoSynchronizeDelay(0);
99  		//or if we don't want to change the values at all (just use the dial knob
100 		//as a monitor displayer) we can put in read only mode
101 		knob.setEditable(false);
102 
103 		//The minimum and maximum values of the dial knob scale can be simply defined 
104 		//with the appropriate setter methods
105 		knob.setMaximum(90);
106 		knob.setMinimum(5.2);
107 		//When the value received from the 3rd party exceeds the set value bounds (minimum/maximum), 
108 		//the dial knob may visually indicate this by tilting the border.  
109 		knob.setTiltingEnabled(true);
110 		//In addition, the marker of the dial knob may change color whenever the value received is out of these bounds
111 		knob.setOutOfBoundsColor(Color.YELLOW);	
112 		
113 		//We can define another set of bounds - the warning bounds
114 		knob.setLowWarningLimit(1);
115 		knob.setHighWarningLimit(99);
116 		//and the color of the marker, when these limits are passed
117 		knob.setWarningColor(Color.ORANGE);
118 		//We can finally defined the alarm bounds
119 		knob.setLowAlarmLimit(0);
120 		knob.setHighAlarmLimit(100);
121 		//and the color of the marker, when these limits are passed
122 		knob.setAlarmColor(Color.RED);
123 
124 		//What happens if the remotely received value is larger than the maximum displayed scale 
125 		//on the dial knob? We can set a value policy to handle such cases appropriately.
126 		//We want for the scale to adjust itself (to enlarge) when the value is out of bounds.
127 		RangedValuePolicy rescalingPolicy = new RescalingValuePolicy();
128 		knob.setValuePolicy(rescalingPolicy);
129 		//or we want the scale to maintain its range but shift the bounds so that the new value 
130 		//lies between them 
131 		RangedValuePolicy shiftPolicy = new ShiftValuePolicy();
132 		knob.setValuePolicy(shiftPolicy);
133 		
134 		
135 		//Other properties of the dial knob can also simply be adjusted by calling the
136 		//appropriate setter methods. These properties include background/foreground color, title etc.:
137 		knob.setBackground(Color.RED);
138 		knob.setForeground(Color.GREEN);
139 	}
140 }