001 /*
002 * CreateIndexDialog.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Rosen Marinov, 19/Apr/2002
013 *
014 */
015
016 package gate.gui;
017
018 import java.awt.*;
019 import java.awt.event.ActionEvent;
020 import java.awt.event.ActionListener;
021 import java.io.File;
022 import java.util.Vector;
023
024 import javax.swing.*;
025
026 import gate.creole.ir.*;
027
028 public class CreateIndexDialog extends JDialog {
029
030 private IndexedCorpus ic;
031
032 protected JPanel panel1 = new JPanel();
033 protected JLabel indexTypeLabel = new JLabel();
034 protected JComboBox jComboIType = new JComboBox();
035 protected JLabel locationLabel = new JLabel();
036 protected JTextField locationTextField = new JTextField();
037 protected JButton browse = new JButton();
038 protected JLabel featureLable = new JLabel();
039 protected JTextField featureTextField = new JTextField();
040 protected JList jList1 = null;
041 protected JScrollPane scrollPane = new JScrollPane();
042 protected JButton addButton = new JButton();
043 protected JCheckBox content = new JCheckBox();
044 protected JButton createButton = new JButton();
045 protected JButton cancelButton = new JButton();
046 protected GridBagLayout gridBagLayout1 = new GridBagLayout();
047
048 private Vector fields = new Vector();
049
050 public CreateIndexDialog(Frame owner, IndexedCorpus ic){
051 super(owner, true);
052 this.ic = ic;
053 init();
054 pack();
055 }
056
057 public CreateIndexDialog(Dialog owner, IndexedCorpus ic){
058 super(owner, true);
059 this.ic = ic;
060 init();
061 }
062
063 private void init(){
064 panel1.setLayout(gridBagLayout1);
065 indexTypeLabel.setText("Index Type");
066 locationLabel.setText("Location");
067 browse.setToolTipText("Browse location directory");
068 browse.setText("Browse");
069 featureLable.setText("Feature/Field Name");
070 addButton.setText("Add Field");
071 content.setSelected(true);
072 content.setText("Content");
073 createButton.setText("Create");
074 cancelButton.setText("Cancel");
075
076 jComboIType.addItem("Lucene");
077 jComboIType.setSelectedItem("Lucene");
078
079
080 jList1 = new JList(fields);
081 scrollPane.getViewport().setView(jList1);
082
083 this.getContentPane().add(panel1, BorderLayout.NORTH);
084
085 cancelButton.addActionListener( new ActionListener(){
086 public void actionPerformed(ActionEvent e){
087 cancelAction();
088 }
089 });
090
091 createButton.addActionListener( new ActionListener(){
092 public void actionPerformed(ActionEvent e){
093 createAction();
094 }
095 });
096
097 browse.addActionListener( new ActionListener(){
098 public void actionPerformed(ActionEvent e){
099 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 == JFileChooser.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 }
|