93 |
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.users.rights;
|
|
|
15 |
|
174 |
ilm |
16 |
import org.openconcerto.sql.Log;
|
93 |
ilm |
17 |
import org.openconcerto.sql.TM;
|
|
|
18 |
import org.openconcerto.sql.element.SQLElement;
|
|
|
19 |
import org.openconcerto.sql.element.SQLElementDirectory;
|
|
|
20 |
import org.openconcerto.sql.model.DBRoot;
|
|
|
21 |
import org.openconcerto.sql.model.SQLName;
|
|
|
22 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
23 |
import org.openconcerto.ui.component.combo.ISearchableCombo;
|
|
|
24 |
import org.openconcerto.utils.i18n.Grammar;
|
|
|
25 |
import org.openconcerto.utils.model.DefaultIMutableListModel;
|
|
|
26 |
|
|
|
27 |
import java.beans.PropertyChangeEvent;
|
|
|
28 |
import java.beans.PropertyChangeListener;
|
|
|
29 |
import java.util.ArrayList;
|
|
|
30 |
import java.util.List;
|
|
|
31 |
|
|
|
32 |
import javax.swing.JComponent;
|
|
|
33 |
import javax.swing.JTextField;
|
|
|
34 |
|
|
|
35 |
public class SQLTableRightEditor implements RightEditor {
|
|
|
36 |
@SuppressWarnings("unchecked")
|
|
|
37 |
@Override
|
|
|
38 |
public void setValue(final String object, final DBRoot root, final SQLElementDirectory directory, final JComponent editorComponent) {
|
|
|
39 |
((ISearchableCombo<SQLTableComboItem>) editorComponent).setValue(SQLTableComboItem.createFromString(object, root, directory));
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
@Override
|
|
|
43 |
public JComponent getRightEditor(final String right, final DBRoot root, final SQLElementDirectory directory, final JTextField fieldObject) {
|
174 |
ilm |
44 |
final ISearchableCombo<SQLTableComboItem> comboMenu = new ISearchableCombo<>();
|
|
|
45 |
final DefaultIMutableListModel<SQLTableComboItem> comboItems = new DefaultIMutableListModel<>();
|
|
|
46 |
final List<SQLTableComboItem> result = new ArrayList<>();
|
93 |
ilm |
47 |
result.add(SQLTableComboItem.createFromTable(null));
|
174 |
ilm |
48 |
for (SQLElement elt : directory.getElements()) {
|
|
|
49 |
result.add(SQLTableComboItem.create(elt.getTable(), elt));
|
93 |
ilm |
50 |
}
|
|
|
51 |
comboItems.addAll(result);
|
|
|
52 |
comboMenu.initCache(comboItems);
|
|
|
53 |
comboMenu.addValueListener(new PropertyChangeListener() {
|
|
|
54 |
|
|
|
55 |
@Override
|
|
|
56 |
public void propertyChange(PropertyChangeEvent evt) {
|
174 |
ilm |
57 |
final SQLTableComboItem selectedItem = comboMenu.getSelectedItem();
|
93 |
ilm |
58 |
if (selectedItem != null) {
|
|
|
59 |
fieldObject.setText(selectedItem.getValue());
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
});
|
|
|
63 |
return comboMenu;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public static void register() {
|
|
|
67 |
final SQLTableRightEditor editor = new SQLTableRightEditor();
|
|
|
68 |
for (final String code : TableAllRights.getCodes())
|
|
|
69 |
RightEditorManager.getInstance().register(code, editor);
|
|
|
70 |
}
|
|
|
71 |
|
174 |
ilm |
72 |
public static class SQLTableComboItem {
|
93 |
ilm |
73 |
|
174 |
ilm |
74 |
public static SQLTableComboItem create(final SQLTable t, final SQLElement elt) {
|
93 |
ilm |
75 |
assert elt == null || elt.getTable() == t;
|
|
|
76 |
return elt != null ? createFromElement(elt) : createFromTable(t);
|
|
|
77 |
}
|
|
|
78 |
|
174 |
ilm |
79 |
public static SQLTableComboItem createFromElement(final SQLElement elt) {
|
|
|
80 |
String variant = elt.getName().getVariant(Grammar.SINGULAR);
|
|
|
81 |
if (variant.contains("_")) {
|
|
|
82 |
// quick patch for not translated tables
|
|
|
83 |
Log.get().warning(elt.getName() + " not translated");
|
|
|
84 |
variant = variant.replace('_', ' ').toLowerCase();
|
|
|
85 |
}
|
|
|
86 |
return new SQLTableComboItem(elt.getTable(), variant);
|
93 |
ilm |
87 |
}
|
|
|
88 |
|
174 |
ilm |
89 |
public static SQLTableComboItem createFromTable(final SQLTable t) {
|
|
|
90 |
return new SQLTableComboItem(t, t == null ? TM.tr("rights.allTables") : t.getName().toLowerCase().replace('_', ' '));
|
93 |
ilm |
91 |
}
|
|
|
92 |
|
174 |
ilm |
93 |
public static SQLTableComboItem createFromString(final String s, final DBRoot r, final SQLElementDirectory dir) {
|
93 |
ilm |
94 |
if (s == null)
|
|
|
95 |
return createFromTable(null);
|
|
|
96 |
final SQLName n = SQLName.parse(s);
|
|
|
97 |
if (n.getItemCount() != 1)
|
|
|
98 |
throw new IllegalArgumentException("Not 1 item : " + n);
|
|
|
99 |
final SQLTable t = r.findTable(n.getName());
|
|
|
100 |
if (t == null)
|
|
|
101 |
// allow to use unknown table (e.g. not yet created)
|
174 |
ilm |
102 |
return new SQLTableComboItem(s, n.getName().toLowerCase().replace('_', ' '));
|
93 |
ilm |
103 |
else
|
|
|
104 |
return create(t, dir.getElement(t));
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
private final String value;
|
|
|
108 |
private final String label;
|
|
|
109 |
|
|
|
110 |
protected SQLTableComboItem(final SQLTable t, final String label) {
|
|
|
111 |
this(TableAllRights.tableToString(t, false), label);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
protected SQLTableComboItem(final String value, final String label) {
|
|
|
115 |
this.value = value;
|
|
|
116 |
this.label = label;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public String getValue() {
|
|
|
120 |
return this.value;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
@Override
|
|
|
124 |
public String toString() {
|
|
|
125 |
return this.label;
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
}
|