问题
I have generated a sample Multi-page Editor through the Eclipse wizard. Then, I have modified the sample plugin in order to have two pages:
- Text Editor
- Master Details Block
For the Master Details Block, I have used this tutorial.
I can open the Master Details Block page and I am also able to view the initialized objects in the list and display the corresponding details page. Now, I want to replace the static object with entries from the loaded file. My problem is, that I don't know how I can parse these entries from the text file. Do I need to implement my own parser, including the file handling or is this already implemented through a IFileEditorInput
interface?
In my ScrolledPropertiesBlock
class, I call the method viewer.setContentProvider(new MasterContentProvider());
. I am sure that I need to modify the MasterContentProvider
class implementation. So far, I have this:
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) {
}
}
If I delete the line MyEditorInput input = new MyEditorInput("test");
and do my cast instead, I get the following exception:
java.lang.ClassCastException: org.eclipse.ui.part.FileEditorInput cannot be cast to my.plugin.editor.MyEditorInput
Do I need to have a MyEditorInput
which extends FormEditorInput
(like in the example) and then implements 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;
}
}
The SimpleModel
class looks like this:
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$
}
}
回答1:
Eclipse gives you the input in an IEditorInput
- this probably be an IFileEditorInput
if the file is in the workspace. You can't cast this to something else. Creating new editor inputs does not help.
So you will have to read and parse the contents of the file from the editor input Eclipse gives you.
For a IFileEditorInput
you can call getFile
to get the input IFile
. You can then call IFile.getContents
to get an input stream containing the file's contents.
来源:https://stackoverflow.com/questions/34606920/how-to-read-opened-file-and-display-content-in-eclipse-master-details-block