JFace中对话框的经典运用
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; } }