tutorial to JFace TreeViewer very good details.
At first we change our staticaly views and root objects.
change in ViewPart1 (thus was it called my TreeViewer ViewPart)
private TreeViewer viewer;
private RootNames root;
public RootNames getTreeRoot() {
return root;
}
public TreeViewer getTreeViewer() {
return viewer;
}
now change the create the Command Add and Del and add them to menu. (see Vogella )
public class CommandAdd extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
System.out.println("start");
ViewPart1 view = (ViewPart1) HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().findView(ViewPart1.ID);
RootNames root = view.getTreeRoot();
HostsNames hn = new HostsNames("212.40.32.55", "212.40.32.55");
root.addChild(hn);
hn.addChild(new Commandos("syslog", "syslog"));
hn.addChild(new Commandos("top", "top -d 1"));
hn.addChild(new Commandos("network", "network"));
view.getTreeViewer().refresh();
return null;
}
}
public class CommandDel extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
ViewPart1 view = (ViewPart1) HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().findView(ViewPart1.ID);
RootNames root = view.getTreeRoot();
HostsNames hn = root.getChildren()[0];
Commandos c = hn.children.get(0);
hn.removeChild(c);
view.getTreeViewer().refresh();
return null;
}
}
Now we create a second View where we simple put some text.
The commands simple add and del some item of tree.
public class OutputView extends ViewPart {
public static final String ID = "zzz.zmytest.Tree.Views.OutputView";
public Text text;
@Override
public void createPartControl(Composite parent) {
text = new Text(parent, SWT.BORDER);
text.setText("Imagine a fantastic user interface here");
}
@Override
public void setFocus() {
}
}
we will simple alter the text.
selection Listener
selection Listener
we implement ISelectionListener:
public class OutputView extends ViewPart implements ISelectionListener{
@Override
public void createPartControl(Composite parent) {
...
getSite().getPage().addSelectionListener((ISelectionListener) this);
}
@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
text.setText(selection.toString());
}
}
add the view in the perspective:
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
String editorArea = layout.getEditorArea();
layout.setEditorAreaVisible(false);
layout.addStandaloneView(ViewPart1.ID, false, IPageLayout.LEFT, 0.25f, editorArea);
IFolderLayout folder = layout.createFolder("outputs", IPageLayout.TOP, 0.5f, editorArea);
folder.addPlaceholder(OutputView.ID + ":*");
folder.addView(OutputView.ID);
layout.getViewLayout(ViewPart1.ID).setCloseable(false);
}
}
set the tree view to selection provider:
public class ViewPart1 extends ViewPart {
...
public void createPartControl(Composite parent) {
...
// add to selection provider
getSite().setSelectionProvider(viewer);
}
improve the listener:
@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
if (selection instanceof IStructuredSelection) {
Object obj = ((IStructuredSelection) selection).getFirstElement();
if (obj != null) {
if (obj instanceof HostsNames) {
HostsNames hn = (HostsNames)obj;
text.setText("hostname: "+hn.name);
} else if (obj instanceof Commandos) {
Commandos co = (Commandos)obj;
HostsNames hn = co.parent;
text.setText("commando: "+co.name+ " von host: "+hn.name);
}
}
}
}
the listener look for IStructuredSelection, get the object and test the object for our class instances.
Now we can add further views provider and listener without to handle the wired code.s
Keine Kommentare:
Kommentar veröffentlichen