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 					@Override
155 					public void actionPerformed(ActionEvent theE) {
156 						updateOk();
157 						dispose();
158 					}
159 				});
160 				buttonPane.add(myOkButton);
161 				getRootPane().setDefaultButton(myOkButton);
162 			}
163 			{
164 				JButton cancelButton = new JButton("Cancel");
165 				cancelButton.addActionListener(new ActionListener() {
166 					public void actionPerformed(ActionEvent e) {
167 						myFileString = null;
168 						myUrlString = null;
169 						dispose();
170 					}
171 				});
172 				cancelButton.setActionCommand("Cancel");
173 				buttonPane.add(cancelButton);
174 			}
175 		}
176 
177 		updateOk();
178 	}
179 
180 	public File getFileString() {
181 		return myFileString;
182 	}
183 
184 	public URL getUrlString() {
185 		return myUrlString;
186 	}
187 
188 	private void updateOk() {
189 		if (myFileRadioButton.isSelected() && isNotBlank(myFileTextField.getText())) {
190 			myFileString = new File(myFileTextField.getText());
191 			if (myFileString.exists() && myFileString.isFile() && myFileString.canRead()) {
192 				myFileTextField.setBackground(ColorUtil.OK_BG);
193 			} else {
194 				myFileTextField.setBackground(ColorUtil.ERROR_BG);
195 			}
196 		} else {
197 			myFileString = null;
198 			myFileTextField.setBackground(ColorUtil.OK_BG);
199 		}
200 
201 		if (myUrlRadioButton.isSelected() && isNotBlank(myUrlTextField.getText())) {
202 			try {
203 				myUrlString = new URL(myUrlTextField.getText());
204 				myUrlTextField.setBackground(ColorUtil.OK_BG);
205 			} catch (MalformedURLException e) {
206 				myUrlTextField.setBackground(ColorUtil.ERROR_BG);
207 			}
208 		} else {
209 			myUrlString = null;
210 			myUrlTextField.setBackground(ColorUtil.OK_BG);
211 		}
212 
213 		boolean okEnabled = getFileString() != null || getUrlString() != null;
214 		myOkButton.setEnabled(okEnabled);
215 	}
216 }