Templates were updated by pteague. Also a little change by ed_mann.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / TableElementModel.java
index 891d8f9..b9aeef8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: TableElementModel.java,v 1.1 2004-10-05 20:51:57 jsurfer Exp $
+ * $Id: TableElementModel.java,v 1.2 2006-10-21 23:18:43 pombredanne Exp $
  * Copyright Narushima Hironori. All rights reserved.
  */
 package net.sourceforge.phpeclipse.wizards.html;
@@ -29,59 +29,70 @@ public class TableElementModel {
        final static char[] CHAR_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
 
        StringDivider stringDivider = new StringDivider();
-       
+
        ElementWriter writer;
 
        DocumentBuilder docBuilder;
+
        Document document;
+
        Element tableElement;
+
        String[] columnProperties;
 
-       public TableElementModel(String content, boolean parse) throws FactoryConfigurationError, ParserConfigurationException, SAXException, IOException{
+       public TableElementModel(String content, boolean parse)
+                       throws FactoryConfigurationError, ParserConfigurationException,
+                       SAXException, IOException {
                docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-               if(parse){
+               if (parse) {
                        initAsParse(content);
-               }else{
+               } else {
                        initModel(content);
                }
                columnProperties = createColumnProperties();
-               
+
                // create elementWriter
                writer = new ElementWriter(0, null);
-               writer.setExpandOption("caption", ElementWriter.END_CHANGELINE );
-               writer.setExpandOption("table", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE);
-               writer.setExpandOption("thead", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE);
-               writer.setExpandOption("tfoot", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE);
-               writer.setExpandOption("tbody", ElementWriter.BEGIN_CHANGELINE | ElementWriter.END_CHANGELINE);
+               writer.setExpandOption("caption", ElementWriter.END_CHANGELINE);
+               writer.setExpandOption("table", ElementWriter.BEGIN_CHANGELINE
+                               | ElementWriter.END_CHANGELINE);
+               writer.setExpandOption("thead", ElementWriter.BEGIN_CHANGELINE
+                               | ElementWriter.END_CHANGELINE);
+               writer.setExpandOption("tfoot", ElementWriter.BEGIN_CHANGELINE
+                               | ElementWriter.END_CHANGELINE);
+               writer.setExpandOption("tbody", ElementWriter.BEGIN_CHANGELINE
+                               | ElementWriter.END_CHANGELINE);
                writer.setExpandOption("tr", ElementWriter.END_CHANGELINE);
        }
-       
-       void initModel(String content) throws ParserConfigurationException, SAXException, IOException {
+
+       void initModel(String content) throws ParserConfigurationException,
+                       SAXException, IOException {
                StringReader strReader = new StringReader(content);
                InputSource inputSrc = new InputSource(strReader);
-               
+
                document = docBuilder.parse(inputSrc);
                tableElement = document.getDocumentElement();
-               
+
                Element[] rows = getRows();
                for (int i = 0; i < rows.length; i++) {
                        Element[] cells = chooseCellElements(rows[i]);
                        for (int j = 0; j < cells.length; j++) {
                                Element cell = cells[j];
-                               if( !cell.hasChildNodes() ){
+                               if (!cell.hasChildNodes()) {
                                        cell.appendChild(document.createTextNode(""));
                                }
                        }
                }
        }
 
-       public void initAsParse(String content) throws ParserConfigurationException, FactoryConfigurationError {
+       public void initAsParse(String content)
+                       throws ParserConfigurationException, FactoryConfigurationError {
                // create new table model.
                document = docBuilder.newDocument();
                tableElement = document.createElement("table");
-               
+
                String[][] cells = stringDivider.divide(content);
-               if(cells.length > 0){
+               if (cells.length > 0) {
                        for (int i = 0; i < cells.length; i++) {
                                String[] rows = cells[i];
                                Element tr = document.createElement("tr");
@@ -92,144 +103,143 @@ public class TableElementModel {
                                }
                                tableElement.appendChild(tr);
                        }
-                       
+
                        setColumnCount(cells[0].length);
-               }else{
+               } else {
                        Element tr = document.createElement("tr");
                        Element td = document.createElement("td");
                        td.appendChild(document.createTextNode(""));
                        tr.appendChild(td);
                        tableElement.appendChild(tr);
-                       
+
                        setColumnCount(1);
                }
        }
 
-       String[] createColumnProperties(){
+       String[] createColumnProperties() {
                int len = getColumnCount();
                String[] props = new String[len];
-               for(int i=0; i<len; i++){
+               for (int i = 0; i < len; i++) {
                        props[i] = toColumnName(i);
                }
                return props;
        }
 
-       public void setRowCount(int rowCount){
+       public void setRowCount(int rowCount) {
                Element[] rows = getRows();
-               if(rowCount > rows.length){
-                       for(int i=rows.length; i<rowCount; i++){
-                               tableElement.appendChild( createRowElement());
+               if (rowCount > rows.length) {
+                       for (int i = rows.length; i < rowCount; i++) {
+                               tableElement.appendChild(createRowElement());
                        }
-               }else if(rowCount < rows.length){
-                       for(int i=rowCount; i<rows.length; i++){
+               } else if (rowCount < rows.length) {
+                       for (int i = rowCount; i < rows.length; i++) {
                                tableElement.removeChild(rows[i]);
                        }
                }
        }
-       
-       public Element[] getRows(){
+
+       public Element[] getRows() {
                ArrayList rows = new ArrayList();
                NodeList nodes = tableElement.getElementsByTagName("tr");
-               for(int i=0; i<nodes.getLength(); i++){
-                       rows.add( nodes.item(i) );
+               for (int i = 0; i < nodes.getLength(); i++) {
+                       rows.add(nodes.item(i));
                }
-               return (Element[])rows.toArray(new Element[rows.size()]);
+               return (Element[]) rows.toArray(new Element[rows.size()]);
        }
-       
-       public int getRowCount(){
+
+       public int getRowCount() {
                return getRows().length;
        }
-       
-       Element createRowElement(){
+
+       Element createRowElement() {
                Element tr = document.createElement("tr");
-               for(int i=0, columnCount = getColumnCount(); i<columnCount; i++){
+               for (int i = 0, columnCount = getColumnCount(); i < columnCount; i++) {
                        Element td = document.createElement("td");
                        td.appendChild(document.createTextNode(""));
                        tr.appendChild(td);
                }
                return tr;
        }
-       
-       public void setColumnCount(int newLength){
+
+       public void setColumnCount(int newLength) {
                NodeList trs = tableElement.getElementsByTagName("tr");
-               for(int i=0; i<trs.getLength(); i++){
-                       Element tr = (Element)trs.item(i);
+               for (int i = 0; i < trs.getLength(); i++) {
+                       Element tr = (Element) trs.item(i);
                        Element[] cells = chooseCellElements(tr);
                        int colLen = cells.length;
-                       
-                       if( newLength > colLen ){
-                               for(int j=0, len = newLength - colLen; j<len; j++){
+
+                       if (newLength > colLen) {
+                               for (int j = 0, len = newLength - colLen; j < len; j++) {
                                        Element cell = document.createElement("td");
                                        cell.appendChild(document.createTextNode(""));
                                        tr.appendChild(cell);
                                }
-                       }else if( newLength < colLen ){
-                               for(int j=newLength; j<colLen; j++){
-                                       tr.removeChild( cells[j]);
+                       } else if (newLength < colLen) {
+                               for (int j = newLength; j < colLen; j++) {
+                                       tr.removeChild(cells[j]);
                                }
                        }
                }
                columnProperties = createColumnProperties();
        }
-       
-       public int getColumnCount(){
+
+       public int getColumnCount() {
                NodeList trs = tableElement.getElementsByTagName("tr");
-               if( trs.getLength() > 0){
-                       Element tr = (Element)trs.item(0);
+               if (trs.getLength() > 0) {
+                       Element tr = (Element) trs.item(0);
                        return chooseCellElements(tr).length;
-               }else{
+               } else {
                        return 0;
                }
        }
-       
-       public static Element[] chooseCellElements(Element tr){
+
+       public static Element[] chooseCellElements(Element tr) {
                NodeList nodeList = tr.getChildNodes();
-               
+
                ArrayList result = new ArrayList();
-               for(int i=0; i<nodeList.getLength(); i++){
+               for (int i = 0; i < nodeList.getLength(); i++) {
                        Node node = nodeList.item(i);
-                       if(node instanceof Element){
+                       if (node instanceof Element) {
                                String nodeName = node.getNodeName();
-                               if(nodeName.equals("td") || nodeName.equals("th") ){
+                               if (nodeName.equals("td") || nodeName.equals("th")) {
                                        result.add(node);
                                }
                        }
                }
-               
-               return (Element[])result.toArray(new Element[result.size()]);
+
+               return (Element[]) result.toArray(new Element[result.size()]);
        }
 
-       public String expandCodes(){
+       public String expandCodes() {
                return writer.expandTag(tableElement);
        }
 
-
-       public static String toColumnName(int i){
+       public static String toColumnName(int i) {
                StringBuffer buff = new StringBuffer();
                int u = i / CHAR_TABLE.length;
-               if( u > 0){
-                       buff.append(CHAR_TABLE[u-1]);
+               if (u > 0) {
+                       buff.append(CHAR_TABLE[u - 1]);
                }
-               buff.append( CHAR_TABLE[i % CHAR_TABLE.length] );
+               buff.append(CHAR_TABLE[i % CHAR_TABLE.length]);
                return buff.toString();
        }
-       
+
        /**
         * Return index of char map. If can not parse values return -1.
-        */     
-       public static int toNumeric(String code){
+        */
+       public static int toNumeric(String code) {
                int result = -1;
-               for(int i=0; i<code.length(); i++){
+               for (int i = 0; i < code.length(); i++) {
                        char c = code.charAt(i);
                        int match = Arrays.binarySearch(CHAR_TABLE, c);
-                       if( match >= 0){
-                               if(result == -1){
+                       if (match >= 0) {
+                               if (result == -1) {
                                        result = 0;
                                }
                                int v = match;
-                               int u = code.length()-1-i;
-                               if(u>0){
-                                       v = CHAR_TABLE.length * u * (v+1);
+                               int u = code.length() - 1 - i;
+                               if (u > 0) {
+                                       v = CHAR_TABLE.length * u * (v + 1);
                                }
                                result += v;
                        }
@@ -237,47 +247,48 @@ public class TableElementModel {
                return result;
        }
 
-       public void move(Element tr, int moveCount){
+       public void move(Element tr, int moveCount) {
                Element[] rows = getRows();
                int index = -1;
-               for(int i=0;i<rows.length; i++){
-                       if(tr.equals(rows[i])){
+               for (int i = 0; i < rows.length; i++) {
+                       if (tr.equals(rows[i])) {
                                index = i;
                        }
                }
-               if(index == -1){
-                       throw new IllegalArgumentException("Invalid row node (not countained in this table):" + tr);
+               if (index == -1) {
+                       throw new IllegalArgumentException(
+                                       "Invalid row node (not countained in this table):" + tr);
                }
-               if(moveCount > 0){
+               if (moveCount > 0) {
                        // move down;
-                       for(int i=index; i<moveCount+index && i<rows.length-1; i++){
-                               tableElement.insertBefore(rows[i+1], rows[i]);
+                       for (int i = index; i < moveCount + index && i < rows.length - 1; i++) {
+                               tableElement.insertBefore(rows[i + 1], rows[i]);
                        }
-               }else if(moveCount < 0){
+               } else if (moveCount < 0) {
                        // move up
-                       for(int i=index; i>=moveCount+index+1 && i >= 1; i--){
-                               tableElement.insertBefore(rows[index], rows[i-1]);
+                       for (int i = index; i >= moveCount + index + 1 && i >= 1; i--) {
+                               tableElement.insertBefore(rows[index], rows[i - 1]);
                        }
-               }else{
+               } else {
                        return;
                }
        }
-       
-       public void insertNewRowBefore(Element tr){
+
+       public void insertNewRowBefore(Element tr) {
                Element newRow = createRowElement();
-               if( tr == null){
+               if (tr == null) {
                        tableElement.appendChild(newRow);
-               }else{
+               } else {
                        tableElement.insertBefore(newRow, tr);
                }
        }
-       
-       public void removeRow(Element tr){
+
+       public void removeRow(Element tr) {
                tableElement.removeChild(tr);
        }
 
        public String[] getColumnProperties() {
-               return (String[])columnProperties.clone();
+               return (String[]) columnProperties.clone();
        }
 
 }