OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 83 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
19 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.erp.modules;
15
 
16
import org.openconcerto.sql.element.SQLElement;
17
import org.openconcerto.sql.element.SQLElementDirectory;
18
import org.openconcerto.sql.model.DBRoot;
19
import org.openconcerto.sql.model.SQLName;
180 ilm 20
import org.openconcerto.sql.sqlobject.SQLRequestComboBox;
19 ilm 21
import org.openconcerto.sql.sqlobject.SQLTextCombo;
22
import org.openconcerto.sql.view.DropManager;
23
import org.openconcerto.sql.view.FileDropHandler;
21 ilm 24
import org.openconcerto.sql.view.list.IListeAction;
83 ilm 25
import org.openconcerto.utils.ListMap;
19 ilm 26
 
27
import java.util.Set;
180 ilm 28
import java.util.function.Supplier;
19 ilm 29
 
30
import javax.swing.text.JTextComponent;
31
 
32
/**
33
 * Allow a module to add JComponent to edit fields.
34
 *
35
 * @author Sylvain
36
 */
37
 
73 ilm 38
public final class ComponentsContext extends ElementContext {
19 ilm 39
 
40
    private final Set<String> createdTables;
41
    // only for non-created tables
83 ilm 42
    private final ListMap<String, String> createdFields;
19 ilm 43
    // * module items
83 ilm 44
    private final ListMap<SQLElement, String> fields;
45
    private final ListMap<SQLElement, IListeAction> rowActions;
19 ilm 46
 
47
    ComponentsContext(SQLElementDirectory dir, DBRoot root, final Set<String> tables, final Set<SQLName> fields) {
73 ilm 48
        super(dir, root);
19 ilm 49
        this.createdTables = tables;
83 ilm 50
        this.createdFields = new ListMap<String, String>();
19 ilm 51
        for (final SQLName f : fields) {
52
            assert f.getItemCount() == 2;
53
            final String tableName = f.getFirst();
54
            if (!this.createdTables.contains(tableName))
83 ilm 55
                this.createdFields.add(tableName, f.getItem(1));
19 ilm 56
        }
83 ilm 57
        this.fields = new ListMap<SQLElement, String>();
58
        this.rowActions = new ListMap<SQLElement, IListeAction>();
19 ilm 59
    }
60
 
61
    private final SQLElement checkField(final String tableName, final String name) {
62
        if (this.createdTables.contains(tableName))
63
            throw new IllegalArgumentException("The table " + tableName + " was created by this module");
64
        if (!this.createdFields.getNonNull(tableName).contains(name))
65
            throw new IllegalArgumentException("The field " + new SQLName(tableName, name).quote() + " wasn't created by this module");
66
        return getElement(tableName);
67
    }
68
 
69
    public final void putAdditionalField(final String tableName, final String name) {
70
        final SQLElement elem = checkField(tableName, name);
71
        if (elem.putAdditionalField(name)) {
83 ilm 72
            this.fields.add(elem, name);
19 ilm 73
        } else {
74
            throw new IllegalStateException("Already added " + name + " in " + elem);
75
        }
76
    }
77
 
180 ilm 78
    public final void putAdditionalTextField(final String tableName, final String name, final Supplier<? extends JTextComponent> comp) {
19 ilm 79
        final SQLElement elem = checkField(tableName, name);
180 ilm 80
        if (elem.putAdditionalTextField(name, comp)) {
83 ilm 81
            this.fields.add(elem, name);
19 ilm 82
        } else {
83
            throw new IllegalStateException("Already added " + name + " in " + elem);
84
        }
85
    }
86
 
180 ilm 87
    public final void putAdditionalTextCombo(final String tableName, final String name, final Supplier<? extends SQLTextCombo> comp) {
19 ilm 88
        final SQLElement elem = checkField(tableName, name);
180 ilm 89
        if (elem.putAdditionalTextCombo(name, comp)) {
83 ilm 90
            this.fields.add(elem, name);
19 ilm 91
        } else {
92
            throw new IllegalStateException("Already added " + name + " in " + elem);
93
        }
94
    }
95
 
180 ilm 96
    public final void putAdditionalCombo(final String tableName, final String name, final Supplier<? extends SQLRequestComboBox> comp) {
97
        final SQLElement elem = checkField(tableName, name);
98
        if (elem.putAdditionalCombo(name, comp)) {
99
            this.fields.add(elem, name);
100
        } else {
101
            throw new IllegalStateException("Already added " + name + " in " + elem);
102
        }
103
    }
104
 
83 ilm 105
    final ListMap<SQLElement, String> getFields() {
19 ilm 106
        return this.fields;
107
    }
108
 
109
    // * actions
110
 
21 ilm 111
    public final void addListAction(final String tableName, final IListeAction action) {
19 ilm 112
        final SQLElement elem = getElement(tableName);
83 ilm 113
        this.rowActions.add(elem, action);
19 ilm 114
        elem.getRowActions().add(action);
115
    }
116
 
83 ilm 117
    final ListMap<SQLElement, IListeAction> getRowActions() {
19 ilm 118
        return this.rowActions;
119
    }
120
 
20 ilm 121
    public final void addFileDropHandler(final String tableName, FileDropHandler handler) {
122
        DropManager.getInstance().add(this.getRoot().getTable(tableName), handler);
19 ilm 123
    }
124
}