⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lucenesearchpanel.java

📁 图像检索的代码b
💻 JAVA
字号:
/*
 * This file is part of Caliph & Emir.
 *
 * Caliph & Emir is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Caliph & Emir is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Caliph & Emir; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * --------------------
 * (c) 2002-2004 by Mathias Lux (mathias@juggle.at)
 * http://www.juggle.at
 */

/*
 * Created by Mathias Lux, mathias@juggle.at
 * User: Mathias Lux, mathias@juggle.at
 * Date: 09.09.2002
 * Time: 20:51:41
 */

package at.lux.fotoretrieval.panels;

import at.lux.fotoretrieval.RetrievalFrame;
import at.lux.fotoretrieval.dialogs.LuceneHelpDialog;
import at.lux.fotoretrieval.retrievalengines.LuceneRetrievalEngine;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

/**
 * XPathSearchPanel
 *
 * @author Mathias Lux, mathias@juggle.at
 */
public class LuceneSearchPanel extends JPanel implements ActionListener {

    private JTextField xpath, path;
    private JButton search, browse;
    private JPanel buttonPanel, xpanel, dp;
    private RetrievalFrame parent;
    private JButton index, searchSemantics;
    private JButton helpButton;

    public LuceneSearchPanel(RetrievalFrame parent) {
        setLayout(new BorderLayout());
        this.parent = parent;
        dp = new JPanel(new BorderLayout());
        path = new JTextField(RetrievalFrame.BASE_DIRECTORY);
        path.setEnabled(false);
        browse = new JButton("Open ...");
        browse.setActionCommand("selectDirectory");
        browse.addActionListener(this);
        // dp.add(new JLabel("Search in directory: "), BorderLayout.WEST);
        dp.add(path, BorderLayout.CENTER);
        dp.add(browse, BorderLayout.EAST);
        dp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Select directory"));

        xpanel = new JPanel(new BorderLayout());
        xpath = new JTextField();
        xpanel.add(new JLabel("Input Query: "), BorderLayout.WEST);
        xpanel.add(xpath, BorderLayout.CENTER);
        xpanel.add(dp, BorderLayout.SOUTH);
        xpanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        buttonPanel = new JPanel(new FlowLayout());
        // adding option to create index via parent Retrievalframe
        index = new JButton("Create index");
        index.setToolTipText("Creates an index for specified directory and subdirectories.");
        index.addActionListener(parent);
        index.setActionCommand("index");

        // adding option to search
        search = new JButton("Search");
        search.addActionListener(this);
        search.setActionCommand("search");

        helpButton = new JButton("Help");
        helpButton.setActionCommand("help");
        helpButton.addActionListener(this);

        searchSemantics = new JButton("Semantic Search");
        searchSemantics.setActionCommand("ssearch");
        searchSemantics.addActionListener(this);

        buttonPanel.add(index);
        buttonPanel.add(helpButton);
        buttonPanel.add(searchSemantics);
        buttonPanel.add(search);

        // Adding a keylistener for enter - start search on enter
        xpath.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                    startSearch();
                }
            }
        });

        // Parameters for search:
        JPanel above = new JPanel(new BorderLayout());
        this.add(xpanel, BorderLayout.NORTH);
        this.add(buttonPanel, BorderLayout.SOUTH);

        // Help for search:
//        JEditorPane helpText = new JEditorPane("text/html", HTML_HELP);
//        helpText.setEditable(false);
//
//        JScrollPane scrollTextHelp = new JScrollPane(helpText);
//        scrollTextHelp.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Help"));

//        this.add(above, BorderLayout.NORTH);
//        this.add(scrollTextHelp, BorderLayout.CENTER);
    }

    private void selectDirectory() {
        JFileChooser jfc = new JFileChooser(".");
        jfc.setMultiSelectionEnabled(false);

        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        jfc.setFileFilter(new FileFilter() {
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                } else
                    return false;
            }

            public String getDescription() {
                return "Directories";
            }
        });
        if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                path.setText(jfc.getSelectedFile().getCanonicalPath());
                RetrievalFrame.BASE_DIRECTORY = jfc.getSelectedFile().getCanonicalPath();
            } catch (IOException e) {
                System.err.println("Error reading directory: IOException - " + e.getMessage());
            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("search")) {
            startSearch();
        } else if (e.getActionCommand().equals("ssearch")) {
            startSemanticSearch();
        } else if (e.getActionCommand().equals("help")) {
            LuceneHelpDialog diag = new LuceneHelpDialog(parent);
            diag.setVisible(true);
            diag.scrollToZero();
        } else if (e.getActionCommand().equals("selectDirectory")) {
            selectDirectory();
        } else {
            JOptionPane.showMessageDialog(this, "Not implemented yet!");
        }
    }

    private void startSemanticSearch() {
        // build the path to the index directoy:
        String indexDir = getPath();
        if (!indexDir.endsWith(System.getProperty("file.separator"))) indexDir += System.getProperty("file.separator");
        indexDir += "idx_semantic";
        File indexDirectory = new File(indexDir);
        // check if there is any index:
        if (!indexDirectory.exists()) {
            // if no: abort with message
            JOptionPane.showMessageDialog(parent, "No index exists for this directory, please create one first.");
        } else {
            // if yes: execute search
            if (xpath.getText().length() > 0) {
                parent.searchForSemanticsInIndex(xpath.getText(), path.getText(), true);
            }
        }
    }

    private void startSearch() {
        // build the path to the index directoy:
        String indexDir = getPath();
        if (!indexDir.endsWith(System.getProperty("file.separator"))) indexDir += System.getProperty("file.separator");
        indexDir += "idx_fulltext";
        File indexDirectory = new File(indexDir);
        // check if there is any index:
        if (!indexDirectory.exists()) {
            // if no: abort with message
            JOptionPane.showMessageDialog(parent, "No index exists for this directory, please create one first.");
        } else {
            // if yes: execute search
            if (xpath.getText().length() > 0) {
                parent.searchForImageInIndex(xpath.getText(), path.getText(), true);
            }
        }
    }

    public String getPath() {
        return path.getText().trim();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -