001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core.domutil;
015:
016: import org.itsnat.core.ItsNatException;
017: import org.itsnat.impl.core.ItsNatDocumentImpl;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.ListIterator;
022: import org.itsnat.core.domutil.ElementListFree;
023: import org.itsnat.core.domutil.ElementTableFree;
024: import org.itsnat.impl.core.util.*;
025: import org.w3c.dom.Element;
026:
027: /**
028: *
029: * @author jmarranz
030: */
031: public abstract class ElementTableFreeImpl extends ElementTableBaseImpl
032: implements ElementTableFree, ElementListFreeInternal {
033:
034: /**
035: * Creates a new instance of ElementTableBaseImpl
036: */
037: public ElementTableFreeImpl(ItsNatDocumentImpl itsNatDoc,
038: boolean master, Element parentElement) {
039: super (itsNatDoc, parentElement);
040:
041: this .rows = (ElementListFreeImpl) itsNatDoc
042: .createElementListFree(parentElement, master);
043: }
044:
045: public ElementListFreeImpl getRowsAsElementListFree() {
046: return (ElementListFreeImpl) rows;
047: }
048:
049: public ElementListFreeImpl getRows() {
050: return (ElementListFreeImpl) rows;
051: }
052:
053: public void addRow(Element rowElem) {
054: addRow2(rowElem);
055: }
056:
057: public Element addRow2(Element rowElem) {
058: // Se deriva
059: rowElem = getRowsAsElementListFree().addElement2(rowElem);
060:
061: return rowElem;
062: }
063:
064: public void insertRowAt(int row, Element rowElem) {
065: insertRowAt2(row, rowElem);
066: }
067:
068: public Element insertRowAt2(int row, Element rowElem) {
069: // Se deriva
070: rowElem = getRowsAsElementListFree().insertElementAt2(row,
071: rowElem);
072:
073: return rowElem;
074: }
075:
076: public Element setRowAt(int row, Element rowElem) {
077: return setRowAt2(row, rowElem).getOldElem();
078: }
079:
080: public ElementPair setRowAt2(int row, Element rowElem) {
081: // Se deriva
082: ElementPair res = getRowsAsElementListFree().setElementAt2(row,
083: rowElem);
084:
085: return res;
086: }
087:
088: public Element[] setCellElementsOfRow(int row, Element[] cells) {
089: Element rowElem = getRowElementAt(row); // Si devuelve null es que está fuera del rango
090: if (rowElem == null)
091: throw new ItsNatException("Row is out of range: " + row);
092: ElementListFreeImpl columns = (ElementListFreeImpl) getColumnsOfRowElementList(
093: row, rowElem);
094: return columns.setElements(cells);
095: }
096:
097: public ElementListBaseImpl newColumnsOfRowElementList(int row,
098: Element rowElem) {
099: // row no se necesita
100: return (ElementListFreeImpl) itsNatDoc.createElementListFree(
101: rowElem, isMaster());
102: }
103:
104: public void addColumn(Element[] cells) {
105: ElementListFreeImpl rows = getRowsAsElementListFree();
106: if (rows.isEmpty())
107: return; // Nada que hacer
108:
109: // Si la longitud de cells es menor que el número de filas dará error
110: int row = 0;
111: for (Iterator it = rows.iterator(); it.hasNext();) {
112: Element rowElem = (Element) it.next();
113:
114: ElementListFreeImpl columns = (ElementListFreeImpl) getColumnsOfRowElementList(
115: row, rowElem);
116: cells[row] = columns.addElement2(cells[row]);
117:
118: row++;
119: }
120: }
121:
122: public void insertColumnAt(int column, Element[] cells) {
123: ElementListFreeImpl rows = getRowsAsElementListFree();
124: if (rows.isEmpty())
125: return; // Nada que hacer
126:
127: // Si la longitud de cells es menor que el número de filas dará error
128: int row = 0;
129: for (Iterator it = rows.iterator(); it.hasNext();) {
130: Element rowElem = (Element) it.next();
131:
132: ElementListFreeImpl columns = (ElementListFreeImpl) getColumnsOfRowElementList(
133: row, rowElem);
134: cells[row] = columns.insertElementAt2(column, cells[row]);
135:
136: row++;
137: }
138: }
139:
140: public Element[] setCellElementsOfColumn(int column, Element[] cells) {
141: ElementListFreeImpl rows = getRowsAsElementListFree();
142: if (rows.isEmpty())
143: return new Element[0]; // Nada que hacer
144:
145: Element[] replaced = new Element[cells.length];
146:
147: // Si la longitud de cells es menor que el número de filas dará error
148: int row = 0;
149: for (Iterator it = rows.iterator(); it.hasNext();) {
150: Element rowElem = (Element) it.next();
151:
152: ElementListFreeImpl columns = (ElementListFreeImpl) getColumnsOfRowElementList(
153: row, rowElem);
154: ElementPair res = columns.setElementAt2(column, cells[row]);
155: cells[row] = res.getNewElem();
156: replaced[row] = res.getOldElem();
157:
158: row++;
159: }
160: return replaced;
161: }
162:
163: public Element setCellElementAt(int row, int column, Element elem) {
164: Element rowElem = getRowElementAt(row); // Si devuelve null es que está fuera del rango
165: if (rowElem == null)
166: throw new ItsNatException("Row is out of range: " + row);
167: ElementListFreeImpl columns = (ElementListFreeImpl) getColumnsOfRowElementList(
168: row, rowElem);
169: ElementPair res = columns.setElementAt2(column, elem);
170: return res.getOldElem();
171: }
172:
173: public ElementListFree getCellElementListOfRow(int row) {
174: Element rowElem = getRowElementAt(row); // Si devuelve null es que está fuera del rango
175: if (rowElem == null)
176: throw new ItsNatException("Row is out of range: " + row);
177: ElementListFreeImpl columns = (ElementListFreeImpl) getColumnsOfRowElementList(
178: row, rowElem);
179: return columns;
180: }
181:
182: public void moveColumn(int columnIndex, int newIndex) {
183: if (columnIndex == newIndex)
184: return;
185:
186: Element[] elements = getCellElementsOfColumn(columnIndex);
187: removeColumnAt(columnIndex);
188: insertColumnAt(newIndex, elements);
189: }
190:
191: // java.util.List
192:
193: public int indexOf(Object o) {
194: return ElementListFreeUtil.indexOf(this , o);
195: }
196:
197: public boolean contains(Object o) {
198: return ElementListFreeUtil.contains(this , o);
199: }
200:
201: public boolean add(Object o) {
202: return ElementListFreeUtil.add(this , o);
203: }
204:
205: public int lastIndexOf(Object o) {
206: return ElementListFreeUtil.lastIndexOf(this , o);
207: }
208:
209: public boolean remove(Object o) {
210: return ElementListFreeUtil.remove(this , o);
211: }
212:
213: public Object[] toArray(Object[] a) {
214: return ElementListFreeUtil.toArray(this , a);
215: }
216:
217: public Object get(int index) {
218: return ElementListFreeUtil.get(this , index);
219: }
220:
221: public Iterator iterator() {
222: return ElementListFreeUtil.iterator(this );
223: }
224:
225: public ListIterator listIterator(int index) {
226: return ElementListFreeUtil.listIterator(this , index);
227: }
228:
229: public ListIterator listIterator() {
230: return ElementListFreeUtil.listIterator(this );
231: }
232:
233: public Object remove(int index) {
234: return ElementListFreeUtil.remove(this , index);
235: }
236:
237: public boolean addAll(int index, Collection c) {
238: return ElementListFreeUtil.addAll(this , index, c);
239: }
240:
241: public boolean containsAll(Collection c) {
242: return ElementListFreeUtil.containsAll(this , c);
243: }
244:
245: public boolean addAll(Collection c) {
246: return ElementListFreeUtil.addAll(this , c);
247: }
248:
249: public boolean removeAll(Collection c) {
250: return ElementListFreeUtil.removeAll(this , c);
251: }
252:
253: public boolean retainAll(Collection c) {
254: return ElementListFreeUtil.retainAll(this , c);
255: }
256:
257: public void add(int index, Object element) {
258: ElementListFreeUtil.add(this , index, element);
259: }
260:
261: public Object set(int index, Object element) {
262: return ElementListFreeUtil.set(this , index, element);
263: }
264:
265: public Object[] toArray() {
266: return ElementListFreeUtil.toArray(this );
267: }
268:
269: public List subList(int fromIndex, int toIndex) {
270: return ElementListFreeUtil.subList(this , fromIndex, toIndex);
271: }
272:
273: public int size() {
274: return ElementListFreeUtil.size(this );
275: }
276:
277: public void clear() {
278: ElementListFreeUtil.clear(this );
279: }
280:
281: // ElementListFreeInternal
282:
283: public void removeElement(int index, Element node) {
284: removeRowAt(index);
285: }
286:
287: public Element insertBeforeElement(int index, Element newNode,
288: Element refNode) {
289: return insertElementAt2(index, newNode);
290: }
291:
292: public Element getPreviousSiblingElement(int index, Element ref) {
293: return getRowsAsElementListFree().getPreviousSiblingElement(
294: index, ref);
295: }
296:
297: public Element getNextSiblingElement(int index, Element ref) {
298: return getRowsAsElementListFree().getNextSiblingElement(index,
299: ref);
300: }
301:
302: public void addElement(Element elem) {
303: addRow(elem);
304: }
305:
306: public Element addElement2(Element elem) {
307: return addRow2(elem);
308: }
309:
310: public Element setElementAt(int index, Element elem) {
311: return setRowAt(index, elem);
312: }
313:
314: public ElementPair setElementAt2(int index, Element elem) {
315: return setRowAt2(index, elem);
316: }
317:
318: public void insertElementAt(int index, Element elem) {
319: insertRowAt(index, elem);
320: }
321:
322: public Element insertElementAt2(int index, Element elem) {
323: return insertRowAt2(index, elem);
324: }
325:
326: public boolean isMaster() {
327: return getRowsAsElementListFree().isMaster();
328: }
329:
330: public Element[] setElements(Element[] newElems) {
331: return getRowsAsElementListFree().setElements(newElems);
332: }
333:
334: }
|