X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/html/TableElementModel.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/html/TableElementModel.java index 891d8f9..b9aeef8 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/html/TableElementModel.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/wizards/html/TableElementModel.java @@ -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 rows.length){ - for(int i=rows.length; i rows.length) { + for (int i = rows.length; i < rowCount; i++) { + tableElement.appendChild(createRowElement()); } - }else if(rowCount < rows.length){ - for(int i=rowCount; i colLen ){ - for(int j=0, len = newLength - colLen; j 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 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 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= 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 0){ + if (moveCount > 0) { // move down; - 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(); } }