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.FlowLayout;
6   import java.awt.GridBagConstraints;
7   import java.awt.GridBagLayout;
8   import java.awt.Insets;
9   import java.awt.event.ActionEvent;
10  import java.awt.event.ActionListener;
11  import java.beans.PropertyChangeEvent;
12  import java.beans.PropertyChangeListener;
13  import java.io.File;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import javax.swing.ImageIcon;
18  import javax.swing.JButton;
19  import javax.swing.JCheckBox;
20  import javax.swing.JDialog;
21  import javax.swing.JFileChooser;
22  import javax.swing.JLabel;
23  import javax.swing.JList;
24  import javax.swing.JPanel;
25  import javax.swing.JProgressBar;
26  import javax.swing.JScrollPane;
27  import javax.swing.JTextField;
28  import javax.swing.ListModel;
29  import javax.swing.border.EmptyBorder;
30  import javax.swing.border.TitledBorder;
31  import javax.swing.event.ChangeEvent;
32  import javax.swing.event.ChangeListener;
33  import javax.swing.event.DocumentEvent;
34  import javax.swing.event.ListDataEvent;
35  import javax.swing.event.ListDataListener;
36  
37  import org.apache.commons.lang.StringUtils;
38  
39  import ca.uhn.hl7v2.testpanel.controller.Hl7V2FileDiffController;
40  import ca.uhn.hl7v2.testpanel.controller.Prefs;
41  import ca.uhn.hl7v2.testpanel.util.SimpleDocumentListener;
42  
43  public class Hl7V2FileDiffDialog extends JDialog {
44  
45  	private static final long serialVersionUID = 1L;
46  
47  	private JButton myBeginButton;
48  	private final JPanel mycontentPanel = new JPanel();
49  	private Hl7V2FileDiffController myController;
50  	private JTextField myFile1Text;
51  	private JTextField myFile2Text;
52  	private JButton myPickFile1Button;
53  	private JButton myPickFile2Button;
54  	private JButton myStopButton;
55  	private JCheckBox myStopOnFirstErrorCheck;
56  	private JProgressBar myProgressBar;
57  	private JCheckBox myShowWholeMessageOnErrorCheckbox;
58  	/**
59  	 * Create the dialog.
60  	 */
61  	public Hl7V2FileDiffDialog(Hl7V2FileDiffController theHl7v2FileDiffController) {
62  		myController = theHl7v2FileDiffController;
63  		initListeners();
64  		
65  		setTitle("HL7 v2 File Diff");
66  		setBounds(100, 100, 546, 414);
67  		getContentPane().setLayout(new BorderLayout());
68  		mycontentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
69  		getContentPane().add(mycontentPanel, BorderLayout.CENTER);
70  		GridBagLayout gbl_contentPanel = new GridBagLayout();
71  		gbl_contentPanel.columnWidths = new int[] { 0, 0 };
72  		gbl_contentPanel.rowHeights = new int[] { 73, 0, 0, 0 };
73  		gbl_contentPanel.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
74  		gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, 1.0, Double.MIN_VALUE };
75  		mycontentPanel.setLayout(gbl_contentPanel);
76  		{
77  			JPanel panel = new JPanel();
78  			GridBagConstraints gbc_panel = new GridBagConstraints();
79  			gbc_panel.insets = new Insets(0, 0, 5, 0);
80  			gbc_panel.fill = GridBagConstraints.HORIZONTAL;
81  			gbc_panel.gridx = 0;
82  			gbc_panel.gridy = 0;
83  			mycontentPanel.add(panel, gbc_panel);
84  			GridBagLayout gbl_panel = new GridBagLayout();
85  			gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0 };
86  			gbl_panel.rowHeights = new int[] { 0, 0, 0 };
87  			gbl_panel.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE };
88  			gbl_panel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
89  			panel.setLayout(gbl_panel);
90  			{
91  				JLabel lblFile = new JLabel(
92  						"<html><center>HL7 v2 File Diff compares two files, each containing one or more HL7 v2 messages, and presents a friendly view of any differences. Messages are compared semantically, meaning that empty fields and components are not compared.</center></html>");
93  				GridBagConstraints gbc_lblFile = new GridBagConstraints();
94  				gbc_lblFile.insets = new Insets(0, 0, 5, 0);
95  				gbc_lblFile.fill = GridBagConstraints.HORIZONTAL;
96  				gbc_lblFile.weightx = 1.0;
97  				gbc_lblFile.gridwidth = 6;
98  				gbc_lblFile.gridx = 0;
99  				gbc_lblFile.gridy = 0;
100 				panel.add(lblFile, gbc_lblFile);
101 			}
102 			{
103 				JLabel lblFile_1 = new JLabel("File 1:");
104 				GridBagConstraints gbc_lblFile_1 = new GridBagConstraints();
105 				gbc_lblFile_1.anchor = GridBagConstraints.EAST;
106 				gbc_lblFile_1.insets = new Insets(0, 0, 0, 5);
107 				gbc_lblFile_1.gridx = 0;
108 				gbc_lblFile_1.gridy = 1;
109 				panel.add(lblFile_1, gbc_lblFile_1);
110 			}
111 			{
112 				myFile1Text = new JTextField();
113 				myFile1Text.setText(Prefs.getInstance().getHl7V2DiffFile1());
114 				GridBagConstraints gbc_File1Text = new GridBagConstraints();
115 				gbc_File1Text.insets = new Insets(0, 0, 0, 5);
116 				gbc_File1Text.fill = GridBagConstraints.HORIZONTAL;
117 				gbc_File1Text.gridx = 1;
118 				gbc_File1Text.gridy = 1;
119 				panel.add(myFile1Text, gbc_File1Text);
120 				myFile1Text.setColumns(10);
121 				myFile1Text.getDocument().addDocumentListener(new SimpleDocumentListener() {
122 					@Override
123 					public void update(DocumentEvent theE) {
124 						Prefs.getInstance().setHl7V2DiffFile1(myFile1Text.getText());
125 						updateButtonStates();
126 					}
127 				});
128 			}
129 			{
130 				myPickFile1Button = new JButton("");
131 				myPickFile1Button.addActionListener(new ActionListener() {
132 					public void actionPerformed(ActionEvent e) {
133 						JTextField textField = myFile1Text;
134 						chooseFile(textField);
135 					}
136 				});
137 				myPickFile1Button.setIcon(new ImageIcon(Hl7V2FileDiffDialog.class.getResource("/ca/uhn/hl7v2/testpanel/images/open.png")));
138 				GridBagConstraints gbc_PickFile1Button = new GridBagConstraints();
139 				gbc_PickFile1Button.insets = new Insets(0, 0, 0, 5);
140 				gbc_PickFile1Button.gridx = 2;
141 				gbc_PickFile1Button.gridy = 1;
142 				panel.add(myPickFile1Button, gbc_PickFile1Button);
143 			}
144 			{
145 				JLabel lblNewLabel = new JLabel("File 2:");
146 				GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
147 				gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
148 				gbc_lblNewLabel.insets = new Insets(0, 0, 0, 5);
149 				gbc_lblNewLabel.gridx = 3;
150 				gbc_lblNewLabel.gridy = 1;
151 				panel.add(lblNewLabel, gbc_lblNewLabel);
152 			}
153 			{
154 				myFile2Text = new JTextField();
155 				myFile2Text.setText(Prefs.getInstance().getHl7V2DiffFile2());
156 				GridBagConstraints gbc_File2TextField = new GridBagConstraints();
157 				gbc_File2TextField.insets = new Insets(0, 0, 0, 5);
158 				gbc_File2TextField.fill = GridBagConstraints.HORIZONTAL;
159 				gbc_File2TextField.gridx = 4;
160 				gbc_File2TextField.gridy = 1;
161 				panel.add(myFile2Text, gbc_File2TextField);
162 				myFile2Text.setColumns(10);
163 				myFile2Text.getDocument().addDocumentListener(new SimpleDocumentListener() {
164 					@Override
165 					public void update(DocumentEvent theE) {
166 						Prefs.getInstance().setHl7V2DiffFile2(myFile2Text.getText());
167 						updateButtonStates();
168 					}
169 				});
170 			}
171 			{
172 				myPickFile2Button = new JButton("");
173 				myPickFile2Button.addActionListener(new ActionListener() {
174 					public void actionPerformed(ActionEvent e) {
175 						chooseFile(myFile2Text);
176 					}
177 				});
178 				myPickFile2Button.setIcon(new ImageIcon(Hl7V2FileDiffDialog.class.getResource("/ca/uhn/hl7v2/testpanel/images/open.png")));
179 				GridBagConstraints gbc_PickFile2Button = new GridBagConstraints();
180 				gbc_PickFile2Button.gridx = 5;
181 				gbc_PickFile2Button.gridy = 1;
182 				panel.add(myPickFile2Button, gbc_PickFile2Button);
183 			}
184 		}
185 		{
186 			JPanel panel = new JPanel();
187 			GridBagConstraints gbc_panel = new GridBagConstraints();
188 			gbc_panel.insets = new Insets(0, 0, 5, 0);
189 			gbc_panel.fill = GridBagConstraints.BOTH;
190 			gbc_panel.gridx = 0;
191 			gbc_panel.gridy = 1;
192 			mycontentPanel.add(panel, gbc_panel);
193 			GridBagLayout gbl_panel = new GridBagLayout();
194 			gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0 };
195 			gbl_panel.rowHeights = new int[] { 0 };
196 			gbl_panel.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 };
197 			gbl_panel.rowWeights = new double[] { Double.MIN_VALUE };
198 			panel.setLayout(gbl_panel);
199 			myBeginButton = new JButton("Begin");
200 			myBeginButton.addActionListener(new ActionListener() {
201 				public void actionPerformed(ActionEvent e) {
202 					myController.begin();
203 				}
204 			});
205 			myBeginButton.setIcon(new ImageIcon(Hl7V2FileDiffDialog.class.getResource("/ca/uhn/hl7v2/testpanel/images/start_all.png")));
206 			GridBagConstraints gbc_BeginButton = new GridBagConstraints();
207 			gbc_BeginButton.anchor = GridBagConstraints.WEST;
208 			gbc_BeginButton.insets = new Insets(0, 0, 0, 5);
209 			gbc_BeginButton.gridx = 0;
210 			gbc_BeginButton.gridy = 0;
211 			panel.add(myBeginButton, gbc_BeginButton);
212 			{
213 				myStopButton = new JButton("Stop");
214 				myStopButton.addActionListener(new ActionListener() {
215 					public void actionPerformed(ActionEvent e) {
216 						myController.cancel();
217 					}
218 				});
219 				myStopButton.setIcon(new ImageIcon(Hl7V2FileDiffDialog.class.getResource("/ca/uhn/hl7v2/testpanel/images/stop_all.png")));
220 				GridBagConstraints gbc_StopButton = new GridBagConstraints();
221 				gbc_StopButton.insets = new Insets(0, 0, 0, 5);
222 				gbc_StopButton.gridx = 1;
223 				gbc_StopButton.gridy = 0;
224 				panel.add(myStopButton, gbc_StopButton);
225 			}
226 			{
227 				myStopOnFirstErrorCheck = new JCheckBox("Stop");
228 				myStopOnFirstErrorCheck.addChangeListener(new ChangeListener() {
229 					public void stateChanged(ChangeEvent e) {
230 						Prefs.getInstance().setHl7V2DiffStopOnFirstError(myStopButton.isSelected());
231 					}
232 				});
233 				{
234 					JLabel lblOnDifference = new JLabel("On Difference:");
235 					GridBagConstraints gbc_lblOnDifference = new GridBagConstraints();
236 					gbc_lblOnDifference.insets = new Insets(0, 0, 0, 5);
237 					gbc_lblOnDifference.gridx = 3;
238 					gbc_lblOnDifference.gridy = 0;
239 					panel.add(lblOnDifference, gbc_lblOnDifference);
240 				}
241 				{
242 					myShowWholeMessageOnErrorCheckbox = new JCheckBox("Show Whole Message");
243 					GridBagConstraints gbc_ShowWholeMessageOnErrorCheckbox = new GridBagConstraints();
244 					gbc_ShowWholeMessageOnErrorCheckbox.insets = new Insets(0, 0, 0, 5);
245 					gbc_ShowWholeMessageOnErrorCheckbox.gridx = 4;
246 					gbc_ShowWholeMessageOnErrorCheckbox.gridy = 0;
247 					panel.add(myShowWholeMessageOnErrorCheckbox, gbc_ShowWholeMessageOnErrorCheckbox);
248 					myShowWholeMessageOnErrorCheckbox.setSelected(Prefs.getInstance().getHl7V2DiffShowWholeMessageOnError());
249 					myShowWholeMessageOnErrorCheckbox.addActionListener(new ActionListener() {
250 						public void actionPerformed(ActionEvent theE) {
251 							Prefs.getInstance().setHl7V2DiffShowWholeMessageOnError(myShowWholeMessageOnErrorCheckbox.isSelected());
252 						}
253 					});
254 				}
255 				myStopOnFirstErrorCheck.setSelected(Prefs.getInstance().getHl7V2DiffStopOnFirstError());
256 				GridBagConstraints gbc_StopOnFirstErrorCheck = new GridBagConstraints();
257 				gbc_StopOnFirstErrorCheck.gridx = 5;
258 				gbc_StopOnFirstErrorCheck.gridy = 0;
259 				panel.add(myStopOnFirstErrorCheck, gbc_StopOnFirstErrorCheck);
260 			}
261 
262 		}
263 		{
264 			JPanel panel = new JPanel();
265 			panel.setBorder(new TitledBorder(null, "Results", TitledBorder.LEADING, TitledBorder.TOP, null, null));
266 			GridBagConstraints gbc_panel = new GridBagConstraints();
267 			gbc_panel.fill = GridBagConstraints.BOTH;
268 			gbc_panel.gridx = 0;
269 			gbc_panel.gridy = 2;
270 			mycontentPanel.add(panel, gbc_panel);
271 			panel.setLayout(new BorderLayout(0, 0));
272 			{
273 				JScrollPane scrollPane = new JScrollPane();
274 				panel.add(scrollPane);
275 				{
276 					JList list = new JList();
277 					list.setModel(new OutputListModel());
278 					scrollPane.setViewportView(list);
279 				}
280 			}
281 		}
282 		{
283 			JPanel buttonPane = new JPanel();
284 			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
285 			getContentPane().add(buttonPane, BorderLayout.SOUTH);
286 			{
287 				myProgressBar = new JProgressBar();
288 				myProgressBar.setStringPainted(true);
289 				myProgressBar.setOpaque(true);
290 				myProgressBar.setValue(0);
291 				myProgressBar.setMaximum(100);
292 				myProgressBar.setPreferredSize(new Dimension(400, 20));
293 				buttonPane.add(myProgressBar);
294 			}
295 			{
296 				JButton cancelButton = new JButton("Close");
297 				cancelButton.setActionCommand("Cancel");
298 				buttonPane.add(cancelButton);
299 			}
300 		}
301 		
302 		updateButtonStates();
303 	}
304 
305 	/**
306 	 * Launch the application.
307 	 */
308 
309 	private void chooseFile(JTextField textField) {
310 		File currentDir = null;
311 		try {
312 			currentDir = new File(textField.getText());
313 			if (currentDir.isFile()) {
314 				currentDir = currentDir.getParentFile();
315 			}
316 		} catch (Exception e2) {
317 			// ignore
318 		}
319 		JFileChooser chooser = new JFileChooser(currentDir);
320 		int result = chooser.showOpenDialog(Hl7V2FileDiffDialog.this);
321 		if (result == JFileChooser.APPROVE_OPTION) {
322 			File file = chooser.getSelectedFile();
323 			textField.setText(file.getAbsolutePath());
324 		}
325 	}
326 
327 	public boolean isShowWholeMessageOnError() {
328 		return myShowWholeMessageOnErrorCheckbox.isSelected();
329 	}
330 	
331 	public String getFile1Text() {
332 		return myFile1Text.getText();
333 	}
334 
335 	public String getFile2Text() {
336 		return myFile2Text.getText();
337 	}
338 
339 	private void initListeners() {
340 		myController.addPropertyChangeListener(Hl7V2FileDiffController.PROP_RUNNING, new PropertyChangeListener() {
341 			public void propertyChange(PropertyChangeEvent theEvt) {
342 				updateButtonStates();
343 				updateProgress();
344 			}
345 		});
346 		myController.addPropertyChangeListener(Hl7V2FileDiffController.PROP_FAILED, new PropertyChangeListener() {
347 			public void propertyChange(PropertyChangeEvent theEvt) {
348 				updateProgress();
349 			}
350 		});
351 		myController.addPropertyChangeListener(Hl7V2FileDiffController.PROP_PERCENT_DONE, new PropertyChangeListener() {
352 			public void propertyChange(PropertyChangeEvent theEvt) {
353 				updateProgress();
354 			}
355 		});
356 	}
357 
358 	private void updateProgress() {
359 		int percentDone = myController.getPercentDone();
360 		myProgressBar.setValue(percentDone);
361 		
362 		if (myController.isFailed()) {
363 			myProgressBar.setString("Failed - " + percentDone + "%");
364 		} else if (myController.isRunning()) {
365 			myProgressBar.setString("Working - " + percentDone + "%");
366 		} else {
367 			myProgressBar.setString("");
368 		}
369 	}
370 
371 	public boolean isStopOnFirstError() {
372 		return myStopOnFirstErrorCheck.isSelected();
373 	}
374 
375 	private void updateButtonStates() {
376 		boolean running = myController.isRunning();
377 		myPickFile1Button.setEnabled(!running);
378 		myPickFile2Button.setEnabled(!running);
379 		myShowWholeMessageOnErrorCheckbox.setEnabled(!running);
380 		myFile1Text.setEnabled(!running);
381 		myFile2Text.setEnabled(!running);
382 		
383 		boolean haveFiles = (StringUtils.isNotBlank(myFile1Text.getText()) && StringUtils.isNotBlank(myFile2Text.getText()));
384 		myBeginButton.setEnabled(haveFiles && !running);
385 		myStopButton.setEnabled(running);
386 		myStopOnFirstErrorCheck.setEnabled(!running);
387 	}
388 
389 	public class OutputListModel implements ListModel {
390 
391 		private List<ListDataListener> myListeners = new ArrayList<ListDataListener>();
392 		
393 		public OutputListModel() {
394 			myController.addPropertyChangeListener(Hl7V2FileDiffController.PROP_OUTPUT, new PropertyChangeListener() {
395 				public void propertyChange(PropertyChangeEvent theEvt) {
396 					for (ListDataListener next : myListeners) {
397 						// TODO: this could be more efficient...
398 						next.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize()));
399 					}
400 				}
401 			});
402 		}
403 		
404 		public void addListDataListener(ListDataListener theL) {
405 			myListeners.add(theL);
406 		}
407 
408 		public String getElementAt(int theIndex) {
409 			String retVal = myController.getOutputLine(theIndex);
410 			return retVal;
411 		}
412 
413 		public int getSize() {
414 			int retVal = myController.getOutputSize();
415 			return retVal;
416 		}
417 
418 		public void removeListDataListener(ListDataListener theL) {
419 			myListeners.remove(theL);
420 		}
421 
422 	}
423 
424 }