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