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



Rev 93 | Blame | Compare with Previous | Last modification | View Log | RSS feed

 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at See the License for the specific
 * language governing permissions and limitations under the License.
 * When distributing the software, include this License Header Notice in each file.
 package org.openconcerto.sql.users.rights;

import org.openconcerto.sql.Log;
import org.openconcerto.sql.TM;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.element.SQLElementDirectory;
import org.openconcerto.sql.model.DBRoot;
import org.openconcerto.sql.model.SQLName;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.ui.component.combo.ISearchableCombo;
import org.openconcerto.utils.i18n.Grammar;
import org.openconcerto.utils.model.DefaultIMutableListModel;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JTextField;

public class SQLTableRightEditor implements RightEditor {
    public void setValue(final String object, final DBRoot root, final SQLElementDirectory directory, final JComponent editorComponent) {
        ((ISearchableCombo<SQLTableComboItem>) editorComponent).setValue(SQLTableComboItem.createFromString(object, root, directory));

    public JComponent getRightEditor(final String right, final DBRoot root, final SQLElementDirectory directory, final JTextField fieldObject) {
        final ISearchableCombo<SQLTableComboItem> comboMenu = new ISearchableCombo<>();
        final DefaultIMutableListModel<SQLTableComboItem> comboItems = new DefaultIMutableListModel<>();
        final List<SQLTableComboItem> result = new ArrayList<>();
        for (SQLElement elt : directory.getElements()) {
            result.add(SQLTableComboItem.create(elt.getTable(), elt));
        comboMenu.addValueListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                final SQLTableComboItem selectedItem = comboMenu.getSelectedItem();
                if (selectedItem != null) {
        return comboMenu;

    public static void register() {
        final SQLTableRightEditor editor = new SQLTableRightEditor();
        for (final String code : TableAllRights.getCodes())
            RightEditorManager.getInstance().register(code, editor);

    public static class SQLTableComboItem {

        public static SQLTableComboItem create(final SQLTable t, final SQLElement elt) {
            assert elt == null || elt.getTable() == t;
            return elt != null ? createFromElement(elt) : createFromTable(t);

        public static SQLTableComboItem createFromElement(final SQLElement elt) {
            String variant = elt.getName().getVariant(Grammar.SINGULAR);
            if (variant.contains("_")) {
                // quick patch for not translated tables
                Log.get().warning(elt.getName() + " not translated");
                variant = variant.replace('_', ' ').toLowerCase();
            return new SQLTableComboItem(elt.getTable(), variant);

        public static SQLTableComboItem createFromTable(final SQLTable t) {
            return new SQLTableComboItem(t, t == null ?"rights.allTables") : t.getName().toLowerCase().replace('_', ' '));

        public static SQLTableComboItem createFromString(final String s, final DBRoot r, final SQLElementDirectory dir) {
            if (s == null)
                return createFromTable(null);
            final SQLName n = SQLName.parse(s);
            if (n.getItemCount() != 1)
                throw new IllegalArgumentException("Not 1 item : " + n);
            final SQLTable t = r.findTable(n.getName());
            if (t == null)
                // allow to use unknown table (e.g. not yet created)
                return new SQLTableComboItem(s, n.getName().toLowerCase().replace('_', ' '));
                return create(t, dir.getElement(t));

        private final String value;
        private final String label;

        protected SQLTableComboItem(final SQLTable t, final String label) {
            this(TableAllRights.tableToString(t, false), label);

        protected SQLTableComboItem(final String value, final String label) {
            this.value = value;
            this.label = label;

        public String getValue() {
            return this.value;

        public String toString() {
            return this.label;