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.NumberField;
33  import com.cosylab.util.CommonException;
34  
35  import de.desy.acop.displayers.AcopNumberLabel;
36  import de.desy.acop.transport.AccessMode;
37  import de.desy.acop.transport.ConnectionParameters;
38  
39  public class NumberLabelExample {
40  
41  	public static void main(String[] args) {
42  		
43  		//How to connect number label to a remote property?
44  		//First we create an instance of the AcopNumberLabel object.
45  		AcopNumberLabel numberLabel = new AcopNumberLabel();
46  		//We require ConnectionParameters which describe the remote property
47  		ConnectionParameters parameters = new ConnectionParameters("TINE",
48  				"context", "server", "device", "property", AccessMode.POLL, 1000);
49  		//These parameters describe a remote property 'context/server/device/property'
50  		//in the TINE control system. We will request a constant monitoring,
51  		//therefore AccessMode.POLL was used. Polling rate is set to 1000 milliseconds,
52  		//which means that value on the number label will be updated every 1000 milliseconds.
53  		//Parameters are then attached to the number label. Connection is created automatically.
54  		try {
55  			numberLabel.setConnectionParameters(parameters);
56  		} catch (CommonException e) {
57  			e.printStackTrace();
58  		} catch (PropertyVetoException e) {
59  			e.printStackTrace();
60  		}
61  		//If no exception occurred, number label should show the remote value.
62  		
63  		
64  		//The number label will set the value on the property only when the user
65  		//navigates away from the label (the label loses focus). We want to set a
66  		//new value while typing it in
67  		numberLabel.setSettingPolicy(NumberField.SET_AS_TYPED);
68  		//Or we want to avoid the overload and only change the value when one 
69  		//presses the [Enter] key. 
70  		numberLabel.setSettingPolicy(NumberField.SET_ON_APPLY);
71  		//Finally we can switch back to set a new value only when navigating 
72  		//away from the number label.
73  		numberLabel.setSettingPolicy(NumberField.SET_ON_EXIT);
74  		
75  				
76  		//The incoming values are too large (large exponents) and have a large extent.
77  		//We want to show values in more readable form. Let us show the logarithm of
78  		//the value. For this we will use a logarithmic converter with logarithm base 10:
79  		LogarithmicConverter logConverter = new LogarithmicConverter(10);
80  		try {
81  			numberLabel.setConverter(logConverter);
82  		} catch (PropertyVetoException e) {
83  			e.printStackTrace();
84  		}
85  		//Instead of real value 'x' number label will now show log_10(x).
86  		//Similarly we can shift the values to be comparable with some other property.
87  		//We will use linear converter:
88  		LinearConverter linConverter = new LinearConverter(5,4);
89  		try {
90  			numberLabel.setConverter(linConverter);
91  		} catch (PropertyVetoException e) {
92  			e.printStackTrace();
93  		}
94  		//Instead of value 'x' number label will now show 5 x + 4. We can even combine both 
95  		//converters into a single one.
96  		try {
97  			ConverterChain chain = new ConverterChain(new Converter[]{logConverter, linConverter});
98  			numberLabel.setConverter(chain);
99  		} catch (PropertyVetoException e) {
100 			e.printStackTrace();
101 		}
102 		//Number label will now show 5 (log_10(x)) + 4.
103 		
104 		//If we don't want to change the values at all (just use the number label
105 		//as a monitor displayer) we can put in read only mode
106 		numberLabel.setEditable(false);
107 
108 		//When the value received from the 3rd party exceeds the set value bounds (minimum/maximum), 
109 		//the number label may visually indicate this by tilting the border in a given color.  
110 		numberLabel.setTiltingEnabled(true);
111 		numberLabel.setOutOfBoundsColor(Color.YELLOW);
112 
113 		//Other properties of the number label can also simply be adjusted by calling the
114 		//appropriate setter methods. These properties include maximum/minimum value
115 		//background/foreground color, title etc.:
116 		numberLabel.setMaximum(100);
117 		numberLabel.setMinimum(5.2);
118 		numberLabel.setBackground(Color.RED);
119 		numberLabel.setForeground(Color.GREEN);
120 	}
121 }