1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import org.jaxen.JaxenException; 41 import org.mozilla.javascript.Context; 42 import org.mozilla.javascript.Function; 43 import org.mozilla.javascript.Scriptable; 44 45 import com.gargoylesoftware.htmlunit.html.HtmlElement; 46 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath; 47 import com.gargoylesoftware.htmlunit.javascript.ElementArray; 48 import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; 49 50 58 public class RowContainer extends HTMLElement { 59 60 private static final long serialVersionUID = 3258129146093056308L; 61 private ElementArray rows_; 63 66 public RowContainer() { 67 } 68 69 73 public void jsConstructor() { 74 } 75 76 80 public Object jsxGet_rows() { 81 if (rows_ == null) { 82 rows_ = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME); 83 try { 84 rows_.init(getDomNodeOrDie(), new HtmlUnitXPath(".//tr")); 85 } 86 catch (final JaxenException e) { 87 throw Context.reportRuntimeError("Failed to initialize rowContainer.rows: " + e.getMessage()); 88 } 89 } 90 return rows_; 91 } 92 93 99 public void jsxFunction_deleteRow(final int rowIndex) { 100 final ElementArray rows = (ElementArray) jsxGet_rows(); 101 final boolean rowIndexValid = (rowIndex >= 0 && rowIndex < rows.jsGet_length()); 102 if (rowIndexValid) { 103 final SimpleScriptable row = (SimpleScriptable) rows.jsFunction_item(new Integer (rowIndex)); 104 row.getDomNodeOrDie().remove(); 105 } 106 } 107 108 120 public static Object jsxFunction_insertRow( 121 final Context cx, final Scriptable s, final Object [] args, 122 final Function f) { 123 final RowContainer rowContainer = (RowContainer) s; 124 final ElementArray rows = (ElementArray) rowContainer.jsxGet_rows(); 125 final Number rowIndex; 126 if (args.length > 0) { 127 rowIndex = (Number ) args[0]; 128 } 129 else { 130 rowIndex = null; 131 } 132 final int r; 133 if (rowIndex == null || rowIndex.intValue() == -1) { 134 r = rows.jsGet_length() - 1; 135 } 136 else { 137 r = rowIndex.intValue(); 138 } 139 final boolean rowIndexValid = (r >= 0 && r <= rows.jsGet_length()); 140 if (rowIndexValid) { 141 final HtmlElement newRow = rowContainer.getDomNodeOrDie().getPage().createElement("tr"); 142 if (rows.jsGet_length() == 0 || (r == rows.jsGet_length())) { 143 rowContainer.getDomNodeOrDie().appendChild(newRow); 144 } 145 else { 146 final SimpleScriptable row = (SimpleScriptable) rows.jsFunction_item(new Integer (r)); 147 if (r == rows.jsGet_length() - 1) { 149 row.getDomNodeOrDie().getParentNode().appendChild(newRow); 150 } 151 else { 152 row.getDomNodeOrDie().insertBefore(newRow); 153 } 154 } 155 return rowContainer.getScriptableFor(newRow); 156 } 157 else { 158 throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount"); 159 } 160 } 161 162 169 public Object jsxFunction_moveRow(final int sourceIndex, final int targetIndex) { 170 final ElementArray rows = (ElementArray) jsxGet_rows(); 171 final boolean sourceIndexValid = (sourceIndex >= 0 && sourceIndex < rows.jsGet_length()); 172 final boolean targetIndexValid = (targetIndex >= 0 && targetIndex < rows.jsGet_length()); 173 if (sourceIndexValid && targetIndexValid) { 174 final SimpleScriptable sourceRow = (SimpleScriptable) rows.jsFunction_item(new Integer (sourceIndex)); 175 final SimpleScriptable targetRow = (SimpleScriptable) rows.jsFunction_item(new Integer (targetIndex)); 176 targetRow.getDomNodeOrDie().insertBefore(sourceRow.getDomNodeOrDie()); 177 return sourceRow; 178 } 179 else { 180 return null; 181 } 182 } 183 184 } 185 | Popular Tags |