Skip to main content



      Home
Home » Eclipse Projects » Rich Client Platform (RCP) » JFace TreeViewer - Notifying Model
JFace TreeViewer - Notifying Model [message #464326] Fri, 02 March 2007 03:22 Go to next message
Eclipse UserFriend
Hi All,

I have a tree viewer which is constructed using Customer Model
object...And I have defined one action where I can edit the tree item in
the tree viewer(i.e, I am changing the model)...

How can I notify the Model in response to the changes in UI...Please
give me an idea

Thanks in advance,
Swetha
Re: JFace TreeViewer - Notifying Model [message #464332 is a reply to message #464326] Fri, 02 March 2007 04:24 Go to previous messageGo to next message
Eclipse UserFriend
Swetha schrieb:
> Hi All,
>
> I have a tree viewer which is constructed using Customer Model
> object...And I have defined one action where I can edit the tree item in
> the tree viewer(i.e, I am changing the model)...

Maybe I don't get you right but:

If you edit in the TreeViewer (adding,removing,changing items) you are
really changing the model and you don't need to inform the model because
it is the model you changed.

The other way round is the tricky part when the underlying model changes
because of any action you need to update the Viewer.

here you have to distinguish 2 change types:
- structural changes(adding,removing if element): TreeViewer#refresh()
- attribute value changes(e.g. surname attribute changed):
TreeViewer#update()

If my assumption is not right please show some example code.

Tom
Re: JFace TreeViewer - Notifying Model [message #464334 is a reply to message #464332] Fri, 02 March 2007 04:57 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

May be I am confused... Let me ask once again..

Suppose I have changed 5th node(Which is actually Customer Object) text
from node5 to treeNode5 i.e, Customer Object is changed in UI. Where I
need to update the customer object..

For Model changes , we have refresh() method and update() method to
update the UI.

For UI changes where to update the model...

Please clarify this.. May be my understanding is incorrect..

Thanks,
Swetha
Re: JFace TreeViewer - Notifying Model [message #464336 is a reply to message #464334] Fri, 02 March 2007 05:04 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

How do you change the text show me the code you execute to change text
displayed in your Tree! The normal way of doing this would be using
CellEditor and in ICellModifier#setValue(Object,Object,String) setting
the value back to your model object and calling TreeViewer#update(*).

Tom

Swetha schrieb:
> Hi,
>
> May be I am confused... Let me ask once again..
>
> Suppose I have changed 5th node(Which is actually Customer Object) text
> from node5 to treeNode5 i.e, Customer Object is changed in UI. Where I
> need to update the customer object..
>
> For Model changes , we have refresh() method and update() method to
> update the UI.
>
> For UI changes where to update the model...
>
> Please clarify this.. May be my understanding is incorrect..
>
> Thanks,
> Swetha
>
>
>
>
>
>
>
Re: JFace TreeViewer - Notifying Model [message #464338 is a reply to message #464336] Fri, 02 March 2007 05:24 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

The following is the method for editing tree item text.....In a mouse
click event of tree i called this method for editing tree item...

public void handleEdit(final TreeItem item)
{
final Color black =
getSite().getShell().getDisplay().getSystemColor(
SWT.COLOR_BLACK);
final TreeEditor editor = new TreeEditor(treeViewer.getTree());
if (item != null)
{
boolean isCarbon = SWT.getPlatform().equals("carbon");

composite = new CompositetreeViewer.getTree(), SWT.NONE);
if (!isCarbon)
composite.setBackground(black);
final Text text = new Text(composite, SWT.NONE);
final int inset = isCarbon ? 0 : 1;
composite.addListener(SWT.Resize, new Listener()
{
public void handleEvent(Event e)
{
Rectangle rect = composite.getClientArea();
text.setBounds(rect.x + inset, rect.y + inset,
rect.width
- inset * 2, rect.height - inset * 2);
}
});
Listener textListener = new Listener()
{
public void handleEvent(final Event e)
{
switch (e.type)
{
case SWT.FocusOut:
item.setText(text.getText());
// System.out.println("Have to handle the
// database insert operation");
composite.dispose();
break;
case SWT.Verify:
String newText = text.getText();
String leftText = newText.substring(0,
e.start);
String rightText = newText.substring(e.end,
newText
.length());
GC gc = new GC(text);
Point size = gc.textExtent(leftText + e.text
+ rightText);
gc.dispose();
size = text.computeSize(size.x, SWT.DEFAULT);
editor.horizontalAlignment = SWT.LEFT;
Rectangle itemRect = item.getBounds(),
rect = GlobalVariablesClass.treeCopy
.getClientArea();
editor.minimumWidth = Math.max(size.x,
itemRect.width)
+ inset * 2;
int left = itemRect.x,
right = rect.x + rect.width;
editor.minimumWidth =
Math.min(editor.minimumWidth,
right - left);
editor.minimumHeight = size.y + inset * 2;
editor.layout();
break;
case SWT.Traverse:
switch (e.detail)
{
case SWT.TRAVERSE_RETURN:
item.setText(text.getText());
// FALL THROUGH
case SWT.TRAVERSE_ESCAPE:
composite.dispose();
e.doit = false;
}
break;
}
}
};
text.addListener(SWT.FocusOut, textListener);
text.addListener(SWT.Traverse, textListener);

text.addListener(SWT.Verify, textListener);
editor.setEditor(composite, item);
text.setText(item.getText());
text.selectAll();
text.setFocus();
}

}
Re: JFace TreeViewer - Notifying Model [message #464404 is a reply to message #464338] Fri, 02 March 2007 06:16 Go to previous message
Eclipse UserFriend
Well you are doing all the editing stuff your own and completely using
TreeViewer wrong because you are never supposed to call things like:

- item.setText(text.getText()),
- text.setText(item.getText());
- ...

In fact you should use the CellEditors provided by JFace who are
designed to leave all this code out and provide you the infrastructure
to editing items in a Viewer.

Although you could do everything your own as you do it now it's error
prone, ... and I'd suggest that you adopt the standard way of doing things.

Examples how to use 3.2-API are:
-http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface. snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippe ts/viewers/Snippet009CellEditors.java?revision=1.1&view= markup

Example how to use the 3.3-API are:
-http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface. snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippe ts/viewers/Snippet024TableViewerExploreNewAPI.java?view=mark up

if you want to use your current code you get access to the model object
by calling item.getData().

Tom

Swetha schrieb:
> Hi,
>
> The following is the method for editing tree item text.....In a mouse
> click event of tree i called this method for editing tree item...
>
> public void handleEdit(final TreeItem item)
> {
> final Color black =
> getSite().getShell().getDisplay().getSystemColor(
> SWT.COLOR_BLACK);
> final TreeEditor editor = new TreeEditor(treeViewer.getTree());
> if (item != null)
> {
> boolean isCarbon = SWT.getPlatform().equals("carbon");
> composite = new CompositetreeViewer.getTree(),
> SWT.NONE);
> if (!isCarbon)
> composite.setBackground(black);
> final Text text = new Text(composite, SWT.NONE);
> final int inset = isCarbon ? 0 : 1;
> composite.addListener(SWT.Resize, new Listener()
> {
> public void handleEvent(Event e)
> {
> Rectangle rect = composite.getClientArea();
> text.setBounds(rect.x + inset, rect.y + inset,
> rect.width
> - inset * 2, rect.height - inset * 2);
> }
> });
> Listener textListener = new Listener()
> {
> public void handleEvent(final Event e)
> {
> switch (e.type)
> {
> case SWT.FocusOut:
> item.setText(text.getText());
> // System.out.println("Have to handle the
> // database insert operation");
> composite.dispose();
> break;
> case SWT.Verify:
> String newText = text.getText();
> String leftText = newText.substring(0, e.start);
> String rightText = newText.substring(e.end,
> newText
> .length());
> GC gc = new GC(text);
> Point size = gc.textExtent(leftText + e.text
> + rightText);
> gc.dispose();
> size = text.computeSize(size.x, SWT.DEFAULT);
> editor.horizontalAlignment = SWT.LEFT;
> Rectangle itemRect = item.getBounds(),
> rect = GlobalVariablesClass.treeCopy
> .getClientArea();
> editor.minimumWidth = Math.max(size.x,
> itemRect.width)
> + inset * 2;
> int left = itemRect.x,
> right = rect.x + rect.width;
> editor.minimumWidth =
> Math.min(editor.minimumWidth,
> right - left);
> editor.minimumHeight = size.y + inset * 2;
> editor.layout();
> break;
> case SWT.Traverse:
> switch (e.detail)
> {
> case SWT.TRAVERSE_RETURN:
> item.setText(text.getText());
> // FALL THROUGH
> case SWT.TRAVERSE_ESCAPE:
> composite.dispose();
> e.doit = false;
> }
> break;
> }
> }
> };
> text.addListener(SWT.FocusOut, textListener);
> text.addListener(SWT.Traverse, textListener);
>
> text.addListener(SWT.Verify, textListener);
> editor.setEditor(composite, item);
> text.setText(item.getText());
> text.selectAll();
> text.setFocus();
> }
>
> }
>
Previous Topic:Idocumentprovider
Next Topic:Launching a progress Dialog from wizard
Goto Forum:
  


Current Time: Sun Mar 16 00:00:50 EDT 2025

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

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

Back to the top