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

fr.ird.observe.client.ObserveKeyStrokes Maven / Gradle / Ivy

package fr.ird.observe.client;

/*-
 * #%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.ImmutableList;
import fr.ird.observe.client.action.MenuActionSupport;
import fr.ird.observe.client.action.UIActionSupport;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import javax.swing.AbstractButton;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import org.apache.commons.lang3.StringUtils;
import org.nuiton.jaxx.runtime.JAXXObject;


import static fr.ird.observe.client.form.FormUIHandler.GLOBAL_ACTION;
import static javax.swing.Action.ACCELERATOR_KEY;
import static org.nuiton.i18n.I18n.t;

/**
 * Created on 23/12/16.
 *
 * @author Tony Chemit - [email protected]
 * @since 6.0
 */
public abstract class ObserveKeyStrokes {

    public static final KeyStroke KEY_STROKE_STORAGE_DO_CONFIGURE_REMOTE_SERVER = KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK);
    public static final KeyStroke KEY_STROKE_STORAGE_DO_BACKUP = KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK);
    public static final KeyStroke KEY_STROKE_STORAGE_DO_CHOOSE_DIRECTORY = KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK);
    public static final KeyStroke KEY_STROKE_STORAGE_DO_USE_SSL = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK);
    public static final KeyStroke KEY_STROKE_STORAGE_DO_CHOOSE_FILE = KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK);
    public static final KeyStroke KEY_STROKE_RELOAD_DEFAULT_CONFIGURATION = KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
    public static final KeyStroke KEY_STROKE_ESCAPE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    public static final KeyStroke KEY_STROKE_ENTER = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    public static final KeyStroke KEY_STROKE_SHOW_SEINE = KeyStroke.getKeyStroke("ctrl pressed F1");
    public static final KeyStroke KEY_STROKE_SHOW_LONGLINE = KeyStroke.getKeyStroke("ctrl pressed F2");
    public static final KeyStroke KEY_STROKE_SHOW_REFERENTIAL = KeyStroke.getKeyStroke("ctrl pressed F3");
    public static final KeyStroke KEY_STROKE_SHOW_EMPTY_PROGRAM = KeyStroke.getKeyStroke("ctrl pressed F4");
    public static final KeyStroke KEY_STROKE_CONFIGURE_LOCAL_SOURCE = KeyStroke.getKeyStroke("ctrl pressed L");
    public static final KeyStroke KEY_STROKE_CONFIGURE_REMOTE_SOURCE = KeyStroke.getKeyStroke("ctrl pressed R");
    public static final KeyStroke KEY_STROKE_SAVE_TABLE_ENTRY = KeyStroke.getKeyStroke("ctrl pressed S");
    public static final KeyStroke KEY_STROKE_PRESSED_ENTER = KeyStroke.getKeyStroke("pressed ENTER");
    public static final KeyStroke KEY_STROKE_RESET_DATA = KeyStroke.getKeyStroke("pressed F7");
    public static final KeyStroke KEY_STROKE_NEW_NEXT_DATA = KeyStroke.getKeyStroke("pressed F4");
    public static final KeyStroke KEY_STROKE_DELETE_DATA_GLOBAL = KeyStroke.getKeyStroke("pressed F6");
    public static final KeyStroke KEY_STROKE_OPEN_DATA = KeyStroke.getKeyStroke("pressed F2");
    public static final KeyStroke KEY_STROKE_CLOSE_DATA = KeyStroke.getKeyStroke("pressed F3");
    public static final KeyStroke KEY_STROKE_SAVE_DATA = KeyStroke.getKeyStroke("pressed F5");
    public static final KeyStroke KEY_STROKE_CHANGE_FOCUS = KeyStroke.getKeyStroke("pressed F1");
    public static final KeyStroke KEY_STROKE_SAVE = KeyStroke.getKeyStroke("ctrl pressed S");
    public static final KeyStroke KEY_STROKE_SHIFT_ALT_ENTER = KeyStroke.getKeyStroke("alt shift pressed ENTER");
    public static final KeyStroke KEY_STROKE_GO_DOWN = KeyStroke.getKeyStroke("alt pressed D");
    public static final KeyStroke KEY_STROKE_ADD_DCP = KeyStroke.getKeyStroke("ctrl pressed P");
    public static final KeyStroke KEY_STROKE_ADD_SET_LONGLINE = KeyStroke.getKeyStroke("ctrl pressed L");
    public static final KeyStroke KEY_STROKE_GO_UP = KeyStroke.getKeyStroke("alt pressed U");
    public static final KeyStroke KEY_STROKE_EDIT_REFERENTIAL = KeyStroke.getKeyStroke("ctrl pressed M");
    public static final KeyStroke KEY_STROKE_RESET_TABLE_ENTRY = KeyStroke.getKeyStroke("ctrl pressed R");
    public static final KeyStroke KEY_STROKE_ADD_SET_SEINE = KeyStroke.getKeyStroke("ctrl pressed L");
    public static final KeyStroke KEY_STROKE_ADD_FLOATING_OBJECT_REFERENCE = KeyStroke.getKeyStroke("ctrl pressed S");
    public static final KeyStroke KEY_STROKE_EDIT_FLOATING_OBJECT_REFERENCE = KeyStroke.getKeyStroke("ctrl pressed E");
    public static final KeyStroke KEY_STROKE_REMOVE_FLOATING_OBJECT_REFERENCE = KeyStroke.getKeyStroke("ctrl pressed DELETE");

    public static final KeyStroke KEY_STROKE_ALT_ENTER = KeyStroke.getKeyStroke("alt pressed ENTER");
    public static final KeyStroke KEY_STROKE_BACK_TO_REFERENTIAL_LIST = KeyStroke.getKeyStroke("ctrl pressed B");
    public static final KeyStroke KEY_STROKE_RESET = KeyStroke.getKeyStroke("ctrl pressed D");
    public static final KeyStroke KEY_STROKE_SHOW_DECORATOR_POPUP = KeyStroke.getKeyStroke("ctrl pressed F");
    public static final KeyStroke KEY_STROKE_GO_DOWN_TABLE_ENTRY = KeyStroke.getKeyStroke("shift pressed W");
    public static final KeyStroke KEY_STROKE_GO_UP_TABLE_ENTRY = KeyStroke.getKeyStroke("shift pressed Q");
    public static final KeyStroke KEY_STROKE_GO_TAB_1 = KeyStroke.getKeyStroke("ctrl pressed F5");
    public static final KeyStroke KEY_STROKE_GO_TAB_2 = KeyStroke.getKeyStroke("ctrl pressed F6");
    public static final KeyStroke KEY_STROKE_GO_TAB_3 = KeyStroke.getKeyStroke("ctrl pressed F7");
    public static final KeyStroke KEY_STROKE_GO_TAB_4 = KeyStroke.getKeyStroke("ctrl pressed F8");
    public static final KeyStroke KEY_STROKE_GO_TAB_5 = KeyStroke.getKeyStroke("ctrl pressed F9");
    public static final KeyStroke KEY_STROKE_GO_TAB_6 = KeyStroke.getKeyStroke("ctrl pressed F10");
    public static final KeyStroke KEY_STROKE_COPY_FLOATING_OBJECT_PART_TO_RIGHT = KeyStroke.getKeyStroke("ctrl R");
    public static final KeyStroke KEY_STROKE_COPY_FLOATING_OBJECT_PART_TO_LEFT = KeyStroke.getKeyStroke("ctrl L");

    public static final KeyStroke KEY_STROKE_GO_SUB_TAB_1 = KeyStroke.getKeyStroke("shift ctrl pressed F5");
    public static final KeyStroke KEY_STROKE_GO_SUB_TAB_2 = KeyStroke.getKeyStroke("shift ctrl pressed F6");
    public static final KeyStroke KEY_STROKE_GO_SUB_TAB_3 = KeyStroke.getKeyStroke("shift ctrl pressed F7");

    public static final KeyStroke KEY_STROKE_QUIT = KeyStroke.getKeyStroke("ctrl pressed Q");
    public static final KeyStroke KEY_STROKE_DELETE = KeyStroke.getKeyStroke("ctrl pressed U");

    protected static final ImmutableList KEY_STROKE_GO_TABS = ImmutableList.of(
            KEY_STROKE_GO_TAB_1,
            KEY_STROKE_GO_TAB_2,
            KEY_STROKE_GO_TAB_3,
            KEY_STROKE_GO_TAB_4,
            KEY_STROKE_GO_TAB_5,
            KEY_STROKE_GO_TAB_6
    );
    protected static final ImmutableList KEY_STROKE_GO_SUB_TABS = ImmutableList.of(
            KEY_STROKE_GO_SUB_TAB_1,
            KEY_STROKE_GO_SUB_TAB_2,
            KEY_STROKE_GO_SUB_TAB_3
    );

    private static String keyStrokeToStr(KeyStroke actionKey) {
        String result = "";
        if (actionKey != null) {
            result = " (" + StringUtils.removeStart(actionKey.toString().replace("pressed", "+"), "+ ") + ")";
        }
        return result;
    }

    public static void addKeyStore(UIActionSupport action, KeyStroke keyStroke, String label, String shortDescription) {

        String acceleratorStr = "";
        if (keyStroke != null) {
            action.setKeyStroke(keyStroke);
            acceleratorStr = isShowMnemonic() ? keyStrokeToStr(keyStroke) : "";
        }

        action.setText(label == null ? "" : (t(label) + acceleratorStr));
        action.setTooltipText(shortDescription == null ? "" : (t(shortDescription) + acceleratorStr));
    }

    public static void addKeyStroke2(MainUI mainUI, AbstractButton editor, KeyStroke keyStroke) {
        String tip = (String) editor.getClientProperty("toolTipText");
        boolean showMnemonic = isShowMnemonic();
        if (tip != null) {
            if (keyStroke != null && showMnemonic) {
                tip += keyStrokeToStr(keyStroke);
            }
            editor.setToolTipText(tip);
        }

        String text = (String) editor.getClientProperty("text");
        if (text != null) {
            if (keyStroke != null && showMnemonic && !(editor instanceof JMenuItem)) {
                text += keyStrokeToStr(keyStroke);
            }
            editor.setText(text);
        }

        if (text != null || tip != null) {
            String actionId = (String) editor.getClientProperty(GLOBAL_ACTION);
            if (actionId != null) {
                UIActionSupport action = (UIActionSupport) mainUI.getActionMap().get(actionId);
                Objects.requireNonNull(action, "action [" + actionId + "] not found");
                KeyStroke acceleratorKey = action.getAcceleratorKey();
                addKeyStroke(editor, acceleratorKey);
            }
        }

    }

    public static void addKeyStroke(AbstractButton component, KeyStroke actionKey) {

        boolean showMnemonic = isShowMnemonic();
        if (showMnemonic) {
            String actionStr = keyStrokeToStr(actionKey);
            if (component.getText() != null && !component.getText().contains(actionStr)) {
                String text = component.getText() + actionStr;
                component.setText(text);
            }
            if (component.getToolTipText() != null && !component.getToolTipText().contains(actionStr)) {
                String tip = component.getToolTipText() + actionStr;
                component.setToolTipText(tip);
            }
        }
    }

    protected static boolean isShowMnemonic() {
        //FIXME!!!
//        return ObserveClientApplicationContext.get().getConfig().isShowMnemonic();
        return true;
    }

    public static String suffixTextWithKeyStroke(String text, KeyStroke keyStroke) {
        boolean showMnemonic = isShowMnemonic();
        if (showMnemonic) {
            text += keyStrokeToStr(keyStroke);
        }
        return text;
    }

    public static void addKeyStrokeFromMnemonic(AbstractButton editor) {
        int mnemonic = editor.getMnemonic();
        if (mnemonic > 0) {
            if (isShowMnemonic()) {
                String accelerator = " (Alt + " + (char) mnemonic + ")";
                if (editor.getText() != null) {
                    editor.setText(editor.getText() + accelerator);
                }
                if (editor.getToolTipText() != null) {
                    editor.setToolTipText(editor.getToolTipText() + accelerator);
                }
            }
        }
    }

    public static void addKeyStrokeFromMnemonic(JAXXObject jaxxObject) {
        Set done = new LinkedHashSet<>();
        addKeyStrokeFromMnemonic(jaxxObject, done);
    }

    public static void addKeyStrokeToMainTabbedPane(JTabbedPane tabbedPane) {
        if (isShowMnemonic()) {
            int tabCount = tabbedPane.getTabCount();
            for (int i = 0; i < tabCount; i++) {
                String titleAt = tabbedPane.getTitleAt(i);
                tabbedPane.setTitleAt(i, titleAt + keyStrokeToStr(KEY_STROKE_GO_TABS.get(i)));
            }
        }
    }

    public static void addKeyStrokeToSubTabbedPane(JTabbedPane tabbedPane) {
        if (isShowMnemonic()) {
            int tabCount = tabbedPane.getTabCount();
            for (int i = 0; i < tabCount; i++) {
                String titleAt = tabbedPane.getTitleAt(i);
                tabbedPane.setTitleAt(i, titleAt + keyStrokeToStr(KEY_STROKE_GO_SUB_TABS.get(i)));
            }
        }
    }

    protected static void addKeyStrokeFromMnemonic(JAXXObject jaxxObject, Set done) {

        if (done.contains(jaxxObject)) {
            return;
        }
        done.add(jaxxObject);

        jaxxObject.get$objectMap().values().stream().filter(o -> !(o instanceof JMenuItem) && o instanceof AbstractButton).forEach(
                o -> {
                    AbstractButton b = (AbstractButton) o;
                    if (b.getAction() == null || b.getAction().getValue(ACCELERATOR_KEY) == null) {
                        addKeyStrokeFromMnemonic((AbstractButton) o);
                    }
                }
        );

        jaxxObject.get$objectMap().values().stream().filter(o -> o instanceof JAXXObject).forEach(
                o -> addKeyStrokeFromMnemonic((JAXXObject) o, done)
        );
    }


    public static void addKeyStoreToText(AbstractButton editor, MenuActionSupport action) {
        if (editor.getText() != null) {
            editor.setText(suffixTextWithKeyStroke(editor.getText(), action.getAcceleratorKey()));
        }
        if (editor.getToolTipText() != null) {
            editor.setToolTipText(suffixTextWithKeyStroke(editor.getToolTipText(), action.getAcceleratorKey()));
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy