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