Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Collecting all objects for treeviewer
Collecting all objects for treeviewer [message #857416] Thu, 26 April 2012 14:12 Go to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
Hey all,

I've got a small question, I have a treeviewer that lists all my groups & persons. I've done this already on 10 different ways but now I found a way that looks decent enough, so I would like to use this. I just got on problem, I don't know how to add a new group to the tree. This is what I do:

TreeViewer treeViewer = new TreeViewer(composite_1, SWT.BORDER);
		Tree tree = treeViewer.getTree();
		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);

		[color=orangered]GroupList list = new GroupList();
		List<Group> groups = list.getGroups();
		GroupCollection allGroups = SmartsignatureFactory.eINSTANCE.createGroupCollection();
		allGroups.getContaintsGroups().addAll(groups);[/color]

		IObservableList observableList = EMFObservables.observeList(allGroups, 
				SmartsignaturePackage.Literals.GROUP_COLLECTION__CONTAINTS_GROUPS);
		
		IObservableFactory factory = new IObservableFactory() {
			
			@Override
			public IObservable createObservable(Object target) {
				if (target instanceof Group)
				{
					return EMFProperties.list(
							SmartsignaturePackage.Literals.GROUP__CONTAINS_PERSONS).observe(target);				
				} else if (target instanceof IObservable) {
					return (IObservable) target;
				}
				return null;
			}
		};
		
		// Advisor
	    TreeStructureAdvisor advisor = new TreeStructureAdvisor() {
	        @Override
	        public Boolean hasChildren(Object element) {
	            if (element instanceof Group) {
	                return ((Group) element).getContainsPersons().size() != 0;
	            }
	            return super.hasChildren(element);
	        }
	    };
	    
	    ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(
	            factory, advisor);
	    
	    IObservableMap[] maps = new IObservableMap[3];
	    
	    maps[0] = EMFProperties.value(
	            SmartsignaturePackage.Literals.GROUP__NAME).observeDetail(
	            contentProvider.getKnownElements());
	    maps[1] = EMFProperties.value(
	            FeaturePath.fromList(
	            		SmartsignaturePackage.Literals.PERSON__FIRST_NAME))
	            .observeDetail(contentProvider.getKnownElements());
	    maps[2] = EMFProperties.value(
	            FeaturePath.fromList(
	            		SmartsignaturePackage.Literals.PERSON__LAST_NAME))
	            .observeDetail(contentProvider.getKnownElements());
	    
	    ObservableMapLabelProvider labelProvider = new ObservableMapLabelProvider(
	            maps) {
	 
	        public String getColumnText(Object element, int columnIndex) {
	            if (element instanceof Group) {
	                Group group = (Group) element;
	                return group.getName();
	            }
	            if (element instanceof Person) {
	            	Person person = (Person) element;
	            	return person.getFirstName() + " " + person.getLastName();
	            }
	            	
	            return super.getColumnText(element, columnIndex);
	        }
	
	    	public Image getImage(Object element) {
	    		if (element instanceof Group) { 			
	    			return ResourceManager.getPluginImage("smartapps.smartsignature.ui.admin", "icons/user-group-icon.png");
	    		}
	    		return ResourceManager.getPluginImage("smartapps.smartsignature.ui.admin", "icons/user-icon.png");
	    	}
	    	
	    };
	    
		treeViewer.setContentProvider(contentProvider);
	    treeViewer.setLabelProvider(labelProvider);
	    treeViewer.setInput(observableList);
	    


what I want is that instead of creating a list of all the group objects and placing them in a collection object, I'ld like that it does that on it's own.

One option I was thinking about was making it so that whenever a group is added, it also get's added to the groupCollection object, I can do that manually but that just sounds wrong.

Hope you can see what I mean,
any help is much appreciated

[Updated on: Thu, 26 April 2012 14:14]

Report message to a moderator

Re: Collecting all objects for treeviewer [message #865275 is a reply to message #857416] Mon, 30 April 2012 10:14 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
Ok, since I got no answer I'll try again.

What I want is that whenever a group gets added or deleted, my treeviewer that shows the groups and the content from the groups to update itself.
I'm trying to listen to the resource that contains all the groups by using an adapter but I must be something wrong because the adapter never gets triggered. Here is the code from the tree I have so far:

IObservableList observableList = EMFObservables.observeList(allGroups, 
				SmartsignaturePackage.Literals.GROUP_COLLECTION__CONTAINS_GROUPS);
		
		IObservableFactory factory = new IObservableFactory() {
			
			@Override
			public IObservable createObservable(Object target) {
				if (target instanceof Group)
				{
					return EMFProperties.list(
							SmartsignaturePackage.Literals.GROUP__CONTAINS_PERSONS).observe(target);				
				} else if (target instanceof IObservable) {
					return (IObservable) target;
				}
				return null;
			}
		};
		
		// Advisor
	    TreeStructureAdvisor advisor = new TreeStructureAdvisor() {
	        @Override
	        public Boolean hasChildren(Object element) {
	            if (element instanceof Group) {
	                return ((Group) element).getContainsPersons().size() != 0;
	            }
	            return super.hasChildren(element);
	        }
	    };
	    
	    ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(
	            factory, advisor);
	    
	    IObservableMap[] maps = new IObservableMap[3];
	    
	    maps[0] = EMFProperties.value(
	            SmartsignaturePackage.Literals.GROUP__NAME).observeDetail(
	            contentProvider.getKnownElements());
	    maps[1] = EMFProperties.value(
	            FeaturePath.fromList(
	            		SmartsignaturePackage.Literals.PERSON__FIRST_NAME))
	            .observeDetail(contentProvider.getKnownElements());
	    maps[2] = EMFProperties.value(
	            FeaturePath.fromList(
	            		SmartsignaturePackage.Literals.PERSON__LAST_NAME))
	            .observeDetail(contentProvider.getKnownElements());
	    
	    ObservableMapLabelProvider labelProvider = new ObservableMapLabelProvider(
	            maps) {
	 
	        public String getColumnText(Object element, int columnIndex) {
	            if (element instanceof Group) {
	                Group group = (Group) element;
	                return group.getName();
	            }
	            if (element instanceof Person) {
	            	Person person = (Person) element;
	            	return person.getFirstName() + " " + person.getLastName();
	            }
	            	
	            return super.getColumnText(element, columnIndex);
	        }

	    	
	    };
	    
	    treeViewer.setContentProvider(contentProvider);
	    treeViewer.setLabelProvider(labelProvider);
	    treeViewer.setInput(observableList);



And I try to add an EContentAdapter to my resource like this:

ResourceSet resourceSet = new ResourceSetImpl();
		URI fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Groups));
		Resource loadResource = resourceSet.getResource(fileURI, true);
		
		EContentAdapter adapter = new EContentAdapter() {
			public void notifyChanged(Notification notification) {
				treeViewer.refresh();
			}
		};
		loadResource.eAdapters().add(adapter);


But the adapter never gets triggered. The idea of this was that whenever the resource changed, the adapter would get triggered and the tree would refresh itself.

I hope this makes more sense that my first post. Any idea's anyone?
Re: Collecting all objects for treeviewer [message #865285 is a reply to message #865275] Mon, 30 April 2012 10:20 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Comments below.

On 30/04/2012 12:14 PM, El Shorty wrote:
> Ok, since I got no answer I'll try again.
> What I want is that whenever a group gets added or deleted, my
> treeviewer that shows the groups and the content from the groups to
> update itself. I'm trying to listen to the resource that contains all
> the groups by using an adapter but I must be something wrong because
> the adapter never gets triggered. Here is the code from the tree I
> have so far:
>
>
> IObservableList observableList = EMFObservables.observeList(allGroups,
>
> SmartsignaturePackage.Literals.GROUP_COLLECTION__CONTAINS_GROUPS);
>
> IObservableFactory factory = new IObservableFactory() {
>
> @Override
> public IObservable createObservable(Object target) {
> if (target instanceof Group)
> {
> return EMFProperties.list(
>
> SmartsignaturePackage.Literals.GROUP__CONTAINS_PERSONS).observe(target);
> } else if (target instanceof IObservable) {
> return (IObservable) target;
> }
> return null;
> }
> };
>
> // Advisor
> TreeStructureAdvisor advisor = new TreeStructureAdvisor() {
> @Override
> public Boolean hasChildren(Object element) {
> if (element instanceof Group) {
> return ((Group)
> element).getContainsPersons().size() != 0;
> }
> return super.hasChildren(element);
> }
> };
> ObservableListTreeContentProvider contentProvider =
> new ObservableListTreeContentProvider(
> factory, advisor);
> IObservableMap[] maps = new IObservableMap[3];
> maps[0] = EMFProperties.value(
>
> SmartsignaturePackage.Literals.GROUP__NAME).observeDetail(
> contentProvider.getKnownElements());
> maps[1] = EMFProperties.value(
> FeaturePath.fromList(
>
> SmartsignaturePackage.Literals.PERSON__FIRST_NAME))
> .observeDetail(contentProvider.getKnownElements());
> maps[2] = EMFProperties.value(
> FeaturePath.fromList(
>
> SmartsignaturePackage.Literals.PERSON__LAST_NAME))
> .observeDetail(contentProvider.getKnownElements());
> ObservableMapLabelProvider labelProvider = new
> ObservableMapLabelProvider(
> maps) {
> public String getColumnText(Object element, int
> columnIndex) {
> if (element instanceof Group) {
> Group group = (Group) element;
> return group.getName();
> }
> if (element instanceof Person) {
> Person person = (Person) element;
> return person.getFirstName() + " " +
> person.getLastName();
> }
>
> return super.getColumnText(element, columnIndex);
> }
>
>
> };
> treeViewer.setContentProvider(contentProvider);
> treeViewer.setLabelProvider(labelProvider);
> treeViewer.setInput(observableList);
>
>
>
> And I try to add an EContentAdapter to my resource like this:
>
> ResourceSet resourceSet = new ResourceSetImpl();
> URI fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Groups));
> Resource loadResource = resourceSet.getResource(fileURI, true);
>
> EContentAdapter adapter = new EContentAdapter() {
> public void notifyChanged(Notification notification) {
Did you look at what this method does in the base class?
> treeViewer.refresh();
> }
> };
> loadResource.eAdapters().add(adapter);
>
> But the adapter never gets triggered. The idea of this was that
> whenever the resource changed, the adapter would get triggered and the
> tree would refresh itself.
I would have expected that data binding would take care of the refresh
via the observables.
>
> I hope this makes more sense that my first post. Any idea's anyone?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Collecting all objects for treeviewer [message #865340 is a reply to message #865285] Mon, 30 April 2012 10:54 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
It takes care of the refresh using the observables, whenever I adjust an existing group, the adjustment is added but when I add or delete a group nothing changes.

IObservableList observableList = EMFObservables.observeList(allGroups, 
				SmartsignaturePackage.Literals.GROUP_COLLECTION__CONTAINS_GROUPS);


I know it is because it obsevers allGroups, problem here is that allGroups is a collection of the already existing groups, but I don't know how to add the new groups to it, that is why I wanted the adapter so that I could add new groups whenever the adapter was triggered.

Perhaps it is possible that instead of using groupList, I could use the resource that contains all the groups.
That would be:

        ResourceSet resourceSet = new ResourceSetImpl();
	URI fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Groups));
	Resource loadResource = resourceSet.getResource(fileURI, true);


I believe that if I could use loadResource instead of allGroups, my problem would be solved but I have no idea how I could do this, that's why I was trying to check if loadResource changed, because if it did than I could manually add my groups by using the adapter.

I'm hoping I make sense
Re: Collecting all objects for treeviewer [message #865378 is a reply to message #865340] Mon, 30 April 2012 11:18 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
I think you'd typically use EMFProperties or EMFEditProperties and use
the resource method to observe the contents of a resource.

On 30/04/2012 12:54 PM, El Shorty wrote:
> It takes care of the refresh using the observables, whenever I adjust
> an existing group, the adjustment is added but when I add or delete a
> group nothing changes.
>
>
> IObservableList observableList = EMFObservables.observeList(allGroups,
>
> SmartsignaturePackage.Literals.GROUP_COLLECTION__CONTAINS_GROUPS);
>
>
> I know it is because it obsevers allGroups, problem here is that
> allGroups is a collection of the already existing groups, but I don't
> know how to add the new groups to it, that is why I wanted the adapter
> so that I could add new groups whenever the adapter was triggered.
> Perhaps it is possible that instead of using groupList, I could use
> the resource that contains all the groups. That would be:
>
>
> ResourceSet resourceSet = new ResourceSetImpl();
> URI fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Groups));
> Resource loadResource = resourceSet.getResource(fileURI, true);
>
>
> I believe that if I could use loadResource instead of allGroups, my
> problem would be solved but I have no idea how I could do this, that's
> why I was trying to check if loadResource changed, because if it did
> than I could manually add my groups by using the adapter.
>
> I'm hoping I make sense


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Collecting all objects for treeviewer [message #865455 is a reply to message #865378] Mon, 30 April 2012 12:04 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
Not sure if this is what you meant but I know used:

ResourceSet resourceSet = new ResourceSetImpl();
		URI fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Groups));
		final Resource loadResource = resourceSet.getResource(fileURI, true);

IObservableList observableList = EMFProperties.resource().observe(resourceSet.getResource(fileURI, true));		


but the result stays the same, if I add a new group it doesn't add the group in the tree, if I edit an already existing group, the changes are shown perfectly... I'm getting desperate lolz ..
Re: Collecting all objects for treeviewer [message #865546 is a reply to message #865455] Mon, 30 April 2012 12:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
I'm not all that familiar with the data binding implementations, but
what you show I expect should work.

You understood my question about why your content adapter wasn't working?

On 30/04/2012 2:04 PM, El Shorty wrote:
> Not sure if this is what you meant but I know used:
>
>
> ResourceSet resourceSet = new ResourceSetImpl();
> URI fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Groups));
> final Resource loadResource = resourceSet.getResource(fileURI,
> true);
>
> IObservableList observableList =
> EMFProperties.resource().observe(resourceSet.getResource(fileURI, true));
>
>
> but the result stays the same, if I add a new group it doesn't add the
> group in the tree, if I edit an already existing group, the changes
> are shown perfectly... I'm getting desperate lolz ..


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Collecting all objects for treeviewer [message #865556 is a reply to message #865546] Mon, 30 April 2012 13:07 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
Yeah I understood why it wasn't working. Now it works if I change an existing group, but not for adding a new group... But that isn't necessary anymore since the observable list takes care of that. I can't seem to find why my resource appears to be unchanged after I add a new Group...
Re: Collecting all objects for treeviewer [message #865632 is a reply to message #865556] Mon, 30 April 2012 13:51 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
I'll post the full code I'm using for the treeviewer, just in case anyone sees what I'm overlooking...

        public class GroupView extends ViewPart implements ISelectionListener {	
	
	private TreeViewer treeViewer;

	public GroupView() {
		super();
	}

	@Override
	public void createPartControl(Composite parent) {
		
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite_1.setLayout(new TreeColumnLayout());
		
		treeViewer = new TreeViewer(composite_1, SWT.BORDER);
		Tree tree = treeViewer.getTree();
		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);		

		ResourceSet resourceSet = new ResourceSetImpl();
		URI fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Groups));
		Resource loadResource = resourceSet.getResource(fileURI, true);
		
	        IObservableList observableList = EMFProperties.resource().observe(loadResource);
					    
		IObservableFactory factory = new IObservableFactory() {
			
			@Override
			public IObservable createObservable(Object target) {
				if (target instanceof Group)
				{
					return EMFProperties.list(
							SmartsignaturePackage.Literals.GROUP__CONTAINS_PERSONS).observe(target);				
				} else if (target instanceof IObservable) {
					return (IObservable) target;
				}
				return null;
			}
		};
		
		// Advisor
	    TreeStructureAdvisor advisor = new TreeStructureAdvisor() {
	        @Override
	        public Boolean hasChildren(Object element) {
	            if (element instanceof Group) {
	                return ((Group) element).getContainsPersons().size() != 0;
	            }
	            return super.hasChildren(element);
	        }
	    };
	    
	    ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(
	            factory, advisor);
	    
	    
	    IObservableMap[] maps = new IObservableMap[3];
	    
	    maps[0] = EMFProperties.value(
	            SmartsignaturePackage.Literals.GROUP__NAME).observeDetail(
	            contentProvider.getKnownElements());
	    maps[1] = EMFProperties.value(
	            FeaturePath.fromList(
	            		SmartsignaturePackage.Literals.PERSON__FIRST_NAME))
	            .observeDetail(contentProvider.getKnownElements());
	    maps[2] = EMFProperties.value(
	            FeaturePath.fromList(
	            		SmartsignaturePackage.Literals.PERSON__LAST_NAME))
	            .observeDetail(contentProvider.getKnownElements());
	    
	    ObservableMapLabelProvider labelProvider = new ObservableMapLabelProvider(
	            maps) {
	 
	        public String getColumnText(Object element, int columnIndex) {
	            if (element instanceof Group) {
	                Group group = (Group) element;
	                return group.getName();
	            }
	            if (element instanceof Person) {
	            	Person person = (Person) element;
	            	return person.getFirstName() + " " + person.getLastName();
	            }
	            	
	            return super.getColumnText(element, columnIndex);
	        }
	
	    	public Image getImage(Object element) {
	    		if (element instanceof Group) { 			
	    			return ResourceManager.getPluginImage("smartapps.smartsignature.ui.groupoverview", "icons/user-group-icon.png");
	    		}
	    		return ResourceManager.getPluginImage("smartapps.smartsignature.ui.groupoverview", "icons/user-icon.png");
	    	}
	    	
	    };
	    
		treeViewer.setContentProvider(contentProvider);
	    treeViewer.setLabelProvider(labelProvider);
	    treeViewer.setInput(observableList);

		
	}

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

	}

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


Re: Collecting all objects for treeviewer [message #868533 is a reply to message #865632] Wed, 02 May 2012 06:57 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
So where is the code that adds/removes a group? You should try to come
up with code we can locally to reproduce your problem.

We are using EMF-Databinding in many of our projects and if this would
be broken our software would be too. So there's some special about your
code which I can't see without running it.

Tom

Am 30.04.12 15:52, schrieb El Shorty:
> I'll post the full code I'm using for the treeviewer, just in case
> anyone sees what I'm overlooking...
>
>
> public class GroupView extends ViewPart implements
> ISelectionListener {
>
> private TreeViewer treeViewer;
>
> public GroupView() {
> super();
> }
>
> @Override
> public void createPartControl(Composite parent) {
>
> Composite composite = new Composite(parent, SWT.NONE);
> composite.setLayout(new GridLayout(1, false));
>
> Composite composite_1 = new Composite(composite, SWT.NONE);
> composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
> true, 1, 1));
> composite_1.setLayout(new TreeColumnLayout());
>
> treeViewer = new TreeViewer(composite_1, SWT.BORDER);
> Tree tree = treeViewer.getTree();
> tree.setHeaderVisible(true);
> tree.setLinesVisible(true);
>
> ResourceSet resourceSet = new ResourceSetImpl();
> URI fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.Groups));
> Resource loadResource = resourceSet.getResource(fileURI, true);
>
> IObservableList observableList =
> EMFProperties.resource().observe(loadResource);
> IObservableFactory factory = new
> IObservableFactory() {
>
> @Override
> public IObservable createObservable(Object target) {
> if (target instanceof Group)
> {
> return EMFProperties.list(
>
> SmartsignaturePackage.Literals.GROUP__CONTAINS_PERSONS).observe(target);
>
> } else if (target instanceof IObservable) {
> return (IObservable) target;
> }
> return null;
> }
> };
>
> // Advisor
> TreeStructureAdvisor advisor = new TreeStructureAdvisor() {
> @Override
> public Boolean hasChildren(Object element) {
> if (element instanceof Group) {
> return ((Group) element).getContainsPersons().size()
> != 0;
> }
> return super.hasChildren(element);
> }
> };
> ObservableListTreeContentProvider contentProvider = new
> ObservableListTreeContentProvider(
> factory, advisor);
> IObservableMap[] maps = new IObservableMap[3];
> maps[0] = EMFProperties.value(
> SmartsignaturePackage.Literals.GROUP__NAME).observeDetail(
> contentProvider.getKnownElements());
> maps[1] = EMFProperties.value(
> FeaturePath.fromList(
> SmartsignaturePackage.Literals.PERSON__FIRST_NAME))
> .observeDetail(contentProvider.getKnownElements());
> maps[2] = EMFProperties.value(
> FeaturePath.fromList(
> SmartsignaturePackage.Literals.PERSON__LAST_NAME))
> .observeDetail(contentProvider.getKnownElements());
> ObservableMapLabelProvider labelProvider = new
> ObservableMapLabelProvider(
> maps) {
> public String getColumnText(Object element, int
> columnIndex) {
> if (element instanceof Group) {
> Group group = (Group) element;
> return group.getName();
> }
> if (element instanceof Person) {
> Person person = (Person) element;
> return person.getFirstName() + " " +
> person.getLastName();
> }
>
> return super.getColumnText(element, columnIndex);
> }
>
> public Image getImage(Object element) {
> if (element instanceof Group) {
> return
> ResourceManager.getPluginImage("smartapps.smartsignature.ui.groupoverview",
> "icons/user-group-icon.png");
> }
> return
> ResourceManager.getPluginImage("smartapps.smartsignature.ui.groupoverview",
> "icons/user-icon.png");
> }
>
> };
> treeViewer.setContentProvider(contentProvider);
> treeViewer.setLabelProvider(labelProvider);
> treeViewer.setInput(observableList);
>
>
> }
>
> @Override
> public void setFocus() {
> // TODO Auto-generated method stub
>
> }
>
> @Override
> public void selectionChanged(IWorkbenchPart part, ISelection
> selection) {
> // TODO Auto-generated method stub
>
> }
>
>
> }
>
>
>
Re: Collecting all objects for treeviewer [message #868564 is a reply to message #868533] Wed, 02 May 2012 08:23 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
To add new groups I use a wizard. That saves my new group to my xml-file that contains all groups.
The performfinish method from this wizard:

        @Override
	public boolean performFinish() {
		group = newGroup.getGroup();		
		if ((group.getName() != null) && (!group.getName().equals("")))
		{			
			SaveMethod save = new SaveMethod();
			save.save(group);
			openEditor();			
			return true;
		} else {
			MessageDialog.openWarning(getShell(), Messages.incorrect_data, Messages.fill_in_correctly_or_cancel);
			return false;
		}
	}



this code saves my group the already existing xml-file, the file I used to load the resource, but I'm guessing it doens't affect the resource itself. I doubt it is a bug, I think I'm doing something wrong, I just don't know what.
Re: Collecting all objects for treeviewer [message #868578 is a reply to message #868564] Wed, 02 May 2012 08:48 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Generated editors listen for changes in the workspace and unload and
then reload resources that have changed; please look at a generated
editor for a working prototype. Unloading your resource could be a
problem for data binding because all your existing objects from that
resource will be turned into proxies that need to be resolved in the
reloaded resource.


On 02/05/2012 10:23 AM, El Shorty wrote:
> To add new groups I use a wizard. That saves my new group to my
> xml-file that contains all groups.
> The performfinish method from this wizard:
>
>
> @Override
> public boolean performFinish() {
> group = newGroup.getGroup();
> if ((group.getName() != null) && (!group.getName().equals("")))
> {
> SaveMethod save = new SaveMethod();
> save.save(group);
> openEditor();
> return true;
> } else {
> MessageDialog.openWarning(getShell(),
> Messages.incorrect_data, Messages.fill_in_correctly_or_cancel);
> return false;
> }
> }
>
>
>
> this code saves my group the already existing xml-file, the file I
> used to load the resource, but I'm guessing it doens't affect the
> resource itself. I doubt it is a bug, I think I'm doing something
> wrong, I just don't know what.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Collecting all objects for treeviewer [message #868579 is a reply to message #868564] Wed, 02 May 2012 08:49 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
I don't see you attaching the object to the in memory model (a resource,
a parent object) so nothing can get updated.

Tom

Am 02.05.12 10:23, schrieb El Shorty:
> To add new groups I use a wizard. That saves my new group to my xml-file
> that contains all groups.
> The performfinish method from this wizard:
>
>
> @Override
> public boolean performFinish() {
> group = newGroup.getGroup();
> if ((group.getName() != null) && (!group.getName().equals("")))
> {
> SaveMethod save = new SaveMethod();
> save.save(group);
> openEditor();
> return true;
> } else {
> MessageDialog.openWarning(getShell(),
> Messages.incorrect_data, Messages.fill_in_correctly_or_cancel);
> return false;
> }
> }
>
>
>
> this code saves my group the already existing xml-file, the file I used
> to load the resource, but I'm guessing it doens't affect the resource
> itself. I doubt it is a bug, I think I'm doing something wrong, I just
> don't know what.
Re: Collecting all objects for treeviewer [message #868592 is a reply to message #868579] Wed, 02 May 2012 09:10 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
Yeah that's what I tought, so now I've made a parent object called GroupCollection, this should contain all the existing groups, whenever I create a new group I add it to the groupCollection object but again nothing changes. I'm guessing I need to adjust my code some more but don't know where anymore, here is what I have now:

Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite_1.setLayout(new TreeColumnLayout());
		
		treeViewer = new TreeViewer(composite_1, SWT.BORDER);
		Tree tree = treeViewer.getTree();
		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);	

		ResourceSet resourceSet = new ResourceSetImpl();
		URI fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.GroupCollections));
		loadResource = resourceSet.getResource(fileURI, true);	

		observableList = EMFProperties.resource().observe(loadResource);
		
		IObservableFactory factory = new IObservableFactory() {
			
			@Override
			public IObservable createObservable(Object target) {
				if (target instanceof GroupCollection)
				{
					return EMFProperties.list(
							SmartsignaturePackage.Literals.GROUP_COLLECTION__CONTAINS_GROUPS).observe(target);
				}
				else if (target instanceof Group)
				{
					return EMFProperties.list(
							SmartsignaturePackage.Literals.GROUP__CONTAINS_PERSONS).observe(target);				
				} else if (target instanceof IObservable) {
					return (IObservable) target;
				}
				return null;
			}
		};
		
		// Advisor
	    TreeStructureAdvisor advisor = new TreeStructureAdvisor() {
	        @Override
	        public Boolean hasChildren(Object element) {
	        	if (element instanceof GroupCollection) {
	        		return ((GroupCollection) element).getContainsGroups().size() != 0;
	        	}
	        		
	        	else if (element instanceof Group) {
	                return ((Group) element).getContainsPersons().size() != 0;
	            }
	            return super.hasChildren(element);
	        }
	    };
	    
	    ObservableListTreeContentProvider contentProvider = new ObservableListTreeContentProvider(
	            factory, advisor);
	    
	    
	    IObservableMap[] maps = new IObservableMap[4];
	    
	    maps[0] = EMFProperties.value(
	    		SmartsignaturePackage.Literals.GROUP_COLLECTION__NAME).observeDetail(
	    		contentProvider.getKnownElements());
	    			    		
	    maps[1] = EMFProperties.value(
	    		FeaturePath.fromList(
	    				SmartsignaturePackage.Literals.GROUP__NAME)).observeDetail(
	            contentProvider.getKnownElements());
	    maps[2] = EMFProperties.value(
	            FeaturePath.fromList(
	            		SmartsignaturePackage.Literals.PERSON__FIRST_NAME))
	            .observeDetail(contentProvider.getKnownElements());
	    maps[3] = EMFProperties.value(
	            FeaturePath.fromList(
	            		SmartsignaturePackage.Literals.PERSON__LAST_NAME))
	            .observeDetail(contentProvider.getKnownElements());
	    
	    ObservableMapLabelProvider labelProvider = new ObservableMapLabelProvider(
	            maps) {
	 
	        public String getColumnText(Object element, int columnIndex) {
	            if (element instanceof Group) {
	                Group group = (Group) element;
	                return group.getName();
	            }
	            if (element instanceof Person) {
	            	Person person = (Person) element;
	            	return person.getFirstName() + " " + person.getLastName();
	            }
	            	
	            return super.getColumnText(element, columnIndex);
	        }
	
	    	public Image getImage(Object element) {
	    		if (element instanceof Group) { 			
	    			return ResourceManager.getPluginImage("smartapps.smartsignature.ui.groupoverview", "icons/user-group-icon.png");
	    		}
	    		return ResourceManager.getPluginImage("smartapps.smartsignature.ui.groupoverview", "icons/user-icon.png");
	    	}
	    	
	    };
	    
		treeViewer.setContentProvider(contentProvider);
	    treeViewer.setLabelProvider(labelProvider);
	    treeViewer.setInput(observableList);



That's to build my tree. If I'm correct it should be observing my groupCollection, but apparantly I'm wrong because when I add a new group in my wizard like this:

			SaveMethod save = new SaveMethod();
			save.save(group);
			openEditor();
			addToGroupCollection();
			try {
				groupCollection.eResource().save(null);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return true;

	public void addToGroupCollection() {
		ResourceSet resourceSet = new ResourceSetImpl();
		URI fileURI = URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.GroupCollections));
		Resource loadResource = resourceSet.getResource(fileURI, true);	
		List<EObject> collection = loadResource.getContents();
		groupCollection = (GroupCollection) collection.toArray()[0];
		groupCollection.getContainsGroups().add(group);
	}



I'm sorry for all the questions, I'm really trying to understand this but I keep doing something wrong.. I've only been using EMF & RCP for 2 months, and there is alot to learn & understand. I love it but it's a challenge lolz..
Re: Collecting all objects for treeviewer [message #868631 is a reply to message #868592] Wed, 02 May 2012 10:11 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
EMF-Databinding is listening to the in memory model - your code creates
a new resource instance and saves it to the file system. Why are you not
simply attaching the new object to the existing resource instead of
creating a new one all the time?

Did you take a look at the EMF-Databinding example it our source repo it
shows you have to add/remove objects. BTW your new code with the new
root-element requires you to change the observable creation to observe
the list-feature of GroupCollection.

Tom

Am 02.05.12 11:10, schrieb El Shorty:
> Yeah that's what I tought, so now I've made a parent object called
> GroupCollection, this should contain all the existing groups, whenever I
> create a new group I add it to the groupCollection object but again
> nothing changes. I'm guessing I need to adjust my code some more but
> don't know where anymore, here is what I have now:
>
>
> Composite composite = new Composite(parent, SWT.NONE);
> composite.setLayout(new GridLayout(1, false));
>
> Composite composite_1 = new Composite(composite, SWT.NONE);
> composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
> true, 1, 1));
> composite_1.setLayout(new TreeColumnLayout());
>
> treeViewer = new TreeViewer(composite_1, SWT.BORDER);
> Tree tree = treeViewer.getTree();
> tree.setHeaderVisible(true);
> tree.setLinesVisible(true);
>
> ResourceSet resourceSet = new ResourceSetImpl();
> URI fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.GroupCollections));
>
> loadResource = resourceSet.getResource(fileURI, true);
>
> observableList = EMFProperties.resource().observe(loadResource);
>
> IObservableFactory factory = new IObservableFactory() {
>
> @Override
> public IObservable createObservable(Object target) {
> if (target instanceof GroupCollection)
> {
> return EMFProperties.list(
>
> SmartsignaturePackage.Literals.GROUP_COLLECTION__CONTAINS_GROUPS).observe(target);
>
> }
> else if (target instanceof Group)
> {
> return EMFProperties.list(
>
> SmartsignaturePackage.Literals.GROUP__CONTAINS_PERSONS).observe(target);
>
> } else if (target instanceof IObservable) {
> return (IObservable) target;
> }
> return null;
> }
> };
>
> // Advisor
> TreeStructureAdvisor advisor = new TreeStructureAdvisor() {
> @Override
> public Boolean hasChildren(Object element) {
> if (element instanceof GroupCollection) {
> return ((GroupCollection)
> element).getContainsGroups().size() != 0;
> }
>
> else if (element instanceof Group) {
> return ((Group) element).getContainsPersons().size()
> != 0;
> }
> return super.hasChildren(element);
> }
> };
> ObservableListTreeContentProvider contentProvider = new
> ObservableListTreeContentProvider(
> factory, advisor);
> IObservableMap[] maps = new IObservableMap[4];
> maps[0] = EMFProperties.value(
>
> SmartsignaturePackage.Literals.GROUP_COLLECTION__NAME).observeDetail(
> contentProvider.getKnownElements());
>
> maps[1] = EMFProperties.value(
> FeaturePath.fromList(
>
> SmartsignaturePackage.Literals.GROUP__NAME)).observeDetail(
> contentProvider.getKnownElements());
> maps[2] = EMFProperties.value(
> FeaturePath.fromList(
> SmartsignaturePackage.Literals.PERSON__FIRST_NAME))
> .observeDetail(contentProvider.getKnownElements());
> maps[3] = EMFProperties.value(
> FeaturePath.fromList(
> SmartsignaturePackage.Literals.PERSON__LAST_NAME))
> .observeDetail(contentProvider.getKnownElements());
> ObservableMapLabelProvider labelProvider = new
> ObservableMapLabelProvider(
> maps) {
> public String getColumnText(Object element, int
> columnIndex) {
> if (element instanceof Group) {
> Group group = (Group) element;
> return group.getName();
> }
> if (element instanceof Person) {
> Person person = (Person) element;
> return person.getFirstName() + " " +
> person.getLastName();
> }
>
> return super.getColumnText(element, columnIndex);
> }
>
> public Image getImage(Object element) {
> if (element instanceof Group) {
> return
> ResourceManager.getPluginImage("smartapps.smartsignature.ui.groupoverview",
> "icons/user-group-icon.png");
> }
> return
> ResourceManager.getPluginImage("smartapps.smartsignature.ui.groupoverview",
> "icons/user-icon.png");
> }
>
> };
> treeViewer.setContentProvider(contentProvider);
> treeViewer.setLabelProvider(labelProvider);
> treeViewer.setInput(observableList);
>
>
>
> That's to build my tree. If I'm correct it should be observing my
> groupCollection, but apparantly I'm wrong because when I add a new group
> in my wizard like this:
>
>
> SaveMethod save = new SaveMethod();
> save.save(group);
> openEditor();
> addToGroupCollection();
> try {
> groupCollection.eResource().save(null);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> return true;
>
> public void addToGroupCollection() {
> ResourceSet resourceSet = new ResourceSetImpl();
> URI fileURI =
> URI.createFileURI(SmartSignResourceUtil.getPath(DataPart.GroupCollections));
>
> Resource loadResource = resourceSet.getResource(fileURI, true);
> List<EObject> collection = loadResource.getContents();
> groupCollection = (GroupCollection) collection.toArray()[0];
> groupCollection.getContainsGroups().add(group);
> }
>
>
>
> I'm sorry for all the questions, I'm really trying to understand this
> but I keep doing something wrong.. I've only been using EMF & RCP for 2
> months, and there is alot to learn & understand. I love it but it's a
> challenge lolz..
Re: Collecting all objects for treeviewer [message #868649 is a reply to message #868631] Wed, 02 May 2012 10:46 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
Tom Schindl wrote on Wed, 02 May 2012 06:11
EMF-Databinding is listening to the in memory model - your code creates
a new resource instance and saves it to the file system. Why are you not
simply attaching the new object to the existing resource instead of
creating a new one all the time?

Did you take a look at the EMF-Databinding example it our source repo it
shows you have to add/remove objects. BTW your new code with the new
root-element requires you to change the observable creation to observe
the list-feature of GroupCollection.

Tom




Where do I find the EMF-Databinding example? Defenatly want to see that, I used an example I found on internet to try and figure out how this works.

And about my current code, I'm not sure if I know what you mean because I tought I was doing that already here:

		IObservableFactory factory = new IObservableFactory() {
			
			@Override
			public IObservable createObservable(Object target) {
				if (target instanceof GroupCollection)
				{
					return EMFProperties.list(
							SmartsignaturePackage.Literals.GROUP_COLLECTION__CONTAINS_GROUPS).observe(target);
				}




*
I just found your example here: http://tomsondev.bestsolution.at/2009/06/06/galileo-emf-databinding-part-1/

Thing is I see you use the editingdomain, wich I'm not allowed to use yet, they want me to work around it. I'm not sure if that is what you meant but I need to find another way Smile

[Updated on: Wed, 02 May 2012 11:04]

Report message to a moderator

Re: Collecting all objects for treeviewer [message #868679 is a reply to message #868649] Wed, 02 May 2012 11:46 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Am 02.05.12 12:46, schrieb El Shorty:
> Tom Schindl wrote on Wed, 02 May 2012 06:11
>> EMF-Databinding is listening to the in memory model - your code creates
>> a new resource instance and saves it to the file system. Why are you not
>> simply attaching the new object to the existing resource instead of
>> creating a new one all the time?
>>
>> Did you take a look at the EMF-Databinding example it our source repo it
>> shows you have to add/remove objects. BTW your new code with the new
>> root-element requires you to change the observable creation to observe
>> the list-feature of GroupCollection.
>>
>> Tom
>
>
>
> Where do I find the EMF-Databinding example? Defenatly want to see that,
> I used an example I found on internet to try and figure out how this works.
> And about my current code, I'm not sure if I know what you mean because
> I tought I was doing that already here:
>

http://git.eclipse.org/c/emf/org.eclipse.emf.git/tree/examples -
explainations are on my blog at
http://tomsondev.bestsolution.at/category/databinding/emf/tutorials/

>
> IObservableFactory factory = new IObservableFactory() {
>
> @Override
> public IObservable createObservable(Object target) {
> if (target instanceof GroupCollection)
> {
> return EMFProperties.list(
>
> SmartsignaturePackage.Literals.GROUP_COLLECTION__CONTAINS_GROUPS).observe(target);
>
> }
>

You are right that looks fine.
Re: Collecting all objects for treeviewer [message #868752 is a reply to message #868679] Wed, 02 May 2012 13:46 Go to previous messageGo to next message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
I've noticed that in the tutorial in your blog you give foundation as a parameter, and if I'm not wrong that is what is being observed. But how do you initialize this parameter? Because I tried recreating your code, adjusting it to make it fit in my project but it gives me the same problem I've had before. It won't add new groups to the treeviewer.

I know my problem is that the moment I create my groupCollection, I take all existing groups and than use that object to create my tree. So the moment any of these groups change, my groupCollection gets notified and changes along. However when I create a new group it doesn't get's added to my already existing groupCollection, if I restart my application it shows. I know you've probably provided me with the answer already but I just don't see it...
Re: Collecting all objects for treeviewer [message #869750 is a reply to message #868631] Fri, 04 May 2012 08:03 Go to previous message
El Shorty is currently offline El ShortyFriend
Messages: 37
Registered: March 2012
Member
Tom Schindl wrote on Wed, 02 May 2012 06:11
EMF-Databinding is listening to the in memory model - your code creates
a new resource instance and saves it to the file system. Why are you not
simply attaching the new object to the existing resource instead of
creating a new one all the time?




Hey,

I only saw this now, how can I attach the new object to my existing resource? I mean I created my resource in my view and I create my object in my group, how can I add one to another?
Previous Topic:Texo: problem with GroupFeatureGroup
Next Topic:Operation type parameters not erased when generating eInvoke method
Goto Forum:
  


Current Time: Thu Apr 18 05:58:05 GMT 2024

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

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

Back to the top