OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 81 | Rev 93 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.view.list;
15
 
16
import org.openconcerto.sql.model.FieldPath;
81 ilm 17
import org.openconcerto.sql.model.IFieldPath;
17 ilm 18
import org.openconcerto.sql.model.SQLField;
83 ilm 19
import org.openconcerto.sql.model.SQLFieldsSet;
17 ilm 20
import org.openconcerto.sql.model.SQLRowAccessor;
83 ilm 21
import org.openconcerto.sql.model.SQLTable;
17 ilm 22
import org.openconcerto.utils.cc.IClosure;
23
import org.openconcerto.utils.convertor.ValueConvertor;
24
 
83 ilm 25
import java.util.Collections;
17 ilm 26
import java.util.HashSet;
27
import java.util.Set;
28
 
29
import javax.swing.table.TableCellRenderer;
30
import javax.swing.table.TableColumn;
31
 
32
public abstract class SQLTableModelColumn {
33
 
34
    private final String name;
35
    private ValueConvertor converter;
36
    private Class convClass;
37
    private TableCellRenderer renderer;
38
    private IClosure<TableColumn> installer;
39
 
40
    public SQLTableModelColumn(String name) {
41
        super();
42
        this.name = name;
43
        // don't call createRenderer() now as our subclass might not yet be inited
44
        this.renderer = null;
45
        this.installer = null;
46
    }
47
 
48
    /**
49
     * The name to be displayed in the header.
50
     *
51
     * @return the name, eg "Site".
52
     */
53
    public final String getName() {
54
        return this.name;
55
    }
56
 
57
    /**
58
     * The tool tip displayed in the column header.
59
     *
60
     * @return the tool tip string.
61
     */
62
    public String getToolTip() {
63
        return this.getName();
64
    }
65
 
66
    /**
67
     * The unique (among a source) identifier of this column.
68
     *
69
     * @return the identifier, eg "SITE.DESIGNATION".
70
     */
71
    public abstract String getIdentifier();
72
 
73
    /**
74
     * All the fields this column needs to show().
75
     *
76
     * @return a set of fields this depends on.
77
     */
78
    public Set<SQLField> getFields() {
79
        final Set<SQLField> res = new HashSet<SQLField>();
81 ilm 80
        for (final IFieldPath fp : this.getPaths())
17 ilm 81
            res.add(fp.getField());
82
        return res;
83
    }
84
 
83 ilm 85
    /**
86
     * The paths needed to {@link #show(SQLRowAccessor)}.
87
     *
88
     * @return a set of paths.
89
     */
17 ilm 90
    public abstract Set<FieldPath> getPaths();
91
 
92
    /**
83 ilm 93
     * The fields modified in {@link #put(ListSQLLine, Object)}. This implementation returns
94
     * {@link #getFields()}.
95
     *
96
     * @return a set of paths.
97
     */
98
    public SQLFieldsSet getWriteFields() {
99
        return new SQLFieldsSet(this.getFields());
100
    }
101
 
102
    /**
103
     * The tables for which rows can be inserted or deleted in {@link #put(ListSQLLine, Object)}.
104
     * This implementation returns an empty set.
105
     *
106
     * @return the tables.
107
     */
108
    public Set<SQLTable> getWriteTables() {
109
        return Collections.emptySet();
110
    }
111
 
112
    /**
17 ilm 113
     * Which columns does show() needs.
114
     *
115
     * @return a set of identifier.
116
     * @see #getIdentifier()
117
     */
118
    public abstract Set<String> getUsedCols();
119
 
120
    /**
121
     * The class of the object returned by {@link #show(SQLRowAccessor)}.
122
     *
123
     * @return the class of our value.
124
     */
125
    public final Class getValueClass() {
126
        return this.convClass != null ? this.convClass : this.getValueClass_();
127
    }
128
 
129
    public abstract boolean isEditable();
130
 
131
    /**
132
     * Extract the value to be passed to the renderer.
133
     *
134
     * @param r the line to display.
135
     * @return the value for this column.
136
     */
137
    public final Object show(SQLRowAccessor r) {
138
        Object res = this.show_(r);
139
        assert res == null || getValueClass_().isInstance(res) : getValueClass_() + " is not the class of " + res;
140
        if (this.converter != null)
141
            res = this.converter.convert(res);
142
        assert res == null || getValueClass().isInstance(res);
143
        return res;
144
    }
145
 
146
    /**
147
     * Update the value of this column for the passed line to <code>obj</code>.
148
     *
149
     * @param r the line to modify.
150
     * @param obj a value of this column.
151
     */
152
    public final void put(ListSQLLine r, Object obj) {
153
        this.put_(r, this.converter == null ? obj : this.converter.unconvert(obj));
154
    }
155
 
156
    /**
157
     * The class of the object returned by {@link #show_(SQLRowAccessor)}.
158
     *
159
     * @return the class of our value.
160
     */
161
    protected abstract Class getValueClass_();
162
 
163
    protected abstract Object show_(SQLRowAccessor r);
164
 
165
    protected abstract void put_(ListSQLLine r, Object obj);
166
 
167
    public <C> void setConverter(ValueConvertor<?, C> vc, Class<C> c) {
168
        this.converter = vc;
169
        this.convClass = c;
170
    }
171
 
172
    public final TableCellRenderer getRenderer() {
173
        if (this.renderer == null)
174
            this.renderer = this.createDefaultRenderer();
175
        return this.renderer;
176
    }
177
 
178
    /**
179
     * Called by <code>getRenderer()</code> if {@link #setRenderer(TableCellRenderer)} wasn't called
180
     * or was called with <code>null</code>. This implementation returns <code>null</code>.
181
     *
182
     * @return the renderer that {@link #getRenderer()} should return.
183
     */
184
    protected TableCellRenderer createDefaultRenderer() {
185
        return null;
186
    }
187
 
188
    public final void setRenderer(TableCellRenderer renderer) {
189
        this.renderer = renderer;
190
    }
191
 
192
    public void install(TableColumn col) {
193
        // always set the renderer, so we can remove one (set to null)
194
        col.setCellRenderer(this.getRenderer());
195
        if (this.installer != null)
196
            this.installer.executeChecked(col);
197
    }
198
 
199
    public final void setColumnInstaller(IClosure<TableColumn> installer) {
200
        this.installer = installer;
201
    }
202
 
203
    @Override
204
    public String toString() {
205
        return this.getClass().getSimpleName() + " " + this.getIdentifier();
206
    }
207
}