📄 lzzsearch.java
字号:
package com.laozizhu.search.client;
import java.util.Iterator;
import java.util.List;
import net.java2000.tools.HTMLDecoder;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
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.laozizhu.search.EngineFactory;
import com.laozizhu.search.Factory;
import com.laozizhu.search.Item;
import com.laozizhu.search.SearchResult;
import com.laozizhu.search.Store;
import com.laozizhu.search.util.EngineUtil;
/**
* 客户端主程序。
*
* @author 老紫竹(laozizhu.com)
*/
public class LzzSearch {
Store store = (Store) Factory.getBean("LuceneStore");
EngineFactory engineFactory = (EngineFactory) Factory.getBean("EngineFactory");
private TabFolder tabFolder = null;
Shell shell = null;
Table table = null;
StyledText resultShow = null;
Text keyword = null;
Text urlText = null;
Text urlAddResult = null;
public LzzSearch() {
Display display = new Display();
shell = new Shell(display);
shell.setText("老紫竹精确搜索V1.01");
shell.setImage(new Image(display, "LzzSearch.ico"));
shell.setLayout(new GridLayout());
tabFolder = new TabFolder(shell, SWT.BORDER);
searchTab();
addTab();
GridData gd = new GridData(GridData.FILL_BOTH);
tabFolder.setLayoutData(gd);
shell.setMinimumSize(800, 600);
shell.setSize(800, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {// 等待直到结束
display.sleep();
}
}
display.dispose();
}
private void searchTab() {
TabItem tabItem = new TabItem(tabFolder, SWT.NULL);
tabItem.setText("搜索 ");
Composite group = new Composite(tabFolder, SWT.NONE);
GridLayout gl = new GridLayout();
gl.numColumns = 3;
group.setLayout(gl);
tabItem.setControl(group);
// 增加标签
Label inputLabel = new Label(group, SWT.NONE);
inputLabel.setText("输入关键词");
GridData gd = new GridData();
inputLabel.setLayoutData(gd);
// 增加输入框
keyword = new Text(group, SWT.BORDER | SWT.SHADOW_IN);
gd = new GridData(GridData.FILL_HORIZONTAL);
keyword.setLayoutData(gd);
// 处理回车事件
keyword.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.keyCode == 13) {
// e.detail = SWT.TRAVERSE_TAB_NEXT;
e.doit = true;
doSearch();
}
}
});
// 搜索按钮
Button search = new Button(group, SWT.PUSH);
search.setText("搜索");
gd = new GridData();
gd.widthHint = 100;
search.setLayoutData(gd);
// 处理按钮事件
search.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
doSearch();
}
});
// 增加搜索结果列表
Group searchListGroup = new Group(group, SWT.NULL);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 3;
gd.heightHint = 150;
searchListGroup.setLayoutData(gd);
searchListGroup.setText("搜索结果");
searchListGroup.setLayout(new GridLayout());
gd = new GridData(GridData.FILL_BOTH);
table = new Table(searchListGroup, SWT.BORDER | SWT.FULL_SELECTION);
table.setLayoutData(gd);
// 增加列
TableColumn datetime = new TableColumn(table, SWT.LEFT);
TableColumn title = new TableColumn(table, SWT.LEFT);
TableColumn url = new TableColumn(table, SWT.LEFT);
datetime.setText("日期");
title.setText("标题");
url.setText("来源地址");
table.setHeaderVisible(true);
datetime.setWidth(130);
title.setWidth(400);
url.setWidth(200);
table.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
TableItem ti = table.getSelection()[0];
String url = ti.getText(2);
Item item = store.searchByUrl(url);
resultShow.setText(HTMLDecoder.decode(item.getBody()).replaceAll("<br\\s*/?>", "\r\n"));
}
});
// 底部明细
Group resultGroup = new Group(group, SWT.NULL);
resultGroup.setText("查看明细");
gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 3;
gd.heightHint = 250;
resultGroup.setLayoutData(gd);
resultGroup.setLayout(new GridLayout());
gd = new GridData(GridData.FILL_BOTH);
resultShow = new StyledText(resultGroup, SWT.BORDER | SWT.SHADOW_OUT | SWT.V_SCROLL | SWT.WRAP);
resultShow.setBackground(resultShow.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
resultShow.setForeground(new Color(resultShow.getDisplay(), 0, 0, 0));
resultShow.setFont(new Font(resultShow.getDisplay(), "宋体", 12, SWT.NORMAL));
resultShow.setLayoutData(gd);
resultShow.setText("");
}
private void doSearch() {
SearchResult sr = store.seach(keyword.getText(), 0, 10);
List<Item> list = sr.getReturnList();
table.removeAll();
Iterator<Item> it = list.iterator();
Item o;
TableItem tableItem;
while (it.hasNext()) {
o = it.next();
tableItem = new TableItem(table, SWT.NULL);
tableItem.setText(new String[] { o.getDatetimeCreate(), o.getTitle(), o.getUrl() });
}
}
private void doAddUrl() {
String url = urlText.getText();
if (url.trim().length() <= 20) {
return;
}
boolean update = false;
Item item = store.searchByUrl(url);
if (item != null) {
update = true;
}
// 获得数据
item = EngineUtil.get(url);
String result;
if (item == null || !item.isValid()) {
result = (update ? "更新" : "加入") + "索引失败,也许您输入的网址尚未在支持的范围内!";
} else {
// 加入索引
if (store.save(item)) {
result = (update ? "更新" : "加入") + "索引成功:\n\n标题:" + item.getTitle() + "\n作者:" + item.getAuthor() + "\n日期:"
+ item.getDatetimeCreate() + "\n内容:" + item.getBody();
} else {
result = (update ? "更新" : "加入") + "索引失败";
}
}
urlAddResult.setText(url + " " + result);
// 清楚文本
urlText.setText("");
}
private void addTab() {
TabItem tabItem = new TabItem(tabFolder, SWT.NULL);
tabItem.setText("增加 ");
Composite group = new Composite(tabFolder, SWT.NONE);
tabItem.setControl(group);
GridLayout gl = new GridLayout();
gl.numColumns = 3;
group.setLayout(gl);
// 增加标签
Label inputLabel = new Label(group, SWT.NONE);
inputLabel.setText("输入URL");
GridData gd = new GridData();
inputLabel.setLayoutData(gd);
// 增加输入框
urlText = new Text(group, SWT.BORDER | SWT.SHADOW_IN);
gd = new GridData(GridData.FILL_HORIZONTAL);
urlText.setLayoutData(gd);
// 处理回车事件
urlText.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.keyCode == 13) {
// e.detail = SWT.TRAVERSE_TAB_NEXT;
e.doit = true;
doAddUrl();
}
}
});
// 搜索按钮
Button addUrl = new Button(group, SWT.PUSH);
addUrl.setText("加入");
gd = new GridData();
gd.widthHint = 100;
addUrl.setLayoutData(gd);
// 处理按钮事件
addUrl.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
doAddUrl();
}
});
gd = new GridData(GridData.FILL_BOTH);
urlAddResult = new Text(group, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY);
gd.horizontalSpan = 3;
urlAddResult.setLayoutData(gd);
}
public static void main(String[] args) {
new LzzSearch();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -