View Javadoc

1   /**
2    * The contents of this file are subject to the Mozilla Public License Version 1.1
3    * (the "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    * Software distributed under the License is distributed on an "AS IS" basis,
6    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    * specific language governing rights and limitations under the License.
8    *
9    * The Original Code is ""  Description:
10   * ""
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2001.  All Rights Reserved.
14   *
15   * Contributor(s): ______________________________________.
16   *
17   * Alternatively, the contents of this file may be used under the terms of the
18   * GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
19   * applicable instead of those above.  If you wish to allow use of your version of this
20   * file only under the terms of the GPL and not to allow others to use your version
21   * of this file under the MPL, indicate your decision by deleting  the provisions above
22   * and replace  them with the notice and other provisions required by the GPL License.
23   * If you do not delete the provisions above, a recipient may use your version of
24   * this file under either the MPL or the GPL.
25   */
26  package ca.uhn.hl7v2.testpanel.ui.conn;
27  
28  import java.awt.BorderLayout;
29  import java.awt.GridBagConstraints;
30  import java.awt.GridBagLayout;
31  import java.awt.Insets;
32  import java.beans.PropertyChangeEvent;
33  import java.beans.PropertyChangeListener;
34  import java.beans.PropertyVetoException;
35  import java.beans.VetoableChangeListener;
36  import java.net.InetAddress;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import javax.swing.JLabel;
41  import javax.swing.JPanel;
42  import javax.swing.JScrollPane;
43  import javax.swing.JSplitPane;
44  import javax.swing.JTabbedPane;
45  import javax.swing.JTable;
46  import javax.swing.ScrollPaneConstants;
47  import javax.swing.event.TableModelEvent;
48  import javax.swing.event.TableModelListener;
49  import javax.swing.table.TableModel;
50  
51  import ca.uhn.hl7v2.testpanel.controller.Controller;
52  import ca.uhn.hl7v2.testpanel.model.conn.AbstractConnection;
53  import ca.uhn.hl7v2.testpanel.model.conn.AbstractConnection.StatusEnum;
54  import ca.uhn.hl7v2.testpanel.model.conn.InboundConnection;
55  import ca.uhn.hl7v2.testpanel.ui.ActivityTable;
56  import ca.uhn.hl7v2.testpanel.ui.BaseMainPanel;
57  import ca.uhn.hl7v2.testpanel.ui.IDestroyable;
58  
59  public class InboundConnectionPanel extends BaseMainPanel implements IDestroyable {
60  
61  	private ActivityTable myActivityTable;
62  	private InboundConnection myConnection;
63  	private PropertyChangeListener myConnectionsListener;
64  	private JTable myConnectionsTable;
65  	private ConnectionsTableModel myConnectionsTableModel;
66  	private Hl7ConnectionPanel mySettingPanelTab;
67  	private PropertyChangeListener myNameListener;
68  	private Hl7ConnectionPanelHeader myHeaderPanel;
69  	private JSplitPane myActivitySplitPaneTab;
70  	private JTabbedPane myTabbedPane;
71  	private PropertyChangeListener myStatusPropertyChangeListener;
72  	private ValidationHeaderPanel myValidationPanel;
73  	private VetoableChangeListener myNewMessagesPropertyListener;
74  
75  	/**
76  	 * Create the panel.
77  	 * 
78  	 * @param theController
79  	 */
80  	public InboundConnectionPanel(Controller theController) {
81  		GridBagLayout gridBagLayout = new GridBagLayout();
82  		gridBagLayout.columnWidths = new int[] { 559, 0 };
83  		gridBagLayout.rowHeights = new int[] { 0, 0, 315, 0 };
84  		gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
85  		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 1.0, Double.MIN_VALUE };
86  		setLayout(gridBagLayout);
87  
88  		myHeaderPanel = new Hl7ConnectionPanelHeader();
89  		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
90  		gbc_panel_2.insets = new Insets(0, 0, 5, 0);
91  		gbc_panel_2.fill = GridBagConstraints.BOTH;
92  		gbc_panel_2.gridx = 0;
93  		gbc_panel_2.gridy = 0;
94  		add(myHeaderPanel, gbc_panel_2);
95  
96  		myValidationPanel = new ValidationHeaderPanel(theController);
97  		GridBagConstraints gbc_ValidationPanel = new GridBagConstraints();
98  		gbc_ValidationPanel.insets = new Insets(0, 0, 5, 0);
99  		gbc_ValidationPanel.fill = GridBagConstraints.BOTH;
100 		gbc_ValidationPanel.gridx = 0;
101 		gbc_ValidationPanel.gridy = 1;
102 		add(myValidationPanel, gbc_ValidationPanel);
103 
104 		myTabbedPane = new JTabbedPane(JTabbedPane.TOP);
105 		GridBagConstraints gbc_TabbedPane = new GridBagConstraints();
106 		gbc_TabbedPane.fill = GridBagConstraints.BOTH;
107 		gbc_TabbedPane.gridx = 0;
108 		gbc_TabbedPane.gridy = 2;
109 		add(myTabbedPane, gbc_TabbedPane);
110 		mySettingPanelTab = new Hl7ConnectionPanel(theController);
111 		myTabbedPane.addTab("Settings", null, mySettingPanelTab, null);
112 		mySettingPanelTab.setBorder(null);
113 
114 		myActivitySplitPaneTab = new JSplitPane();
115 		myTabbedPane.addTab("Activity", null, myActivitySplitPaneTab, null);
116 		myActivitySplitPaneTab.setResizeWeight(0.3);
117 
118 		JPanel panel_1 = new JPanel();
119 		myActivitySplitPaneTab.setLeftComponent(panel_1);
120 		panel_1.setLayout(new BorderLayout(0, 0));
121 
122 		JLabel lblConnections = new JLabel("Connections");
123 		panel_1.add(lblConnections, BorderLayout.NORTH);
124 
125 		JScrollPane scrollPane = new JScrollPane();
126 		panel_1.add(scrollPane, BorderLayout.CENTER);
127 
128 		myConnectionsTableModel = new ConnectionsTableModel();
129 		myConnectionsTable = new JTable();
130 		myConnectionsTable.setModel(myConnectionsTableModel);
131 		scrollPane.setViewportView(myConnectionsTable);
132 
133 		JPanel panel = new JPanel();
134 		myActivitySplitPaneTab.setRightComponent(panel);
135 		panel.setLayout(new BorderLayout(0, 0));
136 
137 		JLabel lblActivity = new JLabel("Activity");
138 		panel.add(lblActivity, BorderLayout.NORTH);
139 
140 		myActivityTable = new ActivityTable();
141 		myActivityTable.getScrollPane().setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
142 		myActivityTable.getScrollPane().setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
143 		myActivityTable.setController(theController);
144 		panel.add(myActivityTable, BorderLayout.CENTER);
145 	}
146 
147 	public void destroy() {
148 		mySettingPanelTab.destroy();
149 		myConnection.removePropertyChangeListener(InboundConnection.CONNECTIONS_PROPERTY, myConnectionsListener);
150 		myConnection.removePropertyChangeListener(InboundConnection.NAME_PROPERTY, myNameListener);
151 		myActivityTable.destroy();
152 		myHeaderPanel.destroy();
153 		myValidationPanel.destroy();
154 		myConnection.removePropertyChangeListener(AbstractConnection.STATUS_PROPERTY, myStatusPropertyChangeListener);
155 		myConnection.removeVetoableChangeListener(AbstractConnection.NEW_MESSAGES_PROPERTY, myNewMessagesPropertyListener);
156 	}
157 
158 	public void setConnection(InboundConnection theConnection) {
159 		mySettingPanelTab.setConnection(theConnection);
160 		myHeaderPanel.setConnection(theConnection);
161 		myConnection = theConnection;
162 		myActivityTable.setConnection(theConnection);
163 		myValidationPanel.setConnection(theConnection);
164 
165 		theConnection.clearNewMessages();
166 		myNewMessagesPropertyListener = new VetoableChangeListener() {
167 			@Override
168 			public void vetoableChange(PropertyChangeEvent theEvt) throws PropertyVetoException {
169 				/*
170 				 * This window displays messages as they arrive, so the message model
171 				 * object doesn't need to accumulate the count
172 				 */
173 				if (theEvt.getPropertyName() == AbstractConnection.NEW_MESSAGES_PROPERTY) {
174 					Integer oldValue = (Integer) theEvt.getOldValue();
175 					Integer newValue = (Integer) theEvt.getNewValue();
176 					if (oldValue != null && newValue != null && newValue > oldValue) {
177 						throw new PropertyVetoException("", theEvt);
178 					}
179 				}
180 			}
181 		};
182 		myConnection.addVetoableyChangeListener(AbstractConnection.NEW_MESSAGES_PROPERTY, myNewMessagesPropertyListener);
183 		
184 
185 		myConnectionsListener = new PropertyChangeListener() {
186 			public void propertyChange(PropertyChangeEvent theEvt) {
187 				myConnectionsTableModel.update();
188 			}
189 		};
190 		theConnection.addPropertyChangeListener(InboundConnection.CONNECTIONS_PROPERTY, myConnectionsListener);
191 		myConnectionsTableModel.update();
192 
193 		myNameListener = new PropertyChangeListener() {
194 			public void propertyChange(PropertyChangeEvent theEvt) {
195 				updateWindowTitle();
196 			}
197 		};
198 		theConnection.addPropertyChangeListener(InboundConnection.NAME_PROPERTY, myNameListener);
199 		updateWindowTitle();
200 
201 		if (theConnection.getStatus() == StatusEnum.STARTED || theConnection.getStatus() == StatusEnum.TRYING_TO_START) {
202 			myTabbedPane.setSelectedIndex(1);
203 		} else {
204 			myTabbedPane.setSelectedIndex(0);
205 		}
206 
207 		myStatusPropertyChangeListener = new PropertyChangeListener() {
208 			public void propertyChange(PropertyChangeEvent theEvt) {
209 				StatusEnum oldVal = (StatusEnum) theEvt.getOldValue();
210 				StatusEnum newVal = (StatusEnum) theEvt.getNewValue();
211 				if (oldVal == StatusEnum.STOPPED && (newVal == StatusEnum.TRYING_TO_START || newVal == StatusEnum.STARTED)) {
212 					myTabbedPane.setSelectedIndex(1);
213 				} else if ((oldVal == StatusEnum.TRYING_TO_START || oldVal == StatusEnum.STARTED) && newVal == StatusEnum.STOPPED) {
214 					myTabbedPane.setSelectedIndex(0);
215 				}
216 			}
217 		};
218 		myConnection.addPropertyChangeListener(AbstractConnection.STATUS_PROPERTY, myStatusPropertyChangeListener);
219 	}
220 
221 	private void updateWindowTitle() {
222 		setWindowTitle(myConnection.getName());
223 	}
224 
225 	private class ConnectionsTableModel implements TableModel {
226 
227 		private List<TableModelListener> myTableListeners = new ArrayList<TableModelListener>();
228 
229 		public ConnectionsTableModel() {
230 		}
231 
232 		public void addTableModelListener(TableModelListener theL) {
233 			myTableListeners.add(theL);
234 		}
235 
236 		public Class<?> getColumnClass(int theColumnIndex) {
237 			return String.class;
238 		}
239 
240 		public int getColumnCount() {
241 			return 1;
242 		}
243 
244 		public String getColumnName(int theColumnIndex) {
245 			return "Address";
246 		}
247 
248 		public int getRowCount() {
249 			return myConnection.getConnections().size();
250 		}
251 
252 		public Object getValueAt(int theRowIndex, int theColumnIndex) {
253 			InetAddress address = myConnection.getConnections().get(theRowIndex).getRemoteAddress();
254 			int port = myConnection.getConnections().get(theRowIndex).getRemotePort();
255 			return address.getCanonicalHostName() + ":" + port;
256 		}
257 
258 		public boolean isCellEditable(int theRowIndex, int theColumnIndex) {
259 			return false;
260 		}
261 
262 		public void removeTableModelListener(TableModelListener theL) {
263 			myTableListeners.remove(theL);
264 		}
265 
266 		public void setValueAt(Object theAValue, int theRowIndex, int theColumnIndex) {
267 			throw new UnsupportedOperationException();
268 		}
269 
270 		public void update() {
271 			for (TableModelListener next : myTableListeners) {
272 				TableModelEvent event = new TableModelEvent(this);
273 				next.tableChanged(event);
274 			}
275 		}
276 
277 	}
278 
279 }