2 * $Id: TableElementCellModifier.java,v 1.3 2006-10-21 23:18:43 pombredanne Exp $
3 * Copyright Narushima Hironori. All rights reserved.
5 package net.sourceforge.phpeclipse.wizards.html;
7 import javax.xml.parsers.DocumentBuilderFactory;
9 import org.eclipse.jface.util.IPropertyChangeListener;
10 import org.eclipse.jface.util.PropertyChangeEvent;
11 import org.eclipse.jface.viewers.ICellModifier;
12 import org.eclipse.swt.widgets.Item;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17 import org.w3c.dom.Text;
22 public class TableElementCellModifier implements ICellModifier {
24 DocumentBuilderFactory builderFactory;
26 IPropertyChangeListener listener;
28 public TableElementCellModifier(IPropertyChangeListener listener) {
29 builderFactory = DocumentBuilderFactory.newInstance();
30 this.listener = listener;
33 public boolean canModify(Object element, String property) {
34 return getValue(element, property) != null;
37 public Object getValue(Object trElem, String property) {
38 if (trElem instanceof Element) {
39 Element e = (Element) trElem;
40 if (e.getNodeName().equals("tr")) {
41 int v = TableElementModel.toNumeric(property);
42 Element[] cells = TableElementModel.chooseCellElements(e);
43 if (v >= 0 && v < cells.length) {
44 NodeList nodes = cells[v].getChildNodes();
45 if (nodes.getLength() == 1) {
46 Node n = nodes.item(0);
47 if (n instanceof Text) {
48 return n.getNodeValue();
57 public void modify(Object element, String property, Object value) {
58 if (element instanceof Item) {
59 element = ((Item) element).getData();
61 Element trElem = (Element) element;
62 int index = TableElementModel.toNumeric(property);
63 Element cellElem = TableElementModel.chooseCellElements(trElem)[index];
65 NodeList nodes = cellElem.getChildNodes();
66 for (int i = 0; i < nodes.getLength(); i++) {
67 cellElem.removeChild(nodes.item(i));
69 Document doc = cellElem.getOwnerDocument();
71 if (value instanceof String) {
72 cellElem.appendChild(doc.createTextNode((String) value));
75 if (listener != null) {
76 String oldValue = nodes.item(0).getNodeValue();
77 listener.propertyChange(new PropertyChangeEvent(this, property,
78 (String) value, oldValue));