View Javadoc

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.displayers;
24  
25  import java.beans.PropertyVetoException;
26  import java.util.Map;
27  
28  import javax.swing.Action;
29  
30  import com.cosylab.gui.DialKnobDisplayer;
31  import com.cosylab.gui.InfoDialog;
32  import com.cosylab.gui.displayers.DataConsumer;
33  import com.cosylab.gui.displayers.DataState;
34  import com.cosylab.gui.displayers.DisplayerUtilities;
35  import com.cosylab.gui.displayers.DoubleConsumer;
36  import com.cosylab.gui.displayers.DoubleConsumerMulticaster;
37  import com.cosylab.gui.displayers.DoubleSeqConsumer;
38  import com.cosylab.gui.util.UserSettingsProtection;
39  import com.cosylab.util.CommonException;
40  
41  import de.desy.acop.displayers.tools.AcopDisplayer;
42  import de.desy.acop.displayers.tools.AcopDisplayerTransferHandler;
43  import de.desy.acop.displayers.tools.AcopInfoDialog;
44  import de.desy.acop.transport.ConnectionParameters;
45  import de.desy.acop.transport.adapters.AcopTransportDataSource;
46  import de.desy.acop.transport.adapters.AdapterFactory;
47  import de.desy.acop.transport.adapters.AdapterFactoryService;
48  import de.desy.tine.definitions.TArrayType;
49   
50  /**
51   * <code>AcopDialKnob</code> is a single double value displayer.
52   * The component consists of a round knob which is sensitive to mouse
53   * movement. The needle on the knob shows the value of the dial knob. This value
54   * is settable by dragging the knob. In addition to this needle there is a black
55   * trailer which shows the value read from the remote property. The position of
56   * the trailer is not settable through the user interface.
57   * <p>
58   * The connection point for this displayer should be set using the 
59   * <i>connectionParameters</i> property, where the remote name of the 
60   * {@link ConnectionParameters} points to the desired property.
61   * </p>
62   * 
63   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
64   * @version $Id: Templates.xml,v 1.10 2004/01/13 16:17:13 jbobnar Exp $
65   *
66   */
67  public class AcopDialKnob extends DialKnobDisplayer implements DoubleSeqConsumer,  
68  	AcopDisplayer {
69  
70  	private static final long serialVersionUID = -2686822634461337516L;
71  	private ConnectionParameters connectionParameters;
72  	private int arrayIndex=0;
73  	private AcopInfoDialog dialog;
74  
75  	/**
76  	 * Constructs new AcopDialKnob.
77  	 *
78  	 */
79  	public AcopDialKnob() {
80  		super();
81  		new AcopDisplayerTransferHandler(this);
82  		UserSettingsProtection.setProtection(this,DisplayerUtilities.COMMON_NUMERIC_DISPLAYER_PROPERTIES,false);
83  	}
84  
85  	/*
86  	 * (non-Javadoc)
87  	 * @see com.cosylab.gui.displayers.DoubleSeqConsumer#updateValue(long, double[])
88  	 */
89  	public void updateValue(long timestamp, double[] value) throws CommonException {
90  	    updateValue(timestamp, DisplayerUtilities.extract(arrayIndex,value));	
91      }
92  
93  	/* (non-Javadoc)
94  	 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
95  	 */
96  	@Override
97  	public DataConsumer getDataConsumer(Class type)
98  	{
99  		if (type == DoubleConsumer.class || type == DoubleSeqConsumer.class) {
100 			return this;
101 		}
102 		
103 		return DoubleConsumerMulticaster.createDataConsumer(type, this);
104 	}
105 	
106 	/*
107 	 * (non-Javadoc)
108 	 * @see de.desy.acop.displayers.AcopDisplayerConnector#getConnectionParameters()
109 	 */
110 	public ConnectionParameters getConnectionParameters() {
111 		return connectionParameters;
112 	}
113 	
114 	/*
115 	 * (non-Javadoc)
116 	 * @see com.cosylab.gui.DialKnobDisplayer#setCharacteristics(java.util.Map)
117 	 */
118 	@Override
119 	public void setCharacteristics(Map characteristics) {
120 		super.setCharacteristics(characteristics);
121 		
122 		Object o  = characteristics.get("arrayType");
123 		if (o != null) {
124 			if (((TArrayType)o).isChannel()) {
125 				try {
126 					UserSettingsProtection.setUnprotected(this,"arrayIndex",0);
127 				} catch (Exception e) {
128 					e.printStackTrace();
129 				}
130 			}
131 		}
132 	}
133 	
134 	/*
135 	 * (non-Javadoc)
136 	 * @see de.desy.acop.displayers.AcopDisplayerConnector#setConnectionParameters(de.desy.acop.transport.ConnectionParameters)
137 	 */
138 	public void setConnectionParameters(ConnectionParameters param) throws CommonException, PropertyVetoException {
139 		if (param!=null && connectionParameters != null) {
140 			if(param.equals(connectionParameters)) return;
141 		}
142 		updateDataState(new DataState(DataState.UNDEFINED));
143 		
144 		ConnectionParameters old = connectionParameters;
145 		this.connectionParameters = param;
146 		AdapterFactory factory = AdapterFactoryService.getInstance().getAdapterFactory();
147 		
148 		if (getDataSource() != null)
149 			factory.releaseDataSource((AcopTransportDataSource) getDataSource());
150 		if (param == null) {
151 			setDataSource(null);
152 			setTitle("No Connection");
153 		} else {
154 			if (param.getPropertySize() == ConnectionParameters.AUTO_PROPERTY_SIZE) {
155 				param = param.deriveWithPropertySize(1);
156 			}
157 			AcopTransportDataSource ds = factory.createDataSource(param);
158 			setDataSource(ds);
159 		}
160 		
161 		firePropertyChange(CONNECTION_PARAMETERS_PROPERTY,old, connectionParameters);
162 	}
163 
164 	
165 	/*
166 	 * (non-Javadoc)
167 	 * @see de.desy.acop.displayers.tools.AcopDisplayer#getArrayIndex()
168 	 */
169 	public int getArrayIndex() {
170 		return arrayIndex;
171 	}
172 
173 	/*
174 	 * (non-Javadoc)
175 	 * @see de.desy.acop.displayers.tools.AcopDisplayer#setArrayIndex(int)
176 	 */
177 	public void setArrayIndex(int arrayIndex) {
178 		if (this.arrayIndex == arrayIndex) return;
179 		int oldValue = this.arrayIndex;
180 		this.arrayIndex = arrayIndex;
181 		firePropertyChange("arrayIndex", oldValue, this.arrayIndex);
182 	}
183 
184 	/*
185 	 * (non-Javadoc)
186 	 * @see com.cosylab.gui.DialKnobDisplayer#getInfoDialog()
187 	 */
188 	@Override
189 	public InfoDialog getInfoDialog() {
190 		if (dialog == null) {
191 			dialog = new AcopInfoDialog(this);
192 		}
193 		return dialog;
194 	}
195 	
196 	/**
197 	 * Enables/disables Properties item in the popup menu.
198 	 * @param enable
199 	 */
200 	public void setPropertiesPopupEnabled(boolean enable) {
201 		Action[] actions = getPopupManager().getActions();
202 		for (Action a : actions) {
203 			if (a != null && "Preferences...".equals(a.getValue(Action.NAME))) {
204 				a.setEnabled(enable);
205 				return;
206 			}
207 		}
208 	}
209 	
210 	/**
211 	 * Returns true if Properties item is enabled in the popup menu.
212 	 * @return
213 	 */
214 	public boolean isPropertiesPopupEnabled() {
215 		Action[] actions = getPopupManager().getActions();
216 		for (Action a : actions) {
217 			if (a != null && "Preferences...".equals(a.getValue(Action.NAME))) {
218 				return a.isEnabled();
219 			}
220 		}
221 		return false;
222 	}
223 }
224 /* __oOo__ */