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
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.erp.config.MainFrame;
17
import org.openconcerto.sql.element.SQLElement;
18
import org.openconcerto.sql.element.SQLElementDirectory;
19
import org.openconcerto.sql.model.DBRoot;
20
import org.openconcerto.sql.model.SQLName;
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.SQLElementEditAction;
25
import org.openconcerto.sql.view.SQLElementListAction;
26
import org.openconcerto.sql.view.list.IListeAction;
19 ilm 27
import org.openconcerto.sql.view.list.RowAction;
28
import org.openconcerto.utils.CollectionMap;
29
 
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.Collection;
33
import java.util.List;
34
import java.util.Set;
35
 
36
import javax.swing.Action;
37
import javax.swing.JMenuItem;
38
import javax.swing.text.JTextComponent;
39
 
40
/**
41
 * Allow a module to add JComponent to edit fields.
42
 *
43
 * @author Sylvain
44
 */
45
 
46
public final class ComponentsContext {
47
 
48
    private final SQLElementDirectory dir;
49
    private final DBRoot root;
50
    private final Set<String> createdTables;
51
    // only for non-created tables
52
    private final CollectionMap<String, String> createdFields;
53
    // * module items
54
    private final CollectionMap<SQLElement, String> fields;
55
    private final CollectionMap<SQLElement, RowAction> rowActions;
56
    private final List<JMenuItem> menuItems;
57
 
58
    ComponentsContext(SQLElementDirectory dir, DBRoot root, final Set<String> tables, final Set<SQLName> fields) {
59
        super();
60
        this.dir = dir;
61
        this.root = root;
62
        this.createdTables = tables;
63
        this.createdFields = new CollectionMap<String, String>();
64
        for (final SQLName f : fields) {
65
            assert f.getItemCount() == 2;
66
            final String tableName = f.getFirst();
67
            if (!this.createdTables.contains(tableName))
68
                this.createdFields.put(tableName, f.getItem(1));
69
        }
70
        this.fields = new CollectionMap<SQLElement, String>();
71
        this.rowActions = new CollectionMap<SQLElement, RowAction>();
72
        this.menuItems = new ArrayList<JMenuItem>();
73
    }
74
 
75
    private final DBRoot getRoot() {
76
        return this.root;
77
    }
78
 
79
    private final SQLElement checkField(final String tableName, final String name) {
80
        if (this.createdTables.contains(tableName))
81
            throw new IllegalArgumentException("The table " + tableName + " was created by this module");
82
        if (!this.createdFields.getNonNull(tableName).contains(name))
83
            throw new IllegalArgumentException("The field " + new SQLName(tableName, name).quote() + " wasn't created by this module");
84
        return getElement(tableName);
85
    }
86
 
87
    public final SQLElement getElement(final String tableName) {
25 ilm 88
        final SQLElement element = this.dir.getElement(this.getRoot().getTable(tableName));
89
        if (element == null) {
90
            throw new IllegalArgumentException("Not element found for table " + tableName);
91
        }
92
        return element;
19 ilm 93
    }
94
 
95
    public final void putAdditionalField(final String tableName, final String name) {
96
        final SQLElement elem = checkField(tableName, name);
97
        if (elem.putAdditionalField(name)) {
98
            this.fields.put(elem, name);
99
        } else {
100
            throw new IllegalStateException("Already added " + name + " in " + elem);
101
        }
102
    }
103
 
104
    public final void putAdditionalField(final String tableName, final String name, final JTextComponent comp) {
105
        final SQLElement elem = checkField(tableName, name);
106
        if (elem.putAdditionalField(name, comp)) {
107
            this.fields.put(elem, name);
108
        } else {
109
            throw new IllegalStateException("Already added " + name + " in " + elem);
110
        }
111
    }
112
 
113
    public final void putAdditionalField(final String tableName, final String name, final SQLTextCombo comp) {
114
        final SQLElement elem = checkField(tableName, name);
115
        if (elem.putAdditionalField(name, comp)) {
116
            this.fields.put(elem, name);
117
        } else {
118
            throw new IllegalStateException("Already added " + name + " in " + elem);
119
        }
120
    }
121
 
122
    final CollectionMap<SQLElement, String> getFields() {
123
        return this.fields;
124
    }
125
 
126
    // * actions
127
 
21 ilm 128
    public final void addListAction(final String tableName, final IListeAction action) {
19 ilm 129
        final SQLElement elem = getElement(tableName);
130
        this.rowActions.put(elem, action);
131
        elem.getRowActions().add(action);
132
    }
133
 
134
    final CollectionMap<SQLElement, RowAction> getRowActions() {
135
        return this.rowActions;
136
    }
137
 
21 ilm 138
    public final SQLElementEditAction createEditAction(final String table) {
139
        return new SQLElementEditAction(getElement(table));
19 ilm 140
    }
141
 
21 ilm 142
    public final SQLElementListAction createListAction(final String table) {
143
        return new SQLElementListAction(getElement(table));
19 ilm 144
    }
145
 
146
    public final void addMenuItem(final Action action, final String menu) {
147
        this.addMenuItem(action, menu, null);
148
    }
149
 
150
    public final void addMenuItem(final Action action, final String menu, final String group) {
151
        this.addMenuItem(action, new String[] { menu, group });
152
    }
153
 
154
    public final void addMenuItem(final Action action, final String... path) {
155
        this.addMenuItem(action, Arrays.asList(path));
156
    }
157
 
158
    /**
159
     * Adds a menu item to the main frame.
160
     *
161
     * @param action the action to perform.
162
     * @param path where to add the item.
163
     * @see MainFrame#addMenuItem(Action, List)
164
     */
165
    public final void addMenuItem(final Action action, final List<String> path) {
166
        final JMenuItem item = MainFrame.getInstance().addMenuItem(action, path);
167
        this.menuItems.add(item);
168
    }
169
 
170
    final Collection<JMenuItem> getMenuItems() {
171
        return this.menuItems;
172
    }
173
 
20 ilm 174
    public final void addFileDropHandler(final String tableName, FileDropHandler handler) {
175
        DropManager.getInstance().add(this.getRoot().getTable(tableName), handler);
19 ilm 176
    }
177
}