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