Home » Eclipse Projects » Rich Client Platform (RCP) » JFace TreeViewer - Notifying Model
JFace TreeViewer - Notifying Model [message #464326] |
Fri, 02 March 2007 03:22  |
Eclipse User |
|
|
|
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 #464338 is a reply to message #464336] |
Fri, 02 March 2007 05:24   |
Eclipse User |
|
|
|
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  |
Eclipse User |
|
|
|
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();
> }
>
> }
>
|
|
|
Goto Forum:
Current Time: Sun Mar 16 00:00:50 EDT 2025
Powered by FUDForum. Page generated in 0.25379 seconds
|