I created my own eclipse view and is trying to add a link in a popup menu. I am able to add the link but it's grayed out. I wonder how I can activate the link. I just want to be able to click on the link and trigger run(). DeleteAction is the class i want to trigger. SegmentReferencesView is the view I created. Would be very thankful for help.
This is from the plugin.xml:
<extension point="org.eclipse.ui.popupMenus">
<viewerContribution
id="se.test.views.categories.segmentreferences.ui.views"
targetID="se.test.views.categories.segmentreferences.ui.views.SegmentReferencesView">
<action
class="se.test.views.categories.segmentreferences.ui.views.DeleteAction"
enablesFor="1"
icon="icons/Delete.gif"
id="se.test.views.categories.segmentreferences.ui.views.DeleteReferenceAction"
label="Do action"
menubarPath="additions-ext">
</action>
</viewerContribution>
This is the Java class:
public class DeleteAction implements IViewActionDelegate {
#Override
public void init(org.eclipse.ui.IViewPart view) {
super.init(view);
};
#Override
public void run(IAction action) {
}
}
Your view must set the view site 'Selection Provider'. This is used by the menu system to find out what is selected. If you are using a TableViewer or TreeViewer you can just do:
getSite().setSelectionProvider(viewer);
in the view code immediately after you have created viewer (which should be the TableViewer or TreeViewer).
I managed to get the link to work by extending org.eclipse.core.commands.AbstractHandler in the DeleteAction class. I don't know if this is the best way to do it but it's working for now.
public class DeleteAction extends AbstractHandler implements IViewActionDelegate {
#Override
public void init(org.eclipse.ui.IViewPart view) {
// Not used
}
#Override
public void run(IAction action) {
System.out.println("run"); //$NON-NLS-1$
}
#Override
public void selectionChanged(IAction action, ISelection selection) {
// Not used
}
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
return null;
}
}
Related
I have been following Android sample provided with the documentation to design new (relatively simple) keyboard. At this point I am stuck with configuration change (like theme, textSize or bit complex stuff) which requires IME to recreate itself.
public class ImePreferences extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme_WithActionBar);
super.onCreate(savedInstanceState);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// reload ime
Log.d("anbani", "Stopping service " + String.valueOf(stopService(new Intent(this, SoftKeyboard.class))));
Log.d("anbani", "prefs changed");
}
...
}
This does not help. StopService value is returned true but there is no effect.
Is there some trick to get an instance of package keyboard without prior reference to it? Or am missing something simple here?
Any tips would be highly appreciated!
Solution:
#Override public View onCreateInputView() {
// load preferences
return mInputView;
}
#Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
super.onStartInputView(attribute, restarting);
setInputView(onCreateInputView());
...
}
I would like to trigger method when ViewPart is being hidden (hidden == it starts to be invisible) and it is being opened (opened == it starts to be visible). Something like:
void onHide(){removeAllListeners();}
void onShow(){refreshView();}
I tried to use:
getSite().getPage().addSelectionListener(new ISelectionListener() {
#Override
public void selectionChanged(final IWorkbenchPart workbenchPart, final ISelection selection) {
System.out.println("TEST");
}});
but it works only when ViewPart become invisible and it is not triggered when become visible
This has nothing to do with selection being changed. Use an IPartListener2.
You can use IPartListener2 (or the older IPartListener) to listen to changes to the state of all parts.
getSite().getPage().addPartListener(new IPartListener2() ....
You probably want to react to the 'partHidden' and 'partVisible' methods. In these methods you will need to check that the event is for your part:
IViewPart myViewPart = ... your view part
#Override
public void partHidden(IWorkbenchPartReference ref) {
IWorkbenchPart part = ref.getPart(false);
if (part == myViewPart) {
// Event is for your view
}
}
#Override
public void partVisible(IWorkbenchPartReference ref) {
IWorkbenchPart part = ref.getPart(false);
if (part == myViewPart) {
// Event is for your view
}
}
Greetings fellow Stackoverflowians,
I am developing an Eclipse RCP application, and must add a SelectionListener to the Project Explorer view the moment after it's created.
I've realized that I cannot do this in the Activator of my contributing plug-in, whereas in order to get the SelectionService via PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService() I must have an active workbench window (which is null when the Activator start() is called)
So my question:
When can I get the SelectionService so that the Project Explorer view has been created and is visible, but the user has not yet been able to 'push any buttons'?
Any opinions and suggestions are appreciated!
When you really want to track user selection from startup without having a UI (like a view) that can register an ISelectionListener on creation, you can us a startup hook.
Eclipse provides the extension point org.eclipse.ui.startup. It accepts a class that implements the interface org.eclipse.ui.IStartup. It will be called after the UI has been created, so the ISelectionService is already available then:
public class StartupHook implements IStartup, ISelectionListener {
#Override
public void earlyStartup() {
final IWorkbench workbench = PlatformUI.getWorkbench();
workbench.addWindowListener(new IWindowListener() {
#Override
public void windowOpened(IWorkbenchWindow window) {
addSelectionListener(window);
}
#Override
public void windowClosed(IWorkbenchWindow window) {
removeSelectionListener(window);
}
/* ... */
});
workbench.getDisplay().asyncExec(new Runnable() {
public void run() {
for (IWorkbenchWindow window : workbench.getWorkbenchWindows()) {
addSelectionListener(window);
}
}
});
}
private void addSelectionListener(IWorkbenchWindow window) {
if (window != null) {
window.getSelectionService().addSelectionListener("org.eclipse.ui.navigator.ProjectExplorer", this);
}
}
private void removeSelectionListener(IWorkbenchWindow window) {
if (window != null) {
window.getSelectionService().removeSelectionListener("org.eclipse.ui.navigator.ProjectExplorer", this);
}
}
#Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
// TODO handle selection changes
System.out.println("selection changed");
}
}
Note that it is discouraged to use this UI startup hook as it forces OSGi to activate your bundle very early (and so all dependent bundles too!) and slows down system startup. So please make sure that your bundle is neat and slim. Reduce bundle dependencies to a minimum. Sometimes it is necessary move the startup hook code into a separate bundle to achieve that.
You could add your selection listener in the ApplicationWorkbenchWindowAdvisor.postWindowOpen() method (at this point you can be sure workbench is already created). If you want to add it, when 'Project explorer view' gets visible, then it is possible to do something like this:
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().addPartListener(new PartAdapter() {
#Override
public void partVisible(IWorkbenchPartReference partRef) {
if ("org.eclipse.ui.navigator.ProjectExplorer".equals(partRef.getId())) {
// add selection listener
}
}
});
UPD: OK, if you don't have access to AppliationWorkbenchWindowAdvisor (which is weird, if you are developing Eclipse RCP product), then ASAIK, there is no clean solution to get notified about availability of the UI. So one of such solutions could be adding a job, which would wait for the UI to be initializd. In your plugin Activator.start() method consider adding following job (sure you can extract it to separate class and improve in many ways, but for the beginning it should be sufficient):
Job j = new Job("") {
#Override
protected IStatus run(IProgressMonitor monitor) {
final boolean[] workbenchAvailable = new boolean[] { false };
Display.getDefault().syncExec(new Runnable() {
#Override
public void run() {
if (PlatformUI.isWorkbenchRunning() && PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) {
workbenchAvailable[0] = true;
}
}
});
if (workbenchAvailable[0]) {
System.out.println("Workbench is available");
Display.getDefault().asyncExec(new Runnable() {
#Override
public void run() {
ISelectionService selectionService =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
System.out.println(selectionService);
}
});
} else {
System.out.println("Waiting for the Workbench ...");
schedule(1000);
}
return Status.OK_STATUS;
}
};
j.schedule();
Context
I'm building a plugin for eclipse 3.4 and more.
I have a view with id mrp.view with a menuContribution set to toolbar:mrp.view.
This menuContribution has some command, and I have this one:
<handler
class="mrp.handlers.export"
commandId="mrp.commands.export">
</handler>
<command
commandId="mrp.commands.export"
label="My command"
style="push">
</command>
My handler, mrp.handlers.export has a dynamic ìsEnabled()` method, looking like that :
#Override
public boolean isEnabled() {
return !getMySelection().isEmpty();
}
Question
How can I refresh the button on my toolbar when data changed ?
(refresh is done automatically if I click anothr button of the toolbar, but if I don't...)
I tried..
ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
service.refreshElements("mrp.commands.export", null);
But it doesn't seems to do anything.
Also this one:
public class Export extends AbstractHandler implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
setBaseEnabled(!getSelection().isEmpty());
}
// ....
}
It is called, but the icon on my view's menu is not refreshed (on eclipse 3.7).
Did I do something wrong ?
Your handler must fire an event when it's enablement changes. If you have the change update your handler using org.eclipse.core.commands.AbstractHandler.setBaseEnabled(boolean) it will fire the required event.
Thanls to Paul Webster's answer, I got it working.
public class Export extends AbstractHandler implements PropertyChangeListener {
public Export() {
Activator.getDefault().AddListener(this);
setBaseEnabled(!getMySelection().isEmpty());
}
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// My handler
return null;
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(Activator.EVENT_SELECTION_CHANGED)) {
boolean before = isEnabled();
boolean after = !getMySelection().isEmpty();
if (after != before) {
setBaseEnabled(after);
}
}
}
}
I am using celltable , GWT2.3.0
How to determine which button is clicked by user ?
Is there any way to find out NEXT,PREV,LAST & FIRST button clicked ?
Any help or guidance in this matter would be appreciated
Well the easist way I see right now, is write your own Simple pager and overwrite the "lastPage", "lastPageStart", "nextPage" and "previousPage" functions.
In my test example, which I build in ~5min it lookes like this:
public class Ui_mySimplePager extends SimplePager {
public Ui_mySimplePager(TextLocation center, Resources pagerResources,
boolean b, int i, boolean c) {
super(center, pagerResources, b, i, c);
}
#Override
public void lastPage() {
Window.alert("lastPage");
super.lastPage();
}
#Override
public void lastPageStart() {
Window.alert("lastPageStart");
super.lastPageStart();
}
#Override
public void nextPage() {
Window.alert("nextPage");
super.nextPage();
}
#Override
public void previousPage() {
Window.alert("previousPage");
super.previousPage();
}
}