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

📄 pages.java

📁 模拟的土豆网视频网站
💻 JAVA
字号:
package cn.myvideosite.searcher.util;

import java.io.IOException;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.Hits;

import cn.myvideosite.commons.Constant;

public class Pages {

	/**
	 * @param args
	 */
	private static final int HITS_PER_PAGE = 10; // 每10条 换一页
	private static int currentRecord;   //当前记录
	/**
	 * 
	 * @param hits
	 * @param table   下一页
	 * @throws CorruptIndexException
	 * @throws IOException   
	 */
	public static void next(Hits hits, JTable table, boolean isVideo)throws CorruptIndexException, IOException{
		 currentRecord += HITS_PER_PAGE;
		 if(isVideo)  showAlbum(hits, table, currentRecord);
		 else show(hits, table, currentRecord);
		 
	}
	/**
	 * 
	 * @param hits  上一页
	 * @param table
	 * @throws CorruptIndexException
	 * @throws IOException
	 */
	public static void previous(Hits hits, JTable table ,boolean isVideo)throws CorruptIndexException, IOException{
		 currentRecord -= HITS_PER_PAGE;
		 if(isVideo)  showAlbum(hits, table, currentRecord);
		 else show(hits, table, currentRecord);
	}
	/**
	 * 
	 * @param hits
	 * @param table  判断是否为最后一个
	 * @param recordStart
	 */
	public static boolean isEnd(Hits hits){
		int length = hits.length();
		if((currentRecord + HITS_PER_PAGE) >= length) return true;
		return false;		
	}
	/**
	 * 
	 * @param hits
	 * @param table  判断是否为第一个
	 * @param recordStart
	 */
	public static boolean isFirst(){		
		if((currentRecord - HITS_PER_PAGE) < 0) return true;
		return false;		
	}	
	/**
	 * 
	 * @param hits
	 * @param table  显示视频方法
	 * @param recordStart
	 */
	public static void show(Hits hits, JTable table, int recordStart) {
		DefaultTableModel tableModel = new DefaultTableModel();
		tableModel.setColumnIdentifiers(Constant.TABLE_HEADER);
		
		if(hits != null && hits.length()>0){
			if(recordStart <= 0) recordStart = 0;
			else if(recordStart >= hits.length()) recordStart -= HITS_PER_PAGE;
			int end = Math.min(hits.length(), recordStart + HITS_PER_PAGE);//hits.length()与recordStart + HITS_PER_PAGE
			                                                                  //比较 拿出小的
			   for(int i = recordStart; i < end; i++){
				   try {
					Document doc = hits.doc(i);
					
					Object[] objs = new Object [4];
					objs[0] =  doc.get("videoId");
					objs[1] =  doc.get("title");
					objs[2] =  doc.get("introduction");
					objs[3] =  doc.get("flashAddress");
					
					tableModel.addRow(objs);					
				} catch (CorruptIndexException e) {					
					e.printStackTrace();
				} catch (IOException e) {				
					e.printStackTrace();
				}				
			   }
		}
		table.setModel(tableModel);
		table.updateUI();		
	}
	/**
	 * 
	 * @param hits
	 * @param table    显示专辑方法
	 * @param recordStart
	 */
	public static void showAlbum(Hits hits, JTable table, int recordStart){
		DefaultTableModel tableModel = new DefaultTableModel();
		tableModel.setColumnIdentifiers(Constant.TABLE_HEADER);
		
		if(hits != null && hits.length()>0){
			if(recordStart <= 0) recordStart = 0;
			else if(recordStart >= hits.length()) recordStart -= HITS_PER_PAGE;
			int end = Math.min(hits.length(), recordStart + HITS_PER_PAGE);
			
			for(int i = recordStart; i <end; i++){
				try {
					Document doc  = hits.doc(i);
					
					Object[] objs = new Object [4];
					objs[0] =  doc.get("albumId");
					objs[1] =  doc.get("title");
					objs[2] =  doc.get("albumIntro");
					objs[3] =  doc.get("albumAddr");
					
					tableModel.addRow(objs);					
				} catch (CorruptIndexException e) {					
					e.printStackTrace();
				} catch (IOException e) {					
					e.printStackTrace();
				}								
			}
		}
		table.setModel(tableModel);
		table.updateUI();
	}
}

⌨️ 快捷键说明

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