tags:

views:

66

answers:

1

All, I created a plugin using the "create plugin with a view" wizard, which created a tree viewer. The wizard creates the Actions directly in the view, and consequently all parts of the view are visible to the actions. This is fine for simple plugins but not for "real-world" plugins, particularly if you need key binding.

My question is: what is the "preferred" way for views and actions to communicate with one another when the Actions are not classes in the View?

For example: I have that tree viewer. The user selects various items in the tree, and then right clicks and a menu appears with my Action in it. The user clicks the menu item. That then fires off my Action.

A) How does the Action get the "data" of the selection in that tree viewer, i.e. the underlying objects comprising the tree

b) When the Action runs, I want it to take each selected element, perform some operation, and then update the underlying object and also have the tree's visible appearance change as a result of the operation I've performed... i.e. I want to have the Action communicate back to the tree viewer. Perhaps I want to change the Icon of that selected item's element, for example. In addition, when the Action starts, I want it to communicate not just with the tree view, but also other visible elements of the view... maybe it should disable certain buttons that otherwise would be visible while the action isnt' running, and then when the Action completes its task I want it to communicate back to the view to update the buttons and maybe increment some counters in some label.

c) In addition, when the tree updates as a result of the Action performing some operation, I'd like another viewer in the same view to respond. For example, maybe there's a TableViewer in the view, underneath the tree, and its job is to show certain details of the selected elements in the tree view. So the Action runs, it operates on all selected elements in the tree; it updates those elements, and as it updates those elements, the "details" table viewer updates as a result of the new data in the tree viewer's objects.

Currently, I have something akin to this working, but I know my approach is wrong. The Actions are separate classes, but I'm creating a new instance of them in the View, passing the view into the Action's constructor, and providing getters and setters on the view which the Action calls directly. In addition, since I'm not doing any of this with plugin.xml, I don't get configurable key binding.... just the hard-coded keybindings I've added in the view.

Bottom line is I want to really learn "the right way" to do this in Eclipse, not just "a way that works". Examples are MOST welcome!

Thanks.

A: 

I'm not sure it is necessarily the right way to do it but the way I solved a similar problem was to make my action class extent IEditorActionDelegate.

Here is a snippet of what I have, hope it helps.

public class SubmitEditorActionDelegate implements IEditorActionDelegate { 
private IEditorPart targetEditor;

private ISelection selection;

/*
 * (non-Javadoc)
 * 
 * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction,
 *      org.eclipse.ui.IEditorPart)
 */
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
 this.targetEditor = targetEditor;
}
/*
 * (non-Javadoc)
 * 
 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
 *      org.eclipse.jface.viewers.ISelection)
 */
public void selectionChanged(IAction action, ISelection selection) {
 logger.fine("selectionChanged" + selection +"action ="+action);
 if (selection != null && (selection instanceof ITextSelection)){ 
  this.selection = selection;
 }else {
  this.selection=null;
 }   

}

public void run(IAction action) {
 if (targetEditor != null && targetEditor instanceof ProgramEditor) {
  ProgramEditor editor = (ProgramEditor) targetEditor;

  IDocumentProvider docProvider = editor.getDocumentProvider();
  IEditorInput input = editor.getEditorInput();

   String selectedText=((ITextSelection) selection).getText();
            }
     }

}

Grem
I think that's where I need to start. But.... how to get at the data in that selection object? and how to communicate back to the view? that's where I'm getting lost.
marc esher
edited my original answer to add some extra sample code in the run() method to show usages of the attributes .hope it helps.
Grem