📄 searchdialog.java
字号:
package com.galaxyworkstation.view;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.lucene.document.Document;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import com.galaxyworkstation.control.CDManager;
import com.galaxyworkstation.model.ImageFactory;
import com.galaxyworkstation.model.MyException;
import com.galaxyworkstation.model.SWTResourceManager;
import com.galaxyworkstation.model.Search;
public class SearchDialog extends Dialog {
private Table table;
private Text text;
protected Object result;
public static Shell shell;
private Label countLabel;
private Button searchButton;
private Button createFolderButton;
private Button createDiskButton;
private Button deleteIndexButton;
/**
* Create the dialog
*/
public SearchDialog(Shell parent, int style) {
super(parent, style);
}
/**
* Create the dialog
*/
public SearchDialog(Shell parent) {
this(parent, SWT.NONE);
}
/**
* Open the dialog
*/
public Object open() {
createContents();
addListener();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
/**
* Create contents of the dialog
*/
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setDragDetect(false);
shell.setImage(SWTResourceManager.getImage(SearchDialog.class, "../../../image/search16.gif"));
shell.setSize(500, 375);
shell.setLocation((shell.getDisplay().getClientArea().width - shell
.getClientArea().width) / 2, (shell.getDisplay()
.getClientArea().height - shell.getClientArea().height) / 2);
shell.setText("硬盘搜索");
final Group group = new Group(shell, SWT.NONE);
group.setBounds(10, 5, 474, 333);
countLabel = new Label(group, SWT.NONE);
countLabel.setAlignment(SWT.CENTER);
countLabel.setBounds(335, 16, 106, 25);
text = new Text(group, SWT.BORDER);
text.setBounds(10, 50, 398, 20);
searchButton = new Button(group, SWT.NONE);
searchButton.setImage(SWTResourceManager.getImage(SearchDialog.class, "../../../image/search16.gif"));
searchButton.setBounds(414, 47, 50, 25);
table = new Table(group, SWT.BORDER|SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setBounds(10, 79, 454, 244);
final TableColumn filenameColumn = new TableColumn(table, SWT.NONE);
filenameColumn.setWidth(147);
filenameColumn.setText("文件名");
final TableColumn sizeColumn = new TableColumn(table, SWT.NONE);
sizeColumn.setAlignment(SWT.RIGHT);
sizeColumn.setWidth(67);
sizeColumn.setText("大小");
final TableColumn pathColumn = new TableColumn(table, SWT.NONE);
pathColumn.setWidth(229);
pathColumn.setText("路径");
createFolderButton = new Button(group, SWT.NONE);
createFolderButton.setText("建立目录索引");
createFolderButton.setBounds(10, 18, 84, 25);
createDiskButton = new Button(group, SWT.NONE);
createDiskButton.setText("建立全盘索引");
createDiskButton.setBounds(114, 18, 84, 25);
deleteIndexButton = new Button(group, SWT.NONE);
deleteIndexButton.setText("删除磁盘索引");
deleteIndexButton.setBounds(220, 18, 84, 25);
}
/*************************************************
************Register Listener *******************
*************************************************/
private void addListener(){
text.addFocusListener(searchFocusListener);
text.addModifyListener(searchListener);
searchButton.addSelectionListener(serachButtonListener);
table.addMouseListener(tableClickListener);
createFolderButton.addSelectionListener(createFolderListener);
createDiskButton.addSelectionListener(createDiskListener);
deleteIndexButton.addSelectionListener(deleteButtonListener);
}
/*********************************************************
******************* Listener ****************************
*********************************************************/
/**
* 搜索框文字改变事件
*/
private ModifyListener searchListener = new ModifyListener() {
public void modifyText(final ModifyEvent e) {
try {
table.removeAll();
countLabel.setText("");
String keyword = text.getText().trim();
if (keyword.length() >= 2) {
ArrayList<Document> result = CDManager.action.search(keyword,
Search.DISC_SEARCH);
TableItem tableItem;
long size = 0;
String s;
Document doc;
int count = result.size();
for (int i = 1; i < count; i++) {
doc = result.get(i);
tableItem = new TableItem(table, SWT.NONE);
if (doc.get("isLeaf").equals("f")) {
tableItem.setText(new String[] {
doc.get("name"), "", doc.get("path") });
tableItem.setImage(SWTResourceManager.getImage(
MainGUI.class, ImageFactory.FOLDER));
} else if (doc.get("isLeaf").equals("t")) {
size = Long.parseLong(doc.get("size"));
size /= 1024;
s = String.format("%,d KB", ++size);
tableItem.setText(new String[] {
doc.get("name"), s, doc.get("path") });
tableItem.setImage(SWTResourceManager.getImage(
MainGUI.class, ImageFactory.FILE));
}
}
if(count != 0)
countLabel.setText("共搜到 " + result.get(0).get("hits")
+ " 项\n显示前 " + (count - 1) + " 项");
}
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (MyException e1) {
MessageBox msgBox = new MessageBox(shell, SWT.ICON_ERROR
| SWT.OK);
msgBox.setText("错误");
msgBox.setMessage("您输入的关键词太过模糊,请重新输入!");
msgBox.open();
return;
}
}
};
/**
* 搜索框获得焦点事件
*/
private FocusListener searchFocusListener = new FocusListener() {
public void focusGained(FocusEvent e) {
text.setText("");
}
public void focusLost(FocusEvent e) {
}
};
/**
* 搜索按钮点击事件
*/
private SelectionListener serachButtonListener = new SelectionListener(){
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
String keyword = text.getText().trim();
if (keyword.length() < 2) {
MessageBox msgBox = new MessageBox(shell, SWT.ICON_ERROR
| SWT.OK);
msgBox.setText("关键词过短错误");
msgBox.setMessage("您输入的关键词过短,请重新输入!");
msgBox.open();
return;
}
}
};
/**
* 表格鼠标点击事件
*/
private MouseListener tableClickListener = new MouseListener(){
public void mouseDoubleClick(MouseEvent e) {
try {
TableItem tableItem;
if(((Table)e.widget).getSelectionCount() > 0){
tableItem = (TableItem)(((Table)e.widget).getSelection()[0]);
File file = new File(tableItem.getText(2));
if(file.exists()){
if(File.separator.equals("\\")){
Runtime.getRuntime().exec("cmd.exe /c start " + file.getAbsolutePath());
}else{
MessageBox msgBox = new MessageBox(e.display.getShells()[0], SWT.ICON_INFORMATION
| SWT.OK);
msgBox.setText("提示");
msgBox.setMessage("当前版本只支持在Windows平台下执行该文件。");
msgBox.open();
return;
}
}
else{
MessageBox msgBox = new MessageBox(e.display.getShells()[0], SWT.ICON_ERROR
| SWT.OK);
msgBox.setText("错误");
msgBox.setMessage("无法运行,该文件(夹)可能已被移动或删除。");
msgBox.open();
return;
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
public void mouseDown(MouseEvent e) {
}
public void mouseUp(MouseEvent e) {
}
};
/**
* 建立目录索引
*/
private SelectionListener createFolderListener = new SelectionListener(){
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
final DirectoryDialog dirDialog = new DirectoryDialog(shell);
dirDialog.setMessage("请选择将要建立索引的本地目录...");
dirDialog.setText("请选择需要建立的目录..");
String path = dirDialog.open();
if (path != null) {
MessageBox msgBox = new MessageBox(shell, SWT.ICON_QUESTION
| SWT.YES | SWT.NO);
msgBox.setText("确定建立目录索引");
msgBox.setMessage("你确定建立 " + path + " 的索引吗?");
int choice = msgBox.open();
if (choice == SWT.YES) {
CDManager.action.addDiscIndex(path);
msgBox = new MessageBox(shell, SWT.ICON_INFORMATION
| SWT.OK);
msgBox.setText("成功");
msgBox.setMessage("成功创建[" + path + "]目录的索引!");
msgBox.open();
}
}
}
};
/**
* 建立硬盘索引
*/
private SelectionListener createDiskListener = new SelectionListener(){
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
MessageBox msgBox = new MessageBox(shell, SWT.ICON_QUESTION
| SWT.YES | SWT.NO);
msgBox.setText("确定建立全盘索引");
msgBox.setMessage("你确定建立全盘索引吗?");
int choice = msgBox.open();
if (choice == SWT.YES) {
CDManager.action.createWholeDiscIndex();
}
}
};
/**
* 删除全部硬盘索引
*/
private SelectionListener deleteButtonListener = new SelectionListener(){
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
MessageBox msgBox = new MessageBox(shell, SWT.ICON_QUESTION
| SWT.YES | SWT.NO);
msgBox.setText("确定删除硬盘索引");
msgBox.setMessage("你确定删除磁盘(包括目录和全盘)索引吗?");
int choice = msgBox.open();
if (choice == SWT.YES) {
CDManager.action.deleteWholeDiscIndex();
countLabel.setText("索引已全部删除!");
table.clearAll();
}
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -