| CorpusFillerComponent |

    1   /*
    2    *  Copyright (c) 1998-2001, The University of Sheffield.
    3    *
    4    *  This file is part of GATE (see http://gate.ac.uk/), and is free
    5    *  software, licenced under the GNU Library General Public License,
    6    *  Version 2, June 1991 (in the distribution as file licence.html,
    7    *  and also available at http://gate.ac.uk/gate/licence.html).
    8    *
    9    *  Valentin Tablan, 02/Nov/2001
    10   *
    11   *  $Id: CorpusFillerComponent.java,v 1.4 2001/11/28 14:28:17 valyt Exp $
    12   */
    13  package gate.gui;
    14
    15  import javax.swing.*;
    16  import javax.swing.event.*;
    17  import java.awt.event.*;
    18  import java.awt.Insets;
    19  import java.awt.GridBagLayout;
    20  import java.awt.GridBagConstraints;
    21  import java.io.*;
    22  import java.util.*;
    23
    24  import gate.*;
    25  import gate.util.*;
    26
    27  /**
    28   * A simple component that allows the user to select a directory and a list of
    29   * permitted extensions for populating a corpus
    30   */
    31
    32  public class CorpusFillerComponent extends JPanel {
    33
    34    /**
    35     * Creates a corpus filler component
    36     */
    37    public CorpusFillerComponent(){
    38      initLocalData();
    39      initGUIComponents();
    40      initListeners();
    41    }
    42
    43    /**
    44     * Inits local variables to default values
    45     */
    46    protected void initLocalData(){
    47      extensions = new ArrayList();
    48    }
    49
    50
    51    /**
    52     * Creates the UI
    53     */
    54    protected void initGUIComponents(){
    55      setLayout(new GridBagLayout());
    56      //first row
    57      GridBagConstraints constraints = new GridBagConstraints();
    58      constraints.gridx = constraints.RELATIVE;
    59      constraints.gridy = 0;
    60      constraints.gridwidth = 2;
    61      constraints.anchor = constraints.WEST;
    62      constraints.fill = constraints.NONE;
    63      constraints.insets = new Insets(0, 0, 0, 5);
    64      add(new JLabel("Directory URL:"), constraints);
    65
    66      constraints = new GridBagConstraints();
    67      constraints.gridx = constraints.RELATIVE;
    68      constraints.gridy = 0;
    69      constraints.gridwidth = 5;
    70      constraints.fill = constraints.HORIZONTAL;
    71      constraints.insets = new Insets(0, 0, 0, 10);
    72      add(urlTextField = new JTextField(40), constraints);
    73
    74      constraints = new GridBagConstraints();
    75      constraints.gridx = constraints.RELATIVE;
    76      constraints.gridy = 0;
    77      constraints.gridwidth = 1;
    78      constraints.anchor = constraints.NORTHWEST;
    79      add(filerBtn = new JButton(MainFrame.getIcon("loadFile.gif")), constraints);
    80
    81      //second row
    82      constraints = new GridBagConstraints();
    83      constraints.gridx = constraints.RELATIVE;
    84      constraints.gridy = 1;
    85      constraints.gridwidth = 2;
    86      constraints.anchor = constraints.WEST;
    87      constraints.fill = constraints.NONE;
    88      constraints.insets = new Insets(0, 0, 0, 5);
    89      add(new JLabel("Extensions:"), constraints);
    90
    91      constraints = new GridBagConstraints();
    92      constraints.gridx = constraints.RELATIVE;
    93      constraints.gridy = 1;
    94      constraints.gridwidth = 5;
    95      constraints.fill = constraints.HORIZONTAL;
    96      constraints.insets = new Insets(0, 0, 0, 10);
    97      add(extensionsTextField = new JTextField(40), constraints);
    98      extensionsTextField.setEditable(false);
    99      extensionsTextField.setText(extensions.toString());
    100
    101     constraints = new GridBagConstraints();
    102     constraints.gridx = constraints.RELATIVE;
    103     constraints.gridy = 1;
    104     constraints.gridwidth = 1;
    105     constraints.anchor = constraints.NORTHWEST;
    106     add(listEditBtn = new JButton(MainFrame.getIcon("editList.gif")), constraints);
    107
    108     //third row
    109     constraints = new GridBagConstraints();
    110     constraints.gridx = constraints.RELATIVE;
    111     constraints.gridy = 2;
    112     constraints.gridwidth = 2;
    113     constraints.anchor = constraints.WEST;
    114     constraints.fill = constraints.NONE;
    115     constraints.insets = new Insets(0, 0, 0, 5);
    116     add(new JLabel("Encoding:"), constraints);
    117
    118
    119     constraints = new GridBagConstraints();
    120     constraints.gridx = constraints.RELATIVE;
    121     constraints.gridy = 2;
    122     constraints.gridwidth = 4;
    123     constraints.fill = constraints.HORIZONTAL;
    124     add(encodingTextField = new JTextField(15), constraints);
    125
    126     //fourth row
    127     recurseCheckBox = new JCheckBox("Recurse directories");
    128     recurseCheckBox.setSelected(true);
    129     recurseCheckBox.setHorizontalTextPosition(SwingConstants.LEFT);
    130     constraints = new GridBagConstraints();
    131     constraints.gridx = constraints.RELATIVE;
    132     constraints.gridy = 3;
    133     constraints.gridwidth = 3;
    134     constraints.anchor = constraints.NORTHWEST;
    135     add(recurseCheckBox, constraints);
    136
    137   }
    138
    139   /**
    140    * Adds listeners for UI components
    141    */
    142   protected void initListeners(){
    143     filerBtn.addActionListener(new ActionListener() {
    144       public void actionPerformed(ActionEvent e) {
    145         JFileChooser filer = MainFrame.getFileChooser();
    146         filer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    147         filer.setDialogTitle("Select a directory");
    148
    149         filer.resetChoosableFileFilters();
    150         filer.setAcceptAllFileFilterUsed(true);
    151         filer.setFileFilter(filer.getAcceptAllFileFilter());
    152         int res = filer.showOpenDialog(CorpusFillerComponent.this);
    153         if(res == filer.APPROVE_OPTION){
    154           try {
    155             urlTextField.setText(filer.getSelectedFile().
    156                                  toURL().toExternalForm());
    157           } catch(IOException ioe){}
    158         }
    159       }
    160     });
    161
    162     listEditBtn.addActionListener(new ActionListener() {
    163       public void actionPerformed(ActionEvent e) {
    164         ListEditorDialog listEditor = new ListEditorDialog(
    165           CorpusFillerComponent.this, extensions, String.class.getName());
    166         List answer = listEditor.showDialog();
    167         if(answer != null){
    168           extensions.clear();
    169           extensions.addAll(answer);
    170           extensionsTextField.setText(extensions.toString());
    171         }
    172       }
    173     });
    174   }
    175
    176   /**
    177    * Sets the values for the URL string. This value is not cached so the set
    178    * will actually the text in the text field itself
    179    */
    180   public void setUrlString(String urlString) {
    181     urlTextField.setText(urlString);
    182   }
    183
    184   /**
    185    * Gets the current text in the URL text field.
    186    */
    187   public String getUrlString() {
    188     return urlTextField.getText();
    189   }
    190
    191   /**
    192    * Gets the encoding selected by the user.
    193    */
    194   public String getEncoding(){
    195     return encodingTextField.getText();
    196   }
    197
    198   /**
    199    * Sets the initila value for the encoding field.
    200    */
    201   public void setEncoding(String enc){
    202     encodingTextField.setText(enc);
    203   }
    204
    205   /**
    206    * Sets the current value for the list of permitted extensions.
    207    */
    208   public void setExtensions(java.util.List extensions) {
    209     this.extensions = extensions;
    210     extensionsTextField.setText(extensions.toString());
    211   }
    212
    213
    214   /**
    215    * Gets the current list of permitted extensions
    216    */
    217   public java.util.List getExtensions() {
    218     return extensions;
    219   }
    220
    221   /**
    222    * Test code
    223    */
    224   static public void main(String[] args){
    225     try{
    226       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    227       Gate.init();
    228     }catch(Exception e){
    229       e.printStackTrace();
    230     }
    231     JFrame frame = new JFrame("Foo");
    232     CorpusFillerComponent comp = new CorpusFillerComponent();
    233     frame.getContentPane().add(comp);
    234     frame.pack();
    235     frame.setResizable(false);
    236     frame.setVisible(true);
    237   }
    238
    239   /**
    240    * Should the directory parsed recursively?
    241    */
    242   public void setRecurseDirectories(boolean recurseDirectories) {
    243     recurseCheckBox.setSelected(recurseDirectories);
    244   }
    245
    246   /**
    247    * Should the directory parsed recursively?
    248    */
    249   public boolean isRecurseDirectories() {
    250     return recurseCheckBox.isSelected();
    251   }
    252
    253   /**
    254    * The text field for the directory URL
    255    */
    256   JTextField urlTextField;
    257
    258   /**
    259    * The buttons that opens the file chooser
    260    */
    261   JButton filerBtn;
    262
    263   /**
    264    * The text field for the permitted extensions
    265    */
    266   JTextField extensionsTextField;
    267
    268   /**
    269    * The buton that opens the list editor for the extensions
    270    */
    271   JButton listEditBtn;
    272
    273   /**
    274    * The checkbox for recurse directories
    275    */
    276   JCheckBox recurseCheckBox;
    277
    278   /**
    279    * The textField for the encoding
    280    */
    281   JTextField encodingTextField;
    282   /**
    283    * The list of permitted extensions.
    284    */
    285   private java.util.List extensions;
    286 }

| CorpusFillerComponent |