首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

多项编者页的学习应用

2013-09-11 
多项编辑页的学习应用package com.easyway.plugin.dev.editors.ui.editorsimport java.io.Fileimport ja

多项编辑页的学习应用

package com.easyway.plugin.dev.editors.ui.editors;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;

import com.easyway.plugin.dev.editors.core.CopyUtils;
import com.easyway.plugin.dev.editors.core.Env;
import com.easyway.plugin.dev.editors.ui.listener.MultiPageEditorListener;
import com.easyway.plugin.dev.editors.ui.providers.PropertyContentProvider;
import com.easyway.plugin.dev.editors.ui.providers.PropertyLabelProvider;

/**
?*?? 自定义属性编辑器的应用:
?*?? 页面1:采用文本编辑器编辑属性信息
?*?? 页面2:采用组件编辑属性信息
?* @author longgangbai
?*
?*/
public class MultiPageEditor extends MultiPageEditorPart implements IResourceChangeListener{

?public TextEditor getEditor() {
??return editor;
?}
?public void setEditor(TextEditor editor) {
??this.editor = editor;
?}
?public List<Map<String,String>> getPropsMap() {
??return propsMap;
?}
?public void setPropsMap(List<Map<String,String>> propsMap) {
??this.propsMap = propsMap;
?}
?public TableViewer getTableViewer() {
??return tableViewer;
?}
?public void setTableViewer(TableViewer tableViewer) {
??this.tableViewer = tableViewer;
?}
?public Table getTable() {
??return table;
?}
?public void setTable(Table table) {
??this.table = table;
?}
?public Button getBtnAdd() {
??return btnAdd;
?}
?public void setBtnAdd(Button btnAdd) {
??this.btnAdd = btnAdd;
?}
?public Button getBtnDelete() {
??return btnDelete;
?}
?public void setBtnDelete(Button btnDelete) {
??this.btnDelete = btnDelete;
?}
?public Button getBtnUpdate() {
??return btnUpdate;
?}
?public void setBtnUpdate(Button btnUpdate) {
??this.btnUpdate = btnUpdate;
?}
?/** 文本编辑页面 */
?private TextEditor editor;

?/** 手动添加操作属性信息*/
?List<Map<String,String>> propsMap=new ArrayList<Map<String,String>>();
?
?private TableViewer tableViewer;
?private Table table;
?private Button btnAdd;
?private Button btnDelete;
?private Button btnUpdate;
?/**
? * Creates a multi-page editor example.
? */
?public MultiPageEditor() {
??super();
??ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
?}
?/**
? * 创建文本的属性编辑器
? */
?void createTextPropertyEidtors() {
??try {
???editor = new TextEditor();
???int index = addPage(editor, getEditorInput());
???setPageText(index, editor.getTitle());
??} catch (PartInitException e) {
???ErrorDialog.openError(
????getSite().getShell(),
????"文本编辑器初始化错误 !",
????null,
????e.getStatus());
??}
?}
?/**
? * 属性的编辑组件
? */
?void createPropertyEidtorPage() {

??Composite container = new Composite(getContainer(), SWT.NONE);
??container.setLayout(new GridLayout(2, false));
??{
???{
????tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
????? ?table = tableViewer.getTable();
????{
?????GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
?????gridData.widthHint = 300;
?????table.setLayoutData(gridData);
????}
???}
???{
????Composite btnComposite = new Composite(container, SWT.NONE);
????btnComposite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
????btnComposite.setLayout(new GridLayout(1, false));
???? btnAdd=new Button(btnComposite,SWT.NONE);
???? btnAdd.setText("添加");
???? btnUpdate=new Button(btnComposite,SWT.NONE);
???? btnUpdate.setText("编辑");
???? btnDelete=new Button(btnComposite,SWT.NONE);
???? btnDelete.setText("删除");
???}
??}

??int index = addPage(container);
??setPageText(index, "属性文件");
?}
??? /**
???? * 添加创建所有的编辑页面
???? */
?protected void createPages() {
??//构建文本编辑页面
??createTextPropertyEidtors();
??//构建属性编辑页面
??createPropertyEidtorPage();
??/**
?? * 设置TableViewer的内容
?? */
??setContentTableViewer();
??//绑定监听信息
??new MultiPageEditorListener().setPageListener(this);
??
??
?}
?/**
? * 设置TableViewer的内容
? */
?private void setContentTableViewer() {
??//绑定相关的模型
??TableColumn keyColumn = new TableColumn(table, SWT.CENTER);
??keyColumn.setResizable(true);
??keyColumn.setText("属性名称 ");
??keyColumn.setWidth(150);
??
??TableColumn valueColumn = new TableColumn(table, SWT.CENTER);
??valueColumn.setResizable(true);
??valueColumn.setText(" 属 性 值?? ");
??valueColumn.setWidth(150);
??
??table.setLinesVisible(true);
??table.setHeaderVisible(true);
??
??tableViewer.setColumnProperties(new String[]{Env.PROPERTY_KEY,Env.PROPERTY_VALUE});
??tableViewer.setLabelProvider(new PropertyLabelProvider());
??tableViewer.setContentProvider(new PropertyContentProvider());
??tableViewer.setInput(propsMap);
?}
?/**
? * The <code>MultiPageEditorPart</code> implementation of this
? * <code>IWorkbenchPart</code> method disposes all nested editors.
? * Subclasses may extend.
? */
?public void dispose() {
??ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
??super.dispose();
?}
?/**
? * Saves the multi-page editor's document.
? */
?public void doSave(IProgressMonitor monitor) {
??getEditor(0).doSave(monitor);
?}
?/**
? * Saves the multi-page editor's document as another file.
? * Also updates the text for page 0's tab, and updates this multi-page editor's input
? * to correspond to the nested editor's.
? */
?public void doSaveAs() {
??IEditorPart editor = getEditor(0);
??editor.doSaveAs();
??setPageText(0, editor.getTitle());
??setInput(editor.getEditorInput());
?}
?/* (non-Javadoc)
? * Method declared on IEditorPart
? */
?public void gotoMarker(IMarker marker) {
??setActivePage(0);
??IDE.gotoMarker(getEditor(0), marker);
?}
?/**
? * The <code>MultiPageEditorExample</code> implementation of this method
? * checks that the input is an instance of <code>IFileEditorInput</code>.
? */
?public void init(IEditorSite site, IEditorInput editorInput)
??throws PartInitException {
??if (!(editorInput instanceof IFileEditorInput))
???throw new PartInitException("Invalid Input: Must be IFileEditorInput");
??super.init(site, editorInput);
?}
?/* (non-Javadoc)
? * Method declared on IEditorPart.
? */
?public boolean isSaveAsAllowed() {
??return true;
?}
?/**
? * Calculates the contents of page 2 when the it is activated.
? */
?protected void pageChange(int newPageIndex) {
??try {
???super.pageChange(newPageIndex);
???IFile ifile=((FileEditorInput)editor.getEditorInput()).getFile();
???String fileName=editor.getEditorInput().getName();
???File file=new File(fileName);
???String ecoding=ifile.getCharset();
???CopyUtils.copy(ifile.getContents(), new FileOutputStream(file));
???if(newPageIndex==0){
?????Env.WritePropertesFile(propsMap, file);
???}else if(newPageIndex==1){
????propsMap=Env.getPropertyMaps(file);
????if(CollectionUtils.isNotEmpty(propsMap))
????{
?????tableViewer.setInput(propsMap);
????}
???}
??} catch (FileNotFoundException e) {
???e.printStackTrace();
??} catch (IOException e) {
???e.printStackTrace();
??} catch (CoreException e) {
???e.printStackTrace();
??}
?}
?/**
? * Closes all project files on project close.
? */
?public void resourceChanged(final IResourceChangeEvent event){
??if(event.getType() == IResourceChangeEvent.PRE_CLOSE){
???Display.getDefault().asyncExec(new Runnable(){
????public void run(){
?????IWorkbenchPage[] pages = getSite().getWorkbenchWindow().getPages();
?????for (int i = 0; i<pages.length; i++){
??????if(((FileEditorInput)editor.getEditorInput()).getFile().getProject().equals(event.getResource())){
???????IEditorPart editorPart = pages[i].findEditor(editor.getEditorInput());
???????pages[i].closeEditor(editorPart,true);
??????}
?????}
????}???????????
???});
??}
?}
?
}


我的异常网推荐解决方案:org.eclipse.core.runtime.CoreException,http://www.myexception.cn/eclipse/org.eclipse.core.runtime.CoreException.html

热点排行