📄 dbdataview.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2003 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package com.tanghan.plugin.dbviews;import java.util.ArrayList;import java.util.List;import java.util.ResourceBundle;import org.eclipse.core.resources.ResourcesPlugin;import org.eclipse.core.runtime.IStatus;import org.eclipse.core.runtime.Status;import org.eclipse.jface.action.Action;import org.eclipse.jface.action.IMenuListener;import org.eclipse.jface.action.IMenuManager;import org.eclipse.jface.action.IToolBarManager;import org.eclipse.jface.action.MenuManager;import org.eclipse.jface.action.Separator;import org.eclipse.jface.dialogs.ErrorDialog;import org.eclipse.jface.dialogs.MessageDialog;import org.eclipse.jface.util.Assert;import org.eclipse.jface.viewers.ColumnPixelData;import org.eclipse.jface.viewers.DoubleClickEvent;import org.eclipse.jface.viewers.IDoubleClickListener;import org.eclipse.jface.viewers.IStructuredContentProvider;import org.eclipse.jface.viewers.ITableLabelProvider;import org.eclipse.jface.viewers.LabelProvider;import org.eclipse.jface.viewers.TableLayout;import org.eclipse.jface.viewers.TableViewer;import org.eclipse.jface.viewers.Viewer;import org.eclipse.jface.viewers.ViewerSorter;import org.eclipse.swt.SWT;import org.eclipse.swt.custom.SashForm;//import org.eclipse.swt.events.SelectionAdapter;//import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.layout.FillLayout;//import org.eclipse.swt.layout.GridData;//import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Combo;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Menu;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 org.eclipse.ui.IActionBars;import org.eclipse.ui.ISharedImages;import org.eclipse.ui.PlatformUI;import org.eclipse.ui.part.ViewPart;import com.tanghan.db.SQLResult;import com.tanghan.db.logic.AbstractDealDatabaseInfo;import com.tanghan.db.util.DBConnection;import com.tanghan.plugin.ITanghanConstants;import com.tanghan.plugin.TanghanPlugin;import com.tanghan.util.DealString;import com.tanghan.util.PageList;import com.tanghan.util.TanghanException;//import org.eclipse.core.runtime.IStatus;//import org.eclipse.core.runtime.Status;/** * This sample class demonstrates how to plug-in a new * workbench view. The view shows data obtained from the * model. The sample creates a dummy model on the fly, * but a real implementation would connect to the model * available either in this or another plug-in (e.g. the workspace). * The view is connected to the model using a content provider. * <p> * The view uses a label provider to define how model * objects should be presented in the view. Each * view can present the same model objects using * different labels and icons, if needed. Alternatively, * a single label provider can be shared between views * in order to ensure that objects of the same type are * presented in the same way everywhere. * <p> */public class DBDataView extends ViewPart { private TableViewer viewer; private Table table; /**到首页面的动作*/ private Action firstPageAction; /**到下一页的动作*/ private Action nextPageAction; /**到上一页的动作*/ private Action prePageAction; /**到最后一页的动作*/ private Action endPageAction; /**到最后一页的动作*/ private Action pageListConfigAction; /**隐藏SQL输入筐*/ private Action hideSqlTextAction; /**执行SQL的Action*/ private Action exceteSqlAction; /**已执行sql语句的列表*/ private Combo sqlCombo; /**记录sql语句的列表*/ private Button regSqlButton; protected Composite parent = null; /**显示还是隐藏*/ private boolean hideSqlText = false; /**输入SQL语句的输入筐*/ private Text sqlText; /**双击事件*/ private Action doubleClickAction; /**空列表*/ private static final ArrayList EmptyList = new ArrayList(); /**空列表行*/ private static final Integer[] EmptyDataIndexs = new Integer[0]; private List dataList = EmptyList; private Integer[] dataIndexs = EmptyDataIndexs; private SashForm fullForm ; /**数据连接信息*/ private DBConnection dbConn; /**表*/ private String tableName; /**Page List*/ private PageList pageList; /**该视图对应的插件实例*/ private static TanghanPlugin plugin = TanghanPlugin.getDefault(); /**资源文件*/ private static ResourceBundle res = plugin.getResourceBundle(); /* * The content provider class is responsible for * providing objects to the view. It can wrap * existing objects in adapters or simply return * objects as-is. These objects may be sensitive * to the current input of the view, or ignore * it and always show the same content * (like Task List, for example). */ class ViewContentProvider implements IStructuredContentProvider { public void inputChanged(Viewer v, Object oldInput, Object newInput) { } public void dispose() { } public Object[] getElements(Object parent) { return dataIndexs; } } class ViewLabelProvider extends LabelProvider implements ITableLabelProvider { public String getColumnText(Object obj, int index) { if(obj instanceof Integer){ int idx = ((Integer)obj).intValue(); if(idx>0 && idx<dataList.size() && dataList.get(idx) instanceof String[]){ String[] strData = (String[])dataList.get(idx); if(index>-1&&index<strData.length){ return strData[index]; } } } return ""; } public Image getColumnImage(Object obj, int index) {// return getImage(obj); return null; } public Image getImage(Object obj) { return null;// return PlatformUI.getWorkbench().// getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); } private String[] EmptyArray = new String[0]; } class NameSorter extends ViewerSorter { } /** * The constructor. */ public DBDataView() { } /** * This is a callback that will allow us * to create the viewer and initialize it. */ public void createPartControl(Composite parent) { this.parent = parent; //Composite comp = new Composite(parent, SWT.BORDER); fullForm = new SashForm (parent, SWT.VERTICAL); fullForm.setLayout(new FillLayout()); //执行SQL语句的组件 sqlText = new Text (fullForm, SWT.MULTI | SWT.BORDER); /* Composite composite = new Composite(fullForm, SWT.NULL ); //composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); composite.setLayout(new FillLayout(SWT.VERTICAL)); Composite composite1 = new Composite(composite, SWT.NULL); GridData gridData = new GridData(); gridData.heightHint =1; composite1.setLayoutData(gridData); GridLayout layout= new GridLayout(); layout.marginHeight = 0; layout.marginWidth = 0; layout.numColumns = 2; layout.horizontalSpacing = 0; layout.verticalSpacing = 0; composite1.setLayout(layout); sqlCombo = new Combo(composite1, SWT.READ_ONLY|SWT.DROP_DOWN); sqlCombo.setLayoutData(new GridData(GridData.FILL)); regSqlButton = new Button(composite1, SWT.PUSH); regSqlButton.setLayoutData(new GridData(GridData.FILL)); regSqlButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { // if(MessageDialog.openConfirm(parent.getShell(),res.getString("TanghanPlugin.Message.ifDeleteTitle"),res.getString("TanghanPlugin.Message.ifDelete"))){ } }); */ // 数据显示的组件 createTable(fullForm); fullForm.setWeights(new int[] {20, 80}); // createTable(parent); viewer = new TableViewer(table); viewer.setUseHashlookup(true); createColumns(); viewer.setContentProvider(new ViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); //viewer.setSorter(new NameSorter()); viewer.setInput(ResourcesPlugin.getWorkspace()); makeActions(); hookContextMenu(); hookDoubleClickAction(); contributeToActionBars(); changActionStatus(); hookSQLTextMenu(); } /** * Creates the table control. */ private void createTable(Composite parent) { table = new Table( parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER); table.setLinesVisible(true); TableLayout layout = new TableLayout(); table.setLayout(layout); table.setHeaderVisible(true); }// private ColumnLayoutData columnLayouts =// new ColumnPixelData(190, true); /**新建表的列*/ private void createColumns(){ String[] columnHeaders = null; if(dataList.size()>0 && dataList.get(0) instanceof String[]){ columnHeaders = (String[])dataList.get(0); }else{ columnHeaders = new String[0]; } TableLayout layout = (TableLayout)table.getLayout(); layout.addColumnData(new ColumnPixelData(100, true)); TableColumn[] tbcl = table.getColumns(); int iMax = 0; if(tbcl.length<=columnHeaders.length) iMax = columnHeaders.length; else iMax = tbcl.length; for (int i = 0; i < iMax; i++) { if(i<tbcl.length){ if(i<columnHeaders.length){ tbcl[i].setText(columnHeaders[i]); tbcl[i].setResizable(true); tbcl[i].setWidth(100); }else{ tbcl[i].dispose(); } }else{ TableColumn tc = new TableColumn(table, SWT.NONE, i); tc.setText(columnHeaders[i]); tc.setResizable(true); tc.setWidth(100); } } } /***/ private void clearDatas(){ TableItem[] tbItems = table.getItems(); for(int i = 0;i < tbItems.length; i++){ tbItems[i].dispose(); } } private void hookSQLTextMenu() { MenuManager menuMgr = new MenuManager("#PopupMenu"); menuMgr.setRemoveAllWhenShown(true); menuMgr.addMenuListener(new IMenuListener() { public void menuAboutToShow(IMenuManager manager) { //DBDataView.this.fillContextMenu(manager); manager.add(exceteSqlAction); } }); Menu menu = menuMgr.createContextMenu(viewer.getControl());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -