OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 65 | 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;
15
 
16
import org.openconcerto.sql.model.SQLField;
17
import org.openconcerto.sql.request.SQLRowItemView;
83 ilm 18
import org.openconcerto.sql.sqlobject.SQLTextCombo.AbstractComboCacheSQL;
19
import org.openconcerto.sql.sqlobject.SQLTextCombo.ITextComboCacheExistingValues;
17 ilm 20
import org.openconcerto.sql.sqlobject.SQLTextCombo.ITextComboCacheSQL;
21
import org.openconcerto.sql.sqlobject.itemview.RowItemViewComponent;
22
import org.openconcerto.ui.component.ComboLockedMode;
25 ilm 23
import org.openconcerto.ui.component.IComboCacheListModel;
17 ilm 24
import org.openconcerto.ui.component.combo.ISearchableTextCombo;
25
 
26
/**
27
 * An ISearchableTextCombo with the cache from COMPLETION.
28
 *
29
 * @author Sylvain CUAZ
30
 */
31
public class SQLSearchableTextCombo extends ISearchableTextCombo implements RowItemViewComponent {
32
 
83 ilm 33
    private boolean useFieldValues = false;
34
 
17 ilm 35
    public SQLSearchableTextCombo() {
36
        this(ComboLockedMode.UNLOCKED);
37
    }
38
 
39
    public SQLSearchableTextCombo(boolean locked) {
40
        super(locked);
41
    }
42
 
43
    public SQLSearchableTextCombo(ComboLockedMode mode) {
44
        super(mode);
45
    }
46
 
47
    public SQLSearchableTextCombo(ComboLockedMode mode, int rows, int columns) {
48
        super(mode, rows, columns);
49
    }
50
 
51
    public SQLSearchableTextCombo(ComboLockedMode mode, boolean textArea) {
52
        super(mode, textArea);
53
    }
54
 
55
    public SQLSearchableTextCombo(ComboLockedMode mode, int rows, int columns, boolean textArea) {
56
        super(mode, rows, columns, textArea);
57
    }
58
 
83 ilm 59
    public final boolean getUseFieldValues() {
60
        return this.useFieldValues;
61
    }
62
 
63
    /**
64
     * Whether to use the existing values for the field or values listed in the
65
     * {@link SQLTextCombo#getTableName() completion} table.
66
     *
67
     * @param useFieldValues <code>true</code> to use existing values (thus incompatible with a
68
     *        mutable list), <code>false</code> to use the completion table.
69
     * @return this.
70
     * @throws IllegalStateException if the cache is already
71
     *         {@link #initCache(org.openconcerto.utils.model.IListModel) initialized} or if the mode
72
     *         {@link ComboLockedMode#isListMutable()} and <code>useFieldValues</code> is
73
     *         <code>true</code>.
74
     */
75
    public final SQLSearchableTextCombo setUseFieldValues(final boolean useFieldValues) {
76
        if (this.getCache() != null)
77
            throw new IllegalStateException("Cache already created");
78
        if (this.useFieldValues != useFieldValues) {
79
            if (useFieldValues && this.getMode().isListMutable())
80
                throw new IllegalStateException("Cannot modify list when using field values");
81
            this.useFieldValues = useFieldValues;
82
        }
83
        return this;
84
    }
85
 
65 ilm 86
    @Override
17 ilm 87
    public void init(SQLRowItemView v) {
83 ilm 88
        if (this.getCache() == null) {
89
            final AbstractComboCacheSQL cache = this.useFieldValues ? new ITextComboCacheExistingValues(v.getField()) : new ITextComboCacheSQL(v.getField());
90
            this.initCache(new ISQLListModel(cache).load());
91
        }
19 ilm 92
    }
93
 
94
    /**
95
     * Load <code>cache</code> and only afterwards call
96
     * {@link #initCache(org.openconcerto.utils.model.IListModel)}.
97
     *
98
     * @param cache the cache to set.
99
     */
100
    public void initCacheLater(final ISQLListModel cache) {
25 ilm 101
        cache.initCacheLater(this);
17 ilm 102
    }
103
 
25 ilm 104
    public static class ISQLListModel extends IComboCacheListModel {
17 ilm 105
 
106
        public ISQLListModel(final SQLField f) {
19 ilm 107
            this(new ITextComboCacheSQL(f));
108
        }
109
 
83 ilm 110
        public ISQLListModel(final AbstractComboCacheSQL c) {
25 ilm 111
            super(c);
17 ilm 112
        }
113
    }
114
}