All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.client.form.FormUIBlockingLayerUI Maven / Gradle / Ivy

package fr.ird.observe.client.form;

/*-
 * #%L
 * ObServe Toolkit :: Common Client
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.collect.ImmutableSet;
import fr.ird.observe.client.util.ObserveBlockingLayerUI;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.Set;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdesktop.jxlayer.JXLayer;

/**
 * Created on 09/11/16.
 *
 * @author Tony Chemit - [email protected]
 * @since 6.0
 */
public class FormUIBlockingLayerUI extends ObserveBlockingLayerUI {

    /** Logger */
    private static final Log log = LogFactory.getLog(FormUIBlockingLayerUI.class);

    private static final Set GLOBAL_KEY_CODES = ImmutableSet.of(
            KeyEvent.VK_F1,
            KeyEvent.VK_F2,
            KeyEvent.VK_F3,
            KeyEvent.VK_F4,
            KeyEvent.VK_F5,
            KeyEvent.VK_F6,
            KeyEvent.VK_F7,
            KeyEvent.VK_F8,
            KeyEvent.VK_F9,
            KeyEvent.VK_F10,
            KeyEvent.VK_F11,
            KeyEvent.VK_F12
    );
    private static final long serialVersionUID = -492616734287357818L;

    private final FormUI ui;

    FormUIBlockingLayerUI(FormUI ui) {
        this.ui = ui;
    }

    @Override
    protected void processMouseEvent(MouseEvent e, JXLayer l) {

        switch (e.getID()) {
            case MouseEvent.MOUSE_ENTERED:
                if (log.isDebugEnabled()) {
                    log.debug("Enter in formula zone: " + e);
                }
                //FIXME!!!
//                ObserveClientApplicationContext.get().getMainUI().getModel().setFocusOnNavigation(false);
        }
        super.processMouseEvent(e, l);
    }

    @Override
    protected void processKeyEvent(KeyEvent e, JXLayer l) {

        if (!block) {
            return;
        }

        InputMap inputMap = ((JComponent) ui).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = ((JComponent) ui).getActionMap();

        boolean consumed = e.isConsumed();

        if (!consumed && e.isControlDown() && e.getKeyChar() != '\uFFFF') {

            KeyStroke keyStroke = KeyStroke.getKeyStroke("ctrl pressed " + (char) e.getKeyCode());

            if (keyStroke == null) {
                super.processKeyEvent(e, l);
                return;
            }
            consumed = doAction(keyStroke, inputMap, actionMap);
        }

        if (!consumed && e.getID() == KeyEvent.KEY_RELEASED && !e.isAltDown() && !e.isAltGraphDown()
                && !e.isMetaDown() && GLOBAL_KEY_CODES.contains(e.getKeyCode())) {

            if (!e.isShiftDown()) {

                KeyStroke keyStroke = KeyStroke.getKeyStroke(e.getKeyCode(), e.isControlDown() ? KeyEvent.CTRL_DOWN_MASK : 0);
                if (keyStroke == null) {
                    super.processKeyEvent(e, l);
                    return;
                }

                consumed = doAction(keyStroke, inputMap, actionMap);

            } else if (e.isControlDown()) {

                KeyStroke keyStroke = KeyStroke.getKeyStroke(e.getKeyCode(), KeyEvent.CTRL_DOWN_MASK + KeyEvent.SHIFT_DOWN_MASK);
                if (keyStroke == null) {
                    super.processKeyEvent(e, l);
                    return;
                }

                consumed = doAction(keyStroke, inputMap, actionMap);
            }

        }

        if (consumed) {
            e.consume();
        } else {
            super.processKeyEvent(e, l);
        }

    }

    protected boolean doAction(KeyStroke keyStroke, InputMap inputMap, ActionMap actionMap) {

        String actionName = (String) inputMap.get(keyStroke);
        if (actionName != null) {

            Action action = actionMap.get(actionName);

            JComponent editor = (JComponent) action.getValue("editor");
            if (editor == null || (editor.isVisible() && editor.isEnabled())) {

                if (log.isInfoEnabled()) {
                    log.info("Found action: " + action.getValue(Action.NAME) + " for keyStroke: " + keyStroke);
                }
                SwingUtilities.invokeLater(() -> action.actionPerformed(new ActionEvent(ui, 0, (String) action.getValue(Action.NAME))));
                return true;
            } else {
                if (log.isInfoEnabled()) {
                    log.info("Found disabled action: " + action.getValue(Action.NAME) + " for keyStroke: " + keyStroke);
                }
            }
        }
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy