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

com.thesett.text.impl.model.TextTableImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright The Sett Ltd, 2005 to 2014.
 *
 * 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 com.thesett.text.impl.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.thesett.common.util.Pair;
import com.thesett.common.util.doublemaps.DoubleKeyedMap;
import com.thesett.common.util.doublemaps.HashMapXY;
import com.thesett.text.api.TextTableEvent;
import com.thesett.text.api.TextTableListener;
import com.thesett.text.api.model.TextTableModel;

/**
 * TextTableImpl provides an implementation of the {@link TextTableModel}.
 *
 * 

* * * * * *
CRC Card
Responsibilities Collaborations *
Add or remove strings to table cells.
Provide the table size.
Monitor maximum text width of columns.
Provide ability to register listeners for changes to the table.
Allow table columns and rows to be labelled.
* * @author Rupert Smith */ public class TextTableImpl implements TextTableModel { /** Holds the maximum row sizes in the grid. */ Map maxRowSizes = new HashMap(); /** Holds the maximum column sizes in the grid. */ Map maxColumnSizes = new HashMap(); /** Used to count the maximum row with data in it. */ private int maxRows; /** Used to count the maximum column with data in it. */ private int maxColumns; /** Holds a table with cell data to pretty print. */ DoubleKeyedMap grid = new HashMapXY(10); /** Holds any interested listeners for updates to the table model. */ protected Collection listeners = new HashSet(); /** Holds any column labels. */ private final Map columnLabels = new HashMap(); /** Holds any row labels. */ private final Map rowLabels = new HashMap(); /** Holds any individual cell labels. */ private final Map> cellLabels = new HashMap>(); /** * Updates the maximum row count of the data table. * * @param row The maximum row count reached. */ public void setMaxRowCount(int row) { if (maxRows < row) { maxRows = row; } } /** * Updates the maximum row height for a row of the data table. * * @param row The row to update. * @param height The max height reached. */ public void setMaxRowHeight(int row, int height) { Integer previousValue = maxRowSizes.get(row); if (previousValue == null) { maxRowSizes.put(row, height); } else if (previousValue < height) { maxRowSizes.put(row, height); } } /** {@inheritDoc} */ public void clear() { grid.clear(); } /** {@inheritDoc} */ public boolean containsKey(Integer col, Integer row) { return grid.containsKey((long) col, (long) row); } /** {@inheritDoc} */ public String put(Integer col, Integer row, String value) { maxColumns = (col > maxColumns) ? col : maxColumns; maxRows = (row > maxRows) ? row : maxRows; updateMaxColumnWidth(col, value.length()); String result = grid.put((long) col, (long) row, value); updateListeners(col, row); return result; } /** {@inheritDoc} */ public String get(Integer col, Integer row) { return grid.get((long) col, (long) row); } /** {@inheritDoc} */ public String remove(Integer col, Integer row) { String result = grid.remove((long) col, (long) row); updateListeners(col, row); return result; } /** {@inheritDoc} */ public int size() { return grid.size(); } /** {@inheritDoc} */ public boolean isEmpty() { return grid.isEmpty(); } /** {@inheritDoc} */ public int getRowCount() { return maxRows + 1; } /** {@inheritDoc} */ public int getColumnCount() { return maxColumns + 1; } /** {@inheritDoc} */ public int getMaxColumnSize(int col) { Integer result = maxColumnSizes.get(col); return (result == null) ? 0 : result; } /** {@inheritDoc} */ public void addTextTableListener(TextTableListener listener) { listeners.add(listener); } /** {@inheritDoc} */ public void removeTextTableListener(TextTableListener listener) { listeners.remove(listener); } /** {@inheritDoc} */ public void labelColumn(String label, int column) { columnLabels.put(label, column); } /** {@inheritDoc} */ public void labelRow(String label, int row) { rowLabels.put(label, row); } /** {@inheritDoc} */ public void labelCell(String label, int col, int row) { cellLabels.put(label, new Pair(col, row)); } /** {@inheritDoc} */ public DoubleKeyedMap withColumnLabels() { return new ColumnLabelView(); } /** {@inheritDoc} */ public DoubleKeyedMap withRowLabels() { return new RowLabelView(); } /** {@inheritDoc} */ public DoubleKeyedMap withLabels() { return new RowAndColumnLabelView(); } /** {@inheritDoc} */ public Map withCellLabels() { return new CellLabelView(); } /** Notifies all interested listeners of an update to this model. */ protected void updateListeners(int col, int row) { TextTableEvent event = new TextTableEvent(this, row, col); for (TextTableListener listener : listeners) { listener.changedUpdate(event); } } /** * Updates the maximum column width for a column of the data table. * * @param column The column to update. * @param width The max width reached. */ private void updateMaxColumnWidth(int column, int width) { Integer previousValue = maxColumnSizes.get(column); if (previousValue == null) { maxColumnSizes.put(column, width); } else if (previousValue < width) { maxColumnSizes.put(column, width); } } /** * Provides a base implementation for the label views. */ public abstract class ViewBase { /** {@inheritDoc} */ public void clear() { TextTableImpl.this.clear(); } /** {@inheritDoc} */ public int size() { return TextTableImpl.this.size(); } /** {@inheritDoc} */ public boolean isEmpty() { return TextTableImpl.this.isEmpty(); } } /** * Provides a view onto the table with labeled columns. */ private class ColumnLabelView extends ViewBase implements DoubleKeyedMap { /** {@inheritDoc} */ public boolean containsKey(String label, Integer row) { Integer col = columnLabels.get(label); if (col == null) { return false; } else { return TextTableImpl.this.containsKey(col, row); } } /** {@inheritDoc} */ public String put(String label, Integer row, String value) { Integer col = columnLabels.get(label); if (col != null) { return TextTableImpl.this.put(col, row, value); } else { return null; } } /** {@inheritDoc} */ public String get(String label, Integer row) { Integer col = columnLabels.get(label); if (col != null) { return TextTableImpl.this.get(col, row); } else { return null; } } /** {@inheritDoc} */ public String remove(String label, Integer row) { Integer col = columnLabels.get(label); if (col != null) { return TextTableImpl.this.remove(col, row); } else { return null; } } } /** * Provides a view onto the table with labeled rows. */ private class RowLabelView extends ViewBase implements DoubleKeyedMap { /** {@inheritDoc} */ public boolean containsKey(Integer col, String label) { Integer row = rowLabels.get(label); if (row == null) { return false; } else { return TextTableImpl.this.containsKey(col, row); } } /** {@inheritDoc} */ public String put(Integer col, String label, String value) { Integer row = rowLabels.get(label); if (row != null) { return TextTableImpl.this.put(col, row, value); } else { return null; } } /** {@inheritDoc} */ public String get(Integer col, String label) { Integer row = rowLabels.get(label); if (row != null) { return TextTableImpl.this.get(col, row); } else { return null; } } /** {@inheritDoc} */ public String remove(Integer col, String label) { Integer row = rowLabels.get(label); if (row != null) { return TextTableImpl.this.remove(col, row); } else { return null; } } } /** * Provides a view onto the table with labeled rows and columns. */ private class RowAndColumnLabelView extends ViewBase implements DoubleKeyedMap { /** {@inheritDoc} */ public boolean containsKey(String colLabel, String rowLabel) { Integer row = rowLabels.get(rowLabel); Integer col = columnLabels.get(colLabel); if ((row != null) && (col != null)) { return TextTableImpl.this.containsKey(col, row); } else { return false; } } /** {@inheritDoc} */ public String put(String colLabel, String rowLabel, String value) { Integer row = rowLabels.get(rowLabel); Integer col = columnLabels.get(colLabel); if ((row != null) && (col != null)) { return TextTableImpl.this.put(col, row, value); } else { return null; } } /** {@inheritDoc} */ public String get(String colLabel, String rowLabel) { Integer row = rowLabels.get(rowLabel); Integer col = columnLabels.get(colLabel); if ((row != null) && (col != null)) { return TextTableImpl.this.get(col, row); } else { return null; } } /** {@inheritDoc} */ public String remove(String colLabel, String rowLabel) { Integer row = rowLabels.get(rowLabel); Integer col = columnLabels.get(colLabel); if ((row != null) && (col != null)) { return TextTableImpl.this.remove(col, row); } else { return null; } } } /** * Provides a view onto the table with individually labelled cells. */ private class CellLabelView extends ViewBase implements Map { /** {@inheritDoc} */ public boolean containsKey(Object key) { return cellLabels.containsKey(key); } /** {@inheritDoc} */ public boolean containsValue(Object value) { throw new UnsupportedOperationException(); } /** {@inheritDoc} */ public String get(Object key) { Pair cell = cellLabels.get(key); if (cell != null) { return TextTableImpl.this.get(cell.getFirst(), cell.getSecond()); } else { return null; } } /** {@inheritDoc} */ public String put(String key, String value) { Pair cell = cellLabels.get(key); if (cell != null) { return TextTableImpl.this.put(cell.getFirst(), cell.getSecond(), value); } else { return null; } } /** {@inheritDoc} */ public String remove(Object key) { Pair cell = cellLabels.get(key); if (cell != null) { return TextTableImpl.this.remove(cell.getFirst(), cell.getSecond()); } else { return null; } } /** {@inheritDoc} */ public void putAll(Map m) { for (Map.Entry entry : m.entrySet()) { put(entry.getKey(), entry.getValue()); } } /** {@inheritDoc} */ public Set keySet() { return cellLabels.keySet(); } /** {@inheritDoc} */ public Collection values() { throw new UnsupportedOperationException(); } /** {@inheritDoc} */ public Set> entrySet() { throw new UnsupportedOperationException(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy