View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans.
5    *
6    * CosyBeans is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package de.desy.acop.video;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Component;
24  import java.awt.GridBagConstraints;
25  import java.awt.GridBagLayout;
26  import java.awt.Insets;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.awt.event.WindowAdapter;
30  import java.awt.event.WindowEvent;
31  import java.beans.PropertyChangeEvent;
32  import java.beans.PropertyChangeListener;
33  
34  import javax.swing.JButton;
35  import javax.swing.JDialog;
36  import javax.swing.JFrame;
37  import javax.swing.JPanel;
38  import javax.swing.JTabbedPane;
39  
40  import com.cosylab.gui.infopanel.PropertiesPanel;
41  
42  import de.desy.acop.displayers.info.AcopDebugPanel;
43  import de.desy.acop.displayers.info.CharacteristicsPanel;
44  import de.desy.acop.displayers.info.ConnectionPanel;
45  import de.desy.acop.displayers.tools.AcopDisplayer;
46  import de.desy.acop.displayers.tools.AcopInfoDialog;
47  import de.desy.acop.displayers.tools.ConnectionParametersReceiver;
48  import de.desy.acop.transport.ConnectionFailed;
49  import de.desy.acop.transport.adapters.AcopTransportDataSource;
50  import de.desy.acop.transport.adapters.AdapterFactoryService;
51  import de.desy.tine.client.TLinkFactory;
52  
53  /**
54   * 
55   * <code>AcopVideoInfoDialog</code> is the info dialog for the AcopVideo. It offers
56   * the same functionality as ordinary {@link AcopInfoDialog}, however it does not
57   * extend it because the AcopVideo does not implement the {@link AcopDisplayer}
58   * and does not work with the {@link AcopTransportDataSource}.
59   *
60   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
61   * @version $Id: Templates.xml,v 1.10 2004/01/13 16:17:13 jbobnar Exp $
62   *
63   */
64  public class AcopVideoInfoDialog extends JDialog {
65  	
66  	private static final long serialVersionUID = 513076582058739594L;
67  	
68  	private JPanel contentPanel;
69  	private JTabbedPane tabPane;
70  	//java beans properties displayer
71  	private PropertiesPanel propertiesPanel;
72  	//acop connection information displayer
73  	private ConnectionPanel connectionPanel;
74  	//acop connection characteristics displayer
75  	private CharacteristicsPanel characteristicsPanel;
76  	//acop debug information
77  	private AcopDebugPanel acopDebugPanel;
78  	//the owner of this dialog
79  	private AcopVideo displayer;
80  	private JButton closeButton;
81  	
82  	private PropertyChangeListener propertyListener;
83  	
84  	/**
85  	 * Constructs a new AcopVideoInfoDialog which connects to the supplied displayer.
86  	 * 
87  	 * @param displayer
88  	 */
89  	public AcopVideoInfoDialog(AcopVideo displayer) {
90  		initialize();
91  		setDisplayer(displayer);		
92  	}
93  	
94  	private void initialize() {
95  		setSize(500,400);
96  		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
97  		this.setContentPane(getContentPanel());
98  		this.addWindowListener(new WindowAdapter(){
99  			@Override
100 			public void windowActivated(WindowEvent e) {
101 				getAcopDebugPanel().setDebug(TLinkFactory.getInstance().getDebugLevel());
102 			}
103 		});
104 		this.addPanel(getConnectionPanel(), "Connection", 0, true);
105 		this.addPanel(getCharacteristicsPanel(), "Prop Info", 1, false);
106 		this.addPanel(getAcopDebugPanel(), "Debug", 2, false);
107 		this.addPanel(getPropertiesPanel(), "Bean properties", 3, false);
108 	}
109 	
110 	private ConnectionPanel getConnectionPanel() {
111 		if (connectionPanel == null) {
112 			connectionPanel = new ConnectionPanel();
113 		}
114 		return connectionPanel;
115 	}
116 	
117 	private CharacteristicsPanel getCharacteristicsPanel() {
118 		if (characteristicsPanel == null) {
119 			characteristicsPanel = new CharacteristicsPanel();
120 		}
121 		return characteristicsPanel;
122 	}
123 	
124 	private AcopDebugPanel getAcopDebugPanel() {
125 		if (acopDebugPanel == null) {
126 			acopDebugPanel = new AcopDebugPanel();
127 		}
128 		return acopDebugPanel;
129 	}
130 	
131 	private JPanel getContentPanel() {
132 		if (contentPanel == null) {
133 			contentPanel = new JPanel(new BorderLayout());
134 			contentPanel.add(getTabPane(), BorderLayout.CENTER);
135 			JPanel buttonPanel = new JPanel(new GridBagLayout());
136 			buttonPanel.add(getCloseButton(), new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
137 			contentPanel.add(buttonPanel, BorderLayout.SOUTH);
138 			
139 		}
140 		return contentPanel;
141 	}
142 	
143 	private JButton getCloseButton() {
144 		if (closeButton == null) {
145 			closeButton = new JButton("Close");
146 			closeButton.addActionListener(new ActionListener() {
147 				public void actionPerformed(ActionEvent e) {
148 					dispose();					
149 				}
150 			});
151 		}
152 		return closeButton;
153 	}
154 	
155 	private JTabbedPane getTabPane() {
156 		if (tabPane == null) {
157 			tabPane = new JTabbedPane();
158 		}
159 		return tabPane;
160 	}
161 	
162 	protected PropertiesPanel getPropertiesPanel() {
163 		if (propertiesPanel == null) {
164 			propertiesPanel = new PropertiesPanel();
165 		}
166 
167 		return propertiesPanel;
168 	}
169 	
170 	protected void addPanel(Component panel, String name, int position, boolean select) {
171 		for (int i = 0; i < getTabPane().getTabCount(); i++) {
172 			if (getTabPane().getTitleAt(i).equals(name)) {
173 				getTabPane().removeTabAt(i);
174 				break;
175 			}
176 		}
177 		getTabPane().add(panel, name, position);
178 		if (select) {
179 			getTabPane().setSelectedComponent(panel);
180 		}
181 	}
182 	
183 	protected void addPanel(Component panel, String name) {
184 		for (int i = 0; i < getTabPane().getTabCount(); i++) {
185 			if (getTabPane().getTitleAt(i).equals(name)) {
186 				getTabPane().removeTabAt(i);
187 				break;
188 			}
189 		}
190 		getTabPane().add(panel, name);
191 	}
192 	
193 	/**
194 	 * Hooks this panel to the specified displayer.
195 	 * 
196 	 * @param displayer
197 	 */
198 	public void setDisplayer(AcopVideo displayer) {	
199 		if (this.displayer != null && !this.displayer.equals(displayer)) {
200 			this.displayer.removePropertyChangeListener(getPropertyListener());
201 			if (displayer != null) {
202 				displayer.addPropertyChangeListener(getPropertyListener());
203 			}
204 		} else if (this.displayer == null && displayer != null) {
205 			displayer.addPropertyChangeListener(getPropertyListener());
206 		}
207 		this.displayer = displayer;
208 		if (this.displayer != null) {
209 			this.setTitle(this.displayer.getName());
210 			getPropertiesPanel().setBean(displayer);
211 			setTitle("Info for "+getDisplayer().getClass().getSimpleName());
212 			getConnectionPanel().setConnectionParameters(getDisplayer().getConnectionParameters());
213 			getAcopDebugPanel().setConnectionParameters(getDisplayer().getConnectionParameters());
214 			try {
215 				getCharacteristicsPanel().setCharacteristics(AdapterFactoryService.getInstance().getAdapterFactory().getCharacteristics(getDisplayer().getConnectionParameters()));
216 			} catch (ConnectionFailed e) {
217 				e.printStackTrace();
218 			}
219 		} else {
220 			setTitle("Info dialog");
221 			getPropertiesPanel().setBean(new Object());
222 			getConnectionPanel().setConnectionParameters(null);
223 			getAcopDebugPanel().setConnectionParameters(null);
224 			getCharacteristicsPanel().setCharacteristics(null);
225 		}
226 		getAcopDebugPanel().setDebug(TLinkFactory.getInstance().getDebugLevel());
227 	}
228 	
229 	private PropertyChangeListener getPropertyListener() {
230 		if (propertyListener == null) {
231 			propertyListener = new PropertyChangeListener() {
232 				public void propertyChange(PropertyChangeEvent evt) {
233 					if (ConnectionParametersReceiver.CONNECTION_PARAMETERS_PROPERTY.equals(evt.getPropertyName())) {
234 						getConnectionPanel().setConnectionParameters(getDisplayer().getConnectionParameters());
235 						getAcopDebugPanel().setConnectionParameters(getDisplayer().getConnectionParameters());
236 						try {
237 							getCharacteristicsPanel().setCharacteristics(AdapterFactoryService.getInstance().getAdapterFactory().getCharacteristics(getDisplayer().getConnectionParameters()));
238 						} catch (ConnectionFailed e) {
239 							e.printStackTrace();
240 						}
241 					}
242 				}
243 			};
244 		}
245 		return propertyListener;
246 	}
247 	
248 	/**
249 	 * Returns the displayer connected with this InfoPanel.
250 	 * 
251 	 * @return
252 	 */
253 	public AcopVideo getDisplayer() {
254 		return this.displayer;
255 	}	
256 }