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;
27  
28  import java.awt.BorderLayout;
29  import java.awt.Dimension;
30  import java.awt.FlowLayout;
31  import java.awt.GridBagConstraints;
32  import java.awt.GridBagLayout;
33  import java.awt.Insets;
34  import java.awt.event.ActionEvent;
35  import java.awt.event.ActionListener;
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.HashMap;
39  import java.util.Map;
40  
41  import javax.swing.ButtonGroup;
42  import javax.swing.DefaultListModel;
43  import javax.swing.JButton;
44  import javax.swing.JDialog;
45  import javax.swing.JLabel;
46  import javax.swing.JList;
47  import javax.swing.JPanel;
48  import javax.swing.JRadioButton;
49  import javax.swing.JScrollPane;
50  import javax.swing.ListSelectionModel;
51  import javax.swing.border.EmptyBorder;
52  import javax.swing.border.TitledBorder;
53  import javax.swing.event.ListSelectionEvent;
54  import javax.swing.event.ListSelectionListener;
55  
56  import org.apache.commons.lang.StringUtils;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  import ca.uhn.hl7v2.HL7Exception;
61  import ca.uhn.hl7v2.Version;
62  import ca.uhn.hl7v2.testpanel.controller.Controller;
63  import ca.uhn.hl7v2.testpanel.util.EventMapUtil;
64  import ca.uhn.hl7v2.testpanel.xsd.Hl7V2EncodingTypeEnum;
65  
66  public class AddMessageDialog extends JDialog {
67  	private static final Logger ourLog = LoggerFactory.getLogger(AddMessageDialog.class);
68  
69  	private final JPanel mycontentPanel = new JPanel();
70  	private JList myVersionList;
71  	private JList myMessageTypeList;
72  	private JRadioButton myEr7Radio;
73  	private final ButtonGroup encodingButtonGroup = new ButtonGroup();
74  	private String myCurrentSelectedVersion;
75  	private DefaultListModel myMessageTypeListModel;
76  
77  	private Controller myController;
78  
79  	/**
80  	 * Launch the application.
81  	 */
82  	public static void main(String[] args) {
83  		try {
84  			AddMessageDialog dialog = new AddMessageDialog(null);
85  			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
86  			dialog.setVisible(true);
87  		} catch (Exception e) {
88  			e.printStackTrace();
89  		}
90  	}
91  
92  	/**
93  	 * Create the dialog.
94  	 */
95  	public AddMessageDialog(Controller theController) {
96  		myController = theController;
97  
98  		setMinimumSize(new Dimension(450, 400));
99  		setPreferredSize(new Dimension(450, 400));
100 		setSize(new Dimension(450, 400));
101 		setResizable(false);
102 		setMaximumSize(new Dimension(450, 400));
103 		setTitle("Add Message");
104 		setBounds(100, 100, 450, 401);
105 		getContentPane().setLayout(new BorderLayout());
106 		mycontentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
107 		getContentPane().add(mycontentPanel, BorderLayout.CENTER);
108 		GridBagLayout gbl_contentPanel = new GridBagLayout();
109 		gbl_contentPanel.columnWidths = new int[] { 0, 0 };
110 		gbl_contentPanel.rowHeights = new int[] { 0, 0, 0 };
111 		gbl_contentPanel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
112 		gbl_contentPanel.rowWeights = new double[] { 1.0, 0.0, Double.MIN_VALUE };
113 		mycontentPanel.setLayout(gbl_contentPanel);
114 		{
115 			JPanel panel = new JPanel();
116 			panel.setBorder(new TitledBorder(null, "Message Type", TitledBorder.LEADING, TitledBorder.TOP, null, null));
117 			GridBagConstraints gbc_panel = new GridBagConstraints();
118 			gbc_panel.weighty = 1.0;
119 			gbc_panel.insets = new Insets(0, 0, 5, 0);
120 			gbc_panel.fill = GridBagConstraints.BOTH;
121 			gbc_panel.gridx = 0;
122 			gbc_panel.gridy = 0;
123 			mycontentPanel.add(panel, gbc_panel);
124 			GridBagLayout gbl_panel = new GridBagLayout();
125 			gbl_panel.columnWidths = new int[] { 0, 0, 0 };
126 			gbl_panel.rowHeights = new int[] { 0, 0, 0 };
127 			gbl_panel.columnWeights = new double[] { 1.0, 1.0, Double.MIN_VALUE };
128 			gbl_panel.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
129 			panel.setLayout(gbl_panel);
130 			{
131 				JLabel lblVersion = new JLabel("Version");
132 				GridBagConstraints gbc_lblVersion = new GridBagConstraints();
133 				gbc_lblVersion.insets = new Insets(0, 0, 5, 5);
134 				gbc_lblVersion.gridx = 0;
135 				gbc_lblVersion.gridy = 0;
136 				panel.add(lblVersion, gbc_lblVersion);
137 			}
138 			{
139 				JLabel lblType = new JLabel("Type");
140 				GridBagConstraints gbc_lblType = new GridBagConstraints();
141 				gbc_lblType.insets = new Insets(0, 0, 5, 0);
142 				gbc_lblType.gridx = 1;
143 				gbc_lblType.gridy = 0;
144 				panel.add(lblType, gbc_lblType);
145 			}
146 			{
147 				JScrollPane scrollPane = new JScrollPane();
148 				scrollPane.setViewportBorder(null);
149 				GridBagConstraints gbc_scrollPane = new GridBagConstraints();
150 				gbc_scrollPane.weighty = 1.0;
151 				gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
152 				gbc_scrollPane.fill = GridBagConstraints.BOTH;
153 				gbc_scrollPane.gridx = 0;
154 				gbc_scrollPane.gridy = 1;
155 				panel.add(scrollPane, gbc_scrollPane);
156 				{
157 					myVersionList = new JList();
158 					myVersionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
159 					scrollPane.setViewportView(myVersionList);
160 				}
161 			}
162 			{
163 				JScrollPane scrollPane = new JScrollPane();
164 				scrollPane.setViewportBorder(null);
165 				GridBagConstraints gbc_scrollPane = new GridBagConstraints();
166 				gbc_scrollPane.weighty = 1.0;
167 				gbc_scrollPane.weightx = 1.0;
168 				gbc_scrollPane.fill = GridBagConstraints.BOTH;
169 				gbc_scrollPane.gridx = 1;
170 				gbc_scrollPane.gridy = 1;
171 				panel.add(scrollPane, gbc_scrollPane);
172 				{
173 					myMessageTypeList = new JList();
174 					myMessageTypeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
175 					scrollPane.setViewportView(myMessageTypeList);
176 				}
177 			}
178 		}
179 		{
180 			JPanel panel = new JPanel();
181 			panel.setBorder(new TitledBorder(null, "Options", TitledBorder.LEADING, TitledBorder.TOP, null, null));
182 			GridBagConstraints gbc_panel = new GridBagConstraints();
183 			gbc_panel.fill = GridBagConstraints.BOTH;
184 			gbc_panel.gridx = 0;
185 			gbc_panel.gridy = 1;
186 			mycontentPanel.add(panel, gbc_panel);
187 			GridBagLayout gbl_panel = new GridBagLayout();
188 			gbl_panel.columnWidths = new int[] { 0, 0, 0 };
189 			gbl_panel.rowHeights = new int[] { 0, 0 };
190 			gbl_panel.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
191 			gbl_panel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
192 			panel.setLayout(gbl_panel);
193 			{
194 				JLabel lblEncoding = new JLabel("Encoding");
195 				GridBagConstraints gbc_lblEncoding = new GridBagConstraints();
196 				gbc_lblEncoding.insets = new Insets(0, 0, 0, 5);
197 				gbc_lblEncoding.gridx = 0;
198 				gbc_lblEncoding.gridy = 0;
199 				panel.add(lblEncoding, gbc_lblEncoding);
200 			}
201 			{
202 				JPanel panel_1 = new JPanel();
203 				panel_1.setBorder(null);
204 				GridBagConstraints gbc_panel_1 = new GridBagConstraints();
205 				gbc_panel_1.anchor = GridBagConstraints.WEST;
206 				gbc_panel_1.fill = GridBagConstraints.VERTICAL;
207 				gbc_panel_1.gridx = 1;
208 				gbc_panel_1.gridy = 0;
209 				panel.add(panel_1, gbc_panel_1);
210 				{
211 					myEr7Radio = new JRadioButton("ER7");
212 					myEr7Radio.setSelected(true);
213 					encodingButtonGroup.add(myEr7Radio);
214 					panel_1.add(myEr7Radio);
215 				}
216 				{
217 					JRadioButton myXmlRadio = new JRadioButton("XML");
218 					encodingButtonGroup.add(myXmlRadio);
219 					panel_1.add(myXmlRadio);
220 				}
221 			}
222 		}
223 		{
224 			JPanel buttonPane = new JPanel();
225 			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
226 			getContentPane().add(buttonPane, BorderLayout.SOUTH);
227 			{
228 				JButton okButton = new JButton("OK");
229 				okButton.addActionListener(new ActionListener() {
230 					public void actionPerformed(ActionEvent e) {
231 						try {
232 							String version = (String) myVersionList.getSelectedValue();
233 							String fullType = (String) myMessageTypeList.getSelectedValue();
234 							String structure = myTypesToStructures.get(fullType);
235 							String[] fullTypeBits = fullType.split("\\^");
236 							String type = fullTypeBits[0];
237 							String trigger = fullTypeBits[1];
238 							
239 							Hl7V2EncodingTypeEnum encoding = myEr7Radio.isSelected() ? Hl7V2EncodingTypeEnum.ER_7 : Hl7V2EncodingTypeEnum.XML;
240 							myController.addMessage(version, type, trigger, structure, encoding);
241 						} finally {
242 							setVisible(false);
243 						}
244 					}
245 				});
246 				okButton.setActionCommand("OK");
247 				buttonPane.add(okButton);
248 				getRootPane().setDefaultButton(okButton);
249 			}
250 			{
251 				JButton cancelButton = new JButton("Cancel");
252 				cancelButton.addActionListener(new ActionListener() {
253 					public void actionPerformed(ActionEvent e) {
254 						AddMessageDialog.this.setVisible(false);
255 					}
256 				});
257 				cancelButton.setActionCommand("Cancel");
258 				buttonPane.add(cancelButton);
259 			}
260 		}
261 
262 		initLocal();
263 	}
264 
265 	private void initLocal() {
266 
267 		DefaultListModel versionListModel = new DefaultListModel();
268 		myVersionList.setModel(versionListModel);
269 		for (Version nextVersion : Version.values()) {
270 			versionListModel.addElement(nextVersion.getVersion());
271 		}
272 
273 		myVersionList.setSelectedIndex(versionListModel.getSize() - 1);
274 		myVersionList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
275 			public void valueChanged(ListSelectionEvent theE) {
276 				updateTypes();
277 			}
278 		});
279 
280 		myMessageTypeListModel = new DefaultListModel();
281 		myMessageTypeList.setModel(myMessageTypeListModel);
282 
283 		updateTypes();
284 
285 	}
286 
287 	private Map<String, String> myTypesToStructures = new HashMap<String, String>();
288 
289 	private void updateTypes() {
290 		String newVersion = (String) myVersionList.getSelectedValue();
291 		if (StringUtils.equals(newVersion, myCurrentSelectedVersion)) {
292 			return;
293 		}
294 		myCurrentSelectedVersion = newVersion;
295 
296 		Map<String, String> structures;
297 		try {
298 			structures = EventMapUtil.getEventMap(myCurrentSelectedVersion);
299 		} catch (HL7Exception e) {
300 			ourLog.error("Couldn't load structures", e);
301 			return;
302 		}
303 
304 		ArrayList<String> structureNames = new ArrayList<String>();
305 		for (Object next : structures.keySet()) {
306 			String nextType = next.toString();
307 			String nextStructure = structures.get(nextType);
308 
309 			nextType = nextType.replace('_', '^');
310 			structureNames.add(nextType);
311 
312 			myTypesToStructures.put(nextType, nextStructure);
313 		}
314 
315 		Collections.sort(structureNames);
316 
317 		for (int i = 0; i < structureNames.size(); i++) {
318 			String next = structureNames.get(i);
319 			if (myMessageTypeListModel.size() <= i) {
320 				myMessageTypeListModel.addElement(next);
321 			} else if (!myMessageTypeListModel.getElementAt(i).equals(next)) {
322 				myMessageTypeListModel.insertElementAt(next, i);
323 			}
324 		}
325 
326 		while (myMessageTypeListModel.size() > structureNames.size()) {
327 			myMessageTypeListModel.removeElementAt(myMessageTypeListModel.size() - 1);
328 		}
329 
330 		if (myMessageTypeList.getSelectedIndex() == -1) {
331 			myMessageTypeList.setSelectedIndex(0);
332 		}
333 	}
334 
335 }