VaadinのSQLContainerとtableを使用して、バッチ・データの迅速なブラウズを実現

7480 ワード

次はコードですが、詳細なコメントがあります.ただの例です.
以下を示します.
1、SQLContainerの簡単な使い方
2、tableコントロールの使い方を示した
3、大量のデータクライアントブラウズのサーバー側実現方法を示した
4、flash、html 5の方法より少し良いはずで、クライアントの資源に対する需要は少ない.
package cn.com.dareway.quickgrid;

/*
 *     、    
 *     OSGi     Web    
 *    Vaadin SQLContainer    , Table      
 *              
 */

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import com.vaadin.Application;
import com.vaadin.Application.CustomizedSystemMessages;
import com.vaadin.Application.SystemMessages;
import com.vaadin.addon.sqlcontainer.SQLContainer;
import com.vaadin.addon.sqlcontainer.connection.JDBCConnectionPool;
import com.vaadin.addon.sqlcontainer.connection.SimpleJDBCConnectionPool;
import com.vaadin.addon.sqlcontainer.query.TableQuery;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

/*
 * QuickGrid   Vaadin Application,    Vaadin     
 */
public class QuickGrid extends Application {
	private Window mw;//      
	
	private Label rows;//            
	private JDBCConnectionPool connectionPool = null;//      
	private SQLContainer htContainer = null;//    ,   PowerBuilder DataStore
	boolean editMode = false;//            ,        

	//       
	private void initContainers() {
		try {
			//        ,         ,                  ,  c3p0
			connectionPool = new SimpleJDBCConnectionPool(
					"com.mysql.jdbc.Driver",
					"jdbc:mysql://localhost:3306/csp?Unicode=true&characterEncoding=UTF-8",
					"xzs", "xzsxzs", 2, 5);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//        table          ,   hugetable
		try {
			TableQuery q1 = new TableQuery("hugetable", connectionPool);
			htContainer = new SQLContainer(q1);
			//                 ,               5 ,   100
			//             ,           ,       ,          
			htContainer.setPageLength(1000);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	//     Application   ,     Vaadin    
	@Override
	public void init() {
		//       
		initContainers();
		//        ,              
		mw = new Window("        -QuickGrid");
		//               
		com.vaadin.ui.HorizontalLayout h = new HorizontalLayout();
		h.setSpacing(true);
		h.setMargin(true);
		Label title = new Label("      、    ");
		title.setStyleName("bigfont");
		h.addComponent(title);
		NativeButton helloButton = new NativeButton("  ");
		helloButton.setWidth("40px");
		helloButton.setHeight("40px");
		helloButton.setStyleName("bigfont");
		helloButton.addListener(new ClickListener(){
			@Override
			public void buttonClick(ClickEvent event) {
				getMainWindow().showNotification("Hello,       、     Portal。");
			}	
		});
		h.addComponent(helloButton);
		mw.addComponent(h);
		//        ,           
		final Table ht = new Table();
		ht.setSizeFull();//  100%
		ht.setPageLength(25);//    25   
		ht.setReadOnly(false);//    
		ht.setSelectable(true);//         
		//  UI      
		ht.setContainerDataSource(htContainer);
		//       
		ht.setCaption("      ");
		//         
		ht.setColumnHeaders(new String[] { "ID", "  ", "  ", "  ", "     " });
		//         
		mw.addComponent(ht);
		//      
		Button b = new Button("insert 10000 rows");
		b.addListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				for (int i = 0; i < 10000; i++) {
					Object itemid = htContainer.addItem();
					Item item = htContainer.getItem(itemid);
					double r = Math.random() * 10000;
					item.getItemProperty("name").setValue("  " + r);
					item.getItemProperty("age").setValue(
							Math.round(100 * Math.random()));
					item.getItemProperty("address").setValue("    ");
					item.getItemProperty("idno").setValue("371012198887122217");
				}
				try {
					htContainer.commit();
				} catch (UnsupportedOperationException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				rows.setValue("rows:" + htContainer.size());
			}
		});
		//       
		HorizontalLayout vl = new HorizontalLayout();
		vl.addComponent(b);
		//         
		Button b2 = new Button("      ");
		b2.addListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				try {
					Connection c = connectionPool.reserveConnection();
					Statement st = c.createStatement();
					st.executeUpdate("delete from hugetable");
					c.commit();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				htContainer.refresh();
				rows.setValue("rows:" + htContainer.size());
			}
		});
		vl.addComponent(b2);
		//    
		final Button b4 = new Button("  ");
		b4.setEnabled(false);
		b4.addListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				try {
					htContainer.commit();
				} catch (UnsupportedOperationException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
		vl.addComponent(b4);
		//       
		final Button b5 = new Button("    ");
		b5.setEnabled(false);
		b5.addListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				try {
					htContainer.rollback();
				} catch (UnsupportedOperationException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
		vl.addComponent(b5);
		//    
		Button b6 = new Button("  ");
		b6.addListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				//htContainer.refresh();
				ht.refreshRowCache();
			}
		});
		vl.addComponent(b6);
		//         
		final Button b3 = new Button("        ");
		vl.addComponent(b3);
		b3.addListener(new ClickListener() {
			@Override
			public void buttonClick(ClickEvent event) {
				if (editMode) {
					ht.setEditable(false);
					b3.setCaption("        ");
				} else {
					ht.setEditable(true);
					b3.setCaption("        ");
				}
				editMode = !editMode;
				if (editMode){
					b4.setEnabled(true);
					b5.setEnabled(true);
				}else{
					b4.setEnabled(false);
					b5.setEnabled(false);
				}
			}
		});

		//        
		rows = new Label("rows:" + htContainer.size());
		vl.addComponent(new Label(rows));
		//        
		mw.addComponent(vl);
		ht.addListener(new ItemClickListener(){
			@Override
			public void itemClick(ItemClickEvent event) {
				getMainWindow().showNotification(""+event.getItem().getItemProperty("name"));
			}
		});
		//        
		setMainWindow(mw);
		//         
		this.setTheme("quickgrid");
	}
	
	//     vaadin session timeout  
	public static SystemMessages getSystemMessages() {
		return customizedSystemMessages;
	}

	static CustomizedSystemMessages customizedSystemMessages = new CustomizedSystemMessages();
	static {//  vaadin      ,              :    ,         (    )     
		customizedSystemMessages.setSessionExpiredCaption(null);
		customizedSystemMessages.setSessionExpiredMessage(null);
	}
}