001 /*
002 * Copyright (c) 1995-2010, The University of Sheffield. See the file
003 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004 *
005 * This file is part of GATE (see http://gate.ac.uk/), and is free
006 * software, licenced under the GNU Library General Public License,
007 * Version 2, June 1991 (in the distribution as file licence.html,
008 * and also available at http://gate.ac.uk/gate/licence.html).
009 *
010 * Niraj Aswani, 14/Feb/2008
011 *
012 * $Id: SingleConcatenatedFileInputDialog.java $
013 */
014 package gate.gui;
015
016 import gate.Gate;
017 import gate.corpora.DocType;
018
019 import java.awt.GridBagConstraints;
020 import java.awt.GridBagLayout;
021 import java.awt.Insets;
022 import java.awt.event.ActionEvent;
023 import java.awt.event.ActionListener;
024 import java.io.IOException;
025
026 import javax.swing.JButton;
027 import javax.swing.JComboBox;
028 import javax.swing.JFileChooser;
029 import javax.swing.JFrame;
030 import javax.swing.JLabel;
031 import javax.swing.JPanel;
032 import javax.swing.JTextField;
033 import javax.swing.UIManager;
034
035 /**
036 * A simple component that allows the user to select a trec web file and
037 * encoding
038 */
039
040 public class SingleConcatenatedFileInputDialog extends JPanel {
041
042 public SingleConcatenatedFileInputDialog() {
043 initGUIComponents();
044 initListeners();
045 }
046
047 /**
048 * Creates the UI
049 */
050 protected void initGUIComponents() {
051 setLayout(new GridBagLayout());
052 // first row
053 GridBagConstraints constraints = new GridBagConstraints();
054 constraints.gridx = GridBagConstraints.RELATIVE;
055 constraints.gridy = 0;
056 constraints.gridwidth = 2;
057 constraints.anchor = GridBagConstraints.WEST;
058 constraints.fill = GridBagConstraints.NONE;
059 constraints.insets = new Insets(0, 0, 0, 5);
060 add(new JLabel("File URL:"), constraints);
061
062 constraints = new GridBagConstraints();
063 constraints.gridx = GridBagConstraints.RELATIVE;
064 constraints.gridy = 0;
065 constraints.gridwidth = 5;
066 constraints.fill = GridBagConstraints.HORIZONTAL;
067 constraints.insets = new Insets(0, 0, 0, 10);
068 add(urlTextField = new JTextField(40), constraints);
069
070 constraints = new GridBagConstraints();
071 constraints.gridx = GridBagConstraints.RELATIVE;
072 constraints.gridy = 0;
073 constraints.gridwidth = 1;
074 constraints.anchor = GridBagConstraints.NORTHWEST;
075 add(filerBtn = new JButton(MainFrame.getIcon("open-file")), constraints);
076
077 // second row
078 constraints = new GridBagConstraints();
079 constraints.gridx = GridBagConstraints.RELATIVE;
080 constraints.gridy = 1;
081 constraints.gridwidth = 2;
082 constraints.anchor = GridBagConstraints.WEST;
083 constraints.fill = GridBagConstraints.NONE;
084 constraints.insets = new Insets(0, 0, 0, 5);
085 add(new JLabel("Encoding:"), constraints);
086
087 constraints = new GridBagConstraints();
088 constraints.gridx = GridBagConstraints.RELATIVE;
089 constraints.gridy = 1;
090 constraints.gridwidth = 4;
091 constraints.fill = GridBagConstraints.HORIZONTAL;
092 add(encodingTextField = new JTextField(15), constraints);
093
094 // third row
095 constraints = new GridBagConstraints();
096 constraints.gridx = GridBagConstraints.RELATIVE;
097 constraints.gridy = 2;
098 constraints.gridwidth = 2;
099 constraints.anchor = GridBagConstraints.WEST;
100 constraints.fill = GridBagConstraints.NONE;
101 constraints.insets = new Insets(0, 0, 0, 5);
102 add(new JLabel("Root Element:"), constraints);
103
104 constraints = new GridBagConstraints();
105 constraints.gridx = GridBagConstraints.RELATIVE;
106 constraints.gridy = 2;
107 constraints.gridwidth = 4;
108 constraints.fill = GridBagConstraints.HORIZONTAL;
109 add(documentRootElementTextField = new JTextField("DOC", 15), constraints);
110
111 // fourth row
112 constraints = new GridBagConstraints();
113 constraints.gridx = GridBagConstraints.RELATIVE;
114 constraints.gridy = 3;
115 constraints.gridwidth = 2;
116 constraints.anchor = GridBagConstraints.WEST;
117 constraints.fill = GridBagConstraints.NONE;
118 constraints.insets = new Insets(0, 0, 0, 5);
119 add(new JLabel("Document type:"), constraints);
120
121 constraints = new GridBagConstraints();
122 constraints.gridx = GridBagConstraints.RELATIVE;
123 constraints.gridy = 3;
124 constraints.gridwidth = 4;
125 constraints.fill = GridBagConstraints.HORIZONTAL;
126 documentTypeComboBox = new JComboBox(DocType.values());
127 documentTypeComboBox.setEditable(false);
128 add(documentTypeComboBox, constraints);
129
130 // fifth row
131 constraints = new GridBagConstraints();
132 constraints.gridx = GridBagConstraints.RELATIVE;
133 constraints.gridy = 4;
134 constraints.gridwidth = 2;
135 constraints.anchor = GridBagConstraints.WEST;
136 constraints.fill = GridBagConstraints.NONE;
137 constraints.insets = new Insets(0, 0, 0, 5);
138 add(new JLabel("No. of Docs:"), constraints);
139
140 constraints = new GridBagConstraints();
141 constraints.gridx = GridBagConstraints.RELATIVE;
142 constraints.gridy = 4;
143 constraints.gridwidth = 4;
144 constraints.fill = GridBagConstraints.HORIZONTAL;
145 add(numOfDocumentsToFetchTextField = new JTextField("-1", 15), constraints);
146
147 // sixth row
148 constraints = new GridBagConstraints();
149 constraints.gridx = GridBagConstraints.RELATIVE;
150 constraints.gridy = 5;
151 constraints.gridwidth = 2;
152 constraints.anchor = GridBagConstraints.WEST;
153 constraints.fill = GridBagConstraints.NONE;
154 constraints.insets = new Insets(0, 0, 0, 5);
155 add(new JLabel("Prefix for documents:"), constraints);
156
157 constraints = new GridBagConstraints();
158 constraints.gridx = GridBagConstraints.RELATIVE;
159 constraints.gridy = 5;
160 constraints.gridwidth = 4;
161 constraints.fill = GridBagConstraints.HORIZONTAL;
162 add(documentNamePrefixTextField = new JTextField("Document", 15),
163 constraints);
164 }
165
166 /**
167 * Adds listeners for UI components
168 */
169 protected void initListeners() {
170 filerBtn.addActionListener(new ActionListener() {
171 public void actionPerformed(ActionEvent e) {
172 JFileChooser filer = MainFrame.getFileChooser();
173 filer.setFileSelectionMode(JFileChooser.FILES_ONLY);
174 filer.setDialogTitle("Select a file");
175
176 filer.resetChoosableFileFilters();
177 filer.setAcceptAllFileFilterUsed(true);
178 filer.setFileFilter(filer.getAcceptAllFileFilter());
179 int res = filer.showOpenDialog(SingleConcatenatedFileInputDialog.this);
180 if(res == JFileChooser.APPROVE_OPTION) {
181 try {
182 urlTextField.setText(filer.getSelectedFile().toURI().toURL()
183 .toExternalForm());
184 }
185 catch(IOException ioe) {
186 }
187 }
188 }
189 });
190 }
191
192 /**
193 * Sets the values for the URL string. This value is not cached so the
194 * set will actually the text in the text field itself
195 */
196 public void setUrlString(String urlString) {
197 urlTextField.setText(urlString);
198 }
199
200 /**
201 * Gets the current text in the URL text field.
202 */
203 public String getUrlString() {
204 return urlTextField.getText();
205 }
206
207 /**
208 * Gets the encoding selected by the user.
209 */
210 public String getEncoding() {
211 return encodingTextField.getText();
212 }
213
214 /**
215 * Sets the initila value for the encoding field.
216 */
217 public void setEncoding(String enc) {
218 encodingTextField.setText(enc);
219 }
220
221 /**
222 * Gets the document root element set by user
223 *
224 * @return
225 */
226 public String getDocumentRootElement() {
227 return documentRootElementTextField.getText();
228 }
229
230 /**
231 * Sets the value for documentRootElement field
232 *
233 * @param documentRootElement
234 */
235 public void setDocumentRootElement(String documentRootElement) {
236 this.documentRootElementTextField.setText(documentRootElement);
237 }
238
239 /**
240 * Gets the document name prefix set by user
241 *
242 * @return
243 */
244 public String getDocumentNamePrefix() {
245 return documentNamePrefixTextField.getText();
246 }
247
248 /**
249 * Sets the value for document name prefix
250 *
251 * @param documentNamePrefix
252 */
253 public void setDocumentNamePrefix(String documentNamePrefix) {
254 this.documentNamePrefixTextField.setText(documentNamePrefix);
255 }
256
257 /**
258 * Gets the selected document type.
259 *
260 * @return
261 */
262 public DocType getDocumentType() {
263 return (DocType)this.documentTypeComboBox.getSelectedItem();
264 }
265
266 /**
267 * Sets the document type
268 *
269 * @param documentType
270 */
271 public void setDocumentType(DocType documentType) {
272 this.documentTypeComboBox.setSelectedItem(documentType);
273 }
274
275 /**
276 * Returns the number of documents to fetch
277 *
278 * @return
279 */
280 public int getNumOfDocumentsToFetch() {
281 if(this.numOfDocumentsToFetchTextField.getText().trim().length() == 0) {
282 return -1;
283 }
284 else {
285 // if error in parsing the text as integer, lets obtain all the documents
286 try {
287 return Integer.parseInt(this.numOfDocumentsToFetchTextField.getText()
288 .trim());
289 }
290 catch(NumberFormatException nfe) {
291 return -1;
292 }
293 }
294 }
295
296 /**
297 * Sets the number of documents to fetch
298 *
299 * @param numOfDocumentsToFetch
300 */
301 public void setNumOfDocumentsToFetch(int numOfDocumentsToFetch) {
302 this.numOfDocumentsToFetchTextField.setText("" + numOfDocumentsToFetch);
303 }
304
305 /**
306 * Test code
307 */
308 static public void main(String[] args) {
309 try {
310 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
311 Gate.init();
312 }
313 catch(Exception e) {
314 e.printStackTrace();
315 }
316 JFrame frame = new JFrame("Foo");
317 SingleConcatenatedFileInputDialog comp = new SingleConcatenatedFileInputDialog();
318 frame.getContentPane().add(comp);
319 frame.pack();
320 frame.setResizable(false);
321 frame.setVisible(true);
322 }
323
324 /**
325 * The text field for the directory URL
326 */
327 JTextField urlTextField;
328
329 /**
330 * The buttons that opens the file chooser
331 */
332 JButton filerBtn;
333
334 /**
335 * The textField for the encoding
336 */
337 JTextField encodingTextField;
338
339 /**
340 * The textField for the document root element
341 */
342 JTextField documentRootElementTextField;
343
344 /**
345 * The textField for the document name prefix
346 */
347 JTextField documentNamePrefixTextField;
348
349 /**
350 * Dropdown box with available document types
351 */
352 JComboBox documentTypeComboBox;
353
354 /**
355 * Number of documents to extract from the big document
356 */
357 JTextField numOfDocumentsToFetchTextField;
358
359 }
|