View Javadoc
1   package ca.uhn.hl7v2.testpanel.ui.conn;
2   
3   import java.awt.Dimension;
4   import java.awt.GridBagConstraints;
5   import java.awt.GridBagLayout;
6   import java.awt.Insets;
7   import java.awt.event.ActionEvent;
8   import java.awt.event.ActionListener;
9   import java.beans.PropertyChangeEvent;
10  import java.beans.PropertyChangeListener;
11  
12  import javax.swing.DefaultComboBoxModel;
13  import javax.swing.JCheckBox;
14  import javax.swing.JComboBox;
15  import javax.swing.JPanel;
16  import javax.swing.border.TitledBorder;
17  
18  import ca.uhn.hl7v2.testpanel.controller.Controller;
19  import ca.uhn.hl7v2.testpanel.model.conf.ProfileFileList;
20  import ca.uhn.hl7v2.testpanel.model.conf.ProfileGroup;
21  import ca.uhn.hl7v2.testpanel.model.conn.InboundConnection;
22  import ca.uhn.hl7v2.testpanel.ui.IDestroyable;
23  
24  public class ValidationHeaderPanel extends JPanel implements IDestroyable {
25  	private InboundConnection myConnection;
26  	private Controller myController;
27  	private boolean myHandlingUpdate;
28  	private JComboBox myProfileGroupComboBox;
29  	private PropertyChangeListener myProfileGroupsListener;
30  	private DefaultComboBoxModel myProfileGroupsModel;
31  	private JCheckBox myValidateUsingProfileGroupCheckbox;
32  	private PropertyChangeListener myValidationListener;
33  	private PropertyChangeListener myStatusListener;
34  
35  	/**
36  	 * Create the panel.
37  	 */
38  	public ValidationHeaderPanel(Controller theController) {
39  		setMaximumSize(new Dimension(32767, 60));
40  		myController = theController;
41  
42  		setBorder(new TitledBorder(null, "Message Validation", TitledBorder.CENTER, TitledBorder.TOP, null, null));
43  		GridBagLayout gridBagLayout = new GridBagLayout();
44  		gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
45  		gridBagLayout.rowHeights = new int[] { 0, 0 };
46  		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
47  		gridBagLayout.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
48  		setLayout(gridBagLayout);
49  
50  		myValidateUsingProfileGroupCheckbox = new JCheckBox("Validate Incoming Messages Using Profile Group:");
51  		myValidateUsingProfileGroupCheckbox.addActionListener(new ActionListener() {
52  			public void actionPerformed(ActionEvent e) {
53  				updateModel();
54  			}
55  		});
56  		GridBagConstraints gbc_ValidateUsingProfileGroupCheckbox = new GridBagConstraints();
57  		gbc_ValidateUsingProfileGroupCheckbox.insets = new Insets(0, 0, 0, 5);
58  		gbc_ValidateUsingProfileGroupCheckbox.gridx = 0;
59  		gbc_ValidateUsingProfileGroupCheckbox.gridy = 0;
60  		add(myValidateUsingProfileGroupCheckbox, gbc_ValidateUsingProfileGroupCheckbox);
61  
62  		myProfileGroupsModel = new DefaultComboBoxModel();
63  		myProfileGroupComboBox = new JComboBox(myProfileGroupsModel);
64  		myProfileGroupComboBox.addActionListener(new ActionListener() {
65  			public void actionPerformed(ActionEvent e) {
66  				updateModel();
67  			}
68  		});
69  		myProfileGroupComboBox.setPreferredSize(new Dimension(400, 27));
70  		myProfileGroupComboBox.setMinimumSize(new Dimension(400, 27));
71  		GridBagConstraints gbc_ProfileGroupComboBox = new GridBagConstraints();
72  		gbc_ProfileGroupComboBox.anchor = GridBagConstraints.WEST;
73  		gbc_ProfileGroupComboBox.gridx = 1;
74  		gbc_ProfileGroupComboBox.gridy = 0;
75  		add(myProfileGroupComboBox, gbc_ProfileGroupComboBox);
76  
77  	}
78  
79  	public void destroy() {
80  		myConnection.removePropertyChangeListener(InboundConnection.PROP_VALIDATE_INCOMING, myValidationListener);
81  		myController.getProfileFileList().removePropertyChangeListener(ProfileFileList.PROP_FILES, myProfileGroupsListener);
82  		myConnection.removePropertyChangeListener(InboundConnection.STATUS_PROPERTY, myStatusListener);
83  	}
84  
85  	public void setConnection(InboundConnection theConnection) {
86  		myConnection = theConnection;
87  
88  		myValidationListener = new PropertyChangeListener() {
89  			public void propertyChange(PropertyChangeEvent theEvt) {
90  				updateView();
91  			}
92  		};
93  		myConnection.addPropertyChangeListener(InboundConnection.PROP_VALIDATE_INCOMING, myValidationListener);
94  		updateView();
95  
96  		myProfileGroupsListener = new PropertyChangeListener() {
97  			public void propertyChange(PropertyChangeEvent theEvt) {
98  				updateView();
99  			}
100 		};
101 		myController.getProfileFileList().addPropertyChangeListener(ProfileFileList.PROP_FILES, myProfileGroupsListener);
102 		
103 		myStatusListener = new PropertyChangeListener() {
104 			public void propertyChange(PropertyChangeEvent theEvt) {
105 				updateView();
106 			}
107 		};
108 		myConnection.addPropertyChangeListener(InboundConnection.STATUS_PROPERTY, myStatusListener);
109 		
110 	}
111 
112 	private void updateView() {
113 		if (myHandlingUpdate) {
114 			return;
115 		}
116 
117 		myHandlingUpdate = true;
118 		try {
119 			boolean validationActive = false;
120 			int index = 0;
121 			String profileGroupId = myConnection.getValidateIncomingUsingProfileGroupId();
122 			for (ProfileGroup nextGroup : myController.getProfileFileList().getProfiles()) {
123 				if (myProfileGroupsModel.getSize() <= index || myProfileGroupsModel.getElementAt(index).equals(nextGroup.getName())) {
124 					myProfileGroupsModel.insertElementAt(nextGroup.getName(), index);
125 				}
126 
127 				String nextGroupId = nextGroup.getId();
128 				if (nextGroupId.equals(profileGroupId)) {
129 					myProfileGroupComboBox.setSelectedIndex(index);
130 					validationActive = true;
131 				}
132 
133 				index++;
134 			}
135 
136 			while (myProfileGroupsModel.getSize() > myController.getProfileFileList().getProfiles().size()) {
137 				myProfileGroupsModel.removeElementAt(myProfileGroupsModel.getSize() - 1);
138 			}
139 
140 			if (validationActive) {
141 				myValidateUsingProfileGroupCheckbox.setEnabled(true);
142 				myValidateUsingProfileGroupCheckbox.setSelected(true);
143 				myProfileGroupComboBox.setEnabled(true);
144 			} else if (myProfileGroupsModel.getSize() > 0) {
145 				myValidateUsingProfileGroupCheckbox.setEnabled(true);
146 				myValidateUsingProfileGroupCheckbox.setSelected(false);
147 				myProfileGroupComboBox.setEnabled(true);
148 				if (myProfileGroupComboBox.getSelectedIndex() == -1) {
149 					myProfileGroupComboBox.setSelectedIndex(0);
150 				}
151 			} else {
152 				myValidateUsingProfileGroupCheckbox.setEnabled(false);
153 				myValidateUsingProfileGroupCheckbox.setSelected(false);
154 				myProfileGroupsModel.addElement("None Defined");
155 				myProfileGroupComboBox.setEnabled(false);
156 			}
157 			
158 			if (myConnection.getStatus().isRunning()) {
159 				myProfileGroupComboBox.setEnabled(false);
160 				myValidateUsingProfileGroupCheckbox.setEnabled(false);
161 			} 
162 			
163 		} finally {
164 			myHandlingUpdate = false;
165 		}
166 	}
167 
168 	private void updateModel() {
169 		if (myHandlingUpdate) {
170 			return;
171 		}
172 		myHandlingUpdate = true;
173 		try {
174 			if (myValidateUsingProfileGroupCheckbox.isSelected() && myProfileGroupComboBox.getSelectedIndex() != -1) {
175 				String selectedId = myController.getProfileFileList().getProfiles().get(myProfileGroupComboBox.getSelectedIndex()).getId();
176 				myConnection.setValidateIncomingUsingProfileGroupId(selectedId);
177 			} else {
178 				myConnection.setValidateIncomingUsingProfileGroupId(null);
179 			}
180 		} finally {
181 			myHandlingUpdate = false;
182 		}
183 	}
184 
185 }