View Javadoc
1   package ca.uhn.hl7v2.testpanel.ui.tools;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Dimension;
5   import java.awt.EventQueue;
6   import java.awt.FlowLayout;
7   import java.awt.GridBagConstraints;
8   import java.awt.GridBagLayout;
9   import java.awt.Insets;
10  import java.awt.event.ActionEvent;
11  import java.awt.event.ActionListener;
12  import java.io.File;
13  import java.util.ArrayList;
14  import java.util.Comparator;
15  import java.util.List;
16  
17  import javax.swing.DefaultComboBoxModel;
18  import javax.swing.ImageIcon;
19  import javax.swing.JButton;
20  import javax.swing.JComboBox;
21  import javax.swing.JDialog;
22  import javax.swing.JFileChooser;
23  import javax.swing.JLabel;
24  import javax.swing.JList;
25  import javax.swing.JPanel;
26  import javax.swing.JProgressBar;
27  import javax.swing.JScrollPane;
28  import javax.swing.JTextField;
29  import javax.swing.ListModel;
30  import javax.swing.border.EmptyBorder;
31  import javax.swing.border.TitledBorder;
32  import javax.swing.event.DocumentEvent;
33  import javax.swing.event.ListDataEvent;
34  import javax.swing.event.ListDataListener;
35  
36  import org.apache.commons.lang.StringUtils;
37  
38  import ca.uhn.hl7v2.testpanel.controller.Hl7V2FileSortController;
39  import ca.uhn.hl7v2.testpanel.controller.Prefs;
40  import ca.uhn.hl7v2.testpanel.util.Hl7V2MessageStringComparatorByControlId;
41  import ca.uhn.hl7v2.testpanel.util.Hl7V2MessageStringComparatorByTimestamp;
42  import ca.uhn.hl7v2.testpanel.util.IProgressCallback;
43  import ca.uhn.hl7v2.testpanel.util.LimitedQueue;
44  import ca.uhn.hl7v2.testpanel.util.SimpleDocumentListener;
45  
46  public class Hl7V2FileSortDialog extends JDialog {
47  
48  	private static final String CONTROL_ID = "Control ID (MSH-10)";
49  
50  	private static final long serialVersionUID = 1L;
51  	private static final String TIMESTAMP = "Message Timestamp (MSH-7)";
52  	private JButton myBeginButton;
53  	private boolean myCancelled = false;
54  	private final JPanel mycontentPanel = new JPanel();
55  	private Hl7V2FileSortController myController;
56  	private JTextField myInputFileText;
57  	private JTextField myOutputFileText;
58  	private OutputListModel myOutputListModel;
59  	private JButton myPickFile1Button;
60  
61  	private JButton myPickFile2Button;
62  	private JProgressBar myProgressBar;
63  
64  	private boolean myRunning = false;
65  
66  	private JComboBox mySortByComboBox;
67  
68  	private SortByComboBoxModel mySortByComboBoxModel;
69  
70  	private JButton myStopButton;
71  
72  	private JScrollPane myOutputScrollPane;
73  
74  	/**
75  	 * Create the dialog.
76  	 */
77  	public Hl7V2FileSortDialog(Hl7V2FileSortController theHl7v2FileSortController) {
78  		myController = theHl7v2FileSortController;
79  
80  		setTitle("HL7 v2 File Sort");
81  		setBounds(100, 100, 546, 414);
82  		getContentPane().setLayout(new BorderLayout());
83  		mycontentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
84  		getContentPane().add(mycontentPanel, BorderLayout.CENTER);
85  		GridBagLayout gbl_contentPanel = new GridBagLayout();
86  		gbl_contentPanel.columnWidths = new int[] { 0, 0 };
87  		gbl_contentPanel.rowHeights = new int[] { 73, 0, 0, 0 };
88  		gbl_contentPanel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
89  		gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, 1.0, Double.MIN_VALUE };
90  		mycontentPanel.setLayout(gbl_contentPanel);
91  		{
92  			JPanel panel = new JPanel();
93  			GridBagConstraints gbc_panel = new GridBagConstraints();
94  			gbc_panel.insets = new Insets(0, 0, 5, 0);
95  			gbc_panel.fill = GridBagConstraints.HORIZONTAL;
96  			gbc_panel.gridx = 0;
97  			gbc_panel.gridy = 0;
98  			mycontentPanel.add(panel, gbc_panel);
99  			GridBagLayout gbl_panel = new GridBagLayout();
100 			gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0 };
101 			gbl_panel.rowHeights = new int[] { 0, 0, 0 };
102 			gbl_panel.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE };
103 			gbl_panel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
104 			panel.setLayout(gbl_panel);
105 			{
106 				JLabel lblFile = new JLabel("<html><center>HL7 v2 File Sort takes an input file containing raw HL7 v2 messages, sorts them, and produces a resulting output file. This application uses a large merge-sort, so it can handle very large files.</center></html>");
107 				GridBagConstraints gbc_lblFile = new GridBagConstraints();
108 				gbc_lblFile.insets = new Insets(0, 0, 5, 0);
109 				gbc_lblFile.fill = GridBagConstraints.HORIZONTAL;
110 				gbc_lblFile.weightx = 1.0;
111 				gbc_lblFile.gridwidth = 6;
112 				gbc_lblFile.gridx = 0;
113 				gbc_lblFile.gridy = 0;
114 				panel.add(lblFile, gbc_lblFile);
115 			}
116 			{
117 				JLabel lblFile_1 = new JLabel("Input:");
118 				GridBagConstraints gbc_lblFile_1 = new GridBagConstraints();
119 				gbc_lblFile_1.anchor = GridBagConstraints.EAST;
120 				gbc_lblFile_1.insets = new Insets(0, 0, 0, 5);
121 				gbc_lblFile_1.gridx = 0;
122 				gbc_lblFile_1.gridy = 1;
123 				panel.add(lblFile_1, gbc_lblFile_1);
124 			}
125 			{
126 				myInputFileText = new JTextField();
127 				myInputFileText.setText(Prefs.getInstance().getHl7V2SortInputFile());
128 				GridBagConstraints gbc_File1Text = new GridBagConstraints();
129 				gbc_File1Text.insets = new Insets(0, 0, 0, 5);
130 				gbc_File1Text.fill = GridBagConstraints.HORIZONTAL;
131 				gbc_File1Text.gridx = 1;
132 				gbc_File1Text.gridy = 1;
133 				panel.add(myInputFileText, gbc_File1Text);
134 				myInputFileText.setColumns(10);
135 				myInputFileText.getDocument().addDocumentListener(new SimpleDocumentListener() {
136 					@Override
137 					public void update(DocumentEvent theE) {
138 						Prefs.getInstance().setHl7V2SortInputFile(myInputFileText.getText());
139 						updateButtonStates();
140 					}
141 				});
142 			}
143 			{
144 				myPickFile1Button = new JButton("");
145 				myPickFile1Button.addActionListener(new ActionListener() {
146 					public void actionPerformed(ActionEvent e) {
147 						JTextField textField = myInputFileText;
148 						chooseFile(textField);
149 					}
150 				});
151 				myPickFile1Button.setIcon(new ImageIcon(Hl7V2FileSortDialog.class.getResource("/ca/uhn/hl7v2/testpanel/images/open.png")));
152 				GridBagConstraints gbc_PickFile1Button = new GridBagConstraints();
153 				gbc_PickFile1Button.insets = new Insets(0, 0, 0, 5);
154 				gbc_PickFile1Button.gridx = 2;
155 				gbc_PickFile1Button.gridy = 1;
156 				panel.add(myPickFile1Button, gbc_PickFile1Button);
157 			}
158 			{
159 				JLabel lblNewLabel = new JLabel("Output:");
160 				GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
161 				gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
162 				gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
163 				gbc_lblNewLabel.gridx = 3;
164 				gbc_lblNewLabel.gridy = 1;
165 				panel.add(lblNewLabel, gbc_lblNewLabel);
166 			}
167 			{
168 				myOutputFileText = new JTextField();
169 				myOutputFileText.setText(Prefs.getInstance().getHl7V2SortOutputFile());
170 				GridBagConstraints gbc_File2TextField = new GridBagConstraints();
171 				gbc_File2TextField.insets = new Insets(0, 0, 0, 5);
172 				gbc_File2TextField.fill = GridBagConstraints.HORIZONTAL;
173 				gbc_File2TextField.gridx = 4;
174 				gbc_File2TextField.gridy = 1;
175 				panel.add(myOutputFileText, gbc_File2TextField);
176 				myOutputFileText.setColumns(10);
177 				myOutputFileText.getDocument().addDocumentListener(new SimpleDocumentListener() {
178 					@Override
179 					public void update(DocumentEvent theE) {
180 						Prefs.getInstance().setHl7V2SortOutputFile(myOutputFileText.getText());
181 						updateButtonStates();
182 					}
183 				});
184 			}
185 			{
186 				myPickFile2Button = new JButton("");
187 				myPickFile2Button.addActionListener(new ActionListener() {
188 					public void actionPerformed(ActionEvent e) {
189 						chooseFile(myOutputFileText);
190 					}
191 				});
192 				myPickFile2Button.setIcon(new ImageIcon(Hl7V2FileSortDialog.class.getResource("/ca/uhn/hl7v2/testpanel/images/open.png")));
193 				GridBagConstraints gbc_PickFile2Button = new GridBagConstraints();
194 				gbc_PickFile2Button.gridx = 5;
195 				gbc_PickFile2Button.gridy = 1;
196 				panel.add(myPickFile2Button, gbc_PickFile2Button);
197 			}
198 		}
199 		{
200 			JPanel panel = new JPanel();
201 			GridBagConstraints gbc_panel = new GridBagConstraints();
202 			gbc_panel.insets = new Insets(0, 0, 5, 0);
203 			gbc_panel.fill = GridBagConstraints.BOTH;
204 			gbc_panel.gridx = 0;
205 			gbc_panel.gridy = 1;
206 			mycontentPanel.add(panel, gbc_panel);
207 			GridBagLayout gbl_panel = new GridBagLayout();
208 			gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0 };
209 			gbl_panel.rowHeights = new int[] { 0 };
210 			gbl_panel.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 };
211 			gbl_panel.rowWeights = new double[] { Double.MIN_VALUE };
212 			panel.setLayout(gbl_panel);
213 			myBeginButton = new JButton("Begin");
214 			myBeginButton.addActionListener(new ActionListener() {
215 				public void actionPerformed(ActionEvent e) {
216 					Prefs.getInstance().setHl7V2SortBy(getSortBy());
217 					myController.begin();
218 				}
219 			});
220 			myBeginButton.setIcon(new ImageIcon(Hl7V2FileSortDialog.class.getResource("/ca/uhn/hl7v2/testpanel/images/start_all.png")));
221 			GridBagConstraints gbc_BeginButton = new GridBagConstraints();
222 			gbc_BeginButton.anchor = GridBagConstraints.WEST;
223 			gbc_BeginButton.insets = new Insets(0, 0, 0, 5);
224 			gbc_BeginButton.gridx = 0;
225 			gbc_BeginButton.gridy = 0;
226 			panel.add(myBeginButton, gbc_BeginButton);
227 			{
228 				myStopButton = new JButton("Stop");
229 				myStopButton.addActionListener(new ActionListener() {
230 					public void actionPerformed(ActionEvent e) {
231 						myCancelled = true;
232 						updateButtonStates();
233 					}
234 				});
235 				myStopButton.setIcon(new ImageIcon(Hl7V2FileSortDialog.class.getResource("/ca/uhn/hl7v2/testpanel/images/stop_all.png")));
236 				GridBagConstraints gbc_StopButton = new GridBagConstraints();
237 				gbc_StopButton.insets = new Insets(0, 0, 0, 5);
238 				gbc_StopButton.gridx = 1;
239 				gbc_StopButton.gridy = 0;
240 				panel.add(myStopButton, gbc_StopButton);
241 			}
242 			{
243 				JLabel lblSortBy = new JLabel("Sort By:");
244 				GridBagConstraints gbc_lblSortBy = new GridBagConstraints();
245 				gbc_lblSortBy.insets = new Insets(0, 0, 0, 5);
246 				gbc_lblSortBy.anchor = GridBagConstraints.EAST;
247 				gbc_lblSortBy.gridx = 4;
248 				gbc_lblSortBy.gridy = 0;
249 				panel.add(lblSortBy, gbc_lblSortBy);
250 			}
251 			{
252 				mySortByComboBox = new JComboBox();
253 				mySortByComboBoxModel = new SortByComboBoxModel();
254 				mySortByComboBox.setModel(mySortByComboBoxModel);
255 				mySortByComboBox.setSelectedItem(Prefs.getInstance().getHl7V2SortBy());
256 				if (mySortByComboBox.getSelectedIndex() == -1) {
257 					mySortByComboBox.setSelectedIndex(0);
258 				}
259 				GridBagConstraints gbc_comboBox = new GridBagConstraints();
260 				gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
261 				gbc_comboBox.gridx = 5;
262 				gbc_comboBox.gridy = 0;
263 				panel.add(mySortByComboBox, gbc_comboBox);
264 			}
265 
266 		}
267 		{
268 			JPanel panel = new JPanel();
269 			panel.setBorder(new TitledBorder(null, "Results", TitledBorder.LEADING, TitledBorder.TOP, null, null));
270 			GridBagConstraints gbc_panel = new GridBagConstraints();
271 			gbc_panel.fill = GridBagConstraints.BOTH;
272 			gbc_panel.gridx = 0;
273 			gbc_panel.gridy = 2;
274 			mycontentPanel.add(panel, gbc_panel);
275 			panel.setLayout(new BorderLayout(0, 0));
276 			{
277 				myOutputScrollPane = new JScrollPane();
278 				panel.add(myOutputScrollPane);
279 				{
280 					JList list = new JList();
281 					myOutputListModel = new OutputListModel();
282 					list.setModel(myOutputListModel);
283 					myOutputScrollPane.setViewportView(list);
284 				}
285 			}
286 		}
287 		{
288 			JPanel buttonPane = new JPanel();
289 			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
290 			getContentPane().add(buttonPane, BorderLayout.SOUTH);
291 			{
292 				myProgressBar = new JProgressBar();
293 				myProgressBar.setStringPainted(true);
294 				myProgressBar.setOpaque(true);
295 				myProgressBar.setValue(0);
296 				myProgressBar.setMaximum(100);
297 				myProgressBar.setPreferredSize(new Dimension(400, 20));
298 				buttonPane.add(myProgressBar);
299 			}
300 			{
301 				JButton cancelButton = new JButton("Close");
302 				cancelButton.setActionCommand("Cancel");
303 				buttonPane.add(cancelButton);
304 			}
305 		}
306 
307 		updateButtonStates();
308 	}
309 
310 	public void addMessage(String theMsg) {
311 		myOutputListModel.add(theMsg);
312 		EventQueue.invokeLater(new Runnable() {
313 			public void run() {
314 				myOutputScrollPane.getVerticalScrollBar().setValue(myOutputScrollPane.getVerticalScrollBar().getMaximum());
315 			}
316 		});
317 	}
318 
319 	/**
320 	 * Launch the application.
321 	 */
322 
323 	private void chooseFile(JTextField textField) {
324 		File currentDir = null;
325 		try {
326 			currentDir = new File(textField.getText());
327 			if (currentDir.isFile()) {
328 				currentDir = currentDir.getParentFile();
329 			}
330 		} catch (Exception e2) {
331 			// ignore
332 		}
333 		JFileChooser chooser = new JFileChooser(currentDir);
334 		int result = chooser.showOpenDialog(Hl7V2FileSortDialog.this);
335 		if (result == JFileChooser.APPROVE_OPTION) {
336 			File file = chooser.getSelectedFile();
337 			textField.setText(file.getAbsolutePath());
338 		}
339 	}
340 
341 	public String getInputFileText() {
342 		return myInputFileText.getText();
343 	}
344 
345 	public String getOutputFileText() {
346 		return myOutputFileText.getText();
347 	}
348 
349 	private String getSortBy() {
350 		return (String) mySortByComboBox.getSelectedItem();
351 	}
352 
353 	public Comparator<String> provideMessageComparator() {
354 		if (CONTROL_ID.equals(getSortBy())) {
355 			return new Hl7V2MessageStringComparatorByControlId();
356 		}
357 
358 		return new Hl7V2MessageStringComparatorByTimestamp();
359 	}
360 
361 	public IProgressCallback provideProgressCallback() {
362 		return new IProgressCallback() {
363 
364 			public void activityStarted() {
365 				myRunning = true;
366 				myCancelled = false;
367 				EventQueue.invokeLater(new Runnable() {
368 					public void run() {
369 						updateButtonStates();
370 					}
371 				});
372 			}
373 
374 			public void activityStopped() {
375 				myRunning = false;
376 				EventQueue.invokeLater(new Runnable() {
377 					public void run() {
378 						updateButtonStates();
379 					}
380 				});
381 			}
382 
383 			public void progressUpdate(final double theProgress) throws OperationCancelRequestedException {
384 				EventQueue.invokeLater(new Runnable() {
385 					public void run() {
386 						myProgressBar.setValue((int) (myProgressBar.getMaximum() * theProgress));
387 					}
388 				});
389 				if (myCancelled) {
390 					throw new OperationCancelRequestedException();
391 				}
392 			}
393 		};
394 	}
395 
396 	private void updateButtonStates() {
397 		myPickFile1Button.setEnabled(!myRunning);
398 		myPickFile2Button.setEnabled(!myRunning);
399 		myInputFileText.setEnabled(!myRunning);
400 		myOutputFileText.setEnabled(!myRunning);
401 
402 		boolean haveFiles = (StringUtils.isNotBlank(myInputFileText.getText()) && StringUtils.isNotBlank(myOutputFileText.getText()));
403 		myBeginButton.setEnabled(haveFiles && !myRunning);
404 		myStopButton.setEnabled(myRunning && !myCancelled);
405 	}
406 
407 	public class OutputListModel implements ListModel {
408 
409 		private LimitedQueue<String> myData = new LimitedQueue<String>(1000);
410 		private List<ListDataListener> myListeners = new ArrayList<ListDataListener>();
411 
412 		public OutputListModel() {
413 			super();
414 		}
415 
416 		public void add(String theString) {
417 			myData.add(theString);
418 			for (ListDataListener next : myListeners) {
419 				next.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, myData.size()));
420 			}
421 		}
422 
423 		public void addListDataListener(ListDataListener theL) {
424 			myListeners.add(theL);
425 		}
426 
427 		public String getElementAt(int theIndex) {
428 			String retVal = myData.get(theIndex);
429 			return retVal;
430 		}
431 
432 		public int getSize() {
433 			int retVal = myData.size();
434 			return retVal;
435 		}
436 
437 		public void removeListDataListener(ListDataListener theL) {
438 			myListeners.remove(theL);
439 		}
440 
441 	}
442 
443 	public class SortByComboBoxModel extends DefaultComboBoxModel {
444 		public SortByComboBoxModel() {
445 			addElement(CONTROL_ID);
446 			addElement(TIMESTAMP);
447 		}
448 	}
449 
450 }