OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * 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 http://www.gnu.org/licenses/gpl-3.0.html 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.erp.modules;

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.sqlobject.SQLRequestComboBox;
import org.openconcerto.sql.sqlobject.SQLTextCombo;
import org.openconcerto.sql.view.DropManager;
import org.openconcerto.sql.view.FileDropHandler;
import org.openconcerto.sql.view.list.IListeAction;
import org.openconcerto.utils.ListMap;

import java.util.Set;
import java.util.function.Supplier;

import javax.swing.text.JTextComponent;

/**
 * Allow a module to add JComponent to edit fields.
 * 
 * @author Sylvain
 */

public final class ComponentsContext extends ElementContext {

    private final Set<String> createdTables;
    // only for non-created tables
    private final ListMap<String, String> createdFields;
    // * module items
    private final ListMap<SQLElement, String> fields;
    private final ListMap<SQLElement, IListeAction> rowActions;

    ComponentsContext(SQLElementDirectory dir, DBRoot root, final Set<String> tables, final Set<SQLName> fields) {
        super(dir, root);
        this.createdTables = tables;
        this.createdFields = new ListMap<String, String>();
        for (final SQLName f : fields) {
            assert f.getItemCount() == 2;
            final String tableName = f.getFirst();
            if (!this.createdTables.contains(tableName))
                this.createdFields.add(tableName, f.getItem(1));
        }
        this.fields = new ListMap<SQLElement, String>();
        this.rowActions = new ListMap<SQLElement, IListeAction>();
    }

    private final SQLElement checkField(final String tableName, final String name) {
        if (this.createdTables.contains(tableName))
            throw new IllegalArgumentException("The table " + tableName + " was created by this module");
        if (!this.createdFields.getNonNull(tableName).contains(name))
            throw new IllegalArgumentException("The field " + new SQLName(tableName, name).quote() + " wasn't created by this module");
        return getElement(tableName);
    }

    public final void putAdditionalField(final String tableName, final String name) {
        final SQLElement elem = checkField(tableName, name);
        if (elem.putAdditionalField(name)) {
            this.fields.add(elem, name);
        } else {
            throw new IllegalStateException("Already added " + name + " in " + elem);
        }
    }

    public final void putAdditionalTextField(final String tableName, final String name, final Supplier<? extends JTextComponent> comp) {
        final SQLElement elem = checkField(tableName, name);
        if (elem.putAdditionalTextField(name, comp)) {
            this.fields.add(elem, name);
        } else {
            throw new IllegalStateException("Already added " + name + " in " + elem);
        }
    }

    public final void putAdditionalTextCombo(final String tableName, final String name, final Supplier<? extends SQLTextCombo> comp) {
        final SQLElement elem = checkField(tableName, name);
        if (elem.putAdditionalTextCombo(name, comp)) {
            this.fields.add(elem, name);
        } else {
            throw new IllegalStateException("Already added " + name + " in " + elem);
        }
    }

    public final void putAdditionalCombo(final String tableName, final String name, final Supplier<? extends SQLRequestComboBox> comp) {
        final SQLElement elem = checkField(tableName, name);
        if (elem.putAdditionalCombo(name, comp)) {
            this.fields.add(elem, name);
        } else {
            throw new IllegalStateException("Already added " + name + " in " + elem);
        }
    }

    final ListMap<SQLElement, String> getFields() {
        return this.fields;
    }

    // * actions

    public final void addListAction(final String tableName, final IListeAction action) {
        final SQLElement elem = getElement(tableName);
        this.rowActions.add(elem, action);
        elem.getRowActions().add(action);
    }

    final ListMap<SQLElement, IListeAction> getRowActions() {
        return this.rowActions;
    }

    public final void addFileDropHandler(final String tableName, FileDropHandler handler) {
        DropManager.getInstance().add(this.getRoot().getTable(tableName), handler);
    }
}