All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jtables.table.impl.AnnotatedMutableTableImpl Maven / Gradle / Ivy

/**********************************************************************
Copyright (c) 2009-2013 Alexander Kerner. All rights reserved.
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 net.sf.jtables.table.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import net.sf.jtables.table.Column;
import net.sf.jtables.table.Row;
import net.sf.jtables.table.TableMutableAnnotated;
import net.sf.kerner.utils.collections.ObjectToIndexMapper;
import net.sf.kerner.utils.collections.impl.ObjectToIndexMapperImpl;
import net.sf.kerner.utils.collections.list.impl.UtilList;
import net.sf.kerner.utils.io.UtilIO;

/**
 * 
 * Default implementation for {@link TableMutableAnnotated}.
 * 
 * 

* Example:
* *

*

* *

 * TODO example
 * 
* *

*

* last reviewed: 2013-02-27 *

* * @author Alexander Kerner * @version 2013-02-27 * * @param * type of elements in {@code Table} */ public class AnnotatedMutableTableImpl extends MutableTableImpl implements TableMutableAnnotated { /** * row mappings. */ protected volatile ObjectToIndexMapper rowMapper = new ObjectToIndexMapperImpl( new ArrayList()); /** * column mappings. */ protected volatile ObjectToIndexMapper colMapper = new ObjectToIndexMapperImpl( new ArrayList()); /** * Creates an empty {@code AnnotatedMutableTableImpl}. */ public AnnotatedMutableTableImpl() { super(); } /** * Creates an {@code AnnotatedMutableTableImpl} with given rows. * * @param rows * rows initially contained by this {@code Table} */ public AnnotatedMutableTableImpl(final List> rows) { super(rows); } public void addColumn(final Object id, final Column row) { this.colMapper.addMapping(id); super.addColumn(row); } public void addColumn(final Object id, final Column row, final int index) { this.colMapper.addMapping(id, index); super.addColumn(index, row); } public void addRow(final Object id, final Row row) { this.rowMapper.addMapping(id); super.addRow(row); } public void addRow(final Object id, final Row row, final int index) { this.rowMapper.addMapping(id, index); super.addRow(index, row); } protected void checkColumnIndex(final Object key) { if (colMapper.containsKey(key)) { // all good } else throw new NoSuchElementException("no element for column index [" + key + "]"); } protected void checkRowIndex(final Object key) { if (rowMapper.containsKey(key)) { // all good } else throw new NoSuchElementException("no element for row index [" + key + "]"); } @Override public synchronized Column getColumn(final int index) { final ColumnImpl r; final Column c = super.getColumn(index); if (c instanceof ColumnImpl) { r = (ColumnImpl) c; } else { r = new ColumnImpl(super.getColumn(index)); } r.setIdentifier(rowMapper.keys()); return r; } public Column getColumn(final Object key) { net.sf.kerner.utils.impl.util.Util.checkForNull(key); checkColumnIndex(key); return getColumn(colMapper.get(key)); } public List getColumnIdentifier() { return new ArrayList(colMapper.keys()); } @Override public Row getRow(final int index) { final RowImpl r; final Row c = super.getRow(index); if (c instanceof RowImpl) { r = (RowImpl) c; } else { r = new RowImpl(super.getRow(index)); } r.setIdentifier(colMapper.keys()); return r; } public Row getRow(final Object key) { net.sf.kerner.utils.impl.util.Util.checkForNull(key); checkRowIndex(key); return getRow(rowMapper.get(key)); } /** * */ public List getRowIdentifier() { return new ArrayList(rowMapper.keys()); } public void setColumnIdentifier(final List ids) { this.colMapper = new ObjectToIndexMapperImpl(ids); } public void setRowIdentifier(final List ids) { this.rowMapper = new ObjectToIndexMapperImpl(ids); } public AnnotatedMutableTableImpl sortByColumnIds() { final List sorted = new ArrayList(UtilList.toStringList(getColumnIdentifier())); Collections.sort(sorted); final AnnotatedMutableTableImpl result = new AnnotatedMutableTableImpl(); result.setColumnIdentifier(sorted); result.setRowIdentifier(getRowIdentifier()); for (final String s : sorted) { result.addColumn(this.getColumn(s)); } return result; } @Override public String toString() { return toString("\t"); } public String toString(final String delimiter) { // TODO maybe a little bit more complicated?! final StringBuilder sb = new StringBuilder(); // print column indices if (getColumnIdentifier().isEmpty()) { // skip } else { final List r = new ArrayList(getColumnIdentifier()); final java.util.Iterator it = r.iterator(); if (getRowIdentifier().isEmpty()) { } else { sb.append(delimiter); } while (it.hasNext()) { sb.append(it.next()); if (it.hasNext()) sb.append(delimiter); } sb.append(UtilIO.NEW_LINE_STRING); } final Iterator> rowIt = getRowIterator(); final Iterator identIt = getRowIdentifier().iterator(); while (rowIt.hasNext() || identIt.hasNext()) { if (identIt.hasNext()) { sb.append(identIt.next()); sb.append(delimiter); } if (rowIt.hasNext()) { final Iterator ii = rowIt.next().iterator(); while (ii.hasNext()) { sb.append(ii.next()); if (ii.hasNext()) sb.append(delimiter); } } if (rowIt.hasNext() || identIt.hasNext()) sb.append(UtilIO.NEW_LINE_STRING); } return sb.toString(); } }