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.javafx.gui;
017
018 import griffon.plugins.glazedlists.ColumnEdit;
019 import griffon.plugins.glazedlists.ColumnWriter;
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 java.util.Objects.requireNonNull;
027
028 /**
029 * Defines an editable {@code FXTableFormat}.
030 *
031 * @author Andres Almiray
032 */
033 public class DefaultFXWritableTableFormat<E> extends DefaultFXTableFormat<E> implements FXWritableTableFormat<E> {
034 protected final ColumnWriter[] columnWriters;
035 protected final ColumnEdit[] columnEdits;
036
037 private static final String WRITER = "writer";
038 private static final String EDITABLE = "editable";
039
040 /**
041 * Creates a {@code FXTableFormat} based on the supplied options.
042 * <p>
043 * Valid option keys are <tt>name</tt>, <tt>title</tt>, <tt>reader</tt>,
044 * <tt>writer</tt>, <tt>editable</tt> and <tt>tableCellFactory</tt>.
045 * </p>
046 *
047 * @param options the options that configure this format
048 */
049 public DefaultFXWritableTableFormat(@Nonnull FXTableFormat.Options... options) {
050 super(options);
051
052 requireNonNull(options, "Argument 'options' must not be null");
053 requireState(options.length > 0, "Argument 'options' must have at least one entry");
054
055 this.columnWriters = new ColumnWriter[options.length];
056 this.columnEdits = new ColumnEdit[options.length];
057
058 int i = 0;
059 for (Options opts : options) {
060 for (Option opt : opts.options) {
061 if (WRITER.equalsIgnoreCase(opt.name)) {
062 columnWriters[i] = (ColumnWriter) opt.value;
063 } else if (EDITABLE.equalsIgnoreCase(opt.name)) {
064 if (opt.value instanceof ColumnEdit) {
065 columnEdits[i] = (ColumnEdit) opt.value;
066 }
067 }
068 }
069
070 if (columnWriters[i] == null) {
071 columnWriters[i] = DefaultJavaFXColumnWriter.INSTANCE;
072 }
073 if (columnEdits[i] == null) {
074 columnEdits[i] = ColumnEdit.DEFAULT;
075 }
076 if (tableCellFactories[i] == null) {
077 tableCellFactories[i] = TableCellFactory.DEFAULT_EDITABLE;
078 }
079
080 i++;
081 }
082 }
083
084 /**
085 * Creates a {@code FXTableFormat} based on the supplied options.
086 * <p>
087 * Valid option keys are <tt>name</tt>, <tt>title</tt>, <tt>reader</tt>,
088 * <tt>writer</tt>, <tt>editable</tt> and <tt>tableCellFactory</tt>.
089 * </p>
090 *
091 * @param options the options that configure this format
092 */
093 public DefaultFXWritableTableFormat(@Nonnull List<Map<String, Object>> options) {
094 super(options);
095
096 this.columnWriters = new ColumnWriter[options.size()];
097 this.columnEdits = new ColumnEdit[options.size()];
098
099 int i = 0;
100 for (Map<String, Object> op : options) {
101 if (op.containsKey(WRITER) && op.get(WRITER) instanceof ColumnWriter) {
102 columnWriters[i] = (ColumnWriter) op.get(WRITER);
103 } else {
104 columnWriters[i] = DefaultJavaFXColumnWriter.INSTANCE;
105 }
106
107 if (op.containsKey(EDITABLE) && op.get(EDITABLE) instanceof ColumnEdit) {
108 columnEdits[i] = (ColumnEdit) op.get(EDITABLE);
109 } else {
110 columnEdits[i] = ColumnEdit.DEFAULT;
111 }
112
113 i++;
114 }
115 }
116
117 @Override
118 @SuppressWarnings("unchecked")
119 public boolean isEditable(@Nonnull E baseObject, int column) {
120 return columnEdits[column].isEditable(baseObject, columnNames[column], column);
121 }
122
123 @Nonnull
124 @Override
125 @SuppressWarnings("unchecked")
126 public E setColumnValue(@Nonnull E baseObject, Object editedValue, int column) {
127 columnWriters[column].setValue(baseObject, columnNames[column], column, editedValue);
128 return baseObject;
129 }
130 }
|