1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
77
78
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 public void vetoableChange(PropertyChangeEvent theEvt) throws PropertyVetoException {
168
169
170
171
172 if (theEvt.getPropertyName() == AbstractConnection.NEW_MESSAGES_PROPERTY) {
173 Integer oldValue = (Integer) theEvt.getOldValue();
174 Integer newValue = (Integer) theEvt.getNewValue();
175 if (oldValue != null && newValue != null && newValue > oldValue) {
176 throw new PropertyVetoException("", theEvt);
177 }
178 }
179 }
180 };
181 myConnection.addVetoableyChangeListener(AbstractConnection.NEW_MESSAGES_PROPERTY, myNewMessagesPropertyListener);
182
183
184 myConnectionsListener = new PropertyChangeListener() {
185 public void propertyChange(PropertyChangeEvent theEvt) {
186 myConnectionsTableModel.update();
187 }
188 };
189 theConnection.addPropertyChangeListener(InboundConnection.CONNECTIONS_PROPERTY, myConnectionsListener);
190 myConnectionsTableModel.update();
191
192 myNameListener = new PropertyChangeListener() {
193 public void propertyChange(PropertyChangeEvent theEvt) {
194 updateWindowTitle();
195 }
196 };
197 theConnection.addPropertyChangeListener(InboundConnection.NAME_PROPERTY, myNameListener);
198 updateWindowTitle();
199
200 if (theConnection.getStatus() == StatusEnum.STARTED || theConnection.getStatus() == StatusEnum.TRYING_TO_START) {
201 myTabbedPane.setSelectedIndex(1);
202 } else {
203 myTabbedPane.setSelectedIndex(0);
204 }
205
206 myStatusPropertyChangeListener = new PropertyChangeListener() {
207 public void propertyChange(PropertyChangeEvent theEvt) {
208 StatusEnum oldVal = (StatusEnum) theEvt.getOldValue();
209 StatusEnum newVal = (StatusEnum) theEvt.getNewValue();
210 if (oldVal == StatusEnum.STOPPED && (newVal == StatusEnum.TRYING_TO_START || newVal == StatusEnum.STARTED)) {
211 myTabbedPane.setSelectedIndex(1);
212 } else if ((oldVal == StatusEnum.TRYING_TO_START || oldVal == StatusEnum.STARTED) && newVal == StatusEnum.STOPPED) {
213 myTabbedPane.setSelectedIndex(0);
214 }
215 }
216 };
217 myConnection.addPropertyChangeListener(AbstractConnection.STATUS_PROPERTY, myStatusPropertyChangeListener);
218 }
219
220 private void updateWindowTitle() {
221 setWindowTitle(myConnection.getName());
222 }
223
224 private class ConnectionsTableModel implements TableModel {
225
226 private List<TableModelListener> myTableListeners = new ArrayList<TableModelListener>();
227
228 public ConnectionsTableModel() {
229 }
230
231 public void addTableModelListener(TableModelListener theL) {
232 myTableListeners.add(theL);
233 }
234
235 public Class<?> getColumnClass(int theColumnIndex) {
236 return String.class;
237 }
238
239 public int getColumnCount() {
240 return 1;
241 }
242
243 public String getColumnName(int theColumnIndex) {
244 return "Address";
245 }
246
247 public int getRowCount() {
248 return myConnection.getConnections().size();
249 }
250
251 public Object getValueAt(int theRowIndex, int theColumnIndex) {
252 InetAddress address = myConnection.getConnections().get(theRowIndex).getRemoteAddress();
253 int port = myConnection.getConnections().get(theRowIndex).getRemotePort();
254 return address.getCanonicalHostName() + ":" + port;
255 }
256
257 public boolean isCellEditable(int theRowIndex, int theColumnIndex) {
258 return false;
259 }
260
261 public void removeTableModelListener(TableModelListener theL) {
262 myTableListeners.remove(theL);
263 }
264
265 public void setValueAt(Object theAValue, int theRowIndex, int theColumnIndex) {
266 throw new UnsupportedOperationException();
267 }
268
269 public void update() {
270 for (TableModelListener next : myTableListeners) {
271 TableModelEvent event = new TableModelEvent(this);
272 next.tableChanged(event);
273 }
274 }
275
276 }
277
278 }