View Javadoc
1   package ca.uhn.hl7v2.testpanel.ui.conf;
2   
3   import static org.apache.commons.lang.StringUtils.*;
4   
5   import java.awt.BorderLayout;
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.net.MalformedURLException;
14  import java.net.URL;
15  
16  import javax.swing.ButtonGroup;
17  import javax.swing.JButton;
18  import javax.swing.JDialog;
19  import javax.swing.JFileChooser;
20  import javax.swing.JPanel;
21  import javax.swing.JRadioButton;
22  import javax.swing.JTextField;
23  import javax.swing.border.EmptyBorder;
24  import javax.swing.event.DocumentEvent;
25  
26  import ca.uhn.hl7v2.testpanel.controller.ConformanceEditorController;
27  import ca.uhn.hl7v2.testpanel.controller.Prefs;
28  import ca.uhn.hl7v2.testpanel.util.ColorUtil;
29  import ca.uhn.hl7v2.testpanel.util.SimpleDocumentListener;
30  
31  public class ImportProfileGroupDialog extends JDialog {
32  
33  	private final JPanel mycontentPanel = new JPanel();
34  	private final ButtonGroup fileOrUrlButtonGroup = new ButtonGroup();
35  	private JTextField myFileTextField;
36  	private JTextField myUrlTextField;
37  	private JRadioButton myUrlRadioButton;
38  	private JRadioButton myFileRadioButton;
39  	private JButton myOkButton;
40  	private File myFileString;
41  	private URL myUrlString;
42  
43  	/**
44  	 * Create the dialog.
45  	 */
46  	public ImportProfileGroupDialog() {
47  		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
48  		setModal(true);
49  		setResizable(false);
50  		setModalityType(ModalityType.APPLICATION_MODAL);
51  		setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
52  		setTitle("Import Profile Group");
53  		setBounds(100, 100, 450, 158);
54  		getContentPane().setLayout(new BorderLayout());
55  		mycontentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
56  		getContentPane().add(mycontentPanel, BorderLayout.CENTER);
57  		GridBagLayout gbl_contentPanel = new GridBagLayout();
58  		gbl_contentPanel.columnWidths = new int[] { 0, 0, 0, 0 };
59  		gbl_contentPanel.rowHeights = new int[] { 0, 0, 0 };
60  		gbl_contentPanel.columnWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
61  		gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
62  		mycontentPanel.setLayout(gbl_contentPanel);
63  		{
64  			myFileRadioButton = new JRadioButton("File:");
65  			myFileRadioButton.addActionListener(new ActionListener() {
66  				public void actionPerformed(ActionEvent e) {
67  					updateOk();
68  				}
69  			});
70  			fileOrUrlButtonGroup.add(myFileRadioButton);
71  			GridBagConstraints gbc_myFileRadioButton = new GridBagConstraints();
72  			gbc_myFileRadioButton.insets = new Insets(0, 0, 5, 5);
73  			gbc_myFileRadioButton.gridx = 0;
74  			gbc_myFileRadioButton.gridy = 0;
75  			mycontentPanel.add(myFileRadioButton, gbc_myFileRadioButton);
76  		}
77  		{
78  			myFileTextField = new JTextField();
79  			myFileTextField.setOpaque(true);
80  			myFileTextField.getDocument().addDocumentListener(new SimpleDocumentListener() {
81  				@Override
82  				public void update(DocumentEvent theE) {
83  					myFileRadioButton.setSelected(true);
84  					updateOk();
85  				}
86  			});
87  			GridBagConstraints gbc_FileTextField = new GridBagConstraints();
88  			gbc_FileTextField.insets = new Insets(0, 0, 5, 5);
89  			gbc_FileTextField.fill = GridBagConstraints.HORIZONTAL;
90  			gbc_FileTextField.gridx = 1;
91  			gbc_FileTextField.gridy = 0;
92  			mycontentPanel.add(myFileTextField, gbc_FileTextField);
93  			myFileTextField.setColumns(10);
94  		}
95  		{
96  			JButton btnBrowse = new JButton("Browse...");
97  			btnBrowse.addActionListener(new ActionListener() {
98  				public void actionPerformed(ActionEvent e) {
99  					File importProfileGroupDir = Prefs.getInstance().getImportProfileGroupDirectory();
100 					JFileChooser chooser = new JFileChooser(importProfileGroupDir);
101 					ConformanceEditorController.createFileSelectionExtentionFolder(chooser);
102 					int result = chooser.showDialog(ImportProfileGroupDialog.this, "Select");
103 					if (result == JFileChooser.APPROVE_OPTION) {
104 						myFileTextField.setText(chooser.getSelectedFile().getAbsolutePath());
105 					}
106 				}
107 			});
108 			GridBagConstraints gbc_btnBrowse = new GridBagConstraints();
109 			gbc_btnBrowse.insets = new Insets(0, 0, 5, 0);
110 			gbc_btnBrowse.gridx = 2;
111 			gbc_btnBrowse.gridy = 0;
112 			mycontentPanel.add(btnBrowse, gbc_btnBrowse);
113 		}
114 		{
115 			myUrlRadioButton = new JRadioButton("URL:");
116 			myUrlRadioButton.addActionListener(new ActionListener() {
117 				public void actionPerformed(ActionEvent e) {
118 					updateOk();
119 				}
120 			});
121 			fileOrUrlButtonGroup.add(myUrlRadioButton);
122 			GridBagConstraints gbc_myUrlRadioButton = new GridBagConstraints();
123 			gbc_myUrlRadioButton.insets = new Insets(0, 0, 0, 5);
124 			gbc_myUrlRadioButton.gridx = 0;
125 			gbc_myUrlRadioButton.gridy = 1;
126 			mycontentPanel.add(myUrlRadioButton, gbc_myUrlRadioButton);
127 		}
128 		{
129 			myUrlTextField = new JTextField();
130 			myUrlTextField.getDocument().addDocumentListener(new SimpleDocumentListener() {
131 				@Override
132 				public void update(DocumentEvent theE) {
133 					myUrlRadioButton.setSelected(true);
134 					updateOk();
135 				}
136 			});
137 			myUrlTextField.setOpaque(true);
138 			GridBagConstraints gbc_UrlTextField = new GridBagConstraints();
139 			gbc_UrlTextField.insets = new Insets(0, 0, 0, 5);
140 			gbc_UrlTextField.fill = GridBagConstraints.HORIZONTAL;
141 			gbc_UrlTextField.gridx = 1;
142 			gbc_UrlTextField.gridy = 1;
143 			mycontentPanel.add(myUrlTextField, gbc_UrlTextField);
144 			myUrlTextField.setColumns(10);
145 		}
146 		{
147 			JPanel buttonPane = new JPanel();
148 			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
149 			getContentPane().add(buttonPane, BorderLayout.SOUTH);
150 			{
151 				myOkButton = new JButton("OK");
152 				myOkButton.setActionCommand("OK");
153 				myOkButton.addActionListener(new ActionListener() {
154 					public void actionPerformed(ActionEvent theE) {
155 						updateOk();
156 						dispose();
157 					}
158 				});
159 				buttonPane.add(myOkButton);
160 				getRootPane().setDefaultButton(myOkButton);
161 			}
162 			{
163 				JButton cancelButton = new JButton("Cancel");
164 				cancelButton.addActionListener(new ActionListener() {
165 					public void actionPerformed(ActionEvent e) {
166 						myFileString = null;
167 						myUrlString = null;
168 						dispose();
169 					}
170 				});
171 				cancelButton.setActionCommand("Cancel");
172 				buttonPane.add(cancelButton);
173 			}
174 		}
175 
176 		updateOk();
177 	}
178 
179 	public File getFileString() {
180 		return myFileString;
181 	}
182 
183 	public URL getUrlString() {
184 		return myUrlString;
185 	}
186 
187 	private void updateOk() {
188 		if (myFileRadioButton.isSelected() && isNotBlank(myFileTextField.getText())) {
189 			myFileString = new File(myFileTextField.getText());
190 			if (myFileString.exists() && myFileString.isFile() && myFileString.canRead()) {
191 				myFileTextField.setBackground(ColorUtil.OK_BG);
192 			} else {
193 				myFileTextField.setBackground(ColorUtil.ERROR_BG);
194 			}
195 		} else {
196 			myFileString = null;
197 			myFileTextField.setBackground(ColorUtil.OK_BG);
198 		}
199 
200 		if (myUrlRadioButton.isSelected() && isNotBlank(myUrlTextField.getText())) {
201 			try {
202 				myUrlString = new URL(myUrlTextField.getText());
203 				myUrlTextField.setBackground(ColorUtil.OK_BG);
204 			} catch (MalformedURLException e) {
205 				myUrlTextField.setBackground(ColorUtil.ERROR_BG);
206 			}
207 		} else {
208 			myUrlString = null;
209 			myUrlTextField.setBackground(ColorUtil.OK_BG);
210 		}
211 
212 		boolean okEnabled = getFileString() != null || getUrlString() != null;
213 		myOkButton.setEnabled(okEnabled);
214 	}
215 }