<commit_before>/*
 * Copyright (c) 2011 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */

package com.haulmont.cuba.desktop.sys;

import com.haulmont.cuba.core.global.ConfigProvider;
import com.haulmont.cuba.desktop.App;
import com.haulmont.cuba.desktop.DesktopConfig;
import com.haulmont.cuba.desktop.gui.components.DesktopComponentsHelper;
import com.haulmont.cuba.gui.DialogParams;
import com.haulmont.cuba.gui.ScreenHistorySupport;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.Window;
import com.haulmont.cuba.gui.config.WindowInfo;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.swing.AbstractAction;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.*;

/**
 * <p>$Id$</p>
 *
 * @author krivopustov
 */
public class DesktopWindowManager extends WindowManager {

    private JTabbedPane tabsPane;

    private final Map<JComponent, WindowBreadCrumbs> tabs = new HashMap<JComponent, WindowBreadCrumbs>();
    private final Map<Window, WindowOpenMode> windowOpenMode = new LinkedHashMap<Window, WindowOpenMode>();
    private final Map<WindowBreadCrumbs,Stack<Map.Entry<Window,Integer>>> stacks = new HashMap<WindowBreadCrumbs,Stack<Map.Entry<Window,Integer>>>();
    private final Map<Window,Integer> windows = new HashMap<Window,Integer>();

    private boolean disableSavingScreenHistory;
    private ScreenHistorySupport screenHistorySupport = new ScreenHistorySupport();

    private Log log = LogFactory.getLog(DesktopWindowManager.class);

    public void setTabsPane(final JTabbedPane tabsPane) {
        this.tabsPane = tabsPane;

        tabsPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                KeyStroke.getKeyStroke("control W"),
                "closeTab"
        );
        tabsPane.getActionMap().put(
                "closeTab",
                new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        closeTab((JComponent) tabsPane.getSelectedComponent());
                    }
                }
        );
    }

    private void closeTab(JComponent tabContent) {
        if (tabContent == null)
            return;
        WindowBreadCrumbs breadCrumbs = tabs.get(tabContent);
        Runnable closeTask = new TabCloseTask(breadCrumbs);
        closeTask.run();
    }

    @Override
    public Collection<Window> getOpenWindows() {
        return new ArrayList<Window>(windowOpenMode.keySet());
    }

    @Override
    protected void putToWindowMap(Window window, Integer hashCode) {
        if (window != null) {
            windows.put(window, hashCode);
        }
    }

    private Integer getWindowHashCode(Window window){
       return windows.get(window);
    }

    @Override
    protected Window getWindow(Integer hashCode) {
        Set<Map.Entry<Window, Integer>> set = windows.entrySet();
        for (Map.Entry<Window, Integer> entry : set) {
            if (hashCode.equals(entry.getValue())) {
                return entry.getKey();
            }
        }
        return null;
    }

    @Override
    protected void checkCanOpenWindow(WindowInfo windowInfo, OpenType openType, Map<String, Object> params) {
    }

    protected boolean hasModalWindow() {
        Set<Map.Entry<Window, WindowOpenMode>> openModes = windowOpenMode.entrySet();
        for (Map.Entry<Window, WindowOpenMode> openMode : openModes) {
            if (OpenType.DIALOG.equals(openMode.getValue().getOpenType()))
                return true;
        }
        return false;
    }

    @Override
    protected void showWindow(Window window, String caption, OpenType openType, boolean multipleOpen) {
        showWindow(window, caption, null, openType, multipleOpen);
    }

    @Override
    protected void showWindow(Window window, String caption, String description, OpenType openType, boolean multipleOpen) {
        boolean forciblyDialog = false;
        if (hasModalWindow()) {
            openType = OpenType.DIALOG;
            forciblyDialog = true;
        }
        window.setCaption(caption);
        window.setDescription(description);

        WindowOpenMode openMode = new WindowOpenMode(window, openType);
        Object windowData;

        switch (openType) {
            case NEW_TAB:
                Integer hashCode = getWindowHashCode(window);
                JComponent tab;
                if (hashCode != null && !multipleOpen && (tab = findTab(hashCode)) != null) {
                    int oldTabPosition = -1;
                    for (int i = 0; i < tabsPane.getTabCount(); i++) {
                        if (tab.equals(tabsPane.getComponentAt(i))) {<commit_msg>Refs #807: Opening a new screen from modal screen<commit_after>/*
 * Copyright (c) 2011 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.
 */

package com.haulmont.cuba.desktop.sys;

import com.haulmont.cuba.core.global.ConfigProvider;
import com.haulmont.cuba.desktop.App;
import com.haulmont.cuba.desktop.DesktopConfig;
import com.haulmont.cuba.desktop.gui.components.DesktopComponentsHelper;
import com.haulmont.cuba.gui.DialogParams;
import com.haulmont.cuba.gui.ScreenHistorySupport;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.Window;
import com.haulmont.cuba.gui.config.WindowInfo;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.swing.AbstractAction;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.OpenType;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.*;

/**
 * <p>$Id$</p>
 *
 * @author krivopustov
 */
public class DesktopWindowManager extends WindowManager {

    private JTabbedPane tabsPane;

    private final Map<JComponent, WindowBreadCrumbs> tabs = new HashMap<JComponent, WindowBreadCrumbs>();
    private final Map<Window, WindowOpenMode> windowOpenMode = new LinkedHashMap<Window, WindowOpenMode>();
    private final Map<WindowBreadCrumbs,Stack<Map.Entry<Window,Integer>>> stacks = new HashMap<WindowBreadCrumbs,Stack<Map.Entry<Window,Integer>>>();
    private final Map<Window,Integer> windows = new HashMap<Window,Integer>();

    private boolean disableSavingScreenHistory;
    private ScreenHistorySupport screenHistorySupport = new ScreenHistorySupport();

    private Log log = LogFactory.getLog(DesktopWindowManager.class);

    public void setTabsPane(final JTabbedPane tabsPane) {
        this.tabsPane = tabsPane;

        tabsPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                KeyStroke.getKeyStroke("control W"),
                "closeTab"
        );
        tabsPane.getActionMap().put(
                "closeTab",
                new AbstractAction() {
                    public void actionPerformed(ActionEvent e) {
                        closeTab((JComponent) tabsPane.getSelectedComponent());
                    }
                }
        );
    }

    private void closeTab(JComponent tabContent) {
        if (tabContent == null)
            return;
        WindowBreadCrumbs breadCrumbs = tabs.get(tabContent);
        Runnable closeTask = new TabCloseTask(breadCrumbs);
        closeTask.run();
    }

    @Override
    public Collection<Window> getOpenWindows() {
        return new ArrayList<Window>(windowOpenMode.keySet());
    }

    @Override
    protected void putToWindowMap(Window window, Integer hashCode) {
        if (window != null) {
            windows.put(window, hashCode);
        }
    }

    private Integer getWindowHashCode(Window window){
       return windows.get(window);
    }

    @Override
    protected Window getWindow(Integer hashCode) {
        Set<Map.Entry<Window, Integer>> set = windows.entrySet();
        for (Map.Entry<Window, Integer> entry : set) {
            if (hashCode.equals(entry.getValue())) {
                return entry.getKey();
            }
        }
        return null;
    }

    @Override
    protected void checkCanOpenWindow(WindowInfo windowInfo, OpenType openType, Map<String, Object> params) {
    }

    protected boolean hasModalWindow() {
        Set<Map.Entry<Window, WindowOpenMode>> openModes = windowOpenMode.entrySet();
        for (Map.Entry<Window, WindowOpenMode> openMode : openModes) {
            if (OpenType.DIALOG.equals(openMode.getValue().getOpenType()))
                return true;
        }
        return false;
    }

    @Override
    protected void showWindow(Window window, String caption, OpenType openType, boolean multipleOpen) {
        showWindow(window, caption, null, openType, multipleOpen);
    }

    @Override
    protected void showWindow(Window window, String caption, String description, OpenType openType, boolean multipleOpen) {
        boolean forciblyDialog = false;
        if (openType != OpenType.DIALOG && hasModalWindow()) {
            openType = OpenType.DIALOG;
            forciblyDialog = true;
        }
        window.setCaption(caption);
        window.setDescription(description);

        WindowOpenMode openMode = new WindowOpenMode(window, openType);
        Object windowData;

        switch (openType) {
            case NEW_TAB:
                Integer hashCode = getWindowHashCode(window);
                JComponent tab;
                if (hashCode != null && !multipleOpen && (tab = findTab(hashCode)) != null) {
                    int oldTabPosition = -1;
                    for (int i = 0; i < tabsPane.getTabCount(); i++) {
                        if (tab.equals(tabsPane.getComponentAt(i))) {
{'fraction_non_alphanumeric': 0.10072422336573279, 'fraction_numerical': 0.001429388221841052, 'mean_word_length': 2.9738735327527452, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5454000', 'n_tokens_mistral': 3004, 'n_tokens_neox': 2848, 'n_words': 630}