OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Rev 73 | 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.sqlobject.itemview;
15
 
16
import org.openconcerto.sql.model.SQLRowAccessor;
17
import org.openconcerto.sql.model.SQLRowValues;
18
import org.openconcerto.ui.valuewrapper.ValueWrapper;
19
import org.openconcerto.utils.cc.IPredicate;
20
import org.openconcerto.utils.checks.ChainValidListener;
21
import org.openconcerto.utils.checks.EmptyListener;
22
import org.openconcerto.utils.checks.EmptyObj;
23
import org.openconcerto.utils.checks.EmptyObjFromVO;
24
import org.openconcerto.utils.checks.EmptyObjHelper;
25
import org.openconcerto.utils.checks.ValidListener;
21 ilm 26
import org.openconcerto.utils.checks.ValidState;
17 ilm 27
 
28
import java.awt.Component;
29
import java.beans.PropertyChangeListener;
30
 
31
/**
32
 * A RIV delegating most of its workings to a ValueWrapper.
33
 *
34
 * @author Sylvain CUAZ
35
 * @param <T> type of value
36
 */
37
public abstract class VWRowItemView<T> extends BaseRowItemView {
38
 
39
    private final ValueWrapper<T> wrapper;
40
    private EmptyObjHelper helper;
41
 
42
    public VWRowItemView(ValueWrapper<T> wrapper) {
43
        this.wrapper = wrapper;
44
    }
45
 
46
    public final ValueWrapper<T> getWrapper() {
47
        return this.wrapper;
48
    }
49
 
50
    protected void init() {
51
        this.helper = this.createHelper();
52
    }
53
 
54
    private final EmptyObjHelper createHelper() {
55
        final EmptyObj eo;
56
        if (this.getWrapper() instanceof EmptyObj)
57
            eo = (EmptyObj) this.getWrapper();
58
        else if (this.getWrapper().getComp() instanceof EmptyObj)
59
            eo = (EmptyObj) this.getWrapper().getComp();
60
        else
61
            eo = new EmptyObjFromVO<T>(this.getWrapper(), this.getEmptyPredicate());
62
 
63
        return new EmptyObjHelper(this, eo);
64
    }
65
 
66
    /**
67
     * The predicate testing whether the value is empty or not. This implementation returns
25 ilm 68
     * {@link EmptyObjFromVO#getDefaultPredicate()}
17 ilm 69
     *
70
     * @return the predicate testing whether the value is empty.
71
     */
72
    protected IPredicate<T> getEmptyPredicate() {
73
        return EmptyObjFromVO.getDefaultPredicate();
74
    }
75
 
76
    public void resetValue() {
77
        this.getWrapper().resetValue();
78
    }
79
 
80
    @SuppressWarnings("unchecked")
81
    public void show(SQLRowAccessor r) {
82
        if (r.getFields().contains(this.getField().getName()))
83
            this.getWrapper().setValue((T) r.getObject(this.getField().getName()));
84
    }
85
 
86
    public void update(SQLRowValues vals) {
87
        vals.put(this.getField().getName(), this.isEmpty() ? SQLRowValues.SQL_DEFAULT : this.getWrapper().getValue());
88
    }
89
 
90
    public final void addValueListener(PropertyChangeListener l) {
91
        this.getWrapper().addValueListener(l);
92
    }
93
 
94
    @Override
95
    public String toString() {
96
        return super.toString() + " using " + this.getWrapper();
97
    }
98
 
99
    // *** emptyObj
100
 
101
    public final boolean isEmpty() {
102
        return this.helper.isEmpty();
103
    }
104
 
105
    public final void addEmptyListener(EmptyListener l) {
106
        this.helper.addEmptyListener(l);
107
    }
108
 
109
    // *** validObj
110
 
21 ilm 111
    @Override
112
    public ValidState getValidState() {
113
        return this.getWrapper().getValidState();
17 ilm 114
    }
115
 
116
    public final void addValidListener(ValidListener l) {
117
        this.getWrapper().addValidListener(new ChainValidListener(this, l));
118
    }
119
 
19 ilm 120
    @Override
121
    public void removeValidListener(ValidListener l) {
122
        this.getWrapper().removeValidListener(new ChainValidListener(this, l));
123
    }
124
 
17 ilm 125
    public final Component getComp() {
126
        return this.getWrapper().getComp();
127
    }
128
 
129
}