我的多页编辑器是FormEditor, 我尝试使用继承TextEdior类,实现IFormPage接口的方式实现一个xml编辑器页面,但是始终无法正确显示文本。文本的输入类型是FileEditorInput。高手帮忙看看这个编辑器类应该怎么写?//添加页面的代码
TextPage textPage = new TextPage(this, "112222", "test name");
addPage(textPage);//实现xml编辑器的代码
public class TextPage extends XMLEditor implements IFormPage{
private FormEditor editor;
private PageForm mform;
private int index;
private String id;
private static class PageForm extends ManagedForm {
public PageForm(TextPage page, ScrolledForm form) {
super(page.getEditor().getToolkit(), form);
setContainer(page);
}
public TextPage getPage() {
return (TextPage)getContainer();
}
public void dirtyStateChanged() {
getPage().getEditor().editorDirtyStateChanged();
}
public void staleStateChanged() {
if (getPage().isActive())
refresh();
}
}
/**
* A constructor that creates the page and initializes it with the editor.
*
* @param editor
* the parent editor
* @param id
* the unique identifier
* @param title
* the page title
*/
public TextPage(FormEditor editor, String id, String title) {
this(id, title);
initialize(editor);
}
/**
* The constructor. The parent editor need to be passed in the
* <code>initialize</code> method if this constructor is used.
*
* @param id
* a unique page identifier
* @param title
* a user-friendly page title
*/
public TextPage(String id, String title) {
super();
this.id = id;
setPartName(title);
}
/**
* Initializes the form page.
*
* @see IEditorPart#init
*/
public void init(IEditorSite site, IEditorInput input) {
//setSite(site);
//setInput(input);
try {
super.init(site, input);
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Primes the form page with the parent editor instance.
*
* @param editor
* the parent editor
*/
public void initialize(FormEditor editor) {
this.editor = editor;
}
/**
* Returns the parent editor.
*
* @return parent editor instance
*/
public FormEditor getEditor() {
return editor;
}
/**
* Returns the managed form owned by this page.
*
* @return the managed form
*/
public IManagedForm getManagedForm() {
return mform;
}
/**
* Implements the required method by refreshing the form when set active.
* Subclasses must call super when overriding this method.
*/
public void setActive(boolean active) {
if (active) {
// We are switching to this page - refresh it
// if needed.
mform.refresh();
}
}
/**
* Tests if the page is active by asking the parent editor if this page is
* the currently active page.
*
* @return <code>true</code> if the page is currently active,
* <code>false</code> otherwise.
*/
public boolean isActive() {
return this.equals(editor.getActivePageInstance());
}
/**
* Creates the part control by creating the managed form using the parent
* editor's toolkit. Subclasses should override
* <code>createFormContent(IManagedForm)</code> to populate the form with
* content.
*
* @param parent
* the page parent composite
*/
public void createPartControl(Composite parent) {
ScrolledForm form = editor.getToolkit().createScrolledForm(parent);
mform = new PageForm(this, form);
createFormContent(mform);
}
/**
* Subclasses should override this method to create content in the form
* hosted in this page.
*
* @param managedForm
* the form hosted in this page.
*/
protected void createFormContent(IManagedForm managedForm) {
final ScrolledForm scrolledForm = managedForm.getForm();
final Composite body = scrolledForm.getBody();
body.setLayout(new FillLayout());
super.createPartControl(scrolledForm.getBody());
}
/**
* Returns the form page control.
*
* @return managed form's control
*/
public Control getPartControl() {
return mform != null ? mform.getForm() : null;
}
/**
* Disposes the managed form.
*/
public void dispose() {
if (mform != null)
mform.dispose();
}
/**
* Returns the unique identifier that can be used to reference this page.
*
* @return the unique page identifier
*/
public String getId() {
return id;
}
/**
* Returns <code>null</code>- form page has no title image. Subclasses
* may override.
*
* @return <code>null</code>
*/
public Image getTitleImage() {
return null;
}
/**
* Sets the focus by delegating to the managed form.
*/
public void setFocus() {
if (mform != null)
mform.setFocus();
}
/**
* @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
*/
public void doSave(IProgressMonitor monitor) {
if (mform != null)
mform.commit(true);
}
/**
* @see org.eclipse.ui.ISaveablePart#doSaveAs()
*/
public void doSaveAs() {
}
/**
* @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
*/
public boolean isSaveAsAllowed() {
return super.isSaveAsAllowed();
}
/**
* Implemented by testing if the managed form is dirty.
*
* @return <code>true</code> if the managed form is dirty,
* <code>false</code> otherwise.
*
* @see org.eclipse.ui.ISaveablePart#isDirty()
*/
public boolean isDirty() {
return mform != null ? mform.isDirty() : false;
}
/**
* Preserves the page index.
*
* @param index
* the assigned page index
*/
public void setIndex(int index) {
this.index = index;
}
/**
* Returns the saved page index.
*
* @return the page index
*/
public int getIndex() {
return index;
}
/**
* Form pages are not editors.
*
* @return <code>false</code>
*/
public boolean isEditor() {
return true;
}
/**
* Attempts to select and reveal the given object by passing the request to
* the managed form.
*
* @param object
* the object to select and reveal in the page if possible.
* @return <code>true</code> if the page has been successfully selected
* and revealed by one of the managed form parts, <code>false</code>
* otherwise.
*/
public boolean selectReveal(Object object) {
if (mform != null)
return mform.setInput(object);
return false;
}
/**
* By default, editor will be allowed to flip the page.
* @return <code>true</code>
*/
public boolean canLeaveThePage() {
return true;
}
protected void doSetInput (IEditorInput in) {
try {
super.doSetInput (in);
} catch (Exception e) {
e.printStackTrace();
}
}
}
TextPage textPage = new TextPage(this, "112222", "test name");
addPage(textPage);//实现xml编辑器的代码
public class TextPage extends XMLEditor implements IFormPage{
private FormEditor editor;
private PageForm mform;
private int index;
private String id;
private static class PageForm extends ManagedForm {
public PageForm(TextPage page, ScrolledForm form) {
super(page.getEditor().getToolkit(), form);
setContainer(page);
}
public TextPage getPage() {
return (TextPage)getContainer();
}
public void dirtyStateChanged() {
getPage().getEditor().editorDirtyStateChanged();
}
public void staleStateChanged() {
if (getPage().isActive())
refresh();
}
}
/**
* A constructor that creates the page and initializes it with the editor.
*
* @param editor
* the parent editor
* @param id
* the unique identifier
* @param title
* the page title
*/
public TextPage(FormEditor editor, String id, String title) {
this(id, title);
initialize(editor);
}
/**
* The constructor. The parent editor need to be passed in the
* <code>initialize</code> method if this constructor is used.
*
* @param id
* a unique page identifier
* @param title
* a user-friendly page title
*/
public TextPage(String id, String title) {
super();
this.id = id;
setPartName(title);
}
/**
* Initializes the form page.
*
* @see IEditorPart#init
*/
public void init(IEditorSite site, IEditorInput input) {
//setSite(site);
//setInput(input);
try {
super.init(site, input);
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Primes the form page with the parent editor instance.
*
* @param editor
* the parent editor
*/
public void initialize(FormEditor editor) {
this.editor = editor;
}
/**
* Returns the parent editor.
*
* @return parent editor instance
*/
public FormEditor getEditor() {
return editor;
}
/**
* Returns the managed form owned by this page.
*
* @return the managed form
*/
public IManagedForm getManagedForm() {
return mform;
}
/**
* Implements the required method by refreshing the form when set active.
* Subclasses must call super when overriding this method.
*/
public void setActive(boolean active) {
if (active) {
// We are switching to this page - refresh it
// if needed.
mform.refresh();
}
}
/**
* Tests if the page is active by asking the parent editor if this page is
* the currently active page.
*
* @return <code>true</code> if the page is currently active,
* <code>false</code> otherwise.
*/
public boolean isActive() {
return this.equals(editor.getActivePageInstance());
}
/**
* Creates the part control by creating the managed form using the parent
* editor's toolkit. Subclasses should override
* <code>createFormContent(IManagedForm)</code> to populate the form with
* content.
*
* @param parent
* the page parent composite
*/
public void createPartControl(Composite parent) {
ScrolledForm form = editor.getToolkit().createScrolledForm(parent);
mform = new PageForm(this, form);
createFormContent(mform);
}
/**
* Subclasses should override this method to create content in the form
* hosted in this page.
*
* @param managedForm
* the form hosted in this page.
*/
protected void createFormContent(IManagedForm managedForm) {
final ScrolledForm scrolledForm = managedForm.getForm();
final Composite body = scrolledForm.getBody();
body.setLayout(new FillLayout());
super.createPartControl(scrolledForm.getBody());
}
/**
* Returns the form page control.
*
* @return managed form's control
*/
public Control getPartControl() {
return mform != null ? mform.getForm() : null;
}
/**
* Disposes the managed form.
*/
public void dispose() {
if (mform != null)
mform.dispose();
}
/**
* Returns the unique identifier that can be used to reference this page.
*
* @return the unique page identifier
*/
public String getId() {
return id;
}
/**
* Returns <code>null</code>- form page has no title image. Subclasses
* may override.
*
* @return <code>null</code>
*/
public Image getTitleImage() {
return null;
}
/**
* Sets the focus by delegating to the managed form.
*/
public void setFocus() {
if (mform != null)
mform.setFocus();
}
/**
* @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor)
*/
public void doSave(IProgressMonitor monitor) {
if (mform != null)
mform.commit(true);
}
/**
* @see org.eclipse.ui.ISaveablePart#doSaveAs()
*/
public void doSaveAs() {
}
/**
* @see org.eclipse.ui.ISaveablePart#isSaveAsAllowed()
*/
public boolean isSaveAsAllowed() {
return super.isSaveAsAllowed();
}
/**
* Implemented by testing if the managed form is dirty.
*
* @return <code>true</code> if the managed form is dirty,
* <code>false</code> otherwise.
*
* @see org.eclipse.ui.ISaveablePart#isDirty()
*/
public boolean isDirty() {
return mform != null ? mform.isDirty() : false;
}
/**
* Preserves the page index.
*
* @param index
* the assigned page index
*/
public void setIndex(int index) {
this.index = index;
}
/**
* Returns the saved page index.
*
* @return the page index
*/
public int getIndex() {
return index;
}
/**
* Form pages are not editors.
*
* @return <code>false</code>
*/
public boolean isEditor() {
return true;
}
/**
* Attempts to select and reveal the given object by passing the request to
* the managed form.
*
* @param object
* the object to select and reveal in the page if possible.
* @return <code>true</code> if the page has been successfully selected
* and revealed by one of the managed form parts, <code>false</code>
* otherwise.
*/
public boolean selectReveal(Object object) {
if (mform != null)
return mform.setInput(object);
return false;
}
/**
* By default, editor will be allowed to flip the page.
* @return <code>true</code>
*/
public boolean canLeaveThePage() {
return true;
}
protected void doSetInput (IEditorInput in) {
try {
super.doSetInput (in);
} catch (Exception e) {
e.printStackTrace();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货