001 /*
002 * Copyright 2014-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package griffon.plugins.glazedlists.gui;
017
018 import ca.odell.glazedlists.gui.TableFormat;
019 import griffon.plugins.glazedlists.ColumnReader;
020
021 import javax.annotation.Nonnull;
022 import java.util.List;
023 import java.util.Map;
024
025 import static griffon.util.GriffonClassUtils.requireState;
026 import static griffon.util.GriffonNameUtils.getNaturalName;
027 import static griffon.util.GriffonNameUtils.isBlank;
028 import static java.util.Objects.requireNonNull;
029
030 /**
031 * @author Andres Almiray
032 */
033 public class DefaultTableFormat<E> implements TableFormat<E> {
034 private static final String ERROR_COLUMN_NAMES_NULL = "Argument 'columnNames' must not be null.";
035
036 protected final String[] columnNames;
037 protected final String[] columnTitles;
038 protected final ColumnReader[] columnReaders;
039
040 private static final String NAME = "name";
041 private static final String TITLE = "title";
042 private static final String READER = "reader";
043
044 public DefaultTableFormat(@Nonnull String[] columnNames) {
045 this.columnNames = requireNonNull(columnNames, ERROR_COLUMN_NAMES_NULL);
046 this.columnTitles = new String[columnNames.length];
047 this.columnReaders = new ColumnReader[columnNames.length];
048
049 for (int i = 0; i < columnNames.length; i++) {
050 columnTitles[i] = getNaturalName(columnNames[i]);
051 columnReaders[i] = ColumnReader.DEFAULT;
052 }
053 }
054
055 public DefaultTableFormat(@Nonnull String[] columnNames, @Nonnull String[] columnTitles, @Nonnull ColumnReader[] columnReaders) {
056 this.columnNames = requireNonNull(columnNames, ERROR_COLUMN_NAMES_NULL);
057 this.columnTitles = requireNonNull(columnTitles, "Argument 'columnTitles' must not be null.");
058 this.columnReaders = requireNonNull(columnReaders, "Argument 'columnReaders' must not be null.");
059 requireState(columnNames.length == columnTitles.length,
060 "Arguments 'columNames' and 'columnTitles' have different cardinality. " + columnNames.length + " != " + columnTitles.length);
061 requireState(columnNames.length == columnReaders.length,
062 "Arguments 'columNames' and 'columnReaders' have different cardinality. " + columnNames.length + " != " + columnReaders.length);
063 }
064
065 public DefaultTableFormat(@Nonnull List<Map<String, Object>> options) {
066 requireNonNull(options, "Argument 'options' must not be null.");
067 requireState(options.size() > 0, "Argument 'options' must have at least one entry.");
068 this.columnNames = new String[options.size()];
069 this.columnTitles = new String[options.size()];
070 this.columnReaders = new ColumnReader[options.size()];
071
072 int i = 0;
073 for (Map<String, Object> op : options) {
074 if (op.containsKey(NAME)) {
075 columnNames[i] = String.valueOf(op.get(NAME));
076 }
077 if (isBlank(columnNames[i])) {
078 throw new IllegalArgumentException("Column " + i + " must have a value for name:");
079 }
080
081 if (op.containsKey(TITLE)) {
082 columnTitles[i] = String.valueOf(op.get(TITLE));
083 } else {
084 columnTitles[i] = getNaturalName(columnNames[i]);
085 }
086
087 if (op.containsKey(READER) && op.get(READER) instanceof ColumnReader) {
088 columnReaders[i] = (ColumnReader) op.get(READER);
089 } else {
090 columnReaders[i] = ColumnReader.DEFAULT;
091 }
092
093 i++;
094 }
095 }
096
097 @Override
098 public int getColumnCount() {
099 return columnNames.length;
100 }
101
102 @Override
103 public String getColumnName(int column) {
104 return columnTitles[column];
105 }
106
107 @Override
108 @SuppressWarnings("unchecked")
109 public Object getColumnValue(E baseObject, int column) {
110 return columnReaders[column].getValue(baseObject, columnNames[column], column);
111 }
112 }
|