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

📄 logdialog.java

📁 CD Manager光盘资料管家婆源代码
💻 JAVA
字号:
package com.galaxyworkstation.view;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.galaxyworkstation.control.CDManager;
import com.galaxyworkstation.model.Log;


public class LogDialog extends Dialog {

	private Table table;
	private Button export;
	private Button delete;
	private Button cancel;
	
	protected Object result;
	protected Shell shell;

	/**
	 * Create the dialog
	 */
	public LogDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 */
	public LogDialog(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 */
	public Object open() {
		createContents();
		addListener();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new FormLayout());
		shell.setSize(420, 363);
		shell.setLocation((shell.getDisplay().getClientArea().width - shell.getClientArea().width) / 2,
				(shell.getDisplay().getClientArea().height - shell.getClientArea().height) / 2);
		shell.setText("查看日志");

		final Group group = new Group(shell, SWT.NONE);
		final FormData fd_group = new FormData();
		fd_group.right = new FormAttachment(100, -5);
		fd_group.left = new FormAttachment(0, 5);
		fd_group.bottom = new FormAttachment(100, -5);
		fd_group.top = new FormAttachment(0, 0);
		group.setLayoutData(fd_group);

		table = new Table(group, SWT.BORDER);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setBounds(10, 22, 384, 260);

		final TableColumn newColumnTableColumn = new TableColumn(table, SWT.NONE);
		newColumnTableColumn.setWidth(118);
		newColumnTableColumn.setText("时间");

		final TableColumn newColumnTableColumn_1 = new TableColumn(table, SWT.NONE);
		newColumnTableColumn_1.setWidth(262);
		newColumnTableColumn_1.setText("操作");

		Log[] logs = CDManager.action.getLogs();
		TableItem tableItem;
		for(int i = 0;i < logs.length;i++){
			tableItem = new TableItem(table,SWT.NONE);
			tableItem.setText(new String[]{logs[i].getDateString(),logs[i].getAction()});
		}

		export = new Button(group, SWT.NONE);
		export.setText("导  出");
		export.setBounds(54, 294, 67, 22);

		delete = new Button(group, SWT.NONE);
		delete.setText("删  除");
		delete.setBounds(169, 294, 67, 22);

		cancel = new Button(group, SWT.NONE);
		cancel.setText("退  出");
		cancel.setBounds(281, 294, 67, 22);
		
	}
	
	/*************************************************
	 ************Register Listener *******************
	 *************************************************/
	
	private void addListener(){
		
		export.addSelectionListener(exportSelection);
		
		delete.addSelectionListener(deleteSelection);
		
		cancel.addSelectionListener(cancelSelection);
		
	}
	
	/*********************************************************
	 ******************* Listener ****************************
	 *********************************************************/	
	
	/**
	 * 导出事件
	 */
	private SelectionListener exportSelection = new SelectionListener(){

		public void widgetDefaultSelected(SelectionEvent e) {
		}

		public void widgetSelected(SelectionEvent e) {
			
			final DirectoryDialog dirDialog = new DirectoryDialog(shell);
			dirDialog.setMessage("请选择要将日志文件导出到的文件夹...");
			String path = dirDialog.open();
			if (path != null) {
				MessageBox msgBox = new MessageBox(shell, SWT.ICON_QUESTION
						| SWT.YES | SWT.NO);
				msgBox.setText("导出操作日志");
				msgBox.setMessage("您确定将操作日志导出到 " + path + " 吗?");
				int choice = msgBox.open();
				if (choice == SWT.YES) {
					CDManager.action.exportLogs(path);
					msgBox = new MessageBox(shell, SWT.ICON_INFORMATION
							| SWT.OK);
					msgBox.setText("成功");
					msgBox.setMessage("日志成功导出为 " + path + File.separator + "log.txt 文件!" );
					msgBox.open();
				}
			}
		}
		
	};
	
	/**
	 * 删除事件
	 */
	private SelectionListener deleteSelection = new SelectionListener(){

		public void widgetDefaultSelected(SelectionEvent e) {
		}

		public void widgetSelected(SelectionEvent e) {
			
			MessageBox msgBox = new MessageBox(shell,SWT.ICON_WARNING|SWT.YES|SWT.NO);
			msgBox.setText("警告");
			msgBox.setMessage("您将删除全部日志,是否继续?");
			int choice = msgBox.open();
			if(choice == SWT.YES){
				CDManager.action.deleteLogs();
				table.removeAll();
				msgBox = new MessageBox(shell, SWT.ICON_INFORMATION
						| SWT.OK);
				msgBox.setText("成功");
				msgBox.setMessage("日志成功删除!" );
				msgBox.open();
			}
		}
		
	};
	
	/**
	 * 取消事件
	 */
	private SelectionListener cancelSelection = new SelectionListener(){

		public void widgetDefaultSelected(SelectionEvent e) {
		}

		public void widgetSelected(SelectionEvent e) {
			
			shell.dispose();
		}
		
	};

}

⌨️ 快捷键说明

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