Eclipse Plugin Open Preferences Page From Menu Item - eclipse

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.

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

How to get the menu name of an IAction?

I've defined a menu and an action in plugin.xml.
When
IAction.run(IAction action){}
is called can I retrieve the name of the menu of this action?
The reason: I've a lot of actions which trigger different wizards. I have to use the menu names as wizard titles. So I thought if I trigger the wizards from the run() method I can retrieve the menu names and set the wizard titles. Otherwise I'll have to copy all the menu names from plugin.properties to messages.properties.
Call the getText method of the action parameter:
public void run(IAction action)
{
String text = action.getText();
... more
}

Eclipse RCP: how to get Show View menu instead of a dialog

I've added to my perspective's org.eclipse.ui.menus
<command
commandId="org.eclipse.ui.views.showView"
style="pulldown">
</command>
This adds Show View item to main menu, but this item is not a menu (as in the Eclipse Window menu). Instead pressing it shows a dialog where I can select a view. How do I get a menu instead?
You have to create ContributionItem class like below:
public class MyShowViewContributionItem extends org.eclipse.ui.internal.ShowViewMenu {
public MyShowViewContributionItem() {
this("om.myplugin.myShowViewId");
}
public MyShowViewContributionItem(String id) {
super(org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow(), id);
}
}
then in your plugin.xml org.eclipse.ui.menus extension:
<menu
label="My Show View">
<dynamic
class="com.myplugin.MyShowViewContributionItem"
id="com.myplugin.myShowViewId">
</dynamic>
</menu>
Cheers,
Max
Just to share on my recent experiment in trying to do the same thing, what Max suggested in his answer will work but leaves you using internal code (resulting in a 'Discouraged Access' warning).
Another approach is to build the menu through your applications action bar advisor. Although, this approach will leave you to having to write code (oppose to use providing menu contributions in the plugin XML definition). Consider the following example:
public class ApplicationActionBarAdvisor extends ActionBarAdvisor
{
private IContributionItem contributionOpenPerspective;
private IContributionItem contributionShowView;
...
protected void makeActions(IWorkbenchWindow window)
{
...
contributionOpenPerspective = ContributionItemFactory.
PERSPECTIVES_SHORTLIST.create(window);
contributionShowView = ContributionItemFactory.
VIEWS_SHORTLIST.create(window);
...
}
protected void fillMenuBar(IMenuManager menuBar)
{
...
MenuManager windowMenu = new MenuManager("&Window",
IWorkbenchActionConstants.M_WINDOW);
menuBar.add(windowMenu);
MenuManager openPerspectiveMenu = new MenuManager("&Open Perspective");
openPerspectiveMenu.add(perspectivesContribution);
windowMenu.add(openPerspectiveMenu);
MenuManager showViewMenu = new MenuManager("Show &View");
showViewMenu.add(viewsContribution);
windowMenu.add(showViewMenu);
...
}
}
A possible downside to this approach is with the interaction between menus created in the advisor and menus created by menu contributions. Since advisor menu items are created before menu contributions, you are left to deal with adding more sorting logic in your menu contributions. This might be fine for most people, however, you lose the 'feel' of a centralized menu structure from org.eclipse.ui.menus (even if the feeling is an illusion when other plugins come into play with their own menu contributions).
I've also included the building of a perspective menu as well; completely option, but I added it if anyone was attempting to perform the same menu building with perspectives.

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

Howto hide a preference page in an eclipse RCP

I have an eclipse rcp and want to hide the security and help prerence pages. How can I do that?
I was looking for the same thing and found the solution in this link:
http://sourceforge.net/apps/trac/fable/wiki/Preferences
Cheers.
Stefan
Disable help preferences ¶
Put the following code into your subclass of org.eclipse.ui.application.WorkbenchAdvisor, and it removes the "Help" group from RCP preference dialog:
public void postStartup() {
PreferenceManager pm = PlatformUI.getWorkbench().getPreferenceManager( );
pm.remove( "org.eclipse.help.ui.browsersPreferencePage" );
}
"org.eclipse.help.ui.browsersPreferencePage" is the ID for the preferences extension point.
Add Perspective preferences ¶
Remark : to find plugin id preferences, select Window-->show view--> PDE Runtime--> Plugin Registry ..... and try to find what you are looking for .....
For example, for "Workbench preferences", have a look in fable.eclipse.ui.ide and extension org.eclipse.ui.preferencePages: id="org.eclipse.ui.preferencePages.Workbench"
If you want to add only perspective (for example) preferences, add a preference extension in MANIFEST.XML :
id : org.eclipse.ui.preferencePages.Perspectives
name:perspective(fable)
class:org.eclipse.ui.internal.ide.dialogs.IDEPerspectivesPreferencePage
//Add : org.eclipse.ui.ide in your Dependencies
In ApplicationWorkBenchAdvisor :
public void postStartup() {
PreferenceManager pm = PlatformUI.getWorkbench().getPreferenceManager( );
pm.remove( ""org.eclipse.ui.preferencePages.Workbench"browsersPreferencePage" );
}
public String getInitialWindowPerspectiveId() {
IPreferenceStore pref = Activator.getDefault().getPreferenceStore();
String ret = pref.getDefaultString(IWorkbenchPreferenceConstants.DEFAULT_PERSPECTIVE_ID);
ret=(ret==null || ret.equals(""))?"yourDefaultPerspectiveID":ret;
return ret;
}//
According to this entry, you could use the "workbench activities" mechanism, and:
define separate activities corresponding to the different access levels
define your actions in regular action sets, grouped according to access level
associate each activity with the appropriate action sets via
activityPatternBinding elements
set the enabled activity ids after authentication, early in the workbench
lifecycle, e.g. from your WorkbenchAdvisor's preStartup() method.
(Note, the above was for a filtering based on user's permissions, but it could be generalize to other criteria.)
Regarding the preference pages for the storage and help, you should bind the id of those pages with an activity you know you can disable:
<activityPatternBinding
activityId="org.eclipse.javaDevelopment"
pattern="org\.eclipse\.help\..*/.*">
</activityPatternBinding>
would disable all menu/preferences/views related to help.
If you use org.eclipse.help.ui.PrefPageHelp\..*, it would only bind prefPageHelp and prefPageHelpContent.
If you add another activity binding extension with
org.eclipse.equinox.security.ui.sec_storage_preferences_context, that would also take care of the Secure Storage preference page.