1   /*
2    *  Copyright (c) 1998-2004, 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.4 2004/07/21 17:10:06 akshay 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 = Class.forName(anIREngineClassName);
95          IREngine engine = (IREngine)aClass.newInstance();
96          engineByName.put(engine.getName(), engine);
97        }catch(ClassNotFoundException cnfe){
98        }catch(IllegalAccessException iae){
99        }catch(InstantiationException ie){
100       }
101     }
102 
103     String[] names = new String[engineByName.size()];
104     int i = 0;
105     Iterator namesIter = engineByName.keySet().iterator();
106     while(namesIter.hasNext()){
107       names[i++] = (String)namesIter.next();
108     }
109     irEngineCombo.setModel(new DefaultComboBoxModel(names));
110     if(oldIREngineName != null && engineByName.containsKey(oldIREngineName)){
111       irEngineCombo.setSelectedItem(oldIREngineName);
112     }else if(engineByName.size() > 0) irEngineCombo.setSelectedIndex(0);
113   }
114 
115   protected void initListeners(){
116   }
117 
118 
119   protected class SelectDirAction extends AbstractAction{
120     public SelectDirAction(){
121       super(null, MainFrame.getIcon("loadFile.gif"));
122       putValue(SHORT_DESCRIPTION, "Click to open a file chooser!");
123     }
124 
125     public void actionPerformed(ActionEvent e){
126       JFileChooser fileChooser = MainFrame.getFileChooser();
127       fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
128       fileChooser.setDialogTitle("Select a directory for the index files");
129       int res = fileChooser.showOpenDialog(CreateIndexGUI.this);
130       if(res == JFileChooser.APPROVE_OPTION) indexLocationTextField.
131                                             setText(fileChooser.
132                                             getSelectedFile().toString());
133     }
134   }
135 
136   protected class EditFeatureListAction extends AbstractAction{
137     public EditFeatureListAction(){
138       super(null, MainFrame.getIcon("editList.gif"));
139       putValue(SHORT_DESCRIPTION, "Click to edit list!");
140     }
141 
142     public void actionPerformed(ActionEvent e){
143       ListEditorDialog listEditor = new ListEditorDialog(CreateIndexGUI.this,
144                                                          featuresList,
145                                                          "java.lang.String");
146       List result = listEditor.showDialog();
147       if(result != null){
148         featuresList.clear();
149         featuresList.addAll(result);
150         if(featuresList.size() > 0){
151           String text = "[" + featuresList.get(0).toString();
152           for(int j = 1; j < featuresList.size(); j++){
153             text += ", " + featuresList.get(j).toString();
154           }
155           text += "]";
156           featuresListTextField.setText(text);
157         }else{
158           featuresListTextField.setText("");
159         }
160       }
161     }
162   }
163 
164   public boolean getUseDocumentContent(){
165     return useContentChk.isSelected();
166   }
167 
168   public List getFeaturesList(){
169     return featuresList;
170   }
171 
172   public String getIndexLocation(){
173     return indexLocationTextField.getText();
174   }
175 
176   public IREngine getIREngine(){
177     return (IREngine)engineByName.get(irEngineCombo.getSelectedItem());
178   }
179 
180   /**
181    * Combobox for selecting IR engine.
182    */
183   JComboBox irEngineCombo;
184 
185   /**
186    * Text field for the location of the index.
187    */
188   JTextField indexLocationTextField;
189 
190   /**
191    * Checkbox for content used.
192    */
193   JCheckBox useContentChk;
194 
195   /**
196    * Text field for the list of features.
197    */
198   JTextField featuresListTextField;
199 
200   /**
201    * The list of features.
202    */
203   List featuresList;
204 
205   /**
206    * A map from IREngine name to IREngine class name.
207    */
208   SortedMap engineByName;
209 
210 }