View Javadoc

1   package de.desy.acop.displayers;
2   
3   import java.beans.PropertyVetoException;
4   
5   import javax.swing.Action;
6   
7   import com.cosylab.gui.CheckBoxController;
8   import com.cosylab.gui.InfoDialog;
9   import com.cosylab.gui.displayers.DataConsumer;
10  import com.cosylab.gui.displayers.DataState;
11  import com.cosylab.gui.displayers.DisplayerUtilities;
12  import com.cosylab.gui.displayers.DoubleSeqConsumer;
13  import com.cosylab.gui.util.UserSettingsProtection;
14  import com.cosylab.util.CommonException;
15  
16  import de.desy.acop.displayers.tools.AcopDisplayer;
17  import de.desy.acop.displayers.tools.AcopDisplayerTransferHandler;
18  import de.desy.acop.displayers.tools.AcopInfoDialog;
19  import de.desy.acop.transport.ConnectionParameters;
20  import de.desy.acop.transport.adapters.AcopTransportDataSource;
21  import de.desy.acop.transport.adapters.AdapterFactory;
22  import de.desy.acop.transport.adapters.AdapterFactoryService;
23  
24  /**
25   * AcopCheckBox is a displayer for displaying and switching between two
26   * double values.
27   * <p>
28   * User can set two different values on <i>selectedValue</i>
29   * and <i>deselectedValue</i> properties. When remote connection point has
30   * value same as selectedValue, CheckBox appear selected; when remote value
31   * is same as deselectedValue, CheckBox appear not selected. If remote value
32   * is some else, than CheckBox switches to UNDEFINED state with cross drawn
33   * over the component.
34   * </p>
35   * <p>
36   * When user changes selection, than proper value is also sent to remote
37   * connection point. If new state is selected, selectedValue is sent; if new
38   * state is deselected, deselectedValue is sent.
39   * </p>
40   * <p>
41   * The connection point for this displayer should be set using the 
42   * <i>connectionParameters</i> property, where the remote name of the 
43   * {@link ConnectionParameters} points to the desired property.
44   * </p>
45   * 
46   * @author <a href="mailto:blaz.hostnik@cosylab.com">Blaz Hostnik, Cosylab</a>
47   */
48  public class AcopCheckBox extends CheckBoxController implements AcopDisplayer {
49  
50  	private static final long serialVersionUID = 1647491427169502213L;
51  
52  	private int arrayIndex=0;
53  
54  	private ConnectionParameters connectionParameters;
55  
56  	private InfoDialog dialog;
57  
58  	/**
59  	 * Default constructor: with "Enabled" text in front of the check box.
60  	 */
61  	public AcopCheckBox() {
62  		this(DEFAULT_TEXT);
63  	}
64  	
65  	/**
66  	 * Constructor with parameters.
67  	 * 
68  	 * @param text in front of the check box
69  	 */
70  	public AcopCheckBox(String title) {
71  		super();
72  		new AcopDisplayerTransferHandler(this);
73  		UserSettingsProtection.setProtection(this,DisplayerUtilities.COMMON_NUMERIC_DISPLAYER_PROPERTIES,false);
74  	}
75  	
76  	@Override
77  	public int getArrayIndex() {
78  		return arrayIndex;
79  	}
80  
81  	@Override
82  	public void setArrayIndex(int index) {
83  		if (arrayIndex == index) return;
84  		int oldValue = arrayIndex;
85  		arrayIndex = index;
86  		firePropertyChange("arrayIndex", oldValue, arrayIndex);
87  	}
88  
89  	@Override
90  	public InfoDialog getInfoDialog() {
91  		if (dialog == null) {
92  			dialog = new AcopInfoDialog(this);
93  		}
94  		return dialog;
95  	}
96  
97  	@Override
98  	public void updateValue(long timestamp, double[] value)
99  			throws CommonException {
100 	    updateValue(timestamp, DisplayerUtilities.extract(arrayIndex,value));	
101 	}
102 
103 	@SuppressWarnings("unchecked")
104 	@Override
105 	public DataConsumer getDataConsumer(Class type) {
106 		if (type == DoubleSeqConsumer.class){
107 			return this;
108 		} else return super.getDataConsumer(type);
109 	}
110 	
111 	@Override
112 	public ConnectionParameters getConnectionParameters() {
113 		return connectionParameters;
114 	}
115 
116 	@Override
117 	public void setConnectionParameters(ConnectionParameters param)
118 			throws CommonException, PropertyVetoException {
119 		if (param!=null && connectionParameters != null) {
120 			if(param.equals(connectionParameters)) return;
121 		}
122 		updateDataState(new DataState(DataState.UNDEFINED));
123 			
124 		ConnectionParameters old = connectionParameters;
125 		this.connectionParameters = param;
126 		AdapterFactory factory = AdapterFactoryService.getInstance().getAdapterFactory();
127 		if (getDataSource() != null)
128 			factory.releaseDataSource((AcopTransportDataSource) getDataSource());
129 		if (param == null) {
130 			setDataSource(null);
131 			setTitle("No Connection");
132 		} else {
133 			if (param.getPropertySize() == ConnectionParameters.AUTO_PROPERTY_SIZE) {
134 				param = param.deriveWithPropertySize(1);
135 			}
136 			AcopTransportDataSource ds = factory.createDataSource(param);
137 			setDataSource(ds);
138 		}
139 		
140 		firePropertyChange(CONNECTION_PARAMETERS_PROPERTY,old, connectionParameters);
141 	}
142 	
143 	/**
144 	 * Enables/disables Properties item in the popup menu.
145 	 * @param enable
146 	 */
147 	public void setPropertiesPopupEnabled(boolean enable) {
148 		Action[] actions = getPopupManager().getActions();
149 		for (Action a : actions) {
150 			if (a != null && "Preferences...".equals(a.getValue(Action.NAME))) {
151 				a.setEnabled(enable);
152 				return;
153 			}
154 		}
155 	}
156 	
157 	/**
158 	 * Returns true if Properties item is enabled in the popup menu.
159 	 * @return
160 	 */
161 	public boolean isPropertiesPopupEnabled() {
162 		Action[] actions = getPopupManager().getActions();
163 		for (Action a : actions) {
164 			if (a != null && "Preferences...".equals(a.getValue(Action.NAME))) {
165 				return a.isEnabled();
166 			}
167 		}
168 		return false;
169 	}
170 
171 
172 }