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