In the eclipse toolbar when you mouse over the build button it comes up with a tooltip
saying which projects are currently selected to be build. Is there any way to get that information in my eclipse plugin? I thought about implementing something with ISelectionListener to keep tracking which projects are selected but there is no reason to do that if I can get them from the same place the build button is taking them.
Ok the answer to this question was to create my own ISelectionListener.
#Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
Object[] allselections= null;
selectedProjects.clear();
// TODO Auto-generated method stub
if(selection instanceof IStructuredSelection) {
allselections = ((IStructuredSelection)selection).toArray();
for(Object element: allselections){
if (element instanceof IResource) {
selectedProjects.add(((IResource)element).getProject());
} else if (element instanceof IAdaptable) {
IResource resource = null;
IAdaptable a = (IAdaptable)element;
resource = (IResource)a.getAdapter(IResource.class);
selectedProjects.add(resource.getProject());
}
}
}
};
Related
at first I checked the other topics about the IStructuredSelection here on StackOverflow but they doesn´t solve my problem.
So I have 2 Views. In both Views I have a TreeViewer and implemented getSite().setSelectionProvider(viewer);
In the MainView everything is working correctly, when I click on a TreeNode (NOTE: These TreeNode's are from a custom class, lets say myTreeNode) the PropertiesView gets updated and my popup menu with different handlers calls the correct function.
One of these functions create a new TreeItem in the other View, which should display some values in different columns. These values get updated every second. This part is also working correctly.
But now the problem occur. When I click on one of the TreeItem's, the PropertiesView does not get updated and the popup menu handler is also not working correctly. The IStructuredSelection strucSelection = (IStructuredSelection) selection; always returns null.
Here is my getAdapter method from my adapter for the PropertiesView:
#Override
public Object getAdapter(Object adaptableObject, Class adapterType)
{
if(adapterType == IPropertySource.class && adaptableObject instanceof UATreeNode)
return new UATreeNodeAdapter((UATreeNode) adaptableObject);
else if(adapterType == IPropertySource.class && adaptableObject instanceof TreeItem)
{
MainView.printOnOPCUAConsole(OPCUAConsoleMessageTypeEnum.NULL, "Instanceof TreeItem");
return new TreeItemAdapter((TreeItem) adaptableObject);
}
else if(adapterType == IPropertySheetPage.class)
{
if(propertyPage == null)
propertyPage = new PropertySheetPage();
return propertyPage;
}
return null;
}
And here is my Handler, which should be called, when the menu in the second view is opend and a command is executed:
#Override
public Object execute(ExecutionEvent event) throws ExecutionException
{
ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getSelectionService().getSelection();
if(selection instanceof IStructuredSelection)
{
IStructuredSelection strucSelection = (IStructuredSelection) selection;
if(strucSelection.getFirstElement() != null)
{
TreeItem treeItem = (TreeItem) strucSelection.getFirstElement();
UaSubscription uaSubscription = (UaSubscription) treeItem.getData("uaSubscription");
}
}
return null;
}
In both cases, the selection is null but I can see that something is selected, because otherwise I think the popup menu would not be displayed.
So my question now, does anybody know how to get the selection from my second View ? And what am I doing wrong?
Ok, I solved this problem with implementing my own elements and provide them via ContentProvider to the TreeViewer.
Then I added a new adaptableType(the new created class) for the adapter-extension in the plugin.xml and added the new Class to the adapter-factory.
I built a treeviewer for a specific project, but now I need to select a specific item/node in this treeviewer.
To build the treeviewer, I did this:
viewer = new TreeViewer(composite);
viewer.getTree().setLayoutData(gridData);
viewer.setContentProvider(new FileTreeContentProvider());
viewer.setLabelProvider(new FileTreeLabelProvider());
viewer.setInput(ResourcesPlugin.getWorkspace().getRoot().getProject(folderName.getText()));
viewer.expandAll();
Until here, everything is ok, but now, I don't know how to use listeners to do something when I select a specific item in my tree. Any idea? Thanks.
Edit: I got it!
viewer.addSelectionChangedListener(
new ISelectionChangedListener(){
public void selectionChanged(SelectionChangedEvent event) {
if(event.getSelection() instanceof IStructuredSelection) {
IStructuredSelection selection = (IStructuredSelection)event.getSelection();
Object o = selection.getFirstElement();
if (o instanceof IFile){
IFile file = (IFile)o;
}else {
//what ?
}
}
}
}
);
This is an excellent first step but there is even a better way which is more in the heart and soul of Eclipse.
Your code is listening to local changes but you want to make your code extendable so that other plugins in Eclipse are also notified when someone selects something in your viewer.
Eclipse 4 API
For this to happen you inject ESelectionService into your part and then forward the selection to the workbench by using the listener you already provided.
#Inject
private ESelectionService selectionService;
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
// set the selection to the service
selectionService.setSelection(
selection.size() == 1 ? selection.getFirstElement() : selection.toArray());
Then, to catch your own selection:
#Inject
void setSelection(#Optional #Named(IServiceConstants.ACTIVE_SELECTION) IFile pFile) {
if (pFile == null) {
//what ?
} else {
// magic!
}
}
Eclipse 3 API
For this to happen you have to register your viewer with the selection framework. Add this in the createPartControl method of the part where you have added your viewer:
getSite().setSelectionProvider(viewer);
Then, to catch your own selection:
getSite().getPage().addPostSelectionListener(this); // Implement ISelectionListener
References: https://wiki.eclipse.org/E4/EAS/Selection
I try to develop news eclipse tool (eclipse plugin), so I want to get multi selected projects from workspace. I try getFirstElement(), but it`s only get first selected projects.
Thank you very much
Here is some codes:
public Object execute(ExecutionEvent event) throws ExecutionException {
part = HandlerUtil.getActivePart(event);
window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
selection = HandlerUtil.getActiveMenuSelection(event);
if (selection instanceof IStructuredSelection) {
IStructuredSelection structured = (IStructuredSelection)selection;
IJavaProject project = null;
Object elem = structured.getFirstElement();
if (elem instanceof IJavaProject) {
project = (IJavaProject)elem;
} else if (elem instanceof IProject) {
project = (IJavaProject)JavaCore.create((IProject)elem);
}
if (project != null) {
}
}
return null;
}
As its name suggests IStructuredSelection.getFirstElement() only gives you the first selection.
To get all the selections use any of the iterator(), toArray() or toList() methods.
For example:
IStructuredSelection structured = (IStructuredSelection)selection;
for (Object elem : structured.toList()) {
IJavaProject project = null;
if (elem instanceof IJavaProject) {
project = (IJavaProject)elem;
} else if (elem instanceof IProject) {
project = (IJavaProject)JavaCore.create((IProject)elem);
}
if (project != null) {
... your code
}
}
I need to disable all childs widgets of some custom panel. I have such method for this:
public static void disableChilds(Widget widget) {
if(widget instanceof HasWidgets) {
Iterator<Widget> iter = ((HasWidgets) widget).iterator();
while(iter.hasNext()) {
Widget element = iter.next();
//Window.alert(element.getClass().toString());
if(element instanceof HasEnabled) {
((HasEnabled) element).setEnabled(false);
}
disableChilds(element);
}
}
}
The problem is that this code works only on Firefox(24.5.0) and only if
Window.alert(element.getClass().toString());
is uncommented. It does not want to work on Chrome(35.0.1916.114) at all.
I would be grateful for any help.
today I changed my Eclipse IDE from 3.7 to 4.2 and my plugin-project has a new feature in the Statusbar of the UI called QuickAccess. But I dont need it, so how can I disable this feature, because the position of my button bar has changed...
For all who have the same problem, it seems that this new feature is hardcoded and can't be disabled :/ https://bugs.eclipse.org/bugs/show_bug.cgi?id=362420
Go to Help --> Install New Software
https://raw.github.com/atlanto/eclipse-4.x-filler/master/pdt_tools.eclipse-4.x-filler.update/
Install that Plugin and Restart the Eclipse. Quick Access automatically hide.
or else you have an option to hide Window --> Hide Quick Access.
Here's a post that shows a way to hide it with CSS. Verified with Eclipse 4.3
Lars Vogel just reported in his blog post "Porting Eclipse 3.x RCP application to Eclipse 4.4 – now without QuickAccess box":
Bug 411821 ([QuickAccess] Contribute SearchField through a fragment or other means)
is now solved.
Thanks to René Brandstetter:
If a RCP app doesn't provide the QuickAccess element in its model, than it will not be visible. So the default is no QuickAcces, easy enough? :)
See the commit 839ee2 for more details
Provide the "QuickAccess" via a e4 application model fragment inside of the "org.eclipse.ui.ide.application".
This removes the "QuickAccess" search field from every none "org.eclipse.ui.ide.application".
You could also hide it and make it work comparable to how it used to work in Eclipse3.7: when user presses ctrl+3 Quick Access functionality pops up (In Eclipse4.3 the ctrl+3 shortcut is still available).
Example of code you could add to your implementation of WorkbenchWindowAdvisor (for Eclipse4.3 rcp application)
private IHandlerActivation quickAccessHandlerActivation;
#Override
public void postWindowOpen() {
hideQuickAccess();
}
private void hideQuickAccess() {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
setQuickAccessVisible(window, false);
final IHandlerService service = (IHandlerService) window.getService(IHandlerService.class);
quickAccessHandlerActivation = service.activateHandler(QUICK_ACCESS_COMMAND_ID, new CustomQuickAccessHandler());
}
private void setQuickAccessVisible(IWorkbenchWindow window, boolean visible) {
if (window instanceof WorkbenchWindow) {
MTrimBar topTrim = ((WorkbenchWindow) window).getTopTrim();
for (MTrimElement element : topTrim.getChildren()) {
if (QUICK_ACCESS_ELEMENT_ID.equals(element.getElementId())) {
element.setVisible(visible);
if (visible) {
Composite control = (Composite) element.getWidget();
control.getChildren()[0].addFocusListener(new QuickAccessFocusListener());
}
break;
}
}
}
}
private class QuickAccessFocusListener implements FocusListener {
#Override
public void focusGained(FocusEvent e) {
//not interested
}
#Override
public void focusLost(FocusEvent e) {
((Control) e.widget).removeFocusListener(this);
hideQuickAccess();
}
}
private class CustomQuickAccessHandler extends AbstractHandler {
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
final IHandlerService service = (IHandlerService) window.getService(IHandlerService.class);
setQuickAccessVisible(window, true);
if (quickAccessHandlerActivation != null) {
service.deactivateHandler(quickAccessHandlerActivation);
try {
return service.executeCommand(QUICK_ACCESS_COMMAND_ID, null);
} catch (NotDefinedException e) {
} catch (NotEnabledException e) {
} catch (NotHandledException e) {
}
}
return null;
}
}