2 * $Id: TableElementCellModifier.java,v 1.2 2005-05-06 00:57:33 stefanbjarni 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;
25 IPropertyChangeListener listener;
27 public TableElementCellModifier(IPropertyChangeListener listener) {
28 builderFactory = DocumentBuilderFactory.newInstance();
29 this.listener = listener;
32 public boolean canModify(Object element, String property) {
33 return getValue(element, property) != null;
36 public Object getValue(Object trElem, String property) {
37 if(trElem instanceof Element){
38 Element e = (Element)trElem;
39 if( e.getNodeName().equals("tr") ){
40 int v = TableElementModel.toNumeric(property);
41 Element[] cells = TableElementModel.chooseCellElements(e);
42 if(v >= 0 && v < cells.length){
43 NodeList nodes = cells[v].getChildNodes();
44 if(nodes.getLength() == 1){
45 Node n = nodes.item(0);
46 if(n instanceof Text){
47 return n.getNodeValue();
56 public void modify(Object element, String property, Object value) {
57 if (element instanceof Item) {
58 element = ((Item) element).getData();
60 Element trElem = (Element)element;
61 int index = TableElementModel.toNumeric(property);
62 Element cellElem = TableElementModel.chooseCellElements(trElem)[index];
64 NodeList nodes = cellElem.getChildNodes();
65 for(int i=0; i<nodes.getLength(); i++){
66 cellElem.removeChild( nodes.item(i) );
68 Document doc = cellElem.getOwnerDocument();
70 if( value instanceof String){
71 cellElem.appendChild(doc.createTextNode((String)value));
75 String oldValue = nodes.item(0).getNodeValue();
76 listener.propertyChange(new PropertyChangeEvent(this, property, (String)value, oldValue));