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.tools;
24  
25  import java.awt.BorderLayout;
26  import java.awt.Component;
27  import java.awt.Dimension;
28  import java.awt.GridBagConstraints;
29  import java.awt.GridBagLayout;
30  import java.awt.Insets;
31  import java.awt.event.ActionEvent;
32  import java.awt.event.ActionListener;
33  import java.beans.PropertyChangeEvent;
34  import java.beans.PropertyChangeListener;
35  
36  import javax.swing.DefaultListCellRenderer;
37  import javax.swing.JButton;
38  import javax.swing.JComboBox;
39  import javax.swing.JDialog;
40  import javax.swing.JFrame;
41  import javax.swing.JLabel;
42  import javax.swing.JList;
43  import javax.swing.JPanel;
44  import javax.swing.JTabbedPane;
45  import javax.swing.ListCellRenderer;
46  
47  import com.cosylab.gui.displayers.MultipleDisplayer;
48  import com.cosylab.gui.infopanel.ConverterPanel;
49  import com.cosylab.gui.infopanel.PropertiesPanel;
50  
51  import de.desy.acop.displayers.info.AcopDebugPanel;
52  import de.desy.acop.displayers.info.CharacteristicsPanel;
53  import de.desy.acop.displayers.info.ConnectionPanel;
54  import de.desy.acop.transport.ConnectionFailed;
55  import de.desy.acop.transport.adapters.AdapterFactoryService;
56  
57  /**
58   * <code>AcopMultipleDisplayerInfoDialog</code> presents information
59   * about the <code>AcopMultiplDisplayer</code>. All information 
60   * in this window is read only and designated for debugging. This dialog
61   * will show connection information and bean properties.
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 AcopMultipleDisplayerInfoDialog extends JDialog {
68  	
69  	private static final long serialVersionUID = 513076582058739594L;
70  		
71  	private JPanel contentPanel;
72  	private JTabbedPane tabPane;
73  	private ConverterPanel converterPanel;
74  	private PropertiesPanel propertiesPanel;
75  	private AcopDebugPanel debugPanel;
76  	private CharacteristicsPanel characteristicsPanel;
77  	private ConnectionPanel connectionPanel;
78  	private MultipleAcopDisplayer<?>displayer;
79  	private JButton closeButton;
80  	private JComboBox parametersComboBox;
81  	private AcopDisplayerParameters selectedParameters;
82  	private JLabel connectionParametersLabel;
83  	private JPanel selectorPanel;
84  	private PropertyChangeListener propertyListener;
85  	
86  	private JTabbedPane connectionTab;
87  	private JPanel connectionContentPanel;
88  	
89  	/**
90  	 * Constructs a new AcopMultipleDisplayerInfoDialog.
91  	 *
92  	 */
93  	public AcopMultipleDisplayerInfoDialog() {
94  		this(null);
95  	}
96  	
97  	/**
98  	 * Constructs a new AcopMultipleDisplayerInfoDialog associated with the given
99  	 * displayer.
100 	 * 
101 	 * @param displayer displayer to be associated with this dialog
102 	 */
103 	public AcopMultipleDisplayerInfoDialog(MultipleAcopDisplayer<?> displayer) {
104 		initialize();
105 		setDisplayer(displayer);
106 		addPanel(getConnectionContentPanel(), "Connection", 0, true);
107 		addPanel(getPropertiesPanel(), "Bean properties", 1, false);
108 		addPanel(getAcopDebugPanel(), "Debug", 2, false);
109 		
110 	}
111 	
112 	private void initialize() {
113 		this.setSize(550,350);
114 		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
115 		this.setContentPane(getContentPanel());
116 	}
117 	
118 	private JPanel getConnectionContentPanel() {
119 		if (connectionContentPanel == null) {
120 			connectionContentPanel = new JPanel(new BorderLayout());
121 			connectionContentPanel.add(getSelectorPanel(), BorderLayout.NORTH);
122 			connectionContentPanel.add(getConnectionTab(), BorderLayout.CENTER);
123 		}
124 
125 		return connectionContentPanel;
126 	}
127 	
128 	
129 	private JTabbedPane getConnectionTab() {
130 		if (connectionTab == null) {
131 			connectionTab = new JTabbedPane();
132 			connectionTab.add(getConnectionPanel(), "Connection", 0);
133 			connectionTab.add(getCharacteristicsPanel(), "Prop Info", 1);
134 			connectionTab.add(getConverterPanel(),"Converter", 2);
135 			connectionTab.setSelectedComponent(getConnectionPanel());
136 			
137 		}
138 		return connectionTab;
139 	}
140 	
141 	private AcopDebugPanel getAcopDebugPanel() {
142 		if (debugPanel == null) {
143 			debugPanel = new AcopDebugPanel();
144 		}
145 		return debugPanel;
146 	}
147 	
148 	private ConnectionPanel getConnectionPanel() {
149 		if (connectionPanel == null) {
150 			connectionPanel = new ConnectionPanel();
151 		}
152 		return connectionPanel;
153 	}
154 	
155 	private CharacteristicsPanel getCharacteristicsPanel() {
156 		if (characteristicsPanel == null) {
157 			characteristicsPanel = new CharacteristicsPanel();
158 		}
159 		return characteristicsPanel;
160 	}
161 	
162 	private JPanel getSelectorPanel() {
163 		if (selectorPanel == null) {
164 			selectorPanel = new JPanel(new GridBagLayout());
165 			connectionParametersLabel = new JLabel("Select parameters: ");
166 			connectionParametersLabel.setMaximumSize(new Dimension(70,21));
167 			connectionParametersLabel.setMaximumSize(new Dimension(70,21));
168 			connectionParametersLabel.setMaximumSize(new Dimension(70,21));
169 			selectorPanel.add(connectionParametersLabel, new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2,2,2,2), 0,0));
170 			selectorPanel.add(getParametersCombo(), new GridBagConstraints(1,0,1,1,1,0,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0,0));
171 		}
172 		return selectorPanel;
173 	}
174 	
175 	private JPanel getContentPanel() {
176 		if (contentPanel == null) {
177 			contentPanel = new JPanel(new BorderLayout());
178 			contentPanel.add(getTabPane(), BorderLayout.CENTER);
179 			JPanel buttonPanel = new JPanel(new GridBagLayout());
180 			buttonPanel.add(getCloseButton(), new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
181 			contentPanel.add(buttonPanel, BorderLayout.SOUTH);
182 			
183 		}
184 		return contentPanel;
185 	}
186 	
187 	private JComboBox getParametersCombo() {
188 		if (parametersComboBox == null) {
189 			parametersComboBox = new JComboBox();
190 			parametersComboBox.setMinimumSize(new Dimension(100, 21));
191 			parametersComboBox.setMaximumSize(new Dimension(100, 21));
192 			parametersComboBox.setPreferredSize(new Dimension(100, 21));
193 			parametersComboBox.setRenderer(new ComboRenderer());
194 			parametersComboBox.addActionListener(new ActionListener() {
195 				public void actionPerformed(ActionEvent e) {
196 					setSelectedParameters((AcopDisplayerParameters)parametersComboBox.getSelectedItem());					
197 				}
198 			});
199 			
200 		}
201 		return parametersComboBox;
202 	}
203 	
204 	private JButton getCloseButton() {
205 		if (closeButton == null) {
206 			closeButton = new JButton("Close");
207 			closeButton.addActionListener(new ActionListener() {
208 				public void actionPerformed(ActionEvent e) {
209 					dispose();					
210 				}
211 			});
212 		}
213 		return closeButton;
214 	}
215 	
216 	private JTabbedPane getTabPane() {
217 		if (tabPane == null) {
218 			tabPane = new JTabbedPane();
219 		}
220 		return tabPane;
221 	}
222 	
223 	private ConverterPanel getConverterPanel() {
224 		if (converterPanel == null) {
225 			converterPanel = new ConverterPanel();
226 		}
227 		return converterPanel;
228 	}
229 	
230 	private PropertiesPanel getPropertiesPanel() {
231 		if (propertiesPanel == null) {
232 			propertiesPanel = new PropertiesPanel();
233 		}
234 
235 		return propertiesPanel;
236 	}
237 	
238 	protected void addPanel(Component panel, String name, int position, boolean select) {
239 		for (int i = 0; i < getTabPane().getTabCount(); i++) {
240 			if (getTabPane().getTitleAt(i).equals(name)) {
241 				getTabPane().removeTabAt(i);
242 				break;
243 			}
244 		}
245 		getTabPane().add(panel, name, position);
246 		if (select) {
247 			getTabPane().setSelectedComponent(panel);
248 		}
249 	}
250 	
251 	protected void addPanel(Component panel, String name) {
252 		for (int i = 0; i < getTabPane().getTabCount(); i++) {
253 			if (getTabPane().getTitleAt(i).equals(name)) {
254 				getTabPane().removeTabAt(i);
255 				break;
256 			}
257 		}
258 		getTabPane().add(panel, name);
259 	}
260 	
261 	/**
262 	 * Sets the displayer to this info dialog. This component will listen
263 	 * to certain properties of the displayer and present their values.
264 	 * 
265 	 * @param displayer
266 	 */
267 	public void setDisplayer(MultipleAcopDisplayer<?> displayer) {
268 		if (this.displayer != null && !this.displayer.equals(displayer)) {
269 			this.displayer.removePropertyChangeListener(getPropertyListener());
270 			if (displayer != null) {
271 				displayer.addPropertyChangeListener(getPropertyListener());
272 			}
273 				
274 		} else if (this.displayer == null && displayer != null) {
275 			displayer.addPropertyChangeListener(getPropertyListener());
276 		}
277 		this.displayer = displayer;
278 		getParametersCombo().removeAllItems();
279 		if (this.displayer != null) {
280 			this.setTitle("Info dialog for " + this.displayer.getClass().getSimpleName() + ".");
281 			getPropertiesPanel().setBean(displayer);
282 			AcopDisplayerParameters[] parameters = this.displayer.getDisplayerParameters();
283 			
284 			if (parameters != null && parameters.length > 0) {
285 				for (AcopDisplayerParameters p :parameters) {
286 					getParametersCombo().addItem(p);
287 				}
288 				getParametersCombo().setSelectedIndex(0);
289 			}
290 		} else {
291 			this.setTitle("Info");
292 			getPropertiesPanel().setBean(new Object());
293 			setSelectedParameters(null);
294 		}
295 		
296 	}
297 	
298 	private void setSelectedParameters(AcopDisplayerParameters parameters) {
299 		this.selectedParameters = parameters;
300 		if (selectedParameters != null) {
301 			getConverterPanel().setConverter(parameters.getConverter());
302 			getConnectionPanel().setConnectionParameters(parameters.getConnectionParameters());
303 			getAcopDebugPanel().setConnectionParameters(parameters.getConnectionParameters());
304 			try {
305 				getCharacteristicsPanel().setCharacteristics(AdapterFactoryService.getInstance().getAdapterFactory().getCharacteristics(parameters.getConnectionParameters()));
306 			} catch (ConnectionFailed e) {
307 				e.printStackTrace();
308 			}
309 		} else {
310 			getConverterPanel().setConverter(null);
311 			getConnectionPanel().setConnectionParameters(null);
312 			getAcopDebugPanel().setConnectionParameters(null);
313 			getCharacteristicsPanel().setCharacteristics(null);
314 		}
315 		
316 	}
317 	
318 	private PropertyChangeListener getPropertyListener() {
319 		if (propertyListener == null) {
320 			propertyListener = new PropertyChangeListener() {
321 				public void propertyChange(PropertyChangeEvent evt) {
322 					if (MultipleAcopDisplayer.DISPLAYER_PARAMETERS_PROPERTY.equals(evt.getPropertyName())) {
323 						Object item = getParametersCombo().getSelectedItem();
324 						getParametersCombo().removeAllItems();
325 						AcopDisplayerParameters[] parameters = displayer.getDisplayerParameters();
326 						if (parameters != null && parameters.length > 0) {
327 							for (AcopDisplayerParameters p :parameters) {
328 								getParametersCombo().addItem(p);
329 							}
330 							getParametersCombo().setSelectedIndex(0);
331 						}
332 						getParametersCombo().setSelectedItem(item);
333 					}
334 				}
335 			};
336 		}
337 		return propertyListener;
338 	}
339 	
340 	public MultipleDisplayer getDisplayer() {
341 		return this.displayer;
342 	}
343 	
344 	private static class ComboRenderer extends DefaultListCellRenderer implements ListCellRenderer {
345 		
346 		private static final long serialVersionUID = 1L;
347 
348 		@Override
349 		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
350 			String text;
351 			if (value instanceof AcopDisplayerParameters) {
352 				text = ((AcopDisplayerParameters)value).getConnectionParameters().toString();
353 				if (((AcopDisplayerParameters)value).getConverter() != null) {
354 					text += ", Converter";
355 				}
356 				
357 				
358 			} else {
359 				text = "";
360 			}
361 			return super.getListCellRendererComponent(list, text, index, isSelected,
362 					cellHasFocus);
363 		}
364 		
365 	}
366 	
367 }