Package enzyme.docindexer

Source Code of enzyme.docindexer.DocIndexerUI

package enzyme.docindexer;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.lucene.analysis.ontology.ChineseOntologyAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.TermVector;
import org.apache.lucene.index.IndexWriter;
import org.hibernate.Session;

import enzyme.database.Doc;
import enzyme.database.HibernateUtil;

public class DocIndexerUI {
  private JFrame frame;

  private JTextField ontologyLocation;

  private JButton chooseOntologyLocation;

  private JButton runIndexing;

  private JProgressBar progressBar;

  private JTextArea docList;

  private JScrollPane docListSP;

  private String indexLocation;

  private String weightLocation;

  private ChineseOntologyAnalyzer analyzer;

  private JFileChooser owlFc;

  private IndexWriter writer;

  public DocIndexerUI() {
    //��ʼ������
    initFrame();
    //��ʼ����ʾ����
    initContentPane();
    //��ʼ����ť
    initActions();
    //��ʼ��owl�ļ�ѡ����
    initFc();
    frame.setVisible(true);
  }

  //��ʼ������
  private void initFrame() {
    frame = new JFrame("DocIndexer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(500, 400);

    double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
    double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
    frame.setLocation((int) (width - frame.getWidth()) / 2,
        (int) (height - frame.getHeight()) / 2);

    frame.setResizable(false);
  }

  //ɾ��ָ��Ŀ¼�µ������ļ�
  private boolean delAllFile(String strFilePath) {
    boolean bool = false;
    try {
      File f = new File(strFilePath);
      if (f.exists() && f.isDirectory()) {
        if (f.listFiles().length == 0)
          f.delete();
        else {
          File[] flist = f.listFiles();
          for (int i = 0; i < flist.length; i++) {
            if (flist[i].isDirectory()) {
              delAllFile(flist[i].getAbsolutePath());
            }
            flist[i].delete();
          }
        }

      }
      f.delete();
      bool = true;
    } catch (Exception ex) {
      ex.printStackTrace();
      JOptionPane.showMessageDialog(frame, ex.getMessage(), "Error!",
          JOptionPane.ERROR_MESSAGE);
    }
    return bool;
  }

  //��ʼ����ť
  private void initActions() {

    chooseOntologyLocation.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {

        int returnVal = owlFc.showOpenDialog(frame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {

          String location = owlFc.getSelectedFile().getAbsolutePath();
          System.out.println(location);

          ontologyLocation.setText(location);

          indexLocation = location.substring(0, location
              .lastIndexOf("\\"))
              + "\\index\\";
          weightLocation = location.substring(0, location
              .lastIndexOf("\\"))
              + "Weights.properties";

          try {
            delAllFile(indexLocation);

            analyzer = new ChineseOntologyAnalyzer(location,
                weightLocation);

            runIndexing.setEnabled(true);
          } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(frame, ex.getMessage(),
                "Error!", JOptionPane.ERROR_MESSAGE);
          }

        }

      }

    });

    runIndexing.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {

        new Thread() {
          public void run() {
            runIndexing.setEnabled(false);
            try {

              //��������
              writer = new IndexWriter(new File(indexLocation),
                  analyzer, true,
                  IndexWriter.MaxFieldLength.LIMITED);

              //�����ݿ�
              Session session = HibernateUtil.getSessionFactory()
                  .openSession();
              session.beginTransaction();
             
              //��ȡ���ݣ��ĵ������Ϣ��
              List docs = session.createQuery(
                  "select doc from Doc as doc").list();
             
              //��ʾ����������
              int size = docs.size();
              progressBar.setMaximum(size);
              progressBar.setValue(0);
              progressBar.setIndeterminate(false);

              //Ϊÿ���ĵ�����
              for (int i = 0; i < size; i++) {
               
                //���½���������
                progressBar.setValue(i + 1);
                Doc doc = (Doc) docs.get(i);
                docList.append(doc.getId() + ":"
                    + doc.getDocTitle() + "\n");
                progressBar.repaint();
                docListSP.getVerticalScrollBar().setValue(
                    docListSP.getVerticalScrollBar()
                        .getMaximum());
                progressBar.repaint();

               
                Document d = new Document();
                //Ϊ�ĵ���ÿ���ֶμ���������
                d.add(new Field("id", "" + doc.getId(),
                    Field.Store.YES,
                    Field.Index.NOT_ANALYZED));
                d.add(new Field("docAuthor",
                    doc.getDocAuthor() == null ? "" : doc
                        .getDocAuthor(),
                    Field.Store.YES,
                    Field.Index.NOT_ANALYZED));
                d.add(new Field("docContent", doc
                    .getDocContent() == null ? "" : doc
                    .getDocContent(), Field.Store.NO,
                    Field.Index.ANALYZED));
                d.add(new Field("docFilePath", doc
                    .getDocFilePath() == null ? "" : doc
                    .getDocFilePath(), Field.Store.YES,
                    Field.Index.NOT_ANALYZED));
                d.add(new Field("docKey",
                    doc.getDocKey() == null ? "" : doc
                        .getDocKey(), Field.Store.YES,
                    Field.Index.NOT_ANALYZED));
                d.add(new Field("docPublishDate", doc
                    .getDocPublishDate() == null ? "" : doc
                    .getDocPublishDate(), Field.Store.YES,
                    Field.Index.NOT_ANALYZED));
                d.add(new Field("docTitle",
                    doc.getDocTitle() == null ? "" : doc
                        .getDocTitle(),
                    Field.Store.YES,
                    Field.Index.NOT_ANALYZED));
                d.add(new Field("docTypeId", ""
                    + doc.getDocTypeId(), Field.Store.YES,
                    Field.Index.NOT_ANALYZED));
                writer.addDocument(d);

              }
              System.out.println(writer.docCount());
             
              //�رձ�������
              writer.close();
              JOptionPane.showMessageDialog(frame,
                  "Index completed!", "Index completed!",
                  JOptionPane.INFORMATION_MESSAGE);

            } catch (Exception ex) {
              ex.printStackTrace();
              JOptionPane.showMessageDialog(frame, ex
                  .getMessage(), "Error!",
                  JOptionPane.ERROR_MESSAGE);
            } catch (ExceptionInInitializerError ex) {
              ex.printStackTrace();
              JOptionPane.showMessageDialog(frame,
                  "Cannot open database!", "Error!",
                  JOptionPane.ERROR_MESSAGE);
              System.exit(0);
            }
            runIndexing.setEnabled(true);
          }
        }.start();
      }

    });

  }

  //��ʼ����ʾ����
  private void initContentPane() {

    JPanel north = new JPanel();
    ontologyLocation = new JTextField(15);
    ontologyLocation.setEditable(false);
    chooseOntologyLocation = new JButton("Choose ontology...");
    runIndexing = new JButton("Run indexing!");
    runIndexing.setEnabled(false);
    north.add(ontologyLocation);
    north.add(chooseOntologyLocation);
    north.add(runIndexing);

    JPanel center = new JPanel();
    progressBar = new JProgressBar();
    progressBar.setStringPainted(true);
    center.add(progressBar);

    JPanel south = new JPanel();
    docList = new JTextArea(16, 40);
    docListSP = new JScrollPane(docList);
    south.add(docListSP);

    frame.getContentPane().add(north, BorderLayout.NORTH);
    frame.getContentPane().add(center, BorderLayout.CENTER);
    frame.getContentPane().add(south, BorderLayout.SOUTH);

  }

  //��ʼ��owl�ļ�ѡ����
  private void initFc() {

    owlFc = new JFileChooser();
    OWLFileFilter owlFilter = new OWLFileFilter();
    owlFilter.addExtension("owl");
    owlFilter.addExtension("rdf");
    owlFilter.addExtension("xml");
    owlFilter.setDescription("OWL file in standard RDF/OWL format");
    owlFc.setFileFilter(owlFilter);

  }

  public static void main(String[] args) {
    new DocIndexerUI();
  }
}
TOP

Related Classes of enzyme.docindexer.DocIndexerUI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.