Eclipse RCP: New Editor Wizard - eclipse

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.

Related

Eclipse EMF adding structured text editor page is it possible?

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.

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);
}

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.

close Eclipse editor if file is open twice

I´m creating my own editor here for Eclipse and found a problem.
If I open a file and it´s already open, Eclipse opens a new editor.
So, I need to either avoid this or close the editor right after it´s opened.
My editor class is a child of MultiPageEditorPart and it has 2 tabs: first one is a Java editor and second one is a text editor. Java editor opens up a .java file and text editor opens up my own file.
I saw some posts saying about how to fix this, but I don´t implement IEditorInput interface anywhere here.
Can anyone help me with this?
Thanks a lot
Here´s my editor definition:
<extension point="org.eclipse.ui.editors">
<editor id="br.com.senior.wb.asas.editor.AsasEditor"
class="br.com.senior.wb.asas.editor.AsasEditor"
contributorClass="br.com.senior.wb.asas.editor.AsasEditorContributor"
extensions="java, afm"
icon="icons/editor_asas.png" name="Editor ASAS">
</editor>
</extension>
If you mean you want to open one editor when either the java or afm file is opened when the other file is already open then you need to use the matchingStrategy attribute of the editor definition to define an editor matching strategy.
Something like:
<extension point="org.eclipse.ui.editors">
<editor id="br.com.senior.wb.asas.editor.AsasEditor"
class="br.com.senior.wb.asas.editor.AsasEditor"
contributorClass="br.com.senior.wb.asas.editor.AsasEditorContributor"
matchingStrategy="br.com.senior.wb.asas.editor.AsasEditorMatchingStrategy"
extensions="java, afm"
icon="icons/editor_asas.png" name="Editor ASAS">
</editor>
</extension>
public class AsasEditorMatchingStrategy implements IEditorMatchingStrategy
{
public boolean matches(IEditorReference editorRef, IEditorInput input)
{
if (!(input instanceof IFileEditorInput))
return false;
IFile inputFile = (IFile)input.getAdapter(IFile.class);
if (inputFile == null)
return false;
IFile currInputFile = (IFile)editorRef.getEditorInput().getAdapter(IFile.class);
if (currInputFile == null)
return false;
if (!inputFile.getProject().equals(currInputFile.getProject()))
return false;
// TODO add more checks that 'inputFile' and 'currInputFile' are a matching pair of files
}

how to call contexts.xml file in eclipse plugin

I have created an eclipse plug-in with a view project. I have a contexts.xml file and i have configured it. Please refer the following code.
<contexts>
<context id="Help" title="Plug-in Help">
<description>context help for the sample view</description>
<topic href="resources/text.html" label="Context-sensitive help">
</topic>
</context>
</contexts>
I have an html file named "text" under resources folder inside the plugin project.
//Listener to invoke the help method of RepositoryAccessor class
bHelp.addMouseListener(new MouseAdapter() {
#Override
public void mouseDown(MouseEvent arg0){
Display display=PlatformUI.getWorkbench().getDisplay();
Shell shell = new Shell(display);
GridLayout grid11=new GridLayout(3,true);
//Layout of controls inside the plugin view
shell.setLayout(grid11);
Text text = new Text(shell, SWT.NONE);
PlatformUI.getWorkbench().getHelpSystem().setHelp(text,"help");
PlatformUI.getWorkbench().getHelpSystem().displayHelp("help");
}
}
bHelp is a button. Once i run the eclipse plugin and click the bHelp button, i get a new shell window and i see only empty label.
Please suggest a method to assign the html contents to the label created in the popup window(new shell).
At Step 1, i click Help Icon and a new shell is open. In Step 2, the label is still showing "sfsf" instead of the contents in "text.html".
You have to use the org.eclipse.help.contexts extension point to tell Eclipse about your contexts.xml:
<extension point="org.eclipse.help.contexts">
<contexts file="contexts.xml"/>
</extension>
Also the setHelp call only registers the help for the control, it does not display the help. If you want to display a help context id use:
PlatformUI.getWorkbench().getHelpSystem().displayHelp("your context id");
Note that the help system always opens its own view to display the help (or expands the dialog if you use TrayDialog or one of its subclasses).
So if you have a Button you would invoke the help with:
button.addSelectionListener(new SelectionAdapter()
{
#Override
public void widgetSelected(SelectionEvent e)
{
PlatformUI.getWorkbench().getHelpSystem().displayHelp("your context id");
}
});