DefaultWritableTableFormat.java
01 /*
02  * Copyright 2014-2016 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package griffon.plugins.glazedlists.gui;
17 
18 import ca.odell.glazedlists.gui.WritableTableFormat;
19 import griffon.plugins.glazedlists.ColumnEdit;
20 import griffon.plugins.glazedlists.ColumnWriter;
21 
22 import javax.annotation.Nonnull;
23 import java.util.List;
24 import java.util.Map;
25 
26 /**
27  @author Andres Almiray
28  */
29 public class DefaultWritableTableFormat<E> extends DefaultAdvancedTableFormat<E> implements WritableTableFormat<E> {
30     protected final ColumnWriter[] columnWriters;
31     protected final ColumnEdit[] columnEdits;
32 
33     private static final String WRITER = "writer";
34     private static final String EDITABLE = "editable";
35 
36     public DefaultWritableTableFormat(@Nonnull List<Map<String, Object>> options) {
37         super(options);
38 
39         this.columnWriters = new ColumnWriter[options.size()];
40         this.columnEdits = new ColumnEdit[options.size()];
41 
42         int i = 0;
43         for (Map<String, Object> op : options) {
44             if (op.containsKey(WRITER&& op.get(WRITERinstanceof ColumnWriter) {
45                 columnWriters[i(ColumnWriterop.get(WRITER);
46             else {
47                 columnWriters[i= ColumnWriter.DEFAULT;
48             }
49 
50             if (op.containsKey(EDITABLE&& op.get(EDITABLEinstanceof ColumnEdit) {
51                 columnEdits[i(ColumnEditop.get(EDITABLE);
52             else {
53                 columnEdits[i= ColumnEdit.DEFAULT;
54             }
55 
56             i++;
57         }
58     }
59 
60     @Override
61     public boolean isEditable(E baseObject, int column) {
62         return columnEdits[column].isEditable(baseObject, columnNames[column], column);
63     }
64 
65     @Override
66     public E setColumnValue(E baseObject, Object editedValue, int column) {
67         columnWriters[column].setValue(baseObject, columnNames[column], column, editedValue);
68         return baseObject;
69     }
70 }