代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
搜素表格数据
作者:
DDT
/ 发布于
2012/6/12
/
599
搜素表格数据
<div>package org.apache.lucene.swing.models;</div> <div>/** * Copyright 2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * <a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a> * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */</div> import java.util.ArrayList;</div> import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableModel;</div> import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.WhitespaceAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.Fieldable; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.swing.models.ListSearcher.CountingCollector; import org.apache.lucene.util.Version;</div> <div>/** * This is a TableModel that encapsulates Lucene * search logic within a TableModel implementation. * It is implemented as a TableModel decorator, * similar to the TableSorter demo from Sun that decorates * a TableModel and provides sorting functionality. The benefit * of this architecture is that you can decorate any TableModel * implementation with this searching table model -- making it * easy to add searching functionality to existing JTables -- or * making new search capable table lucene. * * This decorator works by holding a reference to a decorated ot inner * TableModel. All data is stored within that table model, not this * table model. Rather, this table model simply manages links to * data in the inner table model according to the search. All methods on * TableSearcher forward to the inner table model with subtle filtering * or alteration according to the search criteria. * * Using the table model: * * Pass the TableModel you want to decorate in at the constructor. When * the TableModel initializes, it displays all search results. Call * the search method with any valid Lucene search String and the data * will be filtered by the search string. Users can always clear the search * at any time by searching with an empty string. Additionally, you can * add a button calling the clearSearch() method. * */ public class TableSearcher extends AbstractTableModel {</div> <div> /** * The inner table model we are decorating */ protected TableModel tableModel;</div> <div> /** * This listener is used to register this class as a listener to * the decorated table model for update events */ private TableModelListener tableModelListener;</div> <div> /** * these keeps reference to the decorated table model for data * only rows that match the search criteria are linked */ private ArrayList<Integer> rowToModelIndex = new ArrayList<Integer>();</div> <div> //Lucene stuff.</div> <div> /** * In memory lucene index */ private RAMDirectory directory;</div> <div> /** * Cached lucene analyzer */ private Analyzer analyzer;</div> <div> /** * Links between this table model and the decorated table model * are maintained through links based on row number. This is a * key constant to denote "row number" for indexing */ private static final String ROW_NUMBER = "ROW_NUMBER";</div> <div> /** * Cache the current search String. Also used internally to * key whether there is an active search running or not. i.e. if * searchString is null, there is no active search. */ private String searchString = null;</div> <div> /** * @param tableModel The table model to decorate */ public TableSearcher(TableModel tableModel) { analyzer = new WhitespaceAnalyzer(); tableModelListener = new TableModelHandler(); setTableModel(tableModel); tableModel.addTableModelListener(tableModelListener); clearSearchingState(); } <div> /** * * @return The inner table model this table model is decorating */ public TableModel getTableModel() { return tableModel; } <div> /** * Set the table model used by this table model * @param tableModel The new table model to decorate */ public void setTableModel(TableModel tableModel) {</div> <div> //remove listeners if there... if (this.tableModel != null) { this.tableModel.removeTableModelListener(tableModelListener); } <div> this.tableModel = tableModel; if (this.tableModel != null) { this.tableModel.addTableModelListener(tableModelListener); } <div> //recalculate the links between this table model and //the inner table model since the decorated model just changed reindex();</div> <div> // let all listeners know the table has changed fireTableStructureChanged(); } <div> /** * Reset the search results and links to the decorated (inner) table * model from this table model. */ private void reindex() { try { // recreate the RAMDirectory directory = new RAMDirectory(); IndexWriter writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);</div> <div> // iterate through all rows for (int row=0; row < tableModel.getRowCount(); row++){</div> <div> //for each row make a new document Document document = new Document(); //add the row number of this row in the decorated table model //this will allow us to retrieve the results later //and map this table model's row to a row in the decorated //table model document.add(new Field(ROW_NUMBER, "" + row, Field.Store.YES, Field.Index.ANALYZED)); //iterate through all columns //index the value keyed by the column name //NOTE: there could be a problem with using column names with spaces for (int column=0; column < tableModel.getColumnCount(); column++){ String columnName = tableModel.getColumnName(column); String columnValue = String.valueOf(tableModel.getValueAt(row, column)).toLowerCase(); document.add(new Field(columnName, columnValue, Field.Store.YES, Field.Index.ANALYZED)); } writer.addDocument(document); } writer.optimize(); writer.close(); } catch (Exception e){ e.printStackTrace(); } } <div> /** * @return The current lucene analyzer */ public Analyzer getAnalyzer() { return analyzer; } <div> /** * @param analyzer The new analyzer to use */ public void setAnalyzer(Analyzer analyzer) { this.analyzer = analyzer; //reindex from the model with the new analyzer reindex();</div> <div> //rerun the search if there is an active search if (isSearching()){ search(searchString); } } <div> /** * Run a new search. * * @param searchString Any valid lucene search string */ public void search(String searchString){</div> <div> //if search string is null or empty, clear the search == search all if (searchString == null || searchString.equals("")){ clearSearchingState(); fireTableDataChanged(); return; } <div> try { //cache search String this.searchString = searchString;</div> <div> //make a new index searcher with the in memory (RAM) index. IndexSearcher is = new IndexSearcher(directory, true);</div> <div> //make an array of fields - one for each column String[] fields = new String[tableModel.getColumnCount()]; for (int t=0; t<tableModel.getColumnCount(); t++){ fields[t]=tableModel.getColumnName(t); } <div> //build a query based on the fields, searchString and cached analyzer //NOTE: This is an area for improvement since the MultiFieldQueryParser // has some weirdness. MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_CURRENT, fields, analyzer); Query query = parser.parse(searchString); //reset this table model with the new results resetSearchResults(is, query); } catch (Exception e){ e.printStackTrace(); } <div> //notify all listeners that the table has been changed fireTableStructureChanged(); } <div> /** * * @param hits The new result set to set this table to. */ private void resetSearchResults(IndexSearcher searcher, Query query) { try { //clear our index mapping this table model rows to //the decorated inner table model rowToModelIndex.clear(); CountingCollector countingCollector = new CountingCollector(); searcher.search(query, countingCollector); ScoreDoc[] hits = searcher.search(query, countingCollector.numHits).scoreDocs; //iterate through the hits //get the row number stored at the index //that number is the row number of the decorated //table model row that we are mapping to for (int t=0; t<hits.length; t++){ Document document = searcher.doc(hits[t].doc); Fieldable field = document.getField(ROW_NUMBER); rowToModelIndex.add(Integer.valueOf(field.stringValue())); } } catch (Exception e){ e.printStackTrace(); } } <div> private int getModelRow(int row){ return rowToModelIndex.get(row); } <div> /** * Clear the currently active search * Resets the complete dataset of the decorated * table model. */ private void clearSearchingState(){ searchString = null; rowToModelIndex.clear(); for (int t=0; t<tableModel.getRowCount(); t++){ rowToModelIndex.add(t); } } <div> // TableModel interface methods public int getRowCount() { return (tableModel == null) ? 0 : rowToModelIndex.size(); } <div> public int getColumnCount() { return (tableModel == null) ? 0 : tableModel.getColumnCount(); } <div> @Override public String getColumnName(int column) { return tableModel.getColumnName(column); } <div> @Override public Class<?> getColumnClass(int column) { return tableModel.getColumnClass(column); } <div> @Override public boolean isCellEditable(int row, int column) { return tableModel.isCellEditable(getModelRow(row), column); } <div> public Object getValueAt(int row, int column) { return tableModel.getValueAt(getModelRow(row), column); } <div> @Override public void setValueAt(Object aValue, int row, int column) { tableModel.setValueAt(aValue, getModelRow(row), column); } <div> private boolean isSearching() { return searchString != null; } <div> private class TableModelHandler implements TableModelListener { public void tableChanged(TableModelEvent e) { // If we're not searching, just pass the event along. if (!isSearching()) { clearSearchingState(); reindex(); fireTableChanged(e); return; } <div> // Something has happened to the data that may have invalidated the search. reindex(); search(searchString); fireTableDataChanged(); return; } <div> } <div>} </div>
试试其它关键字
搜素表格数据
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
实现测量程序运行时间及cpu使用时间
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3