| Treeviewer = updating + adding + removing nodes [message #445009] |
Sat, 25 February 2006 17:22  |
Eclipse User |
|
|
|
Originally posted by: lukaszgrobelny.poczta.onet.pl
Hi All,
I got stuck with Treeviewer in my RCP app. I am having problems with refreshing tree while adding new nodes to the tree.
I suppose that I have something messed in my code, but I can't find it :(
My domain objects are: category(Box) + network switch(Switch) (Box can have many Switches and other Boxes). After adding new switch and calling viewer.refresh() nothing changes. While removing Boxes tree looks ok (is updated), but doing the same on Switch objects I get an error:
Unhandled event loop exception
Reason:
com.microsens.midem.GUI.model.Switch
I have tried to follow this topic, but it didn't work for me (problem with adding listeners)
http://www.eclipsezone.com/eclipse/forums/m91953194.html
I would appreciate any help, clues, ideas. This model should support adding and removing objects via popup menu.
This is my code:
public class Box{
private String name;
private Box parent;
private ArrayList<Box> boxes;
private ArrayList<Switch> switches;
public Box(Box parent, String name) {
this.parent = parent;
boxes = new ArrayList<Box>();
switches = new ArrayList<Switch>();
this.name = name;
}
public ArrayList<Box> getBoxes() {
return boxes;
}
public ArrayList<Switch> getSwitches() {
return switches;
}
public String getName() {
return name;
}
public void addBox(Box box) {
boxes.add(box);
box.setParent(this);
}
public void addSwitch(Switch sw) {
switches.add(sw);
}
public boolean removeBox(Box box) {
return boxes.remove(box);
}
public boolean removeSwitch(Switch sw) {
return switches.remove(sw);
}
public Box getParent() {
return parent;
}
public void setParent (Box box) {
this.parent = box;
}
public void remove(Object model) {
if (model instanceof Box)
removeBox((Box)model);
if (model instanceof Switch)
removeSwitch((Switch)model);
}
}
public class Switch {
private Box parent;
private ExtendedSwitch extendedSwitch; // This is my wrapped object
public Switch(Box parent, ExtendedSwitch extendedSwitch) {
this.extendedSwitch = extendedSwitch;
this.parent = parent;
}
public String getName() {
return extendedSwitch.getDescription();
}
public Box getParent() {
return parent;
}
public ExtendedSwitch getSwitch() {
return extendedSwitch;
}
}
public class BoxContentProvider implements ITreeContentProvider {
private static Object[] EMPTY_ARRAY = new Object[0];
public void dispose() {}
public void inputChanged(Viewer viewer,
Object oldInput, Object newInput) { }
public Object[] getChildren(Object parentElement) {
if(parentElement instanceof Box) {
Box box = (Box)parentElement;
return concat(box.getBoxes().toArray(),
box.getSwitches().toArray());
}
return EMPTY_ARRAY;
}
protected Object[] concat(Object[] object, Object[] more) {
Object[] both = new Object[object.length + more.length];
System.arraycopy(object, 0, both, 0, object.length);
System.arraycopy(more, 0, both, object.length, more.length);
return both;
}
public Object getParent(Object element) {
if(element instanceof Box) {
return ((Box)element).getParent();
}
if(element instanceof Switch) {
return ((Switch)element).getParent();
}
return null;
}
public boolean hasChildren(Object element) {
return getChildren(element).length > 0;
}
public Object[] getElements(Object inputElement) {
return getChildren(inputElement);
}
}
And code responsible for adding and removing in ViewPart:
public static void addNewSwitch(ExtendedSwitch extendedSwitch) {
root.addSwitch(new Switch(root,extendedSwitch));
viewer.refresh();
}
public static void removeSelected() {
if (viewer.getSelection().isEmpty()) {
return;
}
IStructuredSelection selection = (IStructuredSelection) viewer
.getSelection();
viewer.getTree().setRedraw(false);
for (Iterator iterator = selection.iterator(); iterator.hasNext();) {
Box model = (Box) iterator.next();
Box parent = (Box)model.getParent();
parent.remove(model);
}
viewer.getTree().setRedraw(true);
viewer.refresh();
}
Best Regards,
Lukasz Grobelny
|
|
|
|
| Re: Treeviewer = updating + adding + removing nodes [message #445084 is a reply to message #445009] |
Mon, 27 February 2006 18:51  |
Eclipse User |
|
|
|
This is the kind of exception that usually revolves around a ClassNotFoundException (except the exception type isn't displayed) or a ClassCastException.
My guess is that your UI code is wired up somehow, and you're getting an object which isn't a Switch being inserted into something that expects it (as a guess; your ArrayList).
I posted a recent hints-and-tips on EclipseZone regarding the use of the debugger to catch uncaught exceptions in the debugger; I strongly recommend that you set up a breakpoint and look out for ClassNotFoundException (unlikely) or ClassCastException (very likely) being thrown. You'll probably find it's an insert into the generic lists, or a result of one of them.
To be honest, your iterator in the last sample bit of code looks suspect; you're assuming that any selected item is a box without doing an instanceof check. I'd bet that's where the classcast is coming from.
Alex.
|
|
|
Powered by
FUDForum. Page generated in 0.25553 seconds