Home » Eclipse Projects » Eclipse Platform » Tree with TreeColumn Editor?
| Tree with TreeColumn Editor? [message #299835] |
Sat, 25 February 2006 07:48  |
Eclipse User |
|
|
|
Originally posted by: dutz.c-ware.de
Hi,
I am trying to implement an Editor using the Tree and TreeColumn controls.
Unfortunetaly I am unable to make any column editable except colum with the
id=0.
Could you please give me a hint at how to make all columns editable?
Chris
|
|
| |
| Re: Tree with TreeColumn Editor? [message #299933 is a reply to message #299874] |
Tue, 28 February 2006 04:38  |
Eclipse User |
|
|
|
Originally posted by: dutz.c-ware.de
With hours of steping through Eclipses code I finally managesd to do what I
want. I am posting my result, hopefully this will help anyone in need :)
Don't be confused ... I am using a Dom4J Model and the wizard-page is used
for configuring Places (which contain Color-Refs, which reference Colors)
used in Colored Petrinets and this plugins attributes are stored in
"sim-qpn" named meta-attribute tags assigned to the color-refs. Just for a
litte Info
Here comes the code:
package de.tud.cs.simqpn.plugin.wiizard.page;
import java.util.List;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardPage;
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.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
import de.cware.qpe.model.DocumentManager;
import de.cware.qpe.utils.IntegerCellEditor;
public class PlacesSettingsWizardPage extends WizardPage {
protected Tree placeTree;
protected TreeViewer placeTreeViewer;
protected String[] columnNames = new String[] { "Name", "stats-level",
"signLevST", "reqAbsPrc", "reqRelPrc", "batchSizeST", "minBatches",
"numBMeansCorlTested" };
protected Element net;
/**
* Constructor for SampleNewWizardPage.
*
* @param pageName
*/
public PlacesSettingsWizardPage(ISelection selection, Element net) {
super("placeSettingsWizardPage");
setTitle("Place settings");
setDescription("Configure simulator-specific place parameters.");
this.net = net;
}
/**
* @see IDialogPage#createControl(Composite)
*/
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
container.setLayout(layout);
layout.numColumns = 3;
layout.verticalSpacing = 20;
// statsDir
Label statsDirLabel = new Label(container, SWT.NULL);
statsDirLabel.setText("Stats directory");
Text statsDirText = new Text(container, SWT.BORDER | SWT.SINGLE);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
statsDirText.setLayoutData(gd);
statsDirText.setText("stats-dir");
Button directorySelectionDialogButton = new Button(container, SWT.NULL);
directorySelectionDialogButton.setText("...");
Label placeTabelLabel = new Label(container, SWT.NULL);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 3;
placeTabelLabel.setLayoutData(gd);
placeTabelLabel.setText("Place Settings");
placeTree = new Tree(container, SWT.BORDER | SWT.H_SCROLL
| SWT.V_SCROLL);
gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 3;
placeTree.setLayoutData(gd);
placeTree.setLinesVisible(true);
placeTree.setHeaderVisible(true);
// Define the tables clumns.
for (int i = 0; i < columnNames.length; i++) {
TreeColumn column = new TreeColumn(placeTree, SWT.LEFT, i);
column.setText(columnNames[i]);
// TIP: Don't forget to set the width. If not set it is set to
// 0 and it will look as if the column didn't exist.
if (i == 0) {
column.setWidth(130);
} else {
column.setWidth(70);
}
}
// TIP: The Tree ist just a plain old treeview, if we want to
// don anything special with it, it has to be done with a
// treeviewer.
placeTreeViewer = new TreeViewer(placeTree);
placeTreeViewer.setColumnProperties(columnNames);
// The content provider provides the tree-viewer with
// the information needed (parents, children, ...)
// One thing I didn't quite understand, is the
// getElements method which seems to be redirected to
// getChildren manually ... don't know why this is done
// but I've seen it done this way everywhere.
placeTreeViewer.setContentProvider(new ITreeContentProvider() {
public Object[] getChildren(Object parentElement) {
Object[] children = null;
Element element = (Element) parentElement;
if ("net".equals(element.getName())) {
XPath xpathSelector = DocumentHelper
.createXPath("places/place");
List placeList = xpathSelector.selectNodes(element);
children = new Object[placeList.size()];
for (int i = 0; i < children.length; i++) {
children[i] = placeList.get(i);
}
} else if ("place".equals(element.getName())) {
XPath xpathSelector = DocumentHelper
.createXPath("color-refs/color-ref");
List colorRefList = xpathSelector.selectNodes(element);
children = new Object[colorRefList.size()];
for (int i = 0; i < children.length; i++) {
children[i] = colorRefList.get(i);
}
}
return children;
}
public Object getParent(Object element) {
Object parent = null;
Element childEelement = (Element) element;
if ("place".equals(childEelement.getName())) {
parent = childEelement.getParent().getParent();
} else if ("color-ref".equals(childEelement.getName())) {
parent = childEelement.getParent().getParent();
}
return parent;
}
public boolean hasChildren(Object parentElement) {
Element element = (Element) parentElement;
if ("net".equals(element.getName())) {
XPath xpathSelector = DocumentHelper
.createXPath("places/place");
return !xpathSelector.selectNodes(element).isEmpty();
} else if ("place".equals(element.getName())) {
XPath xpathSelector = DocumentHelper
.createXPath("color-refs/color-ref");
return !xpathSelector.selectNodes(element).isEmpty();
}
return false;
}
public Object[] getElements(Object inputElement) {
return getChildren(inputElement);
}
public void dispose() {
//
}
public void inputChanged(Viewer viewer, Object oldInput,
Object newInput) {
}
});
// TIP: It seems, that if the table has not defined any TreeColumns then
// a plain LabelProvider will be used. Since, we don't provide an
// instance of LabelProvider, a default one will be used and
// the TableLableProvider is ignored without notice. Took me quite
// a while to find that one out.
placeTreeViewer.setLabelProvider(new ITableLabelProvider() {
public String getColumnText(Object element, int columnIndex) {
Element castedElement = (Element) element;
if ("place".equals(castedElement.getName())) {
Element metaAttributeContainer = castedElement
.element("meta-attributes");
if (metaAttributeContainer == null) {
metaAttributeContainer = castedElement
.addElement("meta-attributes");
}
XPath xpathSelector = DocumentHelper
.createXPath("meta-attribute[@name = 'sim-qpn']");
Element metaAttribute = (Element) xpathSelector
.selectSingleNode(metaAttributeContainer);
if (metaAttribute == null) {
metaAttribute = metaAttributeContainer
.addElement("meta-attribute");
metaAttribute.addAttribute("name", "sim-qpn");
}
if (columnIndex == 0) {
return castedElement.attributeValue("label",
"unlabeled place");
} else if ((1 <= columnIndex) && (columnIndex <= 7)) {
return metaAttribute.attributeValue(
columnNames[columnIndex], "1");
}
return null;
} else if ("color-ref".equals(castedElement.getName())) {
XPath xpathSelector = DocumentHelper
.createXPath("//color[@id = '"
+ castedElement.attributeValue("color-id",
"") + "']");
Element color = (Element) xpathSelector
.selectSingleNode(castedElement);
Element metaAttributeContainer = castedElement
.element("meta-attributes");
if (metaAttributeContainer == null) {
metaAttributeContainer = castedElement
.addElement("meta-attributes");
}
xpathSelector = DocumentHelper
.createXPath("meta-attribute[@name = 'sim-qpn']");
Element metaAttribute = (Element) xpathSelector
.selectSingleNode(metaAttributeContainer);
if (metaAttribute == null) {
metaAttribute = metaAttributeContainer
.addElement("meta-attribute");
metaAttribute.addAttribute("name", "sim-qpn");
}
switch (columnIndex) {
case 0: {
return color.attributeValue("label",
"unlabeled color-ref");
}
case 1: {
return "minObsrvST:";
}
case 2: {
return metaAttribute.attributeValue("minObsrvST", "1");
}
case 3: {
return "maxObsrvST:";
}
case 4: {
return metaAttribute.attributeValue("maxObsrvST", "1");
}
}
}
return null;
}
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public void addListener(ILabelProviderListener listener) {
}
public void removeListener(ILabelProviderListener listener) {
}
public void dispose() {
}
public boolean isLabelProperty(Object element, String property) {
return true;
}
});
// A CellModifier is responsible for editing a cells
// conent. At first canModify is used to check, if
// a cell can be modified. Then getValue is used to
// get the input-object for the CellEditor. Abter
// eiting the modify method deals with updating
// the tables model.
placeTreeViewer.setCellModifier(new ICellModifier() {
public boolean canModify(Object element, String property) {
if (element instanceof Element) {
Element castedElement = (Element) element;
if ("place".equals(castedElement.getName())) {
// The name property is the only one not editalbe.
if ("Name".equals(property)) {
return false;
}
return true;
} else if ("color-ref".equals(castedElement.getName())) {
if ("signLevST".equals(property)) {
return true;
} else if ("reqRelPrc".equals(property)) {
return true;
}
}
}
return false;
}
public Object getValue(Object element, String property) {
if (element instanceof Element) {
Element castedElement = (Element) element;
XPath xpathSelector = DocumentHelper
.createXPath("meta-attributes/meta-attribute[@name = 'sim-qpn']");
Element metaAttribute = (Element) xpathSelector
.selectSingleNode(castedElement);
if ("place".equals(castedElement.getName())) {
return new Integer(metaAttribute.attributeValue(
property, "1"));
} else if ("color-ref".equals(castedElement.getName())) {
if ("signLevST".equals(property)) {
return new Integer(metaAttribute.attributeValue(
"minObsrvST", "1"));
} else if ("reqRelPrc".equals(property)) {
return new Integer(metaAttribute.attributeValue(
"maxObsrvST", "1"));
}
}
}
return null;
}
public void modify(Object element, String property, Object value) {
if (element instanceof TreeItem) {
TreeItem treeItem = (TreeItem) element;
Element castedElement = (Element) treeItem.getData();
Integer integerValue = null;
if (!(value instanceof Integer)) {
return;
}
integerValue = (Integer) value;
XPath xpathSelector = DocumentHelper
.createXPath("meta-attributes/meta-attribute[@name = 'sim-qpn']");
Element metaAttribute = (Element) xpathSelector
.selectSingleNode(castedElement);
if ("place".equals(castedElement.getName())) {
DocumentManager.setAttribute(metaAttribute, property,
integerValue.toString());
} else if ("color-ref".equals(castedElement.getName())) {
if ("signLevST".equals(property)) {
DocumentManager.setAttribute(metaAttribute,
"minObsrvST", integerValue.toString());
} else if ("reqRelPrc".equals(property)) {
DocumentManager.setAttribute(metaAttribute,
"maxObsrvST", integerValue.toString());
}
}
// After modifying the input, make the treeViewer
// update its content.
placeTreeViewer.setInput(net);
}
}
});
// Initialize the cell-editors ...
// I rely don't like the way this is done. I hope
// to find some better way one day. My favourite would
// be some kind of CellModifier Interface with a
// getCellEditor method instead. In this case there
// is no problem. But if I had to have two differen
// Editors for places and color-refs in the same column
// this wouldn't have worked.
CellEditor[] cellEditors = new CellEditor[8];
cellEditors[0] = new IntegerCellEditor(placeTreeViewer.getTree());
cellEditors[1] = cellEditors[0];
cellEditors[2] = cellEditors[0];
cellEditors[3] = cellEditors[0];
cellEditors[4] = cellEditors[0];
cellEditors[5] = cellEditors[0];
cellEditors[6] = cellEditors[0];
cellEditors[7] = cellEditors[0];
placeTreeViewer.setCellEditors(cellEditors);
placeTreeViewer.setInput(net);
dialogChanged();
setControl(container);
}
private void dialogChanged() {
setErrorMessage(null);
}
}
Hope this can help someone.
Chirs
"Grant Gayed" <grant_gayed@ca.ibm.com> schrieb im Newsbeitrag
news:dtv3j7$dbl$1@utils.eclipse.org...
> See the following examples, just change Table* to Tree throughout:
>
> http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/org. eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet88. java
> http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/org. eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet124 .java
>
> Note that in the second snippet, the line that becomes an error when you
> change from Table* to Tree* should become: int index =
> tree.indexOf(tree.getTopItem ());
>
> Grant
>
>
> "Christofer Dutz" <dutz@c-ware.de> wrote in message
> news:dtpjnh$dbk$1@utils.eclipse.org...
>> Hi,
>>
>> I am trying to implement an Editor using the Tree and TreeColumn
>> controls.
>> Unfortunetaly I am unable to make any column editable except colum with
> the
>> id=0.
>>
>> Could you please give me a hint at how to make all columns editable?
>>
>> Chris
>>
>>
>
>
|
|
|
Goto Forum:
Current Time: Sun Nov 09 23:02:02 EST 2025
Powered by FUDForum. Page generated in 0.03260 seconds
|