Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » add Dynamic Data to ComBoboxCellEditor (ComBoboxCellEditor )
add Dynamic Data to ComBoboxCellEditor [message #925719] Fri, 28 September 2012 01:04 Go to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 4
Registered: September 2012
Junior Member
Hello All

In my createControl method, I create a TreeViewer which has many ComboBoxCellEditors. I want to set dynamic data into these boxes. Each ComboBoxCellEditor has different data. I have a ContentProvider and labelProvider classes described for this tree viewer too.


Can anyone help me to how to add different dynamic data into these ComboBoxCellEditor boxes?. Can anyone give me an example?

This will be a great help to me.

Thank You
Sam
Re: add Dynamic Data to ComBoboxCellEditor [message #926098 is a reply to message #925719] Fri, 28 September 2012 09:51 Go to previous messageGo to next message
Lorand Lorincz is currently offline Lorand LorinczFriend
Messages: 35
Registered: April 2011
Member

[Updated on: Fri, 28 September 2012 10:57]

Report message to a moderator

Re: add Dynamic Data to ComBoboxCellEditor [message #926099 is a reply to message #925719] Fri, 28 September 2012 09:52 Go to previous messageGo to next message
Lorand Lorincz is currently offline Lorand LorinczFriend
Messages: 35
Registered: April 2011
Member
Hello Sam,

Are you using a simple TreeViewer with only one column or a TreeViewer with multiple columns? I have only worked with multiple column Trees so I can only help you in that direction, but I'm sure you can adapt the multiple column code to the single column TreeViewer.
If you are using multiple columns you must add the editing support to each column. In the place where you build your TreeColumn's do the following.
After creating the TreeColumn, wrap it in a TreeViewerColumn using its constructor. Define a new org.eclipse.jface.viewers.EditingSupport. Then set the editing support using the setter from the TreeViewerColumn.

EditingSupport has 4 main methods which you have to implement like follows:

org.eclipse.jface.viewers.EditingSupport.canEdit(Object) returns true if the cell can be edited, false otherwise.

org.eclipse.jface.viewers.EditingSupport.getCellEditor(Object) returns a new ComBoboxCellEditor in your case, and sets the input of the ComboBox.

org.eclipse.jface.viewers.EditingSupport.getValue(Object), in your case will return the index of the item selected by default in your combo

org.eclipse.jface.viewers.EditingSupport.setValue(Object, Object) saves the selected value to your backend, and calls a refresh on the TreeViewer.

Hope this helps,
Lori
Re: add Dynamic Data to ComBoboxCellEditor [message #926443 is a reply to message #926099] Fri, 28 September 2012 17:01 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 4
Registered: September 2012
Junior Member
Hello Lori

Thank you very much for your reply. I have two columns - one which has a String Value and the other column - a ComBoboxCellEditor. Based on the String Value, I have to get the data which has to be filled in ComBobox of the second Column. My code is:

TreeColumn attributeColumn = new TreeColumn(attributeTree, SWT.LEFT);
attributeColumn.setText("Sources Object");
attributeColumn.setWidth(400);

TreeColumn RDNColumn = new TreeColumn(attributeTree, SWT.LEFT);
RDNColumn.setText("RDN");
RDNColumn.setWidth(140);

CheckboxCellEditor attrEditor = new CheckboxCellEditor(attributeTree);

RDNEditor = new ComboBoxViewerCellEditor(attributeTree, SWT.READ_ONLY);

/*ComboBoxEditingSupport combo = new ComboBoxEditingSupport(attributeTree);
combo.createControl(attributeTree);*/

CellEditor[] editors = new CellEditor[] { attrEditor, RDNEditor};

TreeViewerColumn RDNTreeColumn = new TreeViewerColumn(attributesTreeViewer, SWT.NONE);
RDNTreeColumn.setEditingSupport(new EditingSupport(attributesTreeViewer) {

@Override
protected boolean canEdit(Object arg0) {
return false;
}

@Override
protected CellEditor getCellEditor(Object arg0) {

return RDNEditor;
}

@Override
protected Object getValue(Object arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
protected void setValue(Object arg0, Object arg1) {
// TODO Auto-generated method stub

}

});

Now the problem is that based on the 'Sources object' TreeColumn value, I have to get the input of the COmbo Box. So, How can I send the data of the 'Sources Object' Tree Column String value into the getCellEditor() function which sets the input of my Combo Box (as per you told).

Can you please help me?

Thank you very very much for your reply.
-Sam
Re: add Dynamic Data to ComBoboxCellEditor [message #926595 is a reply to message #926443] Fri, 28 September 2012 20:09 Go to previous messageGo to next message
Lorand Lorincz is currently offline Lorand LorinczFriend
Messages: 35
Registered: April 2011
Member
Hello Sam,

If you want me to give you a functional snippet please give me the code where you create the TreeViewer, the ContentProvider and the LabelProvider.

Lori
Re: add Dynamic Data to ComBoboxCellEditor [message #926669 is a reply to message #926595] Fri, 28 September 2012 21:57 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 4
Registered: September 2012
Junior Member
No Message Body

[Updated on: Fri, 28 September 2012 22:00]

Report message to a moderator

Re: add Dynamic Data to ComBoboxCellEditor [message #926687 is a reply to message #926595] Fri, 28 September 2012 22:23 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 4
Registered: September 2012
Junior Member
Hi Lori

Here is my code:

Class A which creates the TreeViewer


public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new GridLayout(2, false));
		
		setControl(composite);
		
		Label explanationLabel = new Label(composite, SWT.NONE);
		explanationLabel
		.setText("Check the source objects that you want to publish to the new Global Profile View:");
		GridData explanationLabelData = new GridData();
		explanationLabelData.horizontalSpan = 2;
		explanationLabel.setLayoutData(explanationLabelData);

		attributesTreeViewer = new TreeViewer(composite, SWT.FULL_SELECTION
				| SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);

		// hack to set a default row height
		attributesTreeViewer.getTree().addListener(SWT.MeasureItem,
				new Listener()
		{
			@Override
			public void handleEvent(Event event)
			{
				event.height = 27;
			}
		});
		// end of hack

              attributesTreeViewer.setUseHashlookup(true);
		attributesTreeViewer.setColumnProperties(COLUMN_PROPERTIES);

		Tree attributeTree = attributesTreeViewer.getTree();
		GridData attributeTreeData = new GridData(GridData.FILL_BOTH);
		attributeTreeData.widthHint = 550;
		attributeTreeData.heightHint = 400;
		attributeTree.setLayoutData(attributeTreeData);
		attributeTree.setHeaderVisible(true);
		attributeTree.setLinesVisible(true);
		attributesTreeViewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS);

		TreeColumn attributeColumn = new TreeColumn(attributeTree, SWT.LEFT);
		attributeColumn.setText("Sources Object");
		attributeColumn.setWidth(400);
		
		TreeColumn RDNColumn = new TreeColumn(attributeTree, SWT.LEFT);
		RDNColumn.setText("RDN");
		RDNColumn.setWidth(140);

		CheckboxCellEditor attrEditor = new CheckboxCellEditor(attributeTree);
		
		RDNEditor = new ComboBoxViewerCellEditor(attributeTree, SWT.READ_ONLY);

		CellEditor[] editors = new CellEditor[] { attrEditor, RDNEditor};
		
		initList();  //This function generates the dynamic data and is
                             // stored in a Hash Map

                attributesTreeViewer.setCellEditors(editors);
		attributesTreeViewer
		.setCellModifier(new AttributesViewerCellModifier());

		attributesTreeViewer
		.setContentProvider(new AttributesTreeViewerContentProvider());
		attributesTreeViewer
		.setLabelProvider(new AttributesTreeViewerLabelProvider());

		
		RDNEditor.setContenProvider(new AttributesTreeViewerContentProvider());//ArrayContentProvider());
		RDNEditor.setLabelProvider(new LabelProvider());



Content Provider Class


static class AttributesTreeViewerContentProvider implements
	ITreeContentProvider
	{
		@Override
		public Object[] getChildren(Object arg0)
		{
			SourceNode node = (SourceNode) arg0;
			return node.getChildren().toArray();
		}

		@Override
		public Object getParent(Object arg0)
		{
			SourceNode node = (SourceNode) arg0;
			return node.getParent();
		}

		@Override
		public boolean hasChildren(Object arg0)
		{
			SourceNode node = (SourceNode) arg0;
			return node.hasChildren();
		}

		@Override
		public Object[] getElements(Object arg0)
		{
			return getChildren(arg0);
		}

		@Override
		public void dispose()
		{
		}

		@Override
		public void inputChanged(Viewer arg0, Object arg1, Object arg2)
		{
		}
		
		public Object[] getValue(Object arg0)
		{
			SourceNode node = (SourceNode) arg0;
			return node.getRDNValues();
		}
	}



Label Provider Class


static class AttributesTreeViewerLabelProvider extends LabelProvider
	implements ITableLabelProvider
	{

		@Override
		public Image getColumnImage(Object arg0, int index)
		{
			SourceNode node = (SourceNode) arg0;
			if (node.isRootNode() && index == 0)
			{
				if (node.isSelected())
					return ImageStore.getImage(ImageStore.CHECK);
				else
					return ImageStore.getImage(ImageStore.NOCHECK);
			}

			return null;
		}

		@Override
		public String getColumnText(Object arg0, int index)
		{
			SourceNode node = (SourceNode) arg0;
			if (index == 0)
			{
				if (node.isRootNode()) {
					return node.getSourceObjectName();
				}
				else
				{
					String dn = node.getSourceDn();
					return VdsHelperUtility.extractObjectName(dn) + " ("
					+ VdsHelperUtility.extractParentName(dn) + ")";
				}
			}
			
			if (!node.isRootNode() && index == 1)
			{
				String rdnVal = node.getSelectedRDNValue();
				return rdnVal;
			}
			
			return null;
		}
	}



Another Class which is a CellModifier class implementing ICellModifier


class AttributesViewerCellModifier implements ICellModifier
	{

		@Override
		public boolean canModify(Object element, String property)
		{
			if (element instanceof SourceNode)
			{
				SourceNode node = (SourceNode) element;
				if (node.isRootNode())
					return (property.equals("Sources Object"));	
				else
					return (property.equals("RDN"));
			}
			return false;

		}

		@Override
		public Object getValue(Object element, String property)
		{
			if (element instanceof SourceNode)
			{
				SourceNode node = (SourceNode) element;	
				if(property.equals("RDN")) {
					if(node.isRootNode())
						return null;
					else {
						String rdn = node.getSelectedRDNValue();
						return rdn;
					}
				}
				else
				return node.isSelected();
			}
			return null;
		}

		@Override
		public void modify(Object element, String property, Object value)
		{
			TreeItem treeItem = (TreeItem) element;
			
			if(property.equals("RDN")) {
				SourceNode node = (SourceNode) treeItem.getData();
				if (value instanceof String)
				{
					String valueStr = (String) value;
				    if ((valueStr != null) || (valueStr != ""))
				    {
					node.setSelectedRDNValue(valueStr);
					attributesTreeViewer.refresh();
				    }
				}
			}

			if (property.equals("Sources Object"))
			{
				SourceNode node = (SourceNode) treeItem.getData();
				if (node.isRootNode())
				{
					node.setSelected(!node.isSelected());
					attributesTreeViewer.refresh();
				}
				validatePage();
			}
		}
	}



I need to take the "Sources Object" string value and based on it get the String array from the hashmap (which is derived in initList function) and then add the array into the Combo Box.

SourceNode is the object class. extractParentName() and extractObjectName() are some additional functions which parses the dn.

Thank you very muc again Lori. very much Appreciated.

-Sam
Re: add Dynamic Data to ComBoboxCellEditor [message #927088 is a reply to message #926687] Sat, 29 September 2012 08:36 Go to previous message
Lorand Lorincz is currently offline Lorand LorinczFriend
Messages: 35
Registered: April 2011
Member
OK, I got the problem. This is a perfect example of why EditingSupport is better then using CellModifiers. Because you use CellModifiers you initialize the CellEditors when you build your Tree.
Using Editing Support you get this new method org.eclipse.jface.viewers.EditingSupport.getCellEditor(Object). This method allows you to hook in and build you CellEditor when the cell is clicked. The element parameter is the element that is returned by the content provider for the edited cell.
Instead of initializing your cellEditors and CellModifiers in the createControl(Composite parent) method you should only define an EditingSupport for each TreeColumnViewer. For example, for the second TreeColumnViewer the Editing suport should look something like this:
attributeColumnViewer.setEditingSupport(new EditingSupport(){
     CellEditor getCellEditor(Object element){
          CheckboxCellEditor cellEditor = new CheckboxCellEditor(attributeTree);
          String[] arr = map.get(element.getObjectName());
          cellEditor.setInput(arr);
          return cellEditor;
          }
     boolean canEdit(Object element){
           //the code from your canModify method of the cell modifier
          }
     Object getValue(Object element){
          // the code from getValue method of the cell modifier
          }
     void setValue(Object element){
          //the code from modify method of the cell modifier
          }
});


Practically the EditingSupport replaces what you have done with cell modifiers.
Don't forget, you have to define an EditingSupport for each TreeColumnViewer.

Regards,
Lori

[Updated on: Sat, 29 September 2012 08:37]

Report message to a moderator

Previous Topic:[DataBinding] Xtext EMF EDB Implementation, help needed with List & observeDetails()
Next Topic:JFace TreeViewer refresh problem
Goto Forum:
  


Current Time: Wed Nov 26 09:24:54 GMT 2014

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

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