Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » Basic Editor Question
Basic Editor Question [message #519946] Wed, 10 March 2010 15:14 Go to next message
Darin is currently offline DarinFriend
Messages: 12
Registered: March 2010
Junior Member
Hello,

OK, first off here's my specs.

Java Version : j2sdk1.4.2_05
Eclipse Version : 3.3.2 (Europa)

I just had a basic question in regard to creating an editor that supports syntax highlighting (eventually I plan to expand on it, but I want to get this basic part working first). Basically I want to be able to start my project and be able to select File.. Open, select a file and it automatically starts the Editor for that file extension.

I've created the default Plug-in project, based off the "Plug-in with an editor" template, and my one question is this:

How do I go about setting up the project so that when I launch it (F11), I can just select File.. Open, select the .xml file, and it will automatically launch the XML Editor. As it stands now, I have to create a new Java project, add-in the .xml file, and the right click on the .xml file and tell it to "open with..." XML Editor.

For instance, I can start Eclipse (where I have the CDT installed), select File.. Open, select a .c or .cpp file, and the C/C++ Editor is automatically loaded with the .c/.cpp (syntax highlighting and all). Can you please help me and guide me as to what I need to do to get this functionality implemented?

Thanks for your help.


- Darin
Re: Basic Editor Question [message #520007 is a reply to message #519946] Wed, 10 March 2010 18:29 Go to previous messageGo to next message
Stan is currently offline StanFriend
Messages: 20
Registered: July 2009
Junior Member
Open up your editor extension in plugin.xml and add the file extensions you want to handle into the "extensions" field.
Re: Basic Editor Question [message #520011 is a reply to message #520007] Wed, 10 March 2010 18:50 Go to previous messageGo to next message
Darin is currently offline DarinFriend
Messages: 12
Registered: March 2010
Junior Member
I have xml listed in the extensions, but still a no go. I still have to create a project before it will let me open the file in an XML Editor.

I tried setting default to true in the Extensions tab of plugin.xml, but now when I try to open an xml document, when there is no open project, the Editor just opens and displays "ERROR" on the page with a blue line above it. If I have a project open, then I can just open any xml file and it will display fine without me having to select the "Open with...", "XML Editor" option.


- Darin
Re: Basic Editor Question [message #520061 is a reply to message #520011] Thu, 11 March 2010 01:17 Go to previous messageGo to next message
Stan is currently offline StanFriend
Messages: 20
Registered: July 2009
Junior Member
The plugin.xml change has associated your XML editor with the .xml extension -- now when you double-click on .xml in your project, it creates your editor, and so it does when you file/open a file.

Now, when you file/open a file, the editor gets created with a different kind of editor input -- instead of the "standard" IFileEditorInput, it receives a FileStoreEditorInput. Depending on what you do with it, it sometimes needs to be handled a bit differently.

Most likely, you're doing something in the editor that expects a IFileEditorInput (does your DocumentProvider handle FileStoreEditorInput properly?)... The easiest thing to do is to set a breakpoint on any exception and then try to open an xml file. See where the problem happens and trace back to figure out what's going on.
Re: Basic Editor Question [message #520212 is a reply to message #520061] Thu, 11 March 2010 15:12 Go to previous messageGo to next message
Darin is currently offline DarinFriend
Messages: 12
Registered: March 2010
Junior Member
I'm just using the default Plug-in template, "Plug-in with an editor". I've made no changes to the project what-so-ever.

When I launch with F11, I can't just automatically open an .xml file with File/Open. I have to create a Java project first.

I just need to figure out how can I get the XML Editor to launch automatically when a File/Open request is done on an .xml file, without needed to have a project open?


- Darin
Re: Basic Editor Question [message #520233 is a reply to message #520212] Thu, 11 March 2010 16:09 Go to previous messageGo to next message
Stan is currently offline StanFriend
Messages: 20
Registered: July 2009
Junior Member
So, like I was saying, you *are* already opening your editor. Set a breakpoint on your constructor and you'll see that it's being called. Or, change the extension to xml2 and try to open a .xml2 file and you'll see the same thing.

The default plug-in with editor template is probably very simplistic and uses the StorageDocumentProvider to load up the document... when you open a file outside of the project, the file is accessed through a different IEditorInput which this simple example doesn't handle.

For now, you'll have to stick to files within projects, or code up a better document provider that handles external files, too.

BTW, you don't need a *Java* project -- any project will do...
Re: Basic Editor Question [message #520305 is a reply to message #520233] Thu, 11 March 2010 21:18 Go to previous messageGo to next message
Darin is currently offline DarinFriend
Messages: 12
Registered: March 2010
Junior Member
I think I found a reference that will help me:

http://wiki.eclipse.org/FAQ_How_do_I_open_an_editor_programm atically%3F


Thanks for all your help.

- Darin
Re: Basic Editor Question [message #520341 is a reply to message #520305] Fri, 12 March 2010 01:30 Go to previous messageGo to next message
Stan is currently offline StanFriend
Messages: 20
Registered: July 2009
Junior Member
I don't think that's going to help you, you've already got that part working... but, good luck!
Re: Basic Editor Question [message #524360 is a reply to message #520341] Wed, 31 March 2010 16:10 Go to previous messageGo to next message
Darin is currently offline DarinFriend
Messages: 12
Registered: March 2010
Junior Member
OK, so I did a little hack and I sort of got it working. Basically I determined that if a Project is not currently open, then the element Object passed into createDocument is of type FileStoreEditorInput, whereas if a project is current open, then the element Object is of type FileEditorInput.

So my idea is to create a FileEditorInput object and load it with the data from the FileStoreEditorInput object if a project is not currently open. Then open a temporary project, create a link to the original file in the temporary project, then display the file and delete the temporary project.

Here's how I went about doing it, but I'm still having a couple of issues maybe someone can help me with?

Here's what I changed in the DocumentProvider file that is automatically generated by the "Plug-in with an Editor" template:

protected IDocument createDocument(Object element) throws CoreException
{
    IDocument document = null;

    // Check if the document is of type FileEditorInput.
    if (element instanceof FileEditorInput)
    {
        // If yes, then a project must already be open so just create a new document, easy!
        document = super.createDocument(element);
    }
    // else, check if document is of type FileStoreEditorInput (i.e. project not open).
    else if (element instanceof FileStoreEditorInput)
    {
        // Implement our own code to create a FileEditorInput object from the FileStoreEditorInput object.
        FileStoreEditorInput tester = (FileStoreEditorInput)element;
			
        // Create a new temporary project object and open it.
        IProject project = 
            ResourcesPlugin.getWorkspace().getRoot().getProject("TempFilesTFTF");
        if (!project.exists())
            project.create(null);
        if (!project.isOpen())
            project.open(null);

        // Get the path to the file.
        IPath location = new Path(tester.getURI().getPath());
        IFile file = project.getFile(location.lastSegment());

        // Create a link to the file.
        file.createLink(location, IResource.NONE, null);
			
        // Create the new FileEditorInput.
        FileEditorInput newEditor = new FileEditorInput(file);
			
        // Create the document passing in the new FileEditorInput object (i.e. display the file).
        document = super.createDocument(newEditor);
			
        // Delete the temporary project.
        project.delete(true, null);
    }	
		
    // Attach the PartitionScanner to the document.
    if (document != null)
    {
        IDocumentPartitioner partitioner = new FastPartitioner(
            new MyPartitionScanner(),
            new String[] {MyPartitionScanner.COMMENT });

            partitioner.connect(document);
            document.setDocumentPartitioner(partitioner);				
    }

    return document;
}



Now everything goes as planned, but the file is opened as read-only, any ideas how I can fix this? I've tried playing with the attributes of both the File and Project object's readonly flag (i.e. setting setReadonly to false), but it's not working.

If you have any ideas or can show me a better way of going about opening a file outside of a project, please let me know.


- Darin
Re: Basic Editor Question [message #525525 is a reply to message #524360] Tue, 06 April 2010 18:46 Go to previous messageGo to next message
Darin is currently offline DarinFriend
Messages: 12
Registered: March 2010
Junior Member
Ok, I fixed the read-only bug I was getting. Apparently if you create a linked file in a new temporary project in createDocument, under the FileDocumentProvider class, the document gets opened, but as Read-Only.

To get around this, I implemented the init(IEditorSite, IEditorInput), under my TextEditor class. If the IEditorInput object was of type FileStoreEditorInput object I just turn it into a FileEditorInput object and send it to the super.

All seems to work good now. I can now open a file without needing to have an open project. Though I wish there were a way I could hide the created project folder in the Package Explorer. Any ideas?

Here my revised code:

public void init(IEditorSite site, IEditorInput input)
    throws PartInitException
{
    if (input instanceof FileStoreEditorInput)
    {
        // Implement our own code to create a FileEditorInput 
        // object from the FileStoreEditorInput object.
        FileStoreEditorInput fsEditorInput = (FileStoreEditorInput)input;
	        
        // Get the path to the file.
        IPath location = new Path(fsEditorInput.getURI().getPath());

        // Create a new temporary project object and open it.
        project = ResourcesPlugin.getWorkspace().getRoot()
            .getProject("TempFolderiiiiiiiii");
	        
        IFile file = null;
        try
        {
            // Create a project if one doesn't exist and open it.
            if (!project.exists())
                project.create(null);
            if (!project.isOpen())
                project.open(null);

            // Create a link to the file.
            file = project.getFile(location.lastSegment());
            file.createLink(location, IResource.REPLACE, null);	        
	        
            //FileEditorInput newFileEditorInput = new FileEditorInput(file);
            input = new FileEditorInput(file);
        }
        catch (Exception e)
        {
        }
    }	
		
    // TODO Auto-generated method stub
    super.init(site, input);
}
Re: Basic Editor Question [message #604997 is a reply to message #520011] Thu, 11 March 2010 01:17 Go to previous messageGo to next message
Stan is currently offline StanFriend
Messages: 20
Registered: July 2009
Junior Member
The plugin.xml change has associated your XML editor with the .xml extension -- now when you double-click on .xml in your project, it creates your editor, and so it does when you file/open a file.

Now, when you file/open a file, the editor gets created with a different kind of editor input -- instead of the "standard" IFileEditorInput, it receives a FileStoreEditorInput. Depending on what you do with it, it sometimes needs to be handled a bit differently.

Most likely, you're doing something in the editor that expects a IFileEditorInput (does your DocumentProvider handle FileStoreEditorInput properly?)... The easiest thing to do is to set a breakpoint on any exception and then try to open an xml file. See where the problem happens and trace back to figure out what's going on.
Re: Basic Editor Question [message #605014 is a reply to message #604997] Thu, 11 March 2010 15:12 Go to previous messageGo to next message
Darin is currently offline DarinFriend
Messages: 12
Registered: March 2010
Junior Member
I'm just using the default Plug-in template, "Plug-in with an editor". I've made no changes to the project what-so-ever.

When I launch with F11, I can't just automatically open an .xml file with File/Open. I have to create a Java project first.

I just need to figure out how can I get the XML Editor to launch automatically when a File/Open request is done on an .xml file, without needed to have a project open?


- Darin
Re: Basic Editor Question [message #605022 is a reply to message #605014] Thu, 11 March 2010 16:09 Go to previous messageGo to next message
Stan is currently offline StanFriend
Messages: 20
Registered: July 2009
Junior Member
So, like I was saying, you *are* already opening your editor. Set a breakpoint on your constructor and you'll see that it's being called. Or, change the extension to xml2 and try to open a .xml2 file and you'll see the same thing.

The default plug-in with editor template is probably very simplistic and uses the StorageDocumentProvider to load up the document... when you open a file outside of the project, the file is accessed through a different IEditorInput which this simple example doesn't handle.

For now, you'll have to stick to files within projects, or code up a better document provider that handles external files, too.

BTW, you don't need a *Java* project -- any project will do...
Re: Basic Editor Question [message #605026 is a reply to message #605022] Thu, 11 March 2010 21:18 Go to previous messageGo to next message
Darin is currently offline DarinFriend
Messages: 12
Registered: March 2010
Junior Member
I think I found a reference that will help me:

http://wiki.eclipse.org/FAQ_How_do_I_open_an_editor_programm atically%3F


Thanks for all your help.

- Darin
Re: Basic Editor Question [message #605028 is a reply to message #605026] Fri, 12 March 2010 01:30 Go to previous messageGo to next message
Stan is currently offline StanFriend
Messages: 20
Registered: July 2009
Junior Member
I don't think that's going to help you, you've already got that part working... but, good luck!
Re: Basic Editor Question [message #605400 is a reply to message #605028] Wed, 31 March 2010 16:10 Go to previous messageGo to next message
Darin is currently offline DarinFriend
Messages: 12
Registered: March 2010
Junior Member
OK, so I did a little hack and I sort of got it working. Basically I determined that if a Project is not currently open, then the element Object passed into createDocument is of type FileStoreEditorInput, whereas if a project is current open, then the element Object is of type FileEditorInput.

So my idea is to create a FileEditorInput object and load it with the data from the FileStoreEditorInput object if a project is not currently open. Then open a temporary project, create a link to the original file in the temporary project, then display the file and delete the temporary project.

Here's how I went about doing it, but I'm still having a couple of issues maybe someone can help me with?

Here's what I changed in the DocumentProvider file that is automatically generated by the "Plug-in with an Editor" template:


protected IDocument createDocument(Object element) throws CoreException
{
IDocument document = null;

// Check if the document is of type FileEditorInput.
if (element instanceof FileEditorInput)
{
// If yes, then a project must already be open so just create a new document, easy!
document = super.createDocument(element);
}
// else, check if document is of type FileStoreEditorInput (i.e. project not open).
else if (element instanceof FileStoreEditorInput)
{
// Implement our own code to create a FileEditorInput object from the FileStoreEditorInput object.
FileStoreEditorInput tester = (FileStoreEditorInput)element;

// Create a new temporary project object and open it.
IProject project =
ResourcesPlugin.getWorkspace().getRoot().getProject("TempFilesTFTF ");
if (!project.exists())
project.create(null);
if (!project.isOpen())
project.open(null);

// Get the path to the file.
IPath location = new Path(tester.getURI().getPath());
IFile file = project.getFile(location.lastSegment());

// Create a link to the file.
file.createLink(location, IResource.NONE, null);

// Create the new FileEditorInput.
FileEditorInput newEditor = new FileEditorInput(file);

// Create the document passing in the new FileEditorInput object (i.e. display the file).
document = super.createDocument(newEditor);

// Delete the temporary project.
project.delete(true, null);
}

// Attach the PartitionScanner to the document.
if (document != null)
{
IDocumentPartitioner partitioner = new FastPartitioner(
new MyPartitionScanner(),
new String[] {MyPartitionScanner.COMMENT });

partitioner.connect(document);
document.setDocumentPartitioner(partitioner);
}

return document;
}



Now everything goes as planned, but the file is opened as read-only, any ideas how I can fix this? I've tried playing with the attributes of both the File and Project object's readonly flag (i.e. setting setReadonly to false), but it's not working.

If you have any ideas or can show me a better way of going about opening a file outside of a project, please let me know.


- Darin
Re: Basic Editor Question [message #605479 is a reply to message #605400] Tue, 06 April 2010 18:46 Go to previous message
Darin is currently offline DarinFriend
Messages: 12
Registered: March 2010
Junior Member
Ok, I fixed the read-only bug I was getting. Apparently if you create a linked file in a new temporary project in createDocument, under the FileDocumentProvider class, the document gets opened, but as Read-Only.

To get around this, I implemented the init(IEditorSite, IEditorInput), under my TextEditor class. If the IEditorInput object was of type FileStoreEditorInput object I just turn it into a FileEditorInput object and send it to the super.

All seems to work good now. I can now open a file without needing to have an open project. Though I wish there were a way I could hide the created project folder in the Package Explorer. Any ideas?

Here my revised code:

public void init(IEditorSite site, IEditorInput input)
throws PartInitException
{
if (input instanceof FileStoreEditorInput)
{
// Implement our own code to create a FileEditorInput
// object from the FileStoreEditorInput object.
FileStoreEditorInput fsEditorInput = (FileStoreEditorInput)input;

// Get the path to the file.
IPath location = new Path(fsEditorInput.getURI().getPath());

// Create a new temporary project object and open it.
project = ResourcesPlugin.getWorkspace().getRoot()
.getProject("TempFolderiiiiiiiii");

IFile file = null;
try
{
// Create a project if one doesn't exist and open it.
if (!project.exists())
project.create(null);
if (!project.isOpen())
project.open(null);

// Create a link to the file.
file = project.getFile(location.lastSegment());
file.createLink(location, IResource.REPLACE, null);

//FileEditorInput newFileEditorInput = new FileEditorInput(file);
input = new FileEditorInput(file);
}
catch (Exception e)
{
}
}

// TODO Auto-generated method stub
super.init(site, input);
}
Previous Topic:Hooking into ltk refactoring lifecycle and own PreviewPage
Next Topic:Why are properties translated in one project but not the other?
Goto Forum:
  


Current Time: Tue Mar 19 02:58:54 GMT 2024

Powered by FUDForum. Page generated in 0.03144 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top