OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 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.sql.view.list;

import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.view.list.action.ListEvent;
import org.openconcerto.ui.list.selection.ListSelection;
import org.openconcerto.utils.cc.IPredicate;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JMenuItem;

/**
 * Actions that can be performed with an {@link IListe}.
 * 
 * @author Sylvain CUAZ
 */
public interface IListeAction {

    static public class IListeEvent extends ListEvent {

        private final IListe list;

        IListeEvent(final IListe list, final List<SQLRowValues> selection, final List<? extends SQLRowAccessor> selectionAccessor) {
            super(list, list.getSource().getElem(), list.getTotalRowCount(), selection, selectionAccessor);
            this.list = list;
        }

        public final ListSelection getSelection() {
            return this.list.getSelection();
        }
    }

    /**
     * Allow to build a list of buttons and when to enable/disable them.
     * 
     * @author Sylvain CUAZ
     */
    static public class ButtonsBuilder {

        static final String GROUPNAME_PROPNAME = "GROUPNAME";

        static private final ButtonsBuilder NO_BUTTONS = new ButtonsBuilder(Collections.<JButton, IPredicate<IListeEvent>> emptyMap());

        static public final ButtonsBuilder emptyInstance() {
            return NO_BUTTONS;
        }

        private final Map<JButton, IPredicate<IListeEvent>> map;
        private String defaultGroup;

        public ButtonsBuilder() {
            this(new LinkedHashMap<JButton, IPredicate<IListeEvent>>());
        }

        private ButtonsBuilder(Map<JButton, IPredicate<IListeEvent>> map) {
            super();
            this.map = map;
            this.defaultGroup = null;
        }

        public final String getDefaultGroup() {
            return this.defaultGroup;
        }

        public ButtonsBuilder setDefaultGroup(String defaultGroup) {
            this.defaultGroup = defaultGroup;
            return this;
        }

        public final ButtonsBuilder add(JButton btn) {
            return this.add(btn, IPredicate.<IListeEvent> truePredicate());
        }

        public final ButtonsBuilder add(JButton btn, IPredicate<IListeEvent> pred) {
            return this.add(btn, pred, getDefaultGroup());
        }

        /**
         * Add a button in the passed group.
         * 
         * @param btn the button to add.
         * @param pred should return <code>true</code> when the button must be enabled.
         * @param group the group in which to put the button.
         * @return this.
         */
        public final ButtonsBuilder add(JButton btn, IPredicate<IListeEvent> pred, final String group) {
            btn.putClientProperty(GROUPNAME_PROPNAME, group);
            this.map.put(btn, pred);
            return this;
        }

        // * getter

        Map<JButton, IPredicate<IListeEvent>> getContent() {
            return this.map;
        }
    }

    static public class PopupEvent extends IListeEvent {

        private final boolean clickOnRows;

        PopupEvent(final IListe list, final List<SQLRowValues> selection, final List<? extends SQLRowAccessor> selectionAccessor, final boolean clickOnRows) {
            super(list, selection, selectionAccessor);
            this.clickOnRows = clickOnRows;
        }

        public final boolean isClickOnRows() {
            return this.clickOnRows;
        }
    }

    /**
     * Allow to build a hierarchical menu.
     * 
     * @author Sylvain CUAZ
     */
    static public class PopupBuilder {
        static private final PopupBuilder EmptyInstance = new PopupBuilder(VirtualMenu.EMPTY);

        static public final PopupBuilder emptyInstance() {
            return EmptyInstance;
        }

        private final VirtualMenu rootMenu;

        public PopupBuilder() {
            this((String) null);
        }

        public PopupBuilder(final String defaultGroup) {
            this(VirtualMenu.createRoot(defaultGroup));
        }

        private PopupBuilder(final VirtualMenu rootMenu) {
            this.rootMenu = rootMenu;
        }

        /**
         * Get the root menu of the popup.
         * 
         * @return the menu at the root.
         */
        public final VirtualMenu getMenu() {
            return this.rootMenu;
        }

        public final String getDefaultGroup() {
            return this.getMenu().getDefaultGroupName();
        }

        final JMenuItem getRootMenuItem(final Action defaultAction) {
            // Cannot compare actions since menu items are not required to have an action
            String actionCommand = (String) defaultAction.getValue(Action.ACTION_COMMAND_KEY);
            if (actionCommand == null)
                actionCommand = (String) defaultAction.getValue(Action.NAME);
            if (actionCommand == null)
                return null;
            for (final JMenuItem mi : this.getMenu().getItemsAndPath(false).keySet()) {
                if (actionCommand.equals(mi.getActionCommand()))
                    return mi;
            }
            return null;
        }

        // * actions

        public final PopupBuilder addAction(Action a) {
            return this.addAction(a, getDefaultGroup());
        }

        public final PopupBuilder addAction(Action a, String group) {
            this.getMenu().addItem(new JMenuItem(a), group);
            return this;
        }

        // * items

        public final PopupBuilder addItem(JMenuItem mi) {
            return this.addItem(mi, getDefaultGroup());
        }

        public final PopupBuilder addItem(JMenuItem mi, String group) {
            this.getMenu().addItem(mi, group);
            return this;
        }

        public final PopupBuilder addItemInSubmenu(JMenuItem mi, String submenu) {
            return this.addItemInSubmenu(mi, getDefaultGroup(), submenu, getDefaultGroup());
        }

        public final PopupBuilder addItemInSubmenu(JMenuItem mi, String group, String submenu, String submenuGroup) {
            this.getMenu().getSubmenu(submenu, group).addItem(mi, submenuGroup);
            return this;
        }
    }

    // never null
    ButtonsBuilder getHeaderButtons();

    /**
     * The action performed if this is the {@link IListe#setDefaultRowAction(IListeAction) default}
     * of an <code>IListe</code>. The returned action should have a visible feedback.
     * 
     * @param evt the state of the <code>IListe</code>.
     * @return the default action to perform, can be <code>null</code>.
     */
    Action getDefaultAction(ListEvent evt);

    // never null
    PopupBuilder getPopupContent(PopupEvent evt);
}