Make PHPeclipse compile correctly under Eclipse Version: 3.2.0 Build id: I20060519...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / TableElementCellModifier.java
1 /*
2  * $Id: TableElementCellModifier.java,v 1.2 2005-05-06 00:57:33 stefanbjarni 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         IPropertyChangeListener listener;
26
27         public TableElementCellModifier(IPropertyChangeListener listener) {
28                 builderFactory = DocumentBuilderFactory.newInstance();
29                 this.listener = listener;
30         }
31
32         public boolean canModify(Object element, String property) {
33                 return getValue(element, property) != null;
34         }
35         
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();
48                                                 }
49                                         }
50                                 }
51                         }
52                 }
53                 return null;
54         }
55
56         public void modify(Object element, String property, Object value) {
57                 if (element instanceof Item) {
58                         element = ((Item) element).getData();
59                 }
60                 Element trElem =  (Element)element;
61                 int index = TableElementModel.toNumeric(property);
62                 Element cellElem = TableElementModel.chooseCellElements(trElem)[index];
63                 
64                 NodeList nodes = cellElem.getChildNodes();
65                 for(int i=0; i<nodes.getLength(); i++){
66                         cellElem.removeChild( nodes.item(i) );
67                 }
68                 Document doc = cellElem.getOwnerDocument();
69                 
70                 if( value instanceof String){
71                         cellElem.appendChild(doc.createTextNode((String)value));
72                 }
73                 // notify listener
74                 if(listener != null){
75                         String oldValue = nodes.item(0).getNodeValue();
76                         listener.propertyChange(new PropertyChangeEvent(this, property, (String)value, oldValue));
77                 }
78         }
79
80 }