Updated psf files to reflect new names and additional project files.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / TableElementCellModifier.java
1 /*
2  * $Id: TableElementCellModifier.java,v 1.3 2006-10-21 23:18:43 pombredanne Exp $
3  * Copyright Narushima Hironori. All rights reserved.
4  */
5 package net.sourceforge.phpeclipse.wizards.html;
6
7 import javax.xml.parsers.DocumentBuilderFactory;
8
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;
18
19 /**
20  * 
21  */
22 public class TableElementCellModifier implements ICellModifier {
23
24         DocumentBuilderFactory builderFactory;
25
26         IPropertyChangeListener listener;
27
28         public TableElementCellModifier(IPropertyChangeListener listener) {
29                 builderFactory = DocumentBuilderFactory.newInstance();
30                 this.listener = listener;
31         }
32
33         public boolean canModify(Object element, String property) {
34                 return getValue(element, property) != null;
35         }
36
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();
49                                                 }
50                                         }
51                                 }
52                         }
53                 }
54                 return null;
55         }
56
57         public void modify(Object element, String property, Object value) {
58                 if (element instanceof Item) {
59                         element = ((Item) element).getData();
60                 }
61                 Element trElem = (Element) element;
62                 int index = TableElementModel.toNumeric(property);
63                 Element cellElem = TableElementModel.chooseCellElements(trElem)[index];
64
65                 NodeList nodes = cellElem.getChildNodes();
66                 for (int i = 0; i < nodes.getLength(); i++) {
67                         cellElem.removeChild(nodes.item(i));
68                 }
69                 Document doc = cellElem.getOwnerDocument();
70
71                 if (value instanceof String) {
72                         cellElem.appendChild(doc.createTextNode((String) value));
73                 }
74                 // notify listener
75                 if (listener != null) {
76                         String oldValue = nodes.item(0).getNodeValue();
77                         listener.propertyChange(new PropertyChangeEvent(this, property,
78                                         (String) value, oldValue));
79                 }
80         }
81
82 }