Added plog4u.de plog4u.org
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / TableElementCellModifier.java
1 /*
2  * $Id: TableElementCellModifier.java,v 1.1 2004-10-05 20:51:57 jsurfer 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.*;
10 import org.eclipse.jface.viewers.ICellModifier;
11 import org.eclipse.swt.widgets.Item;
12 import org.w3c.dom.*;
13
14 /**
15  * 
16  */
17 public class TableElementCellModifier implements ICellModifier {
18
19         DocumentBuilderFactory builderFactory;
20         IPropertyChangeListener listener;
21
22         public TableElementCellModifier(IPropertyChangeListener listener) {
23                 builderFactory = DocumentBuilderFactory.newInstance();
24                 this.listener = listener;
25         }
26
27         public boolean canModify(Object element, String property) {
28                 return getValue(element, property) != null;
29         }
30         
31         public Object getValue(Object trElem, String property) {
32                 if(trElem instanceof Element){
33                         Element e = (Element)trElem;
34                         if( e.getNodeName().equals("tr") ){
35                                 int v = TableElementModel.toNumeric(property);
36                                 Element[] cells = TableElementModel.chooseCellElements(e);
37                                 if(v >= 0 && v < cells.length){
38                                         NodeList nodes = cells[v].getChildNodes();
39                                         if(nodes.getLength() == 1){
40                                                 Node n = nodes.item(0);
41                                                 if(n instanceof Text){
42                                                         return n.getNodeValue();
43                                                 }
44                                         }
45                                 }
46                         }
47                 }
48                 return null;
49         }
50
51         public void modify(Object element, String property, Object value) {
52                 if (element instanceof Item) {
53                         element = ((Item) element).getData();
54                 }
55                 Element trElem =  (Element)element;
56                 int index = TableElementModel.toNumeric(property);
57                 Element cellElem = TableElementModel.chooseCellElements(trElem)[index];
58                 
59                 NodeList nodes = cellElem.getChildNodes();
60                 for(int i=0; i<nodes.getLength(); i++){
61                         cellElem.removeChild( nodes.item(i) );
62                 }
63                 Document doc = cellElem.getOwnerDocument();
64                 
65                 if( value instanceof String){
66                         cellElem.appendChild(doc.createTextNode((String)value));
67                 }
68                 // notify listener
69                 if(listener != null){
70                         String oldValue = nodes.item(0).getNodeValue();
71                         listener.propertyChange(new PropertyChangeEvent(this, property, (String)value, oldValue));
72                 }
73         }
74
75 }