IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Eclipse插件(RCP)自定义编辑器添加Dirty效果 -> 正文阅读

[Java知识库]Eclipse插件(RCP)自定义编辑器添加Dirty效果

前言

前一篇文章介绍了如何自定义编辑器,这一篇介绍如何使得自定义编辑器向文本编辑器那样,如果修改了,文章标题处出现一个 * ,表示该文本已被编辑过还未保存。当保存后 * 消失。
编辑时效果如下
在这里插入图片描述
关闭时提示如下
在这里插入图片描述
视频效果

思路

	/**
	 * 转换编辑器状态
	 * 
	 * @param isDirty
	 */
	public void changeDirtyState(boolean isDirty) {
		this.isDirty = isDirty;
		firePropertyChange(PROP_DIRTY);
	}
	
	public void toDirty() {
		changeDirtyState(true);
	}

	public void clearDirty() {
		changeDirtyState(false);
	}
  • 编辑器中各表单组件数据修改后加修改dirty的值
  1. Widget 的子类可以添加addModifyListener监听器,也可以写一个工具方法
	private void addDirtyListener(Widget com) {
		com.addListener(SWT.Modify, e->{
			editor.toDirty();
		});
	}
  1. 对非Widget 子类可在修改完成时改变dirty状态,如我们可以对deleteButton做如下修改:
		delete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				if (dogImage != null) {
					dogImage.dispose();
					dogImage = null;
					dogPhoto.redraw();
					// 新加的代码
					dogImagePath = "";
					editor.toDirty();
				}
			}
		});

最终代码

FormEditor

package com.xzbd.editors;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import com.xzbd.views.FormEditorViewer;

public class FormEditor extends EditorPart {

	public static String ID = "com.xzbd.editors.FormEditor";

	public boolean isDirty = false;
	private FormEditorViewer viewer;

	@Override
	public void doSave(IProgressMonitor monitor) {
		FormEditorData data = viewer.getData();
		System.out.println(data);
		clearDirty();
	}

	@Override
	public void doSaveAs() {

	}

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		this.setSite(site);
		this.setInput(input);
	}

	@Override
	public boolean isDirty() {
		return isDirty;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		viewer = new FormEditorViewer(parent,this);

	}

	@Override
	public void setFocus() {

	}

	/**
	 * 转换编辑器状态
	 * 
	 * @param isDirty
	 */
	public void changeDirtyState(boolean isDirty) {
		this.isDirty = isDirty;
		firePropertyChange(PROP_DIRTY);
	}
	
	public void toDirty() {
		changeDirtyState(true);
	}

	public void clearDirty() {
		changeDirtyState(false);
	}
}

FormEditorViewer

package com.xzbd.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;

import com.xzbd.editors.FormEditor;
import com.xzbd.editors.FormEditorData;


/**
 * 
 * 工程配置项编辑器视图
 *
 */
public class FormEditorViewer {

	private FormEditor editor;
	private Composite parent;

	private Text dogName;
	private Combo dogBreed;
	private Canvas dogPhoto;
	private Image dogImage;
	private String  dogImagePath;
	private List categories;
	private Text ownerName;
	private Text ownerPhone;

	public FormEditorViewer(Composite parent, FormEditor editor) {
		this.parent = parent;
		// 缓存 Editor
		setEditor(editor);

		// 绘制UI
		buildPageUI();
	}

	// 创建UI
	private void buildPageUI() {
		// 设置 parent 布局,采用3列 GridLayout 方式布局
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		parent.setLayout(gridLayout);

		new Label(parent, SWT.NONE).setText("Dog's Name:");
		dogName = new Text(parent, SWT.SINGLE | SWT.BORDER);
		// 设置 label 布局,横向占两列满填充
		GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalSpan = 2;
		dogName.setLayoutData(gridData);
		addDirtyListener(dogName);

		new Label(parent, SWT.NONE).setText("Breed:");
		dogBreed = new Combo(parent, SWT.NONE);
		dogBreed.setItems(new String[] { "Collie", "Pitbull", "Poodle", "Scottie" });
		dogBreed.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
		addDirtyListener(dogBreed);

		Label label = new Label(parent, SWT.NONE);
		label.setText("Categories");
		label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));

		new Label(parent, SWT.NONE).setText("Photo:");
		dogPhoto = new Canvas(parent, SWT.BORDER);
		gridData = new GridData(GridData.FILL_BOTH);
		gridData.widthHint = 80;
		gridData.heightHint = 80;
		gridData.verticalSpan = 3;
		dogPhoto.setLayoutData(gridData);
		dogPhoto.addPaintListener(new PaintListener() {
			public void paintControl(final PaintEvent event) {
				if (dogImage != null) {
					event.gc.drawImage(dogImage, 0, 0);
					editor.toDirty();
				}
			}
		});
		
		
		categories = new List(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
		categories.setItems(new String[] { "Best of Breed", "Prettiest Female", "Handsomest Male", "Best Dressed",
				"Fluffiest Ears", "Most Colors", "Best Performer", "Loudest Bark", "Best Behaved", "Prettiest Eyes",
				"Most Hair", "Longest Tail", "Cutest Trick" });
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
		gridData.verticalSpan = 4;
		int listHeight = categories.getItemHeight() * 12;
		Rectangle trim = categories.computeTrim(0, 0, 0, listHeight);
		gridData.heightHint = trim.height;
		categories.setLayoutData(gridData);
		categories.addTouchListener(e->{
			editor.toDirty();
		});
		
		Button browse = new Button(parent, SWT.PUSH);
		browse.setText("Browse...");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalIndent = 5;
		browse.setLayoutData(gridData);
		browse.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				String fileName = new FileDialog(parent.getShell()).open();
				if (fileName != null) {
					dogImage = new Image(parent.getDisplay(), fileName);
					dogImagePath = fileName;
					editor.toDirty();
				}
			}
		});
		
		
		Button delete = new Button(parent, SWT.PUSH);
		delete.setText("Delete");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
		gridData.horizontalIndent = 5;
		delete.setLayoutData(gridData);
		delete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				if (dogImage != null) {
					dogImage.dispose();
					dogImage = null;
					dogPhoto.redraw();
					
					dogImagePath = "";
					editor.toDirty();
				}
			}
		});
		
		
		Group ownerInfo = new Group(parent, SWT.NONE);
		ownerInfo.setText("Owner Info");
		gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		ownerInfo.setLayout(gridLayout);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalSpan = 2;
		ownerInfo.setLayoutData(gridData);
		new Label(ownerInfo, SWT.NONE).setText("Name:");
		ownerName = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
		ownerName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		addDirtyListener(ownerName);
		
		new Label(ownerInfo, SWT.NONE).setText("Phone:");
		ownerPhone = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
		ownerPhone.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		addDirtyListener(ownerPhone);
		
		
		Button enter = new Button(parent, SWT.PUSH);
		enter.setText("Enter");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gridData.horizontalSpan = 3;
		enter.setLayoutData(gridData);
		enter.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				System.out.println("/nDog Name: " + dogName.getText());
				System.out.println("Dog Breed: " + dogBreed.getText());
				System.out.println("Owner Name: " + ownerName.getText());
				System.out.println("Owner Phone: " + ownerPhone.getText());
				System.out.println("Categories:");
				String cats[] = categories.getSelection();
				for (int i = 0; i < cats.length; i++) {
					System.out.println("/t" + cats[i]);
				}
			}
		});
		if (dogImage != null) {
			dogImage.dispose();
		}

	}

	public FormEditor getEditor() {
		return editor;
	}

	public void setEditor(FormEditor editor) {
		this.editor = editor;
	}
	
	private void addDirtyListener(Widget com) {
		com.addListener(SWT.Modify, e->{
			editor.toDirty();
		});
	}

	public FormEditorData getData() {
		String dogNameText = dogName.getText();
		String dogBreedText = dogBreed.getText();
		String ownerNameText = ownerName.getText();
		String ownerPhoneText = ownerPhone.getText();
		String[] categoriesArr = categories.getSelection();
		FormEditorData data = new FormEditorData(
				dogNameText,
				dogBreedText,
				dogImagePath,
				ownerNameText,
				ownerPhoneText,
				categoriesArr
				);
		return data;
	}
}

FormEditorData

package com.xzbd.editors;

import java.util.Arrays;

public class FormEditorData {
	
	private String dogName;
	private String dogBreed;
	private String dogImagePath;
	private String ownerName;
	private String ownerPhone;
	private String[] categories;
	
	
	public FormEditorData(String dogName, String dogBreed, String dogImagePath, 
			String ownerName, String ownerPhone,
			String[] categories) {
		super();
		this.dogName = dogName;
		this.dogBreed = dogBreed;
		this.dogImagePath = dogImagePath;
		this.ownerName = ownerName;
		this.ownerPhone = ownerPhone;
		this.categories = categories;
	}
	public String getDogName() {
		return dogName;
	}
	public void setDogName(String dogName) {
		this.dogName = dogName;
	}
	public String getDogBreed() {
		return dogBreed;
	}
	public void setDogBreed(String dogBreed) {
		this.dogBreed = dogBreed;
	}
	public String getDogImagePath() {
		return dogImagePath;
	}
	public void setDogImagePath(String dogImagePath) {
		this.dogImagePath = dogImagePath;
	}
	public String getOwnerName() {
		return ownerName;
	}
	public void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}
	public String getOwnerPhone() {
		return ownerPhone;
	}
	public void setOwnerPhone(String ownerPhone) {
		this.ownerPhone = ownerPhone;
	}
	public String[] getCategories() {
		return categories;
	}
	public void setCategories(String[] categories) {
		this.categories = categories;
	}
	@Override
	public String toString() {
		return "FormEditorData [dogName=" + dogName + ",\r\n dogBreed=" + dogBreed + ",\r\n dogImagePath=" + dogImagePath
				+ ",\r\n ownerName=" + ownerName + ",、\r\n ownerPhone=" + ownerPhone + ",\r\n categories="
				+ Arrays.toString(categories) + "]";
	}

	
}

FormEditorData是表单数据对象,当按下Ctrl+S后,FormEditordoSave方法会从viewer中获取最新的值,并打印,其相关代码如下:

	@Override
	public void doSave(IProgressMonitor monitor) {
		FormEditorData data = viewer.getData();
		System.out.println(data);
		clearDirty();
	}

打印内容示例:

FormEditorData [dogName=中华田园犬2,
 dogBreed=Pitbull,
 dogImagePath=C:\Users\sun\Pictures\9958c8db-0a11-4bab-8d12-7782ed892fa1.jpg,
 ownerName=张三,、
 ownerPhone=1899999999,
 categories=[Fluffiest Ears, Best Performer, Best Behaved]]

总结

文章讲述了如何对自定义编辑器添加 dirty 效果,以使得编辑器与文本编辑器具有相同的保存、修改行为,方便客户使用。

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-12-28 22:46:11  更:2021-12-28 22:46:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 8:44:18-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码