Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » To add a node to the tree viewer
To add a node to the tree viewer [message #325988] Wed, 05 March 2008 10:03 Go to next message
Eclipse UserFriend
Originally posted by: rashi.bahal.gmail.com

------=_Part_2754_20725852.1204711462737
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi,
I have made a TreeViewer where there is only one node and i want to add further nodes on the click of a button.
My tree viewer is created with the root node but i am not able to get the instance of the View containing the tree viewer, in the action class, to add further nodes in it.
I am attaching the source also with this post. Plz help me out.
Thanks
------=_Part_2754_20725852.1204711462737
Content-Type: application/octet-stream; name=navigatorview.java
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=navigatorview.java

package com.wrms.sbvrge.views;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.wrms.sbvrge.personTree.MovingBox;
import com.wrms.sbvrge.personTree.Person;
import com.wrms.sbvrge.personTree.PersonParent;

public class NavigatorView extends ViewPart {

public TreeViewer treeViewer;

public NavigatorView() {
// TODO Auto-generated constructor stub
System.out.println("tp");
}

@Override
public void createPartControl(Composite parent) {
parent.getShell().setText("Navigation");
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
gridLayout.verticalSpacing = 2;
parent.setLayout(gridLayout);

treeViewer = new TreeViewer(parent, SWT.FULL_SELECTION | SWT.MULTI);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.grabExcessHorizontalSpace = true;
gd.grabExcessVerticalSpace = true;
gd.horizontalAlignment = gd.FILL;
gd.verticalAlignment = gd.FILL;
treeViewer.getTree().setLayoutData(gd);
treeViewer.setContentProvider(new NavigatorContentProvider());
treeViewer.setLabelProvider(new NavigatorLabelProvider());
treeViewer.setInput(new PersonParent().getRoot());
treeViewer.expandAll();

}

@Override
public void setFocus() {
// TODO Auto-generated method stub

}

public TreeViewer getTreeViewer() {
return treeViewer;
}

public NavigatorView getNavigatorView() {
return this;
}
}

class NavigatorContentProvider implements ITreeContentProvider {

private final Object[] EMPTY = new Object[] {};
@Override
public Object[] getChildren(Object parentElement) {
if(parentElement instanceof PersonParent) {
PersonParent parent = (PersonParent)parentElement;
return parent.getChildren().toArray();
}
return EMPTY;
}

@Override
public Object getParent(Object element) {
return null;
}

@Override
public boolean hasChildren(Object element) {
if(element instanceof PersonParent) {
PersonParent parent = (PersonParent)element;
if(parent.getChildren().size() > 0) {
return true;
}
}
return false;
}

@Override
public Object[] getElements(Object inputElement) {
PersonParent parent = (PersonParent)inputElement;
return parent.getChildren().toArray();
}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub

}
}

class NavigatorLabelProvider implements ILabelProvider {

@Override
public Image getImage(Object element) {
// TODO Auto-generated method stub
return null;
}

@Override
public String getText(Object element) {
PersonParent pm = (PersonParent)element;
return pm.getFname();
}

@Override
public void addListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub

}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public boolean isLabelProperty(Object element, String property) {
// TODO Auto-generated method stub
return false;
}

@Override
public void removeListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub

}

}

------=_Part_2754_20725852.1204711462737
Content-Type: application/octet-stream; name=person.java
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=person.java

package com.wrms.sbvrge.personTree;

import java.util.ArrayList;

public class Person {

public String fname;
public PersonParent parent;

public Person() {
}

public Person(String fname) {
this.fname = fname;
}

public String getFname() {
return fname;
}
}

------=_Part_2754_20725852.1204711462737
Content-Type: application/octet-stream; name=personparent.java
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=personparent.java

package com.wrms.sbvrge.personTree;

import java.util.ArrayList;

public class PersonParent extends Person {

private static PersonParent root;
public ArrayList children;

public PersonParent() {
children = new ArrayList();
}

public PersonParent(String fname) {
this();
this.fname = fname;
}

public void addChild(PersonParent child) {
children.add(child);
child.parent = this;
}

// public Object examples() {
// root = new PersonParent();
// PersonParent parent = new PersonParent("xyz");
// root.addChild(parent);
// return root;
// }

public void createChild(String title) {
PersonParent node = new PersonParent(title);
root.addChild(node);
}

public ArrayList getChildren() {
return children;
}

public static PersonParent getRoot() {
root = new PersonParent();
return root;
}
}

------=_Part_2754_20725852.1204711462737
Content-Type: application/octet-stream; name=addtreenode.java
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=addtreenode.java

package com.wrms.sbvrge.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import com.wrms.sbvrge.personTree.PersonParent;
import com.wrms.sbvrge.views.NavigatorView;

public class AddTreeNodeAction extends Action implements IWorkbenchAction,
ISelectionListener {

private IWorkbenchWindow window;
private PersonParent personParent = new PersonParent();
NavigatorView navigatorView;


public AddTreeNodeAction(IWorkbenchWindow window) {
setText("Add node");
this.window = window;
window.getSelectionService().addSelectionListener(this);
}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
// TODO Auto-generated method stub

}

@SuppressWarnings("static-access")
@Override
public void run() {
System.out.println("shady the great");
personParent.createChild("node");

navigatorView.getNavigatorView().getTreeViewer().setInput(pe rsonParent.getRoot());
navigatorView.getNavigatorView().getTreeViewer().refresh();
}

}

------=_Part_2754_20725852.1204711462737--
Re: To add a node to the tree viewer [message #325991 is a reply to message #325988] Wed, 05 March 2008 11:42 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------010102010400090205010204
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Rashi,

People are really busy so I kind of doubt someone will just debug your
problem for you. Of course I'd like to be proven wrong. Failing that,
perhaps a better approach to take is to use something like EMF to define
the model for your tree and then let EMF generate the code you need to
make it work:
< http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. emf.doc/tutorials/xlibmod/xlibmod.html>


http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. emf.doc/tutorials/xlibmod/xlibmod.html


Rashi Bahal wrote:
> Hi,
> I have made a TreeViewer where there is only one node and i want to add further nodes on the click of a button.
> My tree viewer is created with the root node but i am not able to get the instance of the View containing the tree viewer, in the action class, to add further nodes in it.
> I am attaching the source also with this post. Plz help me out.
> Thanks


--------------010102010400090205010204
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Rashi,<br>
<br>
People are really busy so I kind of doubt someone will just debug your
problem for you.&nbsp; Of course I'd like to be proven wrong.&nbsp; Failing that,
perhaps a better approach to take is to use something like EMF to
define the model for your tree and then let EMF generate the code you
need to make it work:<a
href=" http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. emf.doc/tutorials/xlibmod/xlibmod.html"><br>
</a>
<blockquote><a
href=" http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. emf.doc/tutorials/xlibmod/xlibmod.html"> http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. emf.doc/tutorials/xlibmod/xlibmod.html</a><br>
</blockquote>
<br>
Rashi Bahal wrote:
<blockquote
cite="mid:10845062.29631204711462742.JavaMail.root@cp1.dzone.com"
type="cite">
<pre wrap="">Hi,
I have made a TreeViewer where there is only one node and i want to add further nodes on the click of a button.
My tree viewer is created with the root node but i am not able to get the instance of the View containing the tree viewer, in the action class, to add further nodes in it.
I am attaching the source also with this post. Plz help me out.
Thanks</pre>
</blockquote>
<br>
</body>
</html>

--------------010102010400090205010204--
Re: To add a node to the tree viewer [message #325993 is a reply to message #325991] Wed, 05 March 2008 12:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: rashi.bahal.gmail.com

Hi,
Thanks for the suggestion. But, my problem is that i have to create the model using JFace and i cannot use EMF.
Plz suggest some other option.
Re: To add a node to the tree viewer [message #325994 is a reply to message #325993] Wed, 05 March 2008 12:32 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

Rashi,

EMF uses JFace and supports all the hard things that you'll be spending
weeks or months implementing by hand; but it's your time to spend as you
see fit. It's a best practice to separate your model from your view,
i.e., the Model View Controller pattern. Actions like the one you are
trying to implement would act on the model, and when the model changes,
it produces a notification that the controller uses to update the view.
EMF is built around this pattern and provides the model and controller
portions of it. It's interesting that even your question about how to
get at the view given only an instance of the model is already a bit
suspicious in light of best practices. So the best suggestion I can
give is if you want help from the newsgroup, you need to ask much more
focused questions. If you don't want to explore suggestions, you'll be
on your own.


Rashi Bahal wrote:
> Hi,
> Thanks for the suggestion. But, my problem is that i have to create the model using JFace and i cannot use EMF.
> Plz suggest some other option.
>
Re: To add a node to the tree viewer [message #326029 is a reply to message #325988] Thu, 06 March 2008 04:14 Go to previous message
Wayne Beaton is currently offline Wayne BeatonFriend
Messages: 554
Registered: December 2017
Senior Member
Take a look at the values passed into the selectionChanged() method
(i.e. set up a breakpoint on the method and select the node in your
view). You should find the "part" parameter useful.

Also, I'm pretty sure just refreshing the node should be enough in the
run() method (i.e. refresh(personParent) ).

HTH,

Wayne

On Wed, 2008-03-05 at 05:03 -0500, Rashi Bahal wrote:
> Hi,
> I have made a TreeViewer where there is only one node and i want to add further nodes on the click of a button.
> My tree viewer is created with the root node but i am not able to get the instance of the View containing the tree viewer, in the action class, to add further nodes in it.
> I am attaching the source also with this post. Plz help me out.
> Thanks
Previous Topic:Naming Convention followed in Eclipse IDE
Next Topic:Shared instance of refresh icon?
Goto Forum:
  


Current Time: Fri Jul 12 11:22:15 GMT 2024

Powered by FUDForum. Page generated in 0.04101 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top