Eclipse EMF adding structured text editor page is it possible? - eclipse

have a Eclipse EMF editor which extends MultiPageEditorPart. This editor class is generated via Eclipse EMF. I did not add new codes or anything to this generated editor.
I want to add Structured Text editor page for .xml files.
I tried to add the following code into createPages() function:
try {
StructuredTextEditor sourceEditor = new StructuredTextEditor();
int index = addPage(sourceEditor,getEditorInput());
setPageText(index, "Source");
} catch (PartInitException e) {
e.printStackTrace();
}
But ctrl + z and ctrl + c don't work when i try this.

You would need to set the UNDO/REDO/etc. global action handlers to the corresponding actions in the sourceEditor instance when your editor part is activated, just like in org.eclipse.ui.texteditor.BasicTextEditorActionContributor#doSetActiveEditor(IEditorPart), otherwise the key mapping won't know what's supposed to be run when your editor is active.

Related

SWT FileDialog Browse Location

I have a simple file dialog in my RCP application which lets the users to select a file as per the code snippet below
Label filePathLabel = new Label(composite, SWT.NULL);
filePathLabel.setText("File Path");
Text filePathText = new Text(composite, SWT.BORDER);
filePathText.setText("");
Button browseButton = new Button(composite, SWT.PUSH);
FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE);
fileDialog.setFilterExtensions(new String[] {"*.txt"});
fileDialog.setFilterNames(new String[] {"Textfiles(*.txt)"});
browseButton.addSelectionListener(new SelectionAdapter()
{
#override
public void widgetSelected(final SelectionEvent e)
{
String path = fileDialog.open();
if(path != null && !path.isEmpty())
{
filePathText.setText(path);
}
}
});
The problem I'm facing is that I have not been able to get the previous browse location of the file after I close my RCP application and start it again since all the controls (Text, FileDialog) will be recreated. I save the result of fileDialog.open which returns the path and set the filePathText Text control's setText(Text text) method whenever my WizardPage is reopened to show the previous browse location selected but I loose access to the browse location after I close my RCP application so the next time I reopen my application I have not been able to set the filePathText text to the previously browsed location even though Eclipse does point to the previously browsed location after I click the browse button but I need to know the previously browsed location even before I click browse button so that it can be displayed in the Text control.
I found some suggestions on this site - https://dzone.com/articles/remember-state but I don't think it would help me in remembering the state of the browse location with respect to FileDialog
Please correct me if I'm missing something here.
You use the IDialogSettings mentioned in the link to save and restore information for a wizard. Wizards provide some methods to help.
In the constructor of your main Wizard class set the dialog settings the Wizard should use. This might be:
public MyWizard()
{
setDialogSettings(Activator.getDefault().getDialogSettings());
}
where Activator is the activator for your plug-in (this only works if the activator extends AbstractUIPlugin).
Once you have done this your WizardPage can access the settings:
IDialogSettings settings = getDialogSettings()
When the File Dialog returns the location you can save that in the settings:
settings.put("path", path);
When you are creating the file path Text you can check if you have a saved value:
String savedPath = settings.get("path");
if (savedPath != null) {
filePathText.setText(savedPath);
}

Eclipse RCP: New Editor Wizard

I have a small experience with Eclipse RCP - 3.X and I created my own editor via org.eclipse.ui.editors extension point and in order to have multiple instance of that editor I have implemented a new editor wizard as you can see below;
IFile file = page1.createNewFile();
IWorkbenchWindow window = _workbench.getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
try {
IDE.openEditor(page, file, SimpleEditor.ID, true);
} catch (PartInitException e) {
e.printStackTrace();
}
My question, the only way I found is that, create a new file and associate that file with your unique Editor ID. What I want is, I would like parse some initial values -that defined by user- to editor from the wizard. But we really don't instantiate the EditorPart class in the process.
How can I achieve that?
The IDE.openEditor call returns the IEditorPart that was opened - this will be an instance of your editor class so you can do:
IEditorPart part = IDE.openEditor(page, file, SimpleEditor.ID, true);
if (part instanceof SimpleEditor) {
SimpleEditor editor = (SimpleEditor)part;
// TODO call methods you define in the editor to set the parameters
}
Alternatively you can use a custom IEditorInput and call the IDE
openEditor(IWorkbenchPage page,
IEditorInput input, String editorId)
method. The init method of the editor is given the IEditorInput you specify.

Attaching Properties view to a custom XML Editor

I have created a custom editor in eclipse plugin which shows a XML in Tree-Table(TreeViewer) format with couple of its attributes. For showing remaining attributes I am trying to tie it up with "Properties View", but not really able to make progress on it.
I went through similar question on SO like
How to handle property sheet from customized editor in eclipse plugin development? where it talk about make your viewer contribute to workbench selection and implementing an IPropertySource on object which is selected in editor.
In my case I am directly setting an document object in treeviewer input like below.
IFileEditorInput editorInput = (IFileEditorInput) getEditorInput();
IFile inputIFile = editorInput.getFile();
File f = new File(inputIFile.getLocation().toString());
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(f);
}
catch (SAXException | IOException | ParserConfigurationException e) {
e.printStackTrace();
}
//setting root element of doc as input
treeViewer.setInput(doc.getDocumentElement());
Now on what object should I implement an IPropertySource interface to contribute properties?
Let me know if I am going in right direction or missing something or doing it completely wrong.
Hope this make sense !!
When your selection provider fires a selection changed event the properties page will look at the new selection. If you are using a tree viewer as the provider the selection will be the current object from your tree content provider.
The properties view will try to get an IPropertySourceProvider from the selection. Your object can implement IPropertySourceProvider directly, or provide it view the IAdaptable interface or by using IAdapterFactory.
Once the view has the IPropertySourceProvier it will call the getPropertySource method. Your code must return an IPropertySource object - it is up to you to write this class.

Eclipse RCP: programmatically associate file type with Editor?

How programmatically associate file type with Editor?
That is what Eclipse-RCP Java code can do what is archived with the following UI interaction:
Window -> Preferences
General -> Editors -> File Associations
Add... > File type: *.json
Select *.json file type
Add... (Associated editors) > JavaScript Editor
Make it default
Ralated to Q
https://stackoverflow.com/questions/12429221/eclipse-file-associations-determine-which-editor-in-list-of-associated-editors
Eclipse: associate an editor with a content type
Get associated file extensions for an Eclipse editor
Opening a default editor, on a treeviewer selection eclipse rcp(eg: as eclipse knows that j.java files must be opened in text editor)
eclipse rcp change icon for xml configuration file
I know your questions says "programmatically" but I'll give a complete run down of the methods.
If you are writing the plugin that provides the editor, then you should simply declare the extension in your plugin.xml.
<extension
point="org.eclipse.ui.editors">
<editor
...
extensions="json"
...
If you are distributing a complete RPC application, you can edit the plugin.xml for the plugin that provides the editor or add a plugin that just refers to that editor.
But, if you have to do it programmatically, you are manipulating the internals of an RPC instance. Eclipse does not provide a public API for that but this code will do it:
// error handling is omitted for brevity
String extension = "json";
String editorId = "theplugin.editors.TheEditor";
EditorRegistry editorReg = (EditorRegistry)PlatformUI.getWorkbench().getEditorRegistry();
EditorDescriptor editor = (EditorDescriptor) editorReg.findEditor(editorId);
FileEditorMapping mapping = new FileEditorMapping(extension);
mapping.addEditor(editor);
mapping.setDefaultEditor(editor);
IFileEditorMapping[] mappings = editorReg.getFileEditorMappings();
FileEditorMapping[] newMappings = new FileEditorMapping[mappings.length+1];
for (int i = 0; i < mappings.length; i++) {
newMappings[i] = (FileEditorMapping) mappings[i];
}
newMappings[mappings.length] = mapping;
editorReg.setFileEditorMappings(newMappings);
Associating file type means associating content of ur editor with a predefined one.
This can be easily achieved via plugin.xml..
Just follow the following link:-
Eclipse help Documentation
http://help.eclipse.org/indigo/index.jsp
and search for org.eclipse.core.contenttype.contentTypes.

Eclipse: Perform an action any time an editor receives input

I'm wondering if it is possible to determine what input was just entered inside of an editor in Eclipse - I'm currently working off of the example JDT editor - and then perform an action based on that input.
e.g.: I have a file example.jav open in my editor window. I push the 'a' key. 'a' would appear in the editor window per normal, but 'a' would also print out to the console.
Obviously the operation I'll be performing will be more complicated than a System.out.println() statement, but if someone could help show me where the change gets detected by the editor itself, I can take it from there.
A few notes:
I'm working in Eclipse 3.7.2 with Java 1.7
If you cannot find the JDT example editor, go to Help > Welcome > Samples and click on "Java Editor".
Thanks in advance!
Figured it out!
As the Editor API is so vast in eclipse that it is difficult to know where to start, I focused on adding a KeyListener to my Shell. Turns out that is slightly problematic in SWT, as when an item inside the Shell gains focus, the Shell itself looses focus. After a bit of searching though, I stumbled across someone else who had the same problem. By adding a filter to the Shell's display, you can add a Listener object which works for the entire application. Such as:
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
Shell shell = window.getShell();
shell.getDisplay().addFilter(SWT.KeyDown, new Listener()
{
public void handleEvent(Event event)
{
System.out.println("" + event.character);
}
});
To further this and only worry about keys pressed in a specific non-widget part (otherwise you could just add a KeyListener to that part) you can add a check to make sure that the currently active part is the same as whatever part you wish to perform the actions for by using a simple if check.
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
Shell shell = window.getShell();
shell.getDisplay().addFilter(SWT.KeyDown, new Listener()
{
public void handleEvent(Event event)
{
IWorkbenchPage page = window.getActivePage();
IWorkbenchPart part = page.getActivePart();
IEditorPart editor = page.getActiveEditor();
if(part.equals(editor))
{
System.out.println("" + event.character);
}
}
});
Here is hoping that this helps someone else have an easier time than I had finding the solution!