CreateIndexGUI.java
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  *  Valentin Tablan 17/05/2002
011  *
012  *  $Id: CreateIndexGUI.java 12006 2009-12-01 17:24:28Z thomas_heitz $
013  *
014  */
015 package gate.gui;
016 
017 import java.awt.*;
018 import java.awt.event.ActionEvent;
019 import java.util.*;
020 import java.util.List;
021 
022 import javax.swing.*;
023 
024 import gate.Gate;
025 import gate.creole.ir.IREngine;
026 
027 /**
028  * Provides a gui for creating a IR index on a corpus.
029  */
030 public class CreateIndexGUI extends JPanel {
031 
032   public CreateIndexGUI() {
033     initLocalData();
034     initGUIComponents();
035     initListeners();
036   }
037 
038   protected void initLocalData(){
039     featuresList = new ArrayList();
040     engineByName = new TreeMap();
041   }
042 
043   protected void initGUIComponents(){
044     setLayout(new GridBagLayout());
045 
046     GridBagConstraints constraints = new GridBagConstraints();
047     constraints.anchor = GridBagConstraints.WEST;
048     constraints.fill = GridBagConstraints.HORIZONTAL;
049     constraints.insets = new Insets(2525);
050 
051     //first line
052     constraints.gridy = 0;
053     constraints.gridwidth = 2;
054     add(new JLabel("IR Engine type:"), constraints);
055     constraints.gridwidth = 4;
056 
057     irEngineCombo = new JComboBox();
058     add(irEngineCombo, constraints);
059 
060     //second line
061     constraints.gridy = 1;
062     constraints.gridwidth = 2;
063     add(new JLabel("Index location:"), constraints);
064     constraints.gridwidth = 4;
065     indexLocationTextField = new JTextField(40);
066     add(indexLocationTextField, constraints);
067     constraints.gridwidth = 1;
068     add(new JButton(new SelectDirAction()), constraints);
069 
070     //third line
071     constraints.gridy =2;
072     constraints.gridwidth = 2;
073     add(new JLabel("Features to index:"), constraints);
074     featuresListTextField = new JTextField(40);
075     featuresListTextField.setEditable(false);
076     constraints.gridwidth = 4;
077     add(featuresListTextField, constraints);
078     constraints.gridwidth = 1;
079     add(new JButton(new EditFeatureListAction()), constraints);
080 
081     //fourth line
082     constraints.gridy = 3;
083     constraints.gridwidth = 4;
084     useContentChk = new JCheckBox("Use document content"true);
085     add(useContentChk, constraints);
086 
087     //populate engine names combo
088     String oldIREngineName = (String)irEngineCombo.getSelectedItem();
089 
090     List irEngines = new ArrayList(Gate.getRegisteredIREngines());
091     engineByName.clear();
092     for(int i = 0; i < irEngines.size(); i++){
093       String anIREngineClassName = (String)irEngines.get(i);
094       try{
095         Class aClass =
096           Class.forName(anIREngineClassName, true, Gate.getClassLoader());
097         IREngine engine = (IREngine)aClass.newInstance();
098         engineByName.put(engine.getName(), engine);
099       }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() 0irEngineCombo.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("open-file"));
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 == JFileChooser.APPROVE_OPTIONindexLocationTextField.
133                                             setText(fileChooser.
134                                             getSelectedFile().toString());
135     }
136   }
137 
138   protected class EditFeatureListAction extends AbstractAction{
139     public EditFeatureListAction(){
140       super(null, MainFrame.getIcon("edit-list"));
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 }