`
longgangbai
  • 浏览: 7272746 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JFace中对话框的经典应用

阅读更多
    在项目中必须针对Jface中代码进行相关的重写实现自己的功能。
 源代码如下:
      在项目中不管是属性页还是对话框如果要添加特殊的记忆功能(在项目启动之后,显示上次或者以前的信息),一
般都要是写在本地应用系统中的xml中。
package com.vnvntrip.plugin.dev.views.custom;

import java.io.IOException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
 * 重写jface中dialog的使用功能,记下Dialog中的值,当再次打开这个Dialog的时候,还原这些值。
 * 这就需要把这些Dialog的值保存起来。Dialog的IDialogSettings类提供了这个功能。
 * 记得建立需要的文件,在当前workspace下建立文件夹content,然后在文件夹下建立system.xml文件。当然你也可以利用程序来实现。
 * 
 * 带提示框的Dialog
 * 使用方法和前边相同,不同的是不是继承自Dialog而是继承自TitleAreaDialog,然后在createDialogArea中加入两行
 * setTitle("标题");
 * setMessage("提示信息")
 * setMessage可以加上图片,加入的办法是setMessage("提示信息",IMessageProvider.WARNING);如果想加入其他的图片,调用相应的常量。
 * 
 * @author longgangbai
 *
 */
public class JfaceDialog extends Dialog {

	private Text text;
	public JfaceDialog(Shell parentShell) {
		super(parentShell);
	}
	 /**
         * 添加自己的控件
         */
        @Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new RowLayout());
		text = new Text(container, SWT.BORDER);
		text.setLayoutData(new RowData(100,-1));
		//设置关闭保存的存储信息
		if (text.getText() == null || text.getText().equals("")){
			restoreState();
		}
		return container;
	}
        /**
         * 重写Dialog中按钮的个数
         */
        @Override
	protected void createButtonsForButtonBar(Composite parent) {
	   super.createButtonsForButtonBar(parent);
        }
        /**
         * 加入右上角的最大化和关闭
         */
        @Override
	protected int getShellStyle(){
		return super.getShellStyle()|SWT.RESIZE|SWT.MAX;
	}
        /**
         * 改变dialog的大小
         */
        @Override
	protected Point getInitialSize(){
		return new Point(300,400);//300是宽400是高
	}
       /**
        * 加入自己的按钮
        */
        @Override
	protected void initializeBounds(){
		Composite comp = (Composite)getButtonBar();
		super.createButton(comp, IDialogConstants.OK_ID, "完成", true);
	}
	@Override
	protected void buttonPressed(int button){
		saveState();
	}
	/**
	 * 保存对话框信息的方法
	 */
	public void saveState(){
		if (text.getText() == null || text.getText().equals("")){
			return ;
		}
		IDialogSettings topSettings = getTopSettings();
		IDialogSettings settings =  topSettings.getSection("TestDialog");
		if(settings == null)settings = topSettings.addNewSection("TestDialog");
		settings.put("value", text.getText());
		try{
			topSettings.save("content/system.xml");
		}catch(IOException e){
			System.out.println(e.getMessage());
		}
	}
	/**
	 * 当打开对话框时显示关闭时的各种信息
	 */
	public void restoreState(){
		IDialogSettings topSettings = getTopSettings();
		IDialogSettings settings =  topSettings.getSection("TestDialog");
		if(settings == null) return;
		if (text.getText() == null || text.getText().equals("")){
			text.setText(settings.get("value"));
		}
	}
	/**
	 * 加载保存在对话框的信息
	 * @return
	 */
	public IDialogSettings getTopSettings(){
		IDialogSettings topSettings = new DialogSettings("system");
		try{
			topSettings.load("content/system.xml");
		}catch(IOException e){
			System.out.println(e.getMessage());
		}
		return topSettings;
	}

	

}

 

 

分享到:
评论

相关推荐

    Eclipse_Swt_Jface_核心应用_部分19

    第13章 SWT的高级应用.. 262 13.1 打印支持 262 13.1.1 打印类(Printer)和打印数据类(PrinterData) 262 13.1.2 打印程序示例概述 265 13.1.3 打印程序示例:主窗口程序 265 13.1.4 打印程序示例:打开...

    第10章 对话框的使用.ppt

    JFace库提供了能够满足各种典型应用所需的多种类型对话框。 在使用JFace库提供的组件前,应该在项目的构建路径中导入JFace类库。为了使对话框的一些按钮等元素使用中文提示,还需要添加外部归档 org.eclipse.jface....

    SWT JFace 小制作 文本阅读器

    JFace 是一个基于 Eclipse 平台的应用程序框架,提供了许多有用的功能,例如对话框、菜单、工具栏等。JFace 可以和 SWT 集成,提供了一个完整的应用程序框架。 文本阅读器功能 该应用程序是一个简单的文本阅读器,...

    Eclipse RCP详细教程

    目 录 1.富客户端平台 1 1.1.概述 1 1.2.Eclipse RCP 建设风格——插件,扩展和扩展点 1 2.创建你的第一个RCP程序 1 2.1.创建一个RCP程序 2 2.2.启动你的RCP程序 5 2.3.应用程序VS 产品...23.2.Jface 154

    Eclipse插件开发笔记—PDF—带目录

    第四篇则围绕插件开发和GEF应用两个主题,精心设计了两个程序开发实例,使读者能更加全面地理解插件开发相关技术。 目录: 第一篇 开发基础 第1章 Eclipse平台简介 1.1 Eclipse集成开发环境(IDE)介绍 1.1.1 安装及...

    Eclipse权威开发指南2.pdf

    2.1.1 提示没有任何JRE可用的对话框..... 14 2.1.2 创建您的第一个项目..... 15 2.1.3 Eclipse的外观:编辑器、视图和透视图..... 16 2.1.4 用户界面概述..... 18 2.1.5 首选项介绍..... 19 2.2 基本的Eclipse使用...

    Eclipse权威开发指南3.pdf

    2.1.1 提示没有任何JRE可用的对话框..... 14 2.1.2 创建您的第一个项目..... 15 2.1.3 Eclipse的外观:编辑器、视图和透视图..... 16 2.1.4 用户界面概述..... 18 2.1.5 首选项介绍..... 19 2.2 基本的...

    Eclipse权威开发指南1.pdf

    2.1.1 提示没有任何JRE可用的对话框..... 14 2.1.2 创建您的第一个项目..... 15 2.1.3 Eclipse的外观:编辑器、视图和透视图..... 16 2.1.4 用户界面概述..... 18 2.1.5 首选项介绍..... 19 2.2 基本的...

    Eclipse RCP Plugin 开发自学入门指南(CEC首发)

    Eclipse RCP 允许开发者使用 eclipse结构风格设计弹性的可扩展的应用程序,可重用Eclipse中 已存在的方法和编码模式。俗话说,万事开头难。Eclipse RCP 入手可能会比较困难、费时。接下 来我们将主要讲述如何让RCP ...

    Eclipse RCP Plug-in开发自学教程(Eclipse3.6)

    2.6 应用程序的PLUGIN ID ................................................................................................................21 3 ACTIONS的用法(菜单栏和工具栏) ..............................

Global site tag (gtag.js) - Google Analytics