001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces.model;
034:
035: import javax.faces.model.DataModel;
036:
037: /**
038: * Organizes a linear DataModel in a 2D grid. Used for thumbnail gallerys.
039: *
040: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
041: * @version $Rev: 1 $
042: */
043: public class FxGridDataModel extends DataModel {
044: private final int columns;
045: private DataModel dataModel;
046: private int rowIndex = -1;
047:
048: public FxGridDataModel(DataModel dataModel, int columns) {
049: this .dataModel = dataModel;
050: this .columns = columns;
051: }
052:
053: @Override
054: public boolean isRowAvailable() {
055: return dataModel.isRowAvailable();
056: }
057:
058: @Override
059: public int getRowCount() {
060: return dataModel.getRowCount() / columns
061: + (dataModel.getRowCount() % columns > 0 ? 1 : 0);
062: }
063:
064: @Override
065: public Object getRowData() {
066: Object[] rowData = new Object[columns];
067: dataModel.setRowIndex(rowIndex * columns);
068: try {
069: int ctr = 0;
070: while (dataModel.isRowAvailable() && ctr < columns) {
071: rowData[ctr++] = dataModel.getRowData();
072: dataModel.setRowIndex(dataModel.getRowIndex() + 1);
073: }
074: return rowData;
075: } finally {
076: // reset backing datamodel row index
077: dataModel.setRowIndex(rowIndex * columns);
078: }
079: }
080:
081: @Override
082: public int getRowIndex() {
083: return rowIndex;
084: }
085:
086: @Override
087: public void setRowIndex(int rowIndex) {
088: this .rowIndex = rowIndex;
089: dataModel.setRowIndex(rowIndex >= 0 ? rowIndex * columns : -1);
090: }
091:
092: @Override
093: public Object getWrappedData() {
094: return dataModel;
095: }
096:
097: @Override
098: public void setWrappedData(Object data) {
099: this .dataModel = (DataModel) data;
100: }
101:
102: public int getColumns() {
103: return columns;
104: }
105: }
|