Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2008-2012 Sergey Skladchikov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gwt.advanced.client.datamodel;
import java.util.*;
/**
* This is a data model for the tree grid.
*
* @author Sergey Skladchikov
* @since 1.4.0
*/
public class TreeGridDataModel implements Composite {
/** a map of nested rows */
private Map> subRows = new HashMap>();
/** paging enabled / disbaled flags */
private Map pagingFlags = new HashMap();
/** removed rows of the subtrees */
private Map> removedRowsMap = new HashMap>();
/** delegate model required to perform standard operations */
private EditableGridDataModel delegate;
/** an expandable column index */
private int expandableColumn;
/**
* Creates an instance of this class and initializes internal fields.
*
* @param data is a data array to be placed into the model.
*/
public TreeGridDataModel(Object[][] data) {
setDelegate(new DelegateEditableGridDataModel(data, this));
}
/**
* Creates an instance of this class and loads data using the specified handler.
*
* @param handler is a handler instance to load data.
*/
protected TreeGridDataModel(DataModelCallbackHandler handler) {
setDelegate(new DelegateEditableGridDataModel(handler));
}
/** {@inheritDoc} */
public int getTotalRowCount(TreeGridRow parent) {
if (parent == null)
return getDelegate().getTotalRowCount();
return getChildrenList(parent).size();
}
/** {@inheritDoc} */
public TreeGridRow[] getRows(TreeGridRow parent) {
if (parent == null)
return getRootRows();
List children = getChildrenList(parent);
return children.toArray(new TreeGridRow[children.size()]);
}
/** {@inheritDoc} */
public int addRow(TreeGridRow parent, Object[] child) {
if (parent == null) {
addRow(getDelegate().getRows().length, child);
return getDelegate().getRows().length - 1;
}
int columnCount = Math.max(getTotalColumnCount(), (child != null ? child.length : 0));
TreeGridRow gridRow = (TreeGridRow) getDelegate().createGridRow(columnCount);
gridRow.setParent(parent);
for (int j = 0; j < columnCount; j++) {
if (child != null && child.length > j)
gridRow.add(child[j]);
else
gridRow.add(null);
}
List siblings = getChildrenList(parent);
siblings.add(gridRow);
int index = siblings.size() - 1;
gridRow.setIndex(index);
fireRowEvent(EditableModelEvent.ADD_ROW, parent, index);
return index;
}
/** {@inheritDoc} */
public void update(TreeGridRow parent, Object[][] children) {
if (children == null)
return;
if (parent == null) {
update(children);
return;
}
removeAll(parent);
for (Object[] aChildren : children)
addRow(parent, aChildren);
getDelegate().fireEvent(new CompositeModelEvent(EditableModelEvent.UPDATE_ALL, parent));
}
/** {@inheritDoc} */
public void update(TreeGridRow parent, int row, int column, Object data) {
if (parent == null) {
update(row, column, data);
return;
}
TreeGridRow[] rows = getRows(parent);
rows[row].set(column, data);
CompositeModelEvent event = new CompositeModelEvent(EditableModelEvent.UPDATE_CELL, parent, row);
event.setColumn(column);
getDelegate().fireEvent(event);
}
/** {@inheritDoc} */
public void removeRow(TreeGridRow parent, int row) {
if (parent == null)
removeRow(row);
else {
TreeGridRow removeRow = getChildrenList(parent).get(row);
int size = getChildrenList(removeRow).size();
for (int i = size - 1; i >= 0; i--)
removeRow(removeRow, i);
getChildrenList(parent).remove(removeRow);
getRemovedRowsList(parent).add(removeRow);
remapIndexes(parent);
fireRowEvent(EditableModelEvent.REMOVE_ROW, parent, row);
}
}
/** {@inheritDoc} */
public void removeAll(TreeGridRow parent) {
if (parent == null)
removeAll();
else {
getChildrenList(parent).clear();
getDelegate().fireEvent(new CompositeModelEvent(EditableModelEvent.CLEAN, parent));
}
}
/** {@inheritDoc} */
public void setCurrentPageNumber(TreeGridRow parent, int currentPageNumber) throws IllegalArgumentException {
if (parent == null) {
setCurrentPageNumber(currentPageNumber);
return;
}
parent.setCurrentPageNumber(currentPageNumber);
}
/** {@inheritDoc} */
public int getTotalPagesNumber(TreeGridRow parent) {
if (parent == null)
return getTotalPagesNumber();
if (!isSubtreePagingEnabled(parent))
return getTotalRowCount() > 0 ? 1 : 0;
List rows = getSubRows().get(parent);
if (rows == null)
return 0;
return parent.getTotalPagesNumber();
}
/** {@inheritDoc} */
public int getStartPage(TreeGridRow parent) {
return parent.getStartPage();
}
/** {@inheritDoc} */
public int getEndPage(TreeGridRow parent) {
return parent.getEndPage();
}
/** {@inheritDoc} */
public int getDisplayedPages(TreeGridRow parent) {
if (parent == null)
return getDisplayedPages();
return parent.getDisplayedPages();
}
/** {@inheritDoc} */
public void setDisplayedPages(TreeGridRow parent, int displayedPages) {
if (parent == null) {
setDisplayedPages(displayedPages);
return;
}
parent.setDisplayedPages(displayedPages);
}
/** {@inheritDoc} */
public int getCurrentPageNumber(TreeGridRow parent) {
if (parent == null)
return getCurrentPageNumber();
return parent.getCurrentPageNumber();
}
/** {@inheritDoc} */
public int getPageSize(TreeGridRow parent) {
if (parent == null)
return getPageSize();
return parent.getPageSize();
}
/** {@inheritDoc} */
public void setPageSize(TreeGridRow parent, int size) {
if (parent == null) {
setPageSize(size);
return;
}
parent.setPageSize(size);
}
/** {@inheritDoc} */
public void setSubtreePagingEnabled(TreeGridRow parent, boolean enabled) {
if (parent == null)
return;
getPagingFlags().put(parent, enabled);
}
/** {@inheritDoc} */
public boolean isSubtreePagingEnabled(TreeGridRow parent) {
return parent == null || Boolean.valueOf(String.valueOf(getPagingFlags().get(parent)));
}
/** {@inheritDoc} */
public Object[][] getRemovedRows(TreeGridRow parent) {
if (parent == null)
return getRemovedRows();
List result = getRemovedRowsList(parent);
Object[][] rows = new Object[result.size()][getTotalColumnCount()];
for (int i = 0; i < rows.length; i++)
rows[i] = ((List)result.get(i)).toArray();
return rows;
}
/** {@inheritDoc} */
public void clearRemovedRows(TreeGridRow parent) {
if (parent == null) {
clearRemovedRows();
return;
}
getRemovedRowsList(parent).clear();
}
/** {@inheritDoc} */
public TreeGridRow getRow(TreeGridRow parent, int index) {
return getRows(parent)[index];
}
/** {@inheritDoc} */
public void setExpandableColumn(int index) {
this.expandableColumn = index;
}
/** {@inheritDoc} */
public int getExpandableColumn() {
return expandableColumn;
}
/** {@inheritDoc} */
public void setExpandableColumn(String name) {
int index = getDelegate().getColumnNamesList().indexOf(name);
if (index != -1)
setExpandableColumn(index);
}
/** {@inheritDoc} */
public void setSortColumn(TreeGridRow parent, int column, Comparator