1   /*
2    *  CreateIndexDialog.java
3    *
4    *  Copyright (c) 1998-2001, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Rosen Marinov, 19/Apr/2002
12   *
13   */
14  
15  package gate.gui;
16  
17  import javax.swing.*;
18  import java.awt.*;
19  import java.awt.event.*;
20  import java.io.*;
21  import java.util.*;
22  
23  import gate.creole.ir.*;
24  import gate.creole.ir.lucene.*;
25  import gate.*;
26  import gate.gui.*;
27  
28  public class CreateIndexDialog extends JDialog {
29  
30    private IndexedCorpus ic;
31  
32      protected JPanel panel1 = new JPanel();
33      protected JLabel indexTypeLabel = new JLabel();
34      protected JComboBox jComboIType = new JComboBox();
35      protected JLabel locationLabel = new JLabel();
36      protected JTextField locationTextField = new JTextField();
37      protected JButton browse = new JButton();
38      protected JLabel featureLable = new JLabel();
39      protected JTextField featureTextField = new JTextField();
40      protected JList jList1 = null;
41      protected JScrollPane scrollPane = new JScrollPane();
42      protected JButton addButton = new JButton();
43      protected JCheckBox content = new JCheckBox();
44      protected JButton createButton = new JButton();
45      protected JButton cancelButton = new JButton();
46      protected GridBagLayout gridBagLayout1 = new GridBagLayout();
47  
48      private Vector fields = new Vector();
49  
50    public CreateIndexDialog(Frame owner, IndexedCorpus ic){
51      super(owner, true);
52      this.ic = ic;
53      init();
54      pack();
55    }
56  
57    public CreateIndexDialog(Dialog owner, IndexedCorpus ic){
58      super(owner, true);
59      this.ic = ic;
60      init();
61    }
62  
63    private void init(){
64          panel1.setLayout(gridBagLayout1);
65          indexTypeLabel.setText("Index Type");
66          locationLabel.setText("Location");
67          browse.setToolTipText("Browse location directory");
68          browse.setText("Browse");
69          featureLable.setText("Feature/Field  Name");
70          addButton.setText("Add Field");
71          content.setSelected(true);
72          content.setText("Content");
73          createButton.setText("Create");
74          cancelButton.setText("Cancel");
75  
76          jComboIType.addItem("Lucene");
77          jComboIType.setSelectedItem("Lucene");
78  
79  
80          jList1 = new JList(fields);
81          scrollPane.getViewport().setView(jList1);
82  
83          this.getContentPane().add(panel1, BorderLayout.NORTH);
84  
85          cancelButton.addActionListener( new ActionListener(){
86            public void actionPerformed(ActionEvent e){
87              cancelAction();
88            }
89          });
90  
91          createButton.addActionListener( new ActionListener(){
92            public void actionPerformed(ActionEvent e){
93              createAction();
94            }
95          });
96  
97          browse.addActionListener( new ActionListener(){
98            public void actionPerformed(ActionEvent e){
99              browseAction();
100           }
101         });
102 
103         addButton.addActionListener( new ActionListener(){
104           public void actionPerformed(ActionEvent e){
105             addAction();
106           }
107         });
108 
109         panel1.add(locationLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
110             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
111         panel1.add(indexTypeLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
112             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
113         panel1.add(featureLable, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
114             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
115         panel1.add(locationTextField, new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0
116             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
117         panel1.add(jComboIType, new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0
118             ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
119         panel1.add(browse, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0
120             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
121         panel1.add(addButton, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0
122             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
123         panel1.add(featureTextField, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0
124             ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
125         panel1.add(jList1, new GridBagConstraints(1, 3, 2, 1, 1.0, 1.0
126             ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 185, 87));
127         panel1.add(content, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0
128             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
129         panel1.add(createButton, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0
130             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
131         panel1.add(cancelButton, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0
132             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
133 
134   }
135 
136   private void cancelAction(){
137     this.dispose();
138   }
139 
140   private void createAction(){
141     DefaultIndexDefinition did = new DefaultIndexDefinition();
142 //    did.setIndexType(GateConstants.IR_LUCENE_INVFILE);
143 
144     String location = locationTextField.getText();
145     did.setIndexLocation(location);
146 
147     if (content.isSelected()) {
148       did.addIndexField(new IndexField("body", new DocumentContentReader(), false));
149     }
150 
151     for (int i = 0; i<fields.size(); i++){
152       did.addIndexField(new IndexField(fields.elementAt(i).toString(), null, false));
153     }
154 
155     ic.setIndexDefinition(did);
156 
157     try {
158       ic.getIndexManager().deleteIndex();
159       ic.getIndexManager().createIndex();
160     } catch (IndexException e){
161       e.printStackTrace();
162     }
163     this.dispose();
164   }
165 
166   private void browseAction(){
167     JFileChooser fc =  MainFrame.getFileChooser();
168     fc.setMultiSelectionEnabled(false);
169     fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
170     fc.setDialogTitle("Select location directory");
171     fc.setSelectedFiles(null);
172     int res = fc.showDialog(this, "Select");
173     if (res == fc.APPROVE_OPTION){
174       File f = fc.getSelectedFile();
175       locationTextField.setText(f.getAbsolutePath());
176     }
177   }
178 
179   private void addAction(){
180     fields.add(featureTextField.getText());
181   }
182 
183 }