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 17/05/2002
10   *
11   *  $Id: CreateIndexGUI.java,v 1.1 2002/05/20 10:31:14 valyt Exp $
12   *
13   */
14  package gate.gui;
15  
16  import javax.swing.*;
17  import java.awt.event.*;
18  import java.awt.Insets;
19  import java.awt.GridBagLayout;
20  import java.awt.GridBagConstraints;
21  import java.util.*;
22  import java.io.File;
23  
24  import gate.Gate;
25  import gate.creole.ir.IREngine;
26  import gate.util.*;
27  
28  /**
29   * Provides a gui for creating a IR index on a corpus.
30   */
31  public class CreateIndexGUI extends JPanel {
32  
33    public CreateIndexGUI() {
34      initLocalData();
35      initGUIComponents();
36      initListeners();
37    }
38  
39    protected void initLocalData(){
40      featuresList = new ArrayList();
41      engineByName = new TreeMap();
42    }
43  
44    protected void initGUIComponents(){
45      setLayout(new GridBagLayout());
46  
47      GridBagConstraints constraints = new GridBagConstraints();
48      constraints.anchor = constraints.WEST;
49      constraints.fill = constraints.HORIZONTAL;
50      constraints.insets = new Insets(2, 5, 2, 5);
51  
52      //first line
53      constraints.gridy = 0;
54      constraints.gridwidth = 2;
55      add(new JLabel("IR Engine type:"), constraints);
56      constraints.gridwidth = 4;
57  
58      irEngineCombo = new JComboBox();
59      add(irEngineCombo, constraints);
60  
61      //second line
62      constraints.gridy = 1;
63      constraints.gridwidth = 2;
64      add(new JLabel("Index location:"), constraints);
65      constraints.gridwidth = 4;
66      indexLocationTextField = new JTextField(40);
67      add(indexLocationTextField, constraints);
68      constraints.gridwidth = 1;
69      add(new JButton(new SelectDirAction()), constraints);
70  
71      //third line
72      constraints.gridy =2;
73      constraints.gridwidth = 2;
74      add(new JLabel("Features to index:"), constraints);
75      featuresListTextField = new JTextField(40);
76      featuresListTextField.setEditable(false);
77      constraints.gridwidth = 4;
78      add(featuresListTextField, constraints);
79      constraints.gridwidth = 1;
80      add(new JButton(new EditFeatureListAction()), constraints);
81  
82      //fourth line
83      constraints.gridy = 3;
84      constraints.gridwidth = 4;
85      useContentChk = new JCheckBox("Use document content", true);
86      add(useContentChk, constraints);
87  
88      //populate engine names combo
89      String oldIREngineName = (String)irEngineCombo.getSelectedItem();
90  
91      List irEngines = new ArrayList(Gate.getRegisteredIREngines());
92      engineByName.clear();
93      for(int i = 0; i < irEngines.size(); i++){
94        String anIREngineClassName = (String)irEngines.get(i);
95        try{
96          Class aClass = Class.forName(anIREngineClassName);
97          IREngine engine = (IREngine)aClass.newInstance();
98          engineByName.put(engine.getName(), engine);
99        }catch(ClassNotFoundException cnfe){
100       }catch(IllegalAccessException iae){
101       }catch(InstantiationException ie){
102       }
103     }
104 
105     String[] names = new String[engineByName.size()];
106     int i = 0;
107     Iterator namesIter = engineByName.keySet().iterator();
108     while(namesIter.hasNext()){
109       names[i++] = (String)namesIter.next();
110     }
111     irEngineCombo.setModel(new DefaultComboBoxModel(names));
112     if(oldIREngineName != null && engineByName.containsKey(oldIREngineName)){
113       irEngineCombo.setSelectedItem(oldIREngineName);
114     }else if(engineByName.size() > 0) irEngineCombo.setSelectedIndex(0);
115   }
116 
117   protected void initListeners(){
118   }
119 
120 
121   protected class SelectDirAction extends AbstractAction{
122     public SelectDirAction(){
123       super(null, MainFrame.getIcon("loadFile.gif"));
124       putValue(SHORT_DESCRIPTION, "Click to open a file chooser!");
125     }
126 
127     public void actionPerformed(ActionEvent e){
128       JFileChooser fileChooser = MainFrame.getFileChooser();
129       fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
130       fileChooser.setDialogTitle("Select a directory for the index files");
131       int res = fileChooser.showOpenDialog(CreateIndexGUI.this);
132       if(res == fileChooser.APPROVE_OPTION) indexLocationTextField.
133                                             setText(fileChooser.
134                                             getSelectedFile().toString());
135     }
136   }
137 
138   protected class EditFeatureListAction extends AbstractAction{
139     public EditFeatureListAction(){
140       super(null, MainFrame.getIcon("editList.gif"));
141       putValue(SHORT_DESCRIPTION, "Click to edit list!");
142     }
143 
144     public void actionPerformed(ActionEvent e){
145       ListEditorDialog listEditor = new ListEditorDialog(CreateIndexGUI.this,
146                                                          featuresList,
147                                                          "java.lang.String");
148       List result = listEditor.showDialog();
149       if(result != null){
150         featuresList.clear();
151         featuresList.addAll(result);
152         if(featuresList.size() > 0){
153           String text = "[" + featuresList.get(0).toString();
154           for(int j = 1; j < featuresList.size(); j++){
155             text += ", " + featuresList.get(j).toString();
156           }
157           text += "]";
158           featuresListTextField.setText(text);
159         }else{
160           featuresListTextField.setText("");
161         }
162       }
163     }
164   }
165 
166   public boolean getUseDocumentContent(){
167     return useContentChk.isSelected();
168   }
169 
170   public List getFeaturesList(){
171     return featuresList;
172   }
173 
174   public String getIndexLocation(){
175     return indexLocationTextField.getText();
176   }
177 
178   public IREngine getIREngine(){
179     return (IREngine)engineByName.get(irEngineCombo.getSelectedItem());
180   }
181 
182   /**
183    * Combobox for selecting IR engine.
184    */
185   JComboBox irEngineCombo;
186 
187   /**
188    * Text field for the location of the index.
189    */
190   JTextField indexLocationTextField;
191 
192   /**
193    * Checkbox for content used.
194    */
195   JCheckBox useContentChk;
196 
197   /**
198    * Text field for the list of features.
199    */
200   JTextField featuresListTextField;
201 
202   /**
203    * The list of features.
204    */
205   List featuresList;
206 
207   /**
208    * A map from IREngine name to IREngine class name.
209    */
210   SortedMap engineByName;
211 
212 }