SWT FileDialog Browse Location - eclipse

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

Related

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.

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

Eclipse Plugin Open Preferences Page From Menu Item

I have a
public class PrefMenu extends FieldEditorPreferencePage implements IWorkbenchPreferencePage
implementing the init() and createFieldMethods(). And I do see it in Window>Preferences>PREFMENU_NAME after adding the class to the preferencePage extension.
But how do I open the preference page from a menu-item? I created a command and a handler and the execute()-Method (which works with other commands) does...
//other commands
} else if (commandID.equals(PREFERENCES_COMMAND_ID)){
final PrefMenu prefMenu = new PrefMenu();
prefMenu.init(PlatformUI.getWorkbench());
}
Yet nothing happens when I click on the menu-Item. In the debug mode I see it simply executes the init()-Method and returns. But I want it to open up the Preferences-Window and only close it when I click on OK or Cancel.
Use org.eclipse.ui.dialogs.PreferencesUtil to do this:
String id = ... your preference page id
Shell shell = ... parent shell to use ...
PreferencesUtil.createPreferenceDialogOn(shell, id, new String[] {id}, null).open()
This will open just your preference page in the preferences dialog. You can adjust the string array to include other pages or specify null for the array to show all preference pages.

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!

How to run a class anytime a editor page receive focus on Eclipse?

Is there a way to run a class everytime editor page receive focus, something like prompt message when a class source has changed outside eclipse? Can a plug-in editor or extension do this work?
The FAQ "How do I find out what view or editor is selected?" can help you call your class when the Editor is active (which is when you can test if it has focus as well), by using a IPartService:
Two types of listeners can be added to the part service:
IPartListener
and the poorly named IPartListener2.
You should always use this second one as it can handle part-change events on parts that have not yet been created because they are hidden in a stack behind another part.
This listener will also tell you when a part is made visible or hidden or when an editor's input is changed:
IWorkbenchPage page = ...;
//the active part
IWorkbenchPart active = page.getActivePart();
//adding a listener
IPartListener2 pl = new IPartListener2() {
public void partActivated(IWorkbenchPartReference ref)
System.out.println("Active: "+ref.getTitle());
}
... other listener methods ...
};
page.addPartListener(pl);
Note: IWorkbenchPage implements IPartService directly.
You can also access an activation service by using IWorkbenchWindow.getPartService().
I am click Toolbar or Button to get focus which view or editor current working on RCP eclipse
//class:Current_Workbech extends AbstractHandler to execute() method
public class Current_Workbech extends AbstractHandler{
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IPartService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService();
//MessageDialog box open to get title which view or editor focus and current working
MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(
event).getShell(), "Current Workbench Window", service.getActivePart().getTitle()+"");
return null;
}
}