Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] MemoryEditorInput?

>> Is it possible to open an editor without connecting it to a file? I would
>> like to present a String in an IEditorInput, but can't find any suitable way
>> to do this.
>> 
>> IFile file = root.getFile(new Path("myfile.txt"));
>> FileEditorInput fileEditorInput = new FileEditorInput(file);
>> ITextEditor editor = (ITextEditor)page.openEditor(fileEditorInput,
>> desc.getId());

> It is possible, you just need to implement IStorage and
> IStorageEditorInput accordingly.

Yip, thus tried it, the next seems to do the trick, extend it at will.

-- Wieant


import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPersistableElement;
import org.eclipse.ui.IStorageEditorInput;

public class StringEditorInput implements IStorageEditorInput, IStorage
{
    protected String m_content;

    public StringEditorInput(String content)
    {
        m_content = content;
    }
    public InputStream getContents() throws CoreException
    {
        return new ByteArrayInputStream(m_content.getBytes());
    }
    public IStorage getStorage()
    {
        return this;
    }
    public IPath getFullPath()
    {
        return null;
    }
    public boolean isReadOnly()
    {
        return true;
    }
    public boolean exists()
    {
        return true;
    }
    public ImageDescriptor getImageDescriptor()
    {
        return null;
    }
    public String getName()
    {
        return "String Editor";
    }
    public IPersistableElement getPersistable()
    {
        return null;
    }
    public String getToolTipText()
    {
        return "";
    }
    @SuppressWarnings("unchecked")
    public Object getAdapter(Class adapter)
    {
        return null;
    }
}


Back to the top