CreateIndexGUI.java
001 /*
002  *  Copyright (c) 1995-2012, 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 17858 2014-04-17 14:10:55Z markagreenwood $
013  *
014  */
015 package gate.gui;
016 
017 import gate.Gate;
018 import gate.creole.ir.IREngine;
019 
020 import java.awt.GridBagConstraints;
021 import java.awt.GridBagLayout;
022 import java.awt.Insets;
023 import java.awt.event.ActionEvent;
024 import java.util.ArrayList;
025 import java.util.Iterator;
026 import java.util.List;
027 import java.util.SortedMap;
028 import java.util.TreeMap;
029 
030 import javax.swing.AbstractAction;
031 import javax.swing.DefaultComboBoxModel;
032 import javax.swing.JButton;
033 import javax.swing.JCheckBox;
034 import javax.swing.JComboBox;
035 import javax.swing.JFileChooser;
036 import javax.swing.JLabel;
037 import javax.swing.JPanel;
038 import javax.swing.JTextField;
039 
040 /**
041  * Provides a gui for creating a IR index on a corpus.
042  */
043 @SuppressWarnings("serial")
044 public class CreateIndexGUI extends JPanel {
045 
046   public CreateIndexGUI() {
047     initLocalData();
048     initGUIComponents();
049     initListeners();
050   }
051 
052   protected void initLocalData(){
053     featuresList = new ArrayList<String>();
054     engineByName = new TreeMap<String, IREngine>();
055   }
056 
057   protected void initGUIComponents(){
058     setLayout(new GridBagLayout());
059 
060     GridBagConstraints constraints = new GridBagConstraints();
061     constraints.anchor = GridBagConstraints.WEST;
062     constraints.fill = GridBagConstraints.HORIZONTAL;
063     constraints.insets = new Insets(2525);
064 
065     //first line
066     constraints.gridy = 0;
067     constraints.gridwidth = 2;
068     add(new JLabel("IR Engine type:"), constraints);
069     constraints.gridwidth = 4;
070 
071     irEngineCombo = new JComboBox<String>();
072     add(irEngineCombo, constraints);
073 
074     //second line
075     constraints.gridy = 1;
076     constraints.gridwidth = 2;
077     add(new JLabel("Index location:"), constraints);
078     constraints.gridwidth = 4;
079     indexLocationTextField = new JTextField(40);
080     add(indexLocationTextField, constraints);
081     constraints.gridwidth = 1;
082     add(new JButton(new SelectDirAction()), constraints);
083 
084     //third line
085     constraints.gridy =2;
086     constraints.gridwidth = 2;
087     add(new JLabel("Features to index:"), constraints);
088     featuresListTextField = new JTextField(40);
089     featuresListTextField.setEditable(false);
090     constraints.gridwidth = 4;
091     add(featuresListTextField, constraints);
092     constraints.gridwidth = 1;
093     add(new JButton(new EditFeatureListAction()), constraints);
094 
095     //fourth line
096     constraints.gridy = 3;
097     constraints.gridwidth = 4;
098     useContentChk = new JCheckBox("Use document content"true);
099     add(useContentChk, constraints);
100 
101     //populate engine names combo
102     String oldIREngineName = (String)irEngineCombo.getSelectedItem();
103 
104     List<String> irEngines = new ArrayList<String>(Gate.getRegisteredIREngines());
105     engineByName.clear();
106     for(int i = 0; i < irEngines.size(); i++){
107       String anIREngineClassName = irEngines.get(i);
108       try{
109         Class<?> aClass =
110           Class.forName(anIREngineClassName, true, Gate.getClassLoader());
111         IREngine engine = (IREngine)aClass.newInstance();
112         engineByName.put(engine.getName(), engine);
113       }catch(ClassNotFoundException cnfe){
114       }catch(IllegalAccessException iae){
115       }catch(InstantiationException ie){
116       }
117     }
118 
119     String[] names = new String[engineByName.size()];
120     int i = 0;
121     Iterator<String> namesIter = engineByName.keySet().iterator();
122     while(namesIter.hasNext()){
123       names[i++= namesIter.next();
124     }
125     irEngineCombo.setModel(new DefaultComboBoxModel<String>(names));
126     if(oldIREngineName != null && engineByName.containsKey(oldIREngineName)){
127       irEngineCombo.setSelectedItem(oldIREngineName);
128     }else if(engineByName.size() 0irEngineCombo.setSelectedIndex(0);
129   }
130 
131   protected void initListeners(){
132   }
133 
134 
135   protected class SelectDirAction extends AbstractAction{
136     public SelectDirAction(){
137       super(null, MainFrame.getIcon("open-file"));
138       putValue(SHORT_DESCRIPTION, "Click to open a file chooser!");
139     }
140 
141     @Override
142     public void actionPerformed(ActionEvent e){
143       JFileChooser fileChooser = MainFrame.getFileChooser();
144       fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
145       fileChooser.setDialogTitle("Select a directory for the index files");
146       int res = fileChooser.showOpenDialog(CreateIndexGUI.this);
147       if(res == JFileChooser.APPROVE_OPTIONindexLocationTextField.
148                                             setText(fileChooser.
149                                             getSelectedFile().toString());
150     }
151   }
152 
153   protected class EditFeatureListAction extends AbstractAction{
154     public EditFeatureListAction(){
155       super(null, MainFrame.getIcon("edit-list"));
156       putValue(SHORT_DESCRIPTION, "Click to edit list!");
157     }
158 
159     @Override
160     public void actionPerformed(ActionEvent e){
161       ListEditorDialog listEditor = new ListEditorDialog(CreateIndexGUI.this,
162                                                          featuresList,
163                                                          "java.lang.String");
164       @SuppressWarnings("unchecked")
165       List<String> result = listEditor.showDialog();
166       if(result != null){
167         featuresList.clear();
168         featuresList.addAll(result);
169         if(featuresList.size() 0){
170           String text = "[" + featuresList.get(0).toString();
171           for(int j = 1; j < featuresList.size(); j++){
172             text += ", " + featuresList.get(j).toString();
173           }
174           text += "]";
175           featuresListTextField.setText(text);
176         }else{
177           featuresListTextField.setText("");
178         }
179       }
180     }
181   }
182 
183   public boolean getUseDocumentContent(){
184     return useContentChk.isSelected();
185   }
186 
187   public List<String> getFeaturesList(){
188     return featuresList;
189   }
190 
191   public String getIndexLocation(){
192     return indexLocationTextField.getText();
193   }
194 
195   public IREngine getIREngine(){
196     return engineByName.get(irEngineCombo.getSelectedItem());
197   }
198 
199   /**
200    * Combobox for selecting IR engine.
201    */
202   JComboBox<String> irEngineCombo;
203 
204   /**
205    * Text field for the location of the index.
206    */
207   JTextField indexLocationTextField;
208 
209   /**
210    * Checkbox for content used.
211    */
212   JCheckBox useContentChk;
213 
214   /**
215    * Text field for the list of features.
216    */
217   JTextField featuresListTextField;
218 
219   /**
220    * The list of features.
221    */
222   List<String> featuresList;
223 
224   /**
225    * A map from IREngine name to IREngine class name.
226    */
227   SortedMap<String,IREngine> engineByName;
228 
229 }