<commit_before>/*
 * Copyright 2016 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package stroom.widget.popup.client.view;

import stroom.data.grid.client.Glass;
import stroom.widget.popup.client.presenter.PopupView.PopupType;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.ui.PopupPanel;

public abstract class AbstractPopupPanel extends PopupPanel implements Popup {
    private final PopupType popupType;
    private final Glass dragGlass = new Glass(
            "popupPanel-dragGlass",
            "popupPanel-dragGlassVisible");

    public AbstractPopupPanel(final boolean autoHide, final boolean modal, final PopupType popupType) {
        super(autoHide, modal);
        this.popupType = popupType;
    }

    public Glass getDragGlass() {
        return dragGlass;
    }

    /**
     * Notify the dialog when either the Enter or Escape key is pressed.
     * For dialogs with a close button, the Escape will cause them to close. The Enter key will close the dialog,
     * with a `true` result. The exception to this is where a keyboard modifier (like Shift) is held. This allows
     * the user to press Enter within a dialog without dismissing it, such as when typing in a multiline text field.
     * @param event
     */
    @Override
    protected void onPreviewNativeEvent(final NativePreviewEvent event) {
        super.onPreviewNativeEvent(event);

        if (event.getTypeInt() == Event.ONKEYDOWN) {
            final NativeEvent nativeEvent = event.getNativeEvent();

            switch (nativeEvent.getKeyCode()) {
                case KeyCodes.KEY_ESCAPE:
                    onEscapeKeyPressed();
                    break;
                case KeyCodes.KEY_ENTER:
                    if (!(nativeEvent.getAltKey() || nativeEvent.getCtrlKey() || nativeEvent.getShiftKey())) {
                        onEnterKeyPressed();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    protected void onEscapeKeyPressed() { }

    protected void onEnterKeyPressed() { }
}<commit_msg>Require Ctrl modifier when dismissing a dialog with the Enter key<commit_after>/*
 * Copyright 2016 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package stroom.widget.popup.client.view;

import stroom.data.grid.client.Glass;
import stroom.widget.popup.client.presenter.PopupView.PopupType;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.ui.PopupPanel;

public abstract class AbstractPopupPanel extends PopupPanel implements Popup {
    private final PopupType popupType;
    private final Glass dragGlass = new Glass(
            "popupPanel-dragGlass",
            "popupPanel-dragGlassVisible");

    public AbstractPopupPanel(final boolean autoHide, final boolean modal, final PopupType popupType) {
        super(autoHide, modal);
        this.popupType = popupType;
    }

    public Glass getDragGlass() {
        return dragGlass;
    }

    /**
     * Notify the dialog when either the Enter or Escape key is pressed.
     * For dialogs with a close button, the Escape will cause them to close.
     * The combination Ctrl+Enter key will close the dialog, with a `true` result.
     */
    @Override
    protected void onPreviewNativeEvent(final NativePreviewEvent event) {
        super.onPreviewNativeEvent(event);

        if (event.getTypeInt() == Event.ONKEYDOWN) {
            final NativeEvent nativeEvent = event.getNativeEvent();

            switch (nativeEvent.getKeyCode()) {
                case KeyCodes.KEY_ESCAPE:
                    onEscapeKeyPressed();
                    break;
                case KeyCodes.KEY_ENTER:
                    // Only close the dialog if the Ctrl modifier is pressed
                    if (nativeEvent.getCtrlKey()) {
                        onEnterKeyPressed();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    protected void onEscapeKeyPressed() { }

    protected void onEnterKeyPressed() { }
}
{'fraction_non_alphanumeric': 0.07806487080813634, 'fraction_numerical': 0.0029320139270661537, 'mean_word_length': 2.5533854166666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4560570', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1411, 'n_words': 476}