1   package de.desy.acop.demo;
2   
3   import java.util.Map;
4   
5   import com.cosylab.events.SetEvent;
6   import com.cosylab.events.SetListener;
7   
8   import de.desy.acop.displayers.AcopSlider;
9   import de.desy.acop.transport.ConnectionFailed;
10  import de.desy.acop.transport.ConnectionParameters;
11  import de.desy.acop.transport.adapters.DataSourceUtilities;
12  import de.desy.tine.client.TLink;
13  import de.desy.tine.client.TLinkCallback;
14  import de.desy.tine.dataUtils.TDataType;
15  import de.desy.tine.definitions.TAccess;
16  import de.desy.tine.definitions.TMode;
17  
18  /**
19   *
20   * <code>DisplayerFeedDemo</code> is a demo for Marcus Walla, how to feed
21   * the data to a displayer directly.
22   *
23   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
24   *
25   */
26  public class DisplayerFeedDemo {
27  
28  	private TLink asyncLink;
29  	private TLinkCallback asyncCallback;
30  	private AcopSlider slider;
31  	private double[] data;
32  	
33  	public DisplayerFeedDemo() {
34  		slider = new AcopSlider();
35  
36  		//this will set the default min/max, title etc.
37  		try {
38  			Map<String,Object> characteristics = DataSourceUtilities.getCharacteristics(new ConnectionParameters("TINE/LINAC2/L2.MODUL/#0/sps_rec"));
39  			slider.setCharacteristics(characteristics);
40  		} catch (ConnectionFailed e1) {
41  			e1.printStackTrace();
42  		}
43  		
44  		//establish the connection
45  		data = new double[1];
46  		//the INT16 type is equivalent to short
47  		short[] dataIn = new short[]{50,4,9};
48  		TLink link = new TLink("/LINAC2/L2.MODUL/#0", "sps_rec", new TDataType(data), new TDataType(dataIn), TAccess.CA_READ);
49  		
50  		TLinkCallback cb = new TLinkCallback(){
51  			public void callback(TLink link) {
52  				if (link.getLinkStatus() == 0) {
53  					slider.setReadback(data[0]);
54  				}
55  			}
56  		};
57  		int handle = link.attach(TMode.CM_POLL,cb,1000);
58  		if (handle < 0) {
59  			System.err.println("Error");
60  		}
61  				
62  		//if you want to use the slider as the input widget, you 
63  		//have to trap events from it. There are two options how to do that and
64  		//here is the simple one:
65  		slider.addSetListener(new SetListener(){
66  			public void setPerformed(SetEvent e) {
67  				double value = e.getDoubleValue();
68  				updateLink(value);
69  			}
70  		});
71  	}
72  	
73  	private void updateLink(double value) {
74  		
75  		if (asyncLink == null) {
76  			//here you have to know how to set the values on the link (what kind of
77  			//data you have to provide together with the set value...
78  			//What you have to be careful is to reuse the same TLink object each time
79  			//when this method is called. Otherwise you may run into troubles if you
80  			//move the slider too quickly.
81  			short[] dataIn = new short[]{50,4,9};
82  			asyncLink = new TLink("/LINAC2/L2.MODUL/#0", "sps_rec", new TDataType(), new TDataType(dataIn), TAccess.CA_WRITE);
83  			asyncCallback = new TLinkCallback(){
84  				public void callback(TLink link) {
85  					if (link.getLinkStatus() == 0) {
86  						System.err.println("Error!");
87  					}
88  				}
89  			};
90  		}
91  		asyncLink.attach(TMode.CM_SINGLE, asyncCallback, 1000);
92  	}
93  }