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.ButtonController;
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   * AcopButton is a displayer that sets desired value on remote connection 
26   * point when pressed. User can specify desired value in <i>actionValue</i>
27   * property.
28   * <p>
29   * The connection point for this displayer should be set using the 
30   * <i>connectionParameters</i> property, where the remote name of the 
31   * {@link ConnectionParameters} points to the desired property.
32   * </p>
33   * 
34   * @author <a href="mailto:blaz.hostnik@cosylab.com">Blaz Hostnik, Cosylab</a>
35   */
36  public class AcopButton extends ButtonController implements AcopDisplayer {
37  
38  	private static final long serialVersionUID = -1935682520691479179L;
39  	
40  	private int arrayIndex=0;
41  
42  	private ConnectionParameters connectionParameters;
43  
44  	private InfoDialog dialog;
45  
46  	/**
47  	 * Default constructor: with "Ok" text on button.
48  	 */
49  	public AcopButton() {
50  		this(DEFAULT_TEXT);
51  	}
52  	
53  	/**
54  	 * Constructor with parameters.
55  	 * 
56  	 * @param text on the button
57  	 */
58  	public AcopButton(String text) {
59  		super();
60  		new AcopDisplayerTransferHandler(this);
61  		UserSettingsProtection.setProtection(this,DisplayerUtilities.COMMON_NUMERIC_DISPLAYER_PROPERTIES,false);
62  	}
63  	
64  	@Override
65  	public int getArrayIndex() {
66  		return arrayIndex;
67  	}
68  
69  	@Override
70  	public void setArrayIndex(int index) {
71  		if (arrayIndex == index) return;
72  		int oldValue = arrayIndex;
73  		arrayIndex = index;
74  		firePropertyChange("arrayIndex", oldValue, arrayIndex);
75  	}
76  
77  	@Override
78  	public InfoDialog getInfoDialog() {
79  		if (dialog == null) {
80  			dialog = new AcopInfoDialog(this);
81  		}
82  		return dialog;
83  	}
84  
85  	@Override
86  	public void updateValue(long timestamp, double[] value)
87  			throws CommonException {
88  	}
89  
90  	@SuppressWarnings("unchecked")
91  	@Override
92  	public DataConsumer getDataConsumer(Class type) {
93  		if (type == DoubleSeqConsumer.class){
94  			return this;
95  		} else return super.getDataConsumer(type);
96  	}
97  	
98  	@Override
99  	public ConnectionParameters getConnectionParameters() {
100 		return connectionParameters;
101 	}
102 
103 	@Override
104 	public void setConnectionParameters(ConnectionParameters param)
105 			throws CommonException, PropertyVetoException {
106 		if (param!=null && connectionParameters != null) {
107 			if(param.equals(connectionParameters)) return;
108 		}
109 		updateDataState(new DataState(DataState.UNDEFINED));
110 			
111 		ConnectionParameters old = connectionParameters;
112 		this.connectionParameters = param;
113 		AdapterFactory factory = AdapterFactoryService.getInstance().getAdapterFactory();
114 		if (getDataSource() != null)
115 			factory.releaseDataSource((AcopTransportDataSource) getDataSource());
116 		if (param == null) {
117 			setDataSource(null);
118 			setTitle("No Connection");
119 		} else {
120 			if (param.getPropertySize() == ConnectionParameters.AUTO_PROPERTY_SIZE) {
121 				param = param.deriveWithPropertySize(1);
122 			}
123 			AcopTransportDataSource ds = factory.createDataSource(param);
124 			setDataSource(ds);
125 		}
126 		
127 		firePropertyChange(CONNECTION_PARAMETERS_PROPERTY,old, connectionParameters);
128 	}
129 	
130 	/**
131 	 * Enables/disables Properties item in the popup menu.
132 	 * @param enable
133 	 */
134 	public void setPropertiesPopupEnabled(boolean enable) {
135 		Action[] actions = getPopupManager().getActions();
136 		for (Action a : actions) {
137 			if (a != null && "Preferences...".equals(a.getValue(Action.NAME))) {
138 				a.setEnabled(enable);
139 				return;
140 			}
141 		}
142 	}
143 	
144 	/**
145 	 * Returns true if Properties item is enabled in the popup menu.
146 	 * @return
147 	 */
148 	public boolean isPropertiesPopupEnabled() {
149 		Action[] actions = getPopupManager().getActions();
150 		for (Action a : actions) {
151 			if (a != null && "Preferences...".equals(a.getValue(Action.NAME))) {
152 				return a.isEnabled();
153 			}
154 		}
155 		return false;
156 	}
157 
158 
159 }