Home » Eclipse Projects » Plugin Development Environment (PDE) » Basic Editor Question
Basic Editor Question [message #519946] |
Wed, 10 March 2010 15:14 |
Darin 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 #524360 is a reply to message #520341] |
Wed, 31 March 2010 16:10 |
Darin 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 |
Darin 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 #605400 is a reply to message #605028] |
Wed, 31 March 2010 16:10 |
Darin 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 |
Darin 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);
}
|
|
|
Goto Forum:
Current Time: Tue Mar 19 02:58:54 GMT 2024
Powered by FUDForum. Page generated in 0.03144 seconds
|