如何在 Eclipse Master Details 块中读取打开的文件并显示内容?
How to read opened file and display content in Eclipse Master Details block?
我已经通过 Eclipse 向导生成了一个示例多页编辑器。然后,我修改了示例插件以便有两个页面:
- 文本编辑器
- 主详细信息块
对于Master Details Block,我使用了this tutorial。
我可以打开Master Details Block页面,也可以查看列表中的初始化对象,并显示相应的详情页。现在,我想用加载文件中的条目替换静态对象。我的问题是,我不知道如何从文本文件中解析这些条目。我是否需要实现自己的解析器,包括文件处理,或者这是否已经通过 IFileEditorInput
接口实现?
在我的ScrolledPropertiesBlock
class中,我调用了方法viewer.setContentProvider(new MasterContentProvider());
。我确定我需要修改 MasterContentProvider
class 实现。到目前为止,我有这个:
class MasterContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof FileEditorInput) {
//MyEditorInput input = (MyEditorInput) inputElement;
MyEditorInput input = new MyEditorInput("test");
return input.getModel().getContents();
}
return new Object[0];
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
如果我删除行 MyEditorInput input = new MyEditorInput("test");
并改为进行转换,我会得到以下异常:
java.lang.ClassCastException: org.eclipse.ui.part.FileEditorInput cannot be cast to my.plugin.editor.MyEditorInput
我是否需要一个 MyEditorInput
来扩展 FormEditorInput
(如示例中所示)然后实现 IFileEditorInput
?
public class MyEditorInput extends FormEditorInput {
private SimpleModel model;
public MyEditorInput(String name) {
super(name);
model = new SimpleModel();
}
public SimpleModel getModel() {
return model;
}
}
public class FormEditorInput implements IEditorInput {
private String name;
public FormEditorInput(String name) {
this.name = name;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IEditorInput#exists()
*/
public boolean exists() {
return true;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IEditorInput#getImageDescriptor()
*/
public ImageDescriptor getImageDescriptor() {
return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
ISharedImages.IMG_OBJ_ELEMENT);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IEditorInput#getName()
*/
public String getName() {
return name;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IEditorInput#getPersistable()
*/
public IPersistableElement getPersistable() {
return null;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IEditorInput#getToolTipText()
*/
public String getToolTipText() {
return getName();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
*/
public Object getAdapter(Class adapter) {
return null;
}
}
SimpleModel
class 看起来像这样:
public class SimpleModel {
private ArrayList modelListeners;
private ArrayList objects;
public SimpleModel() {
modelListeners = new ArrayList();
initialize();
}
public void addModelListener(IModelListener listener) {
if (!modelListeners.contains(listener))
modelListeners.add(listener);
}
public void removeModelListener(IModelListener listener) {
modelListeners.remove(listener);
}
public void fireModelChanged(Object[] objects, String type, String property) {
for (int i = 0; i < modelListeners.size(); i++) {
((IModelListener) modelListeners.get(i)).modelChanged(objects,
type, property);
}
}
public Object[] getContents() {
return objects.toArray();
}
private void initialize() {
objects = new ArrayList();
NamedObject[] objects = {
new TypeOne(Messages.getString("SimpleModel.t1_i1"), 2, true, Messages.getString("SimpleModel.text1")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i2"), 1, false, Messages.getString("SimpleModel.text2")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i3"), 3, true, Messages.getString("SimpleModel.text3")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i4"), 0, false, Messages.getString("SimpleModel.text4")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i5"), 1, true, Messages.getString("SimpleModel.text5")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeTwo(Messages.getString("SimpleModel.t2_i1"), false, true), //$NON-NLS-1$
new TypeTwo(Messages.getString("SimpleModel.t2_i2"), true, false)}; //$NON-NLS-1$
add(objects, false);
}
public void add(NamedObject[] objs, boolean notify) {
for (int i = 0; i < objs.length; i++) {
objects.add(objs[i]);
objs[i].setModel(this);
}
if (notify)
fireModelChanged(objs, IModelListener.ADDED, ""); //$NON-NLS-1$
}
public void remove(NamedObject[] objs, boolean notify) {
for (int i = 0; i < objs.length; i++) {
objects.remove(objs[i]);
objs[i].setModel(null);
}
if (notify)
fireModelChanged(objs, IModelListener.REMOVED, ""); //$NON-NLS-1$
}
}
Eclipse 在 IEditorInput
中为您提供输入 - 如果文件在工作区中,这可能是 IFileEditorInput
。您不能将其转换为其他内容。创建新的编辑器输入没有帮助。
所以您必须从 Eclipse 提供给您的编辑器输入中读取和解析文件内容。
对于 IFileEditorInput
,您可以调用 getFile
来获取输入 IFile
。然后您可以调用 IFile.getContents
来获取包含文件内容的输入流。
我已经通过 Eclipse 向导生成了一个示例多页编辑器。然后,我修改了示例插件以便有两个页面:
- 文本编辑器
- 主详细信息块
对于Master Details Block,我使用了this tutorial。
我可以打开Master Details Block页面,也可以查看列表中的初始化对象,并显示相应的详情页。现在,我想用加载文件中的条目替换静态对象。我的问题是,我不知道如何从文本文件中解析这些条目。我是否需要实现自己的解析器,包括文件处理,或者这是否已经通过 IFileEditorInput
接口实现?
在我的ScrolledPropertiesBlock
class中,我调用了方法viewer.setContentProvider(new MasterContentProvider());
。我确定我需要修改 MasterContentProvider
class 实现。到目前为止,我有这个:
class MasterContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof FileEditorInput) {
//MyEditorInput input = (MyEditorInput) inputElement;
MyEditorInput input = new MyEditorInput("test");
return input.getModel().getContents();
}
return new Object[0];
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
如果我删除行 MyEditorInput input = new MyEditorInput("test");
并改为进行转换,我会得到以下异常:
java.lang.ClassCastException: org.eclipse.ui.part.FileEditorInput cannot be cast to my.plugin.editor.MyEditorInput
我是否需要一个 MyEditorInput
来扩展 FormEditorInput
(如示例中所示)然后实现 IFileEditorInput
?
public class MyEditorInput extends FormEditorInput {
private SimpleModel model;
public MyEditorInput(String name) {
super(name);
model = new SimpleModel();
}
public SimpleModel getModel() {
return model;
}
}
public class FormEditorInput implements IEditorInput {
private String name;
public FormEditorInput(String name) {
this.name = name;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IEditorInput#exists()
*/
public boolean exists() {
return true;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IEditorInput#getImageDescriptor()
*/
public ImageDescriptor getImageDescriptor() {
return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
ISharedImages.IMG_OBJ_ELEMENT);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IEditorInput#getName()
*/
public String getName() {
return name;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IEditorInput#getPersistable()
*/
public IPersistableElement getPersistable() {
return null;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.IEditorInput#getToolTipText()
*/
public String getToolTipText() {
return getName();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
*/
public Object getAdapter(Class adapter) {
return null;
}
}
SimpleModel
class 看起来像这样:
public class SimpleModel {
private ArrayList modelListeners;
private ArrayList objects;
public SimpleModel() {
modelListeners = new ArrayList();
initialize();
}
public void addModelListener(IModelListener listener) {
if (!modelListeners.contains(listener))
modelListeners.add(listener);
}
public void removeModelListener(IModelListener listener) {
modelListeners.remove(listener);
}
public void fireModelChanged(Object[] objects, String type, String property) {
for (int i = 0; i < modelListeners.size(); i++) {
((IModelListener) modelListeners.get(i)).modelChanged(objects,
type, property);
}
}
public Object[] getContents() {
return objects.toArray();
}
private void initialize() {
objects = new ArrayList();
NamedObject[] objects = {
new TypeOne(Messages.getString("SimpleModel.t1_i1"), 2, true, Messages.getString("SimpleModel.text1")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i2"), 1, false, Messages.getString("SimpleModel.text2")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i3"), 3, true, Messages.getString("SimpleModel.text3")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i4"), 0, false, Messages.getString("SimpleModel.text4")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i5"), 1, true, Messages.getString("SimpleModel.text5")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeTwo(Messages.getString("SimpleModel.t2_i1"), false, true), //$NON-NLS-1$
new TypeTwo(Messages.getString("SimpleModel.t2_i2"), true, false)}; //$NON-NLS-1$
add(objects, false);
}
public void add(NamedObject[] objs, boolean notify) {
for (int i = 0; i < objs.length; i++) {
objects.add(objs[i]);
objs[i].setModel(this);
}
if (notify)
fireModelChanged(objs, IModelListener.ADDED, ""); //$NON-NLS-1$
}
public void remove(NamedObject[] objs, boolean notify) {
for (int i = 0; i < objs.length; i++) {
objects.remove(objs[i]);
objs[i].setModel(null);
}
if (notify)
fireModelChanged(objs, IModelListener.REMOVED, ""); //$NON-NLS-1$
}
}
Eclipse 在 IEditorInput
中为您提供输入 - 如果文件在工作区中,这可能是 IFileEditorInput
。您不能将其转换为其他内容。创建新的编辑器输入没有帮助。
所以您必须从 Eclipse 提供给您的编辑器输入中读取和解析文件内容。
对于 IFileEditorInput
,您可以调用 getFile
来获取输入 IFile
。然后您可以调用 IFile.getContents
来获取包含文件内容的输入流。