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.InfoDialog;
31  import com.cosylab.gui.WheelswitchDisplayer;
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>AcopWheelswitch</code> is a displayer which can present a single 
52   * double value. The displayer consists of several digits and an up/down button.
53   * In normal mode digits present the value, which is read from the remote 
54   * connection point. When the user presses the up/down button the wheelswitch 
55   * assumes the settable mode - in this case the user can enter the value he 
56   * wants to set on the remote property.
57   * <p>
58   * For proper display of values one have to set the minimum and maximum values 
59   * on the displayer. If the actual value is out of the prescribed bounds,
60   * the displayer will notify the user by changing its visual appearance.
61   * </p>
62   * <p>
63   * The connection point for this displayer should be set using the 
64   * <i>connectionParameters</i> property, where the remote name of the 
65   * {@link ConnectionParameters} points to the desired property.
66   * </p>
67   * 
68   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
69   * @version $Id: Templates.xml,v 1.10 2004/01/13 16:17:13 jbobnar Exp $
70   *
71   */
72  public class AcopWheelswitch extends WheelswitchDisplayer implements  
73  	AcopDisplayer {
74   
75  	private static final long serialVersionUID = -3337991208984786063L;
76  	private ConnectionParameters connectionParameters;
77  	private int arrayIndex=0;
78  	private AcopInfoDialog dialog;
79  	
80  	/**
81  	 * Constructs new AcopWheelswitch.
82  	 *
83  	 */
84  	public AcopWheelswitch() {
85  		super();
86  		setTiltingEnabled(true);
87  		new AcopDisplayerTransferHandler(this);
88  		UserSettingsProtection.setProtection(this,DisplayerUtilities.COMMON_NUMERIC_DISPLAYER_PROPERTIES,false);
89  	}
90  
91  	/*
92  	 * (non-Javadoc)
93  	 * @see com.cosylab.gui.displayers.DoubleSeqConsumer#updateValue(long, double[])
94  	 */
95  	public void updateValue(long timestamp, double[] value) throws CommonException {
96  	    updateValue(timestamp, DisplayerUtilities.extract(arrayIndex,value));	
97      }
98  
99  	/* (non-Javadoc)
100 	 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
101 	 */
102 	@Override
103 	public DataConsumer getDataConsumer(Class type)
104 	{
105 		if (type == DoubleConsumer.class || type == DoubleSeqConsumer.class) {
106 			return this;
107 		}
108 		
109 		return DoubleConsumerMulticaster.createDataConsumer(type, this);
110 	}
111 	
112 	/*
113 	 * (non-Javadoc)
114 	 * @see com.cosylab.gui.DialKnobDisplayer#setCharacteristics(java.util.Map)
115 	 */
116 	@Override
117 	public void setCharacteristics(Map characteristics) {
118 		super.setCharacteristics(characteristics);
119 		
120 		Object o  = characteristics.get("arrayType");
121 		if (o != null) {
122 			if (((TArrayType)o).isChannel()) {
123 				try {
124 					UserSettingsProtection.setUnprotected(this,"arrayIndex",0);
125 				} catch (Exception e) {
126 					e.printStackTrace();
127 				}
128 			}
129 		}
130 	}
131 	
132 	/*
133 	 * (non-Javadoc)
134 	 * @see de.desy.acop.displayers.AcopDisplayerConnector#getConnectionParameters()
135 	 */
136 	public ConnectionParameters getConnectionParameters() {
137 		return connectionParameters;
138 	}
139 	
140 	/*
141 	 * (non-Javadoc)
142 	 * @see de.desy.acop.displayers.AcopDisplayerConnector#setConnectionParameters(de.desy.acop.transport.ConnectionParameters)
143 	 */
144 	public void setConnectionParameters(ConnectionParameters param) throws CommonException, PropertyVetoException {
145 		if (param!=null && connectionParameters != null) {
146 			if(param.equals(connectionParameters)) return;
147 		}
148 		updateDataState(new DataState(DataState.UNDEFINED));
149 			
150 		ConnectionParameters old = connectionParameters;
151 		this.connectionParameters = param;
152 		AdapterFactory factory = AdapterFactoryService.getInstance().getAdapterFactory();
153 		if (getDataSource() != null)
154 			factory.releaseDataSource((AcopTransportDataSource) getDataSource());
155 		if (param == null) {
156 			setDataSource(null);
157 			setTitle("No Connection");
158 		} else {
159 			if (param.getPropertySize() == ConnectionParameters.AUTO_PROPERTY_SIZE) {
160 				param = param.deriveWithPropertySize(1);
161 			}
162 			AcopTransportDataSource ds = factory.createDataSource(param);
163 			setDataSource(ds);
164 		}
165 		
166 		firePropertyChange(CONNECTION_PARAMETERS_PROPERTY,old, connectionParameters);
167 	}
168 
169 	/*
170 	 * (non-Javadoc)
171 	 * @see de.desy.acop.displayers.tools.AcopDisplayer#getArrayIndex()
172 	 */
173 	public int getArrayIndex() {
174 		return arrayIndex;
175 	}
176 
177 	/*
178 	 * (non-Javadoc)
179 	 * @see de.desy.acop.displayers.tools.AcopDisplayer#setArrayIndex(int)
180 	 */
181 	public void setArrayIndex(int arrayIndex) {
182 		if (this.arrayIndex == arrayIndex) return;
183 		int oldValue = this.arrayIndex;
184 		this.arrayIndex = arrayIndex;
185 		firePropertyChange("arrayIndex", oldValue, this.arrayIndex);
186 	}
187 
188 	/*
189 	 * (non-Javadoc)
190 	 * @see com.cosylab.gui.WheelswitchDisplayer#getInfoDialog()
191 	 */
192 	@Override
193 	public InfoDialog getInfoDialog() {
194 		if (dialog == null) {
195 			dialog = new AcopInfoDialog(this);
196 		}
197 		return dialog;
198 	}
199 	
200 	/**
201 	 * Enables/disables Properties item in the popup menu.
202 	 * @param enable
203 	 */
204 	public void setPropertiesPopupEnabled(boolean enable) {
205 		Action[] actions = getPopupManager().getActions();
206 		for (Action a : actions) {
207 			if (a != null && "Preferences...".equals(a.getValue(Action.NAME))) {
208 				a.setEnabled(enable);
209 				return;
210 			}
211 		}
212 	}
213 	
214 	/**
215 	 * Returns true if Properties item is enabled in the popup menu.
216 	 * @return
217 	 */
218 	public boolean isPropertiesPopupEnabled() {
219 		Action[] actions = getPopupManager().getActions();
220 		for (Action a : actions) {
221 			if (a != null && "Preferences...".equals(a.getValue(Action.NAME))) {
222 				return a.isEnabled();
223 			}
224 		}
225 		return false;
226 	}
227 }
228 /* __oOo__ */