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

📄 wareview.java

📁 eclipse rcp 项目实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.niis.myprice.views;

import java.util.ArrayList;
import java.util.List;

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.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
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.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.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.dialogs.ViewSorter;
import org.eclipse.ui.part.ViewPart;

import com.niis.myprice.action.ICommandIds;
import com.niis.myprice.domain.DataTree;
import com.niis.myprice.domain.Kind;
import com.niis.myprice.domain.Node;
import com.niis.myprice.domain.Ware;
import com.niis.myprice.util.message.Messages;

public class WareView extends ViewPart {

	private TableViewer viewer;
	
	private Label introLab;

	private Action addAction;

	private Action modifyAction;

	private Action deleteAction;

	private Action saveAction;

	private Action cancelAction;
	
	private Action refreshAction;

	private Action doubleClickAction;

	private Shell shell;

	public static final String ID = "com.niis.myprice.views.WareView"; //$NON-NLS-1$

	/**
	 * Create contents of the view part
	 * 
	 * @param parent
	 */
	private static final int OP_GROUP_WIDTH = 60;

	private static final int WARE_LIST_DEVIATION = OP_GROUP_WIDTH + 10;

	public void createPartControl(Composite parent) {
		// define a container for layout
		Composite container = new Composite(parent, SWT.NONE);
		// layout
		FormLayout groupLayout = new FormLayout();
		groupLayout.marginHeight = 10;
		groupLayout.marginWidth = 10;
		groupLayout.spacing = 10;
		container.setLayout(groupLayout);
		FormData data;

		final Group introductionGroup = new Group(container, SWT.NONE);
		introductionGroup.setText(Messages.getString("WareView.caption")); //$NON-NLS-1$
		// introductionGroup.setBounds(23, 15, 448, 47);
		data = new FormData();
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(10, 0);
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(100, 10);
		introductionGroup.setLayoutData(data);

		introLab = new Label(introductionGroup, SWT.NONE|SWT.WRAP);
//		introLab.setBounds(10, 0, 27, 12);
		FillLayout flayout = new FillLayout();
		introductionGroup.setLayout(flayout);
		// introductionGroup.setVisible(false);

		final Group opGroup = new Group(container, SWT.NONE);
		opGroup.setText("-"); //$NON-NLS-1$
		// opGroup.setBounds(1,1,1,1);
		data = new FormData();
		data.top = new FormAttachment(introductionGroup, 10);
		data.bottom = new FormAttachment(100, 0);
		// data.left = new FormAttachment(wareListGroup, 0);
		data.width = OP_GROUP_WIDTH;
		data.right = new FormAttachment(100, 0);
		opGroup.setLayoutData(data);

		final Group wareListGroup = new Group(container, SWT.NONE);
		wareListGroup.setText(Messages.getString("WareView.warelist")); //$NON-NLS-1$
		// wareListGroup.setBounds(138, 76, 283, 246);
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.top = new FormAttachment(introductionGroup, 10);
		data.bottom = new FormAttachment(100, 0);
		data.right = new FormAttachment(100, -WARE_LIST_DEVIATION);
		wareListGroup.setLayoutData(data);
		// wareListGroup.setVisible(false);

		final Button addBtn = new Button(opGroup, SWT.NONE);
		addBtn.setText(Messages.getString("WareView.add")); //$NON-NLS-1$
		addBtn.setBounds(10, 27, 44, 22);
		addListener2AddBtn(addBtn);

		final Button modifyBtn = new Button(opGroup, SWT.NONE);
		modifyBtn.setText(Messages.getString("WareView.modify")); //$NON-NLS-1$
		modifyBtn.setBounds(10, 55, 44, 22);
		addListener2ModifyBtn(modifyBtn);

		final Button delBtn = new Button(opGroup, SWT.NONE);
		delBtn.setText(Messages.getString("WareView.delete")); //$NON-NLS-1$
		delBtn.setBounds(10, 83, 44, 22);
		addListener2DelBtn(delBtn);

		final Button refreshBtn = new Button(opGroup, SWT.NONE);
		refreshBtn.setText(Messages.getString("WareView.refresh")); //$NON-NLS-1$
		refreshBtn.setBounds(10, 111, 44, 22);
		addListener2RefreshBtn(refreshBtn);

		final Button searchBtn = new Button(opGroup, SWT.NONE);
		searchBtn.setText(Messages.getString("WareView.search"));
		searchBtn.setBounds(10, 139, 44, 22);
		searchBtn.setVisible(false);

		final Button saveBtn = new Button(opGroup, SWT.NONE);
		saveBtn.setText(Messages.getString("WareView.save")); //$NON-NLS-1$
		saveBtn.setBounds(10, 200, 44, 22);
		saveBtn.setVisible(false);
		addListener2SaveBtn(saveBtn);

		final Button cancelBtn = new Button(opGroup, SWT.NONE);
		cancelBtn.setText(Messages.getString("WareView.cancel")); //$NON-NLS-1$
		cancelBtn.setBounds(10, 228, 44, 22);
		cancelBtn.setVisible(false);
		addListener2CancelBtn(cancelBtn);

		FillLayout tableLayout = new FillLayout();
		wareListGroup.setLayout(tableLayout);

		viewer = new TableViewer(wareListGroup, SWT.BORDER | SWT.FULL_SELECTION
				| SWT.MULTI);
//		viewer.getco
		final Table table = viewer.getTable();
		// table.setFont(new Font(parent.getShell().getDisplay(), "Arial", 11,
		// SWT.BOLD));
		// table.setBounds(10, 17, 258, 214);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
//		table.setSortColumn(column)
//		table.setSortDirection(direction)
		final TableColumn num = new TableColumn(table, SWT.NONE);
		num.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				resetSort(WareViewerSort.NUM);
			}
		});
		num.setAlignment(SWT.CENTER);
		num.setWidth(50);
		num.setText(Messages.getString("WareView.warenum")); //$NON-NLS-1$

		final TableColumn name = new TableColumn(table, SWT.NONE);
		name.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				resetSort(WareViewerSort.NAME);
			}
		});
		name.setWidth(80);
		name.setText(Messages.getString("WareView.warename")); //$NON-NLS-1$
		name.setAlignment(SWT.CENTER);
//		table.setSortColumn(name);
//		table.setSortDirection(SWT.DOWN);
		
		final TableColumn desc = new TableColumn(table, SWT.NONE);
		desc.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				resetSort(WareViewerSort.DESC);
			}
		});
		desc.setWidth(110);
		desc.setText(Messages.getString("WareView.waredesc")); //$NON-NLS-1$

		final TableColumn price = new TableColumn(table, SWT.NONE);
		price.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				resetSort(WareViewerSort.PRICE);
			}
		});
		price.setWidth(70);
		price.setText(Messages.getString("WareView.wareprice")); //$NON-NLS-1$
		price.setAlignment(SWT.RIGHT);

		final TableColumn updDate = new TableColumn(table, SWT.NONE);
		updDate.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				resetSort(WareViewerSort.UPDDATE);
			}
		});
		updDate.setWidth(150);
		updDate.setText(Messages.getString("WareView.wareupddate")); //$NON-NLS-1$
		updDate.setAlignment(SWT.CENTER);

		viewer.setContentProvider(new WareContentProvider());
		viewer.setLabelProvider(new WareLabelProvider());

		viewer.setInput(DataTree.getDt().getAbstractRoot());
		viewer.setSorter(new WareViewerSort());
		//
		createActions();
		hookContextMenu();
		hookDoubleClickAction();
		// contributeToActionBars();
		initializeToolBar();
		initializeMenu();
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	public void setFocus() {
		KindTreeView.activeViewId = ID;
		viewer.getControl().setFocus();
	}
	public void refresh(){
		if(viewer.getInput() instanceof Kind){
			Kind kind = (Kind)viewer.getInput();
			introLab.setText(kind.getDisplayDesc());
		}
		viewer.refresh();
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		makeAddAction();
		makeModifyAction();
		makeDeleteAction();
		makeSaveAction();
		makeCancelAction();
		makeRefreshAction();
		makeDoubleClickAction();

	}

	private void makeAddAction() {
		addAction = new Action() {
			public void run() {
				WareInputDialog wareInputDialog = new WareInputDialog(
						getShell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL,
						viewer, ICommandIds.INSERT);
				wareInputDialog.setText(Messages.getString("WareView.waremanager")); //$NON-NLS-1$
				wareInputDialog.open();
			}
		};
		addAction.setText(Messages.getString("WareView.add")); //$NON-NLS-1$
		addAction.setToolTipText("add Action tooltip"); //$NON-NLS-1$
		addAction.setImageDescriptor(PlatformUI.getWorkbench()
				.getSharedImages().getImageDescriptor(
						ISharedImages.IMG_OBJS_INFO_TSK));

	}

	private void makeModifyAction() {
		modifyAction = new Action() {
			public void run() {
				WareInputDialog wareInputDialog = new WareInputDialog(
						getShell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL,
						viewer, ICommandIds.MODIFY);
				wareInputDialog.setText(Messages.getString("WareView.waremanager")); //$NON-NLS-1$
				wareInputDialog.open();
			}
		};
		modifyAction.setText(Messages.getString("WareView.modify")); //$NON-NLS-1$
		modifyAction.setToolTipText("modify Action tooltip"); //$NON-NLS-1$
		modifyAction.setImageDescriptor(PlatformUI.getWorkbench()
				.getSharedImages().getImageDescriptor(
						ISharedImages.IMG_OBJS_INFO_TSK));

	}

	private void makeDeleteAction() {
		deleteAction = new Action() {
			public void run() {
				boolean result = MessageDialog.openConfirm(getShell(), Messages.getString("WareView.confirmmessage"), //$NON-NLS-1$
						Messages.getString("WareView.confirmdeletethisware")); //$NON-NLS-1$
				if(result){
					ISelection selection = viewer.getSelection();
					Object obj = ((IStructuredSelection) selection)
							.getFirstElement();
					if(obj instanceof Ware){
						Ware ware = (Ware)obj;
						ware.getParent().removeChild(ware);
						viewer.refresh();
					}
				}

			}
		};
		deleteAction.setText(Messages.getString("WareView.delete")); //$NON-NLS-1$
		deleteAction.setToolTipText("delete action tooltip"); //$NON-NLS-1$
		deleteAction.setImageDescriptor(PlatformUI.getWorkbench()
				.getSharedImages().getImageDescriptor(
						ISharedImages.IMG_OBJS_INFO_TSK));

	}
	private void makeRefreshAction(){
			refreshAction = new Action() {
				public void run() {
					viewer.refresh();
				}
			};
			refreshAction.setText("refresh action"); //$NON-NLS-1$
			refreshAction.setToolTipText("refresh action tooltip"); //$NON-NLS-1$
			refreshAction.setImageDescriptor(PlatformUI.getWorkbench()
					.getSharedImages().getImageDescriptor(
							ISharedImages.IMG_OBJS_INFO_TSK));
	}
	private void makeSaveAction() {

⌨️ 快捷键说明

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