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.AcopGauger;
38  import de.desy.acop.transport.AccessMode;
39  import de.desy.acop.transport.ConnectionParameters;
40  
41  public class GaugerExample {
42  
43  	public static void main(String[] args) {
44  		
45  		//How to connect gauger to a remote property?
46  		//First we create an instance of the AcopGauger object.
47  		AcopGauger gauger = new AcopGauger();
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 gauger will be updated every 1000 milliseconds.
55  		//Parameters are then attached to the gauger. Connection is created automatically.
56  		try {
57  			gauger.setConnectionParameters(parameters);
58  		} catch (CommonException e) {
59  			e.printStackTrace();
60  		} catch (PropertyVetoException e) {
61  			e.printStackTrace();
62  		}
63  		//If no exception occurred, gauger should show the remote value.		
64  				
65  		//The incoming 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  			gauger.setConverter(logConverter);
71  		} catch (PropertyVetoException e) {
72  			e.printStackTrace();
73  		}
74  		//Instead of real value 'x' gauger 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  			gauger.setConverter(linConverter);
80  		} catch (PropertyVetoException e) {
81  			e.printStackTrace();
82  		}
83  		//Instead of value 'x' gauger 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  			gauger.setConverter(chain);
88  		} catch (PropertyVetoException e) {
89  			e.printStackTrace();
90  		}
91  		//Gauger will now show 5 (log_10(x)) + 4.
92  		
93  		//We can also adjust the scale of the gauger suitable for displaying logarithms
94  		gauger.setLogarithmicScale();
95  		//or reset back to linear scale
96  		gauger.setLinearScale();
97  
98  		//The minimum and maximum values of the gauger scale can be simply defined 
99  		//with the appropriate setter methods
100 		gauger.setMaximum(90);
101 		gauger.setMinimum(5.2);
102 		//When the value received from the 3rd party exceeds the set value bounds (minimum/maximum), 
103 		//the marker of the gauger may change color whenever the value received is out of these bounds
104 		gauger.setOutOfBoundsColor(Color.YELLOW);	
105 		
106 		//We can define another set of bounds - the warning bounds
107 		gauger.setLowWarningLimit(1);
108 		gauger.setHighWarningLimit(99);
109 		//and the color of the marker, when these limits are passed
110 		gauger.setWarningColor(Color.ORANGE);
111 		
112 		
113 		//What happens if the remotely received value is larger than the maximum displayed scale 
114 		//on the gauger? We can set a value policy to handle such cases appropriately.
115 		//We want for the scale to adjust itself (to enlarge) when the value is out of bounds.
116 		RangedValuePolicy rescalingPolicy = new RescalingValuePolicy();
117 		gauger.setValuePolicy(rescalingPolicy);
118 		//or we want the scale to maintain its range but shift the bounds so that the new value 
119 		//lies between them 
120 		RangedValuePolicy shiftPolicy = new ShiftValuePolicy();
121 		gauger.setValuePolicy(shiftPolicy);
122 		
123 		//Other properties of the gauger can also simply be adjusted by calling the
124 		//appropriate setter methods. These properties include background/foreground color, title etc.:
125 		gauger.setBackground(Color.RED);
126 		gauger.setForeground(Color.GREEN);
127 	}
128 }