<commit_before>/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.ui_swing.components.frames;

import com.dmdirc.ClientModule.GlobalConfig;
import com.dmdirc.DMDircMBassador;
import com.dmdirc.FrameContainer;
import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
import com.dmdirc.addons.ui_swing.SwingController;
import com.dmdirc.addons.ui_swing.UIUtilities;
import com.dmdirc.addons.ui_swing.actions.InputFieldCopyAction;
import com.dmdirc.addons.ui_swing.actions.SearchAction;
import com.dmdirc.addons.ui_swing.components.SwingSearchBar;
import com.dmdirc.addons.ui_swing.dialogs.paste.PasteDialogFactory;
import com.dmdirc.addons.ui_swing.events.SwingActiveWindowChangeRequestEvent;
import com.dmdirc.addons.ui_swing.events.SwingEventBus;
import com.dmdirc.addons.ui_swing.injection.MainWindow;
import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
import com.dmdirc.addons.ui_swing.textpane.ClickTypeValue;
import com.dmdirc.addons.ui_swing.textpane.MouseEventType;
import com.dmdirc.addons.ui_swing.textpane.TextPane;
import com.dmdirc.addons.ui_swing.textpane.TextPaneControlCodeCopyAction;
import com.dmdirc.addons.ui_swing.textpane.TextPaneCopyAction;
import com.dmdirc.addons.ui_swing.textpane.TextPaneEndAction;
import com.dmdirc.addons.ui_swing.textpane.TextPaneFactory;
import com.dmdirc.addons.ui_swing.textpane.TextPaneHomeAction;
import com.dmdirc.addons.ui_swing.textpane.TextPaneListener;
import com.dmdirc.addons.ui_swing.textpane.TextPanePageDownAction;
import com.dmdirc.addons.ui_swing.textpane.TextPanePageUpAction;
import com.dmdirc.commandparser.PopupManager;
import com.dmdirc.commandparser.PopupMenu;
import com.dmdirc.commandparser.PopupMenuItem;
import com.dmdirc.commandparser.PopupType;
import com.dmdirc.commandparser.parsers.CommandParser;
import com.dmdirc.events.FrameClosingEvent;
import com.dmdirc.events.LinkChannelClickedEvent;
import com.dmdirc.events.LinkNicknameClickedEvent;
import com.dmdirc.events.LinkUrlClickedEvent;
import com.dmdirc.interfaces.CommandController;
import com.dmdirc.interfaces.config.AggregateConfigProvider;
import com.dmdirc.interfaces.config.ConfigChangeListener;
import com.dmdirc.plugins.PluginManager;
import com.dmdirc.ui.IconManager;
import com.dmdirc.ui.messages.ColourManager;
import com.dmdirc.ui.messages.ColourManagerFactory;

import java.awt.Point;
import java.awt.Window;
import java.awt.datatransfer.Clipboard;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Optional;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

import net.engio.mbassy.listener.Handler;

/**
 * Implements a generic (internal) frame.
 */
public abstract class TextFrame extends JPanel implements com.dmdirc.interfaces.ui.Window,
        ConfigChangeListener, TextPaneListener {

    /** A version number for this class. */
    private static final long serialVersionUID = 5;
    /** The channel object that owns this frame. */
    protected final FrameContainer frameParent;
    /** Colour manager. */
    protected final ColourManager colourManager;
    /** Frame output pane. */
    private TextPane textPane;
    /** search bar. */
    private SwingSearchBar searchBar;
    /** Command parser for popup commands. */
    private final CommandParser commandParser;
    /** Swing event bus to post events to. */
    private final SwingEventBus swingEventBus;
    /** Manager to use for building popups. */
    private final PopupManager popupManager;
    /** Bus to despatch events on. */
    private final DMDircMBassador eventBus;
    /** Clipboard to copy and paste from. */
    private final Clipboard clipboard;
    /** Boolean to determine if this frame should be popped out of main client. */
    private boolean popout;
    /** DesktopWindowFrame to use for this TextFrame if it is to be popped out of the client. */
    private DesktopWindowFrame popoutFrame;
    /** Desktop place holder object used if this frame is popped out. */
    private DesktopPlaceHolderFrame popoutPlaceholder;
    /** Icon manager to retrieve icons from. */
    private final IconManager iconManager;

    /**
     * Creates a new instance of Frame.
     *
     * @param owner         FrameContainer owning this frame.
     * @param commandParser The command parser to use for this frame.
     * @param deps          Collection of TextPane dependencies.
     */
    protected TextFrame(
            final FrameContainer owner,
            final CommandParser commandParser,
            final TextFrameDependencies deps) {
        this.swingEventBus = deps.swingEventBus;
        this.popupManager = deps.popupManager;
        this.frameParent = owner;
        this.iconManager = deps.iconManager;
        this.eventBus = deps.eventBus;
        this.commandParser = commandParser;
        this.clipboard = deps.clipboard;
        this.colourManager = deps.colourManagerFactory.getColourManager(owner.getConfigManager());

        final AggregateConfigProvider config = owner.getConfigManager();

        initComponents(deps.textPaneFactory, colourManager);
        setFocusable(true);

        getTextPane().addTextPaneListener(this);

        config.addChangeListener("ui", "foregroundcolour", this);
        config.addChangeListener("ui", "backgroundcolour", this);
        config.addChangeListener("ui", "frameBufferSize", this);
        updateColours();

        setLayout(new MigLayout("fill"));
    }

    /**
     * Determines if this frame should be popped out of the client or not. Once this is set to true
     * it will pop out of the client as a free floating Desktop window.
     *
     * If this is set to false then the desktop window for this frame is disposed of and this frame
     * is returned to the client.
     *
     * @param popout Should this frame pop out?
     */
    public void setPopout(final boolean popout) {
        this.popout = popout;
        if (popout) {
            popoutPlaceholder = new DesktopPlaceHolderFrame();
            popoutFrame = new DesktopWindowFrame(this);
            eventBus.subscribe(popoutFrame);
            popoutFrame.addWindowListener(new WindowAdapter() {

                @Override
                public void windowClosing(final WindowEvent e) {
                    setPopout(false);
                }
            });
            popoutFrame.display();
        } else if (popoutFrame != null) {
            popoutPlaceholder = null;
            popoutFrame.dispose();
            eventBus.unsubscribe(popoutFrame);
            popoutFrame = null;
            // TODO: This is a horrible hack really.
            swingEventBus.publishAsync(new SwingActiveWindowChangeRequestEvent(Optional
                    .ofNullable(this)));
        }
    }

    /**
     * Returns the frame for the free floating desktop window associated with this TextFrame. If one
     * does not exist then null is returned.
     *
     * @return Desktop window frame or null if does not exist
     */
    public DesktopWindowFrame getPopoutFrame() {
        return popoutFrame;
    }

    /**
     * Checks if this frame should be popped out of the client or not. Returns our place holder
     * frame if it is to be used or this TextFrame if it is not to be popped out.
     *
     * @return JPanel to use by the client in the window pane
     */
    public JPanel getDisplayFrame() {
        if (popout) {
            return popoutPlaceholder;
        } else {
            return this;
        }
    }

    /**
     * Called when the frame has been selected in the UI.
     */<commit_msg>Uber defensive--<commit_after>/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.ui_swing.components.frames;

import com.dmdirc.ClientModule.GlobalConfig;
import com.dmdirc.DMDircMBassador;
import com.dmdirc.FrameContainer;
import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
import com.dmdirc.addons.ui_swing.SwingController;
import com.dmdirc.addons.ui_swing.UIUtilities;
import com.dmdirc.addons.ui_swing.actions.InputFieldCopyAction;
import com.dmdirc.addons.ui_swing.actions.SearchAction;
import com.dmdirc.addons.ui_swing.components.SwingSearchBar;
import com.dmdirc.addons.ui_swing.dialogs.paste.PasteDialogFactory;
import com.dmdirc.addons.ui_swing.events.SwingActiveWindowChangeRequestEvent;
import com.dmdirc.addons.ui_swing.events.SwingEventBus;
import com.dmdirc.addons.ui_swing.injection.MainWindow;
import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
import com.dmdirc.addons.ui_swing.textpane.ClickTypeValue;
import com.dmdirc.addons.ui_swing.textpane.MouseEventType;
import com.dmdirc.addons.ui_swing.textpane.TextPane;
import com.dmdirc.addons.ui_swing.textpane.TextPaneControlCodeCopyAction;
import com.dmdirc.addons.ui_swing.textpane.TextPaneCopyAction;
import com.dmdirc.addons.ui_swing.textpane.TextPaneEndAction;
import com.dmdirc.addons.ui_swing.textpane.TextPaneFactory;
import com.dmdirc.addons.ui_swing.textpane.TextPaneHomeAction;
import com.dmdirc.addons.ui_swing.textpane.TextPaneListener;
import com.dmdirc.addons.ui_swing.textpane.TextPanePageDownAction;
import com.dmdirc.addons.ui_swing.textpane.TextPanePageUpAction;
import com.dmdirc.commandparser.PopupManager;
import com.dmdirc.commandparser.PopupMenu;
import com.dmdirc.commandparser.PopupMenuItem;
import com.dmdirc.commandparser.PopupType;
import com.dmdirc.commandparser.parsers.CommandParser;
import com.dmdirc.events.FrameClosingEvent;
import com.dmdirc.events.LinkChannelClickedEvent;
import com.dmdirc.events.LinkNicknameClickedEvent;
import com.dmdirc.events.LinkUrlClickedEvent;
import com.dmdirc.interfaces.CommandController;
import com.dmdirc.interfaces.config.AggregateConfigProvider;
import com.dmdirc.interfaces.config.ConfigChangeListener;
import com.dmdirc.plugins.PluginManager;
import com.dmdirc.ui.IconManager;
import com.dmdirc.ui.messages.ColourManager;
import com.dmdirc.ui.messages.ColourManagerFactory;

import java.awt.Point;
import java.awt.Window;
import java.awt.datatransfer.Clipboard;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Optional;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

import net.engio.mbassy.listener.Handler;

/**
 * Implements a generic (internal) frame.
 */
public abstract class TextFrame extends JPanel implements com.dmdirc.interfaces.ui.Window,
        ConfigChangeListener, TextPaneListener {

    /** A version number for this class. */
    private static final long serialVersionUID = 5;
    /** The channel object that owns this frame. */
    protected final FrameContainer frameParent;
    /** Colour manager. */
    protected final ColourManager colourManager;
    /** Frame output pane. */
    private TextPane textPane;
    /** search bar. */
    private SwingSearchBar searchBar;
    /** Command parser for popup commands. */
    private final CommandParser commandParser;
    /** Swing event bus to post events to. */
    private final SwingEventBus swingEventBus;
    /** Manager to use for building popups. */
    private final PopupManager popupManager;
    /** Bus to despatch events on. */
    private final DMDircMBassador eventBus;
    /** Clipboard to copy and paste from. */
    private final Clipboard clipboard;
    /** Boolean to determine if this frame should be popped out of main client. */
    private boolean popout;
    /** DesktopWindowFrame to use for this TextFrame if it is to be popped out of the client. */
    private DesktopWindowFrame popoutFrame;
    /** Desktop place holder object used if this frame is popped out. */
    private DesktopPlaceHolderFrame popoutPlaceholder;
    /** Icon manager to retrieve icons from. */
    private final IconManager iconManager;

    /**
     * Creates a new instance of Frame.
     *
     * @param owner         FrameContainer owning this frame.
     * @param commandParser The command parser to use for this frame.
     * @param deps          Collection of TextPane dependencies.
     */
    protected TextFrame(
            final FrameContainer owner,
            final CommandParser commandParser,
            final TextFrameDependencies deps) {
        this.swingEventBus = deps.swingEventBus;
        this.popupManager = deps.popupManager;
        this.frameParent = owner;
        this.iconManager = deps.iconManager;
        this.eventBus = deps.eventBus;
        this.commandParser = commandParser;
        this.clipboard = deps.clipboard;
        this.colourManager = deps.colourManagerFactory.getColourManager(owner.getConfigManager());

        final AggregateConfigProvider config = owner.getConfigManager();

        initComponents(deps.textPaneFactory, colourManager);
        setFocusable(true);

        getTextPane().addTextPaneListener(this);

        config.addChangeListener("ui", "foregroundcolour", this);
        config.addChangeListener("ui", "backgroundcolour", this);
        config.addChangeListener("ui", "frameBufferSize", this);
        updateColours();

        setLayout(new MigLayout("fill"));
    }

    /**
     * Determines if this frame should be popped out of the client or not. Once this is set to true
     * it will pop out of the client as a free floating Desktop window.
     *
     * If this is set to false then the desktop window for this frame is disposed of and this frame
     * is returned to the client.
     *
     * @param popout Should this frame pop out?
     */
    public void setPopout(final boolean popout) {
        this.popout = popout;
        if (popout) {
            popoutPlaceholder = new DesktopPlaceHolderFrame();
            popoutFrame = new DesktopWindowFrame(this);
            eventBus.subscribe(popoutFrame);
            popoutFrame.addWindowListener(new WindowAdapter() {

                @Override
                public void windowClosing(final WindowEvent e) {
                    setPopout(false);
                }
            });
            popoutFrame.display();
        } else if (popoutFrame != null) {
            popoutPlaceholder = null;
            popoutFrame.dispose();
            eventBus.unsubscribe(popoutFrame);
            popoutFrame = null;
            // TODO: This is a horrible hack really.
            swingEventBus.publishAsync(new SwingActiveWindowChangeRequestEvent(Optional.of(this)));
        }
    }

    /**
     * Returns the frame for the free floating desktop window associated with this TextFrame. If one
     * does not exist then null is returned.
     *
     * @return Desktop window frame or null if does not exist
     */
    public DesktopWindowFrame getPopoutFrame() {
        return popoutFrame;
    }

    /**
     * Checks if this frame should be popped out of the client or not. Returns our place holder
     * frame if it is to be used or this TextFrame if it is not to be popped out.
     *
     * @return JPanel to use by the client in the window pane
     */
    public JPanel getDisplayFrame() {
        if (popout) {
            return popoutPlaceholder;
        } else {
            return this;
        }
    }

    /**
     * Called when the frame has been selected in the UI.
     */
{'fraction_non_alphanumeric': 0.07999108734402852, 'fraction_numerical': 0.001002673796791444, 'mean_word_length': 3.407807512889762, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3958300', 'n_tokens_mistral': 5198, 'n_tokens_neox': 4990, 'n_words': 1484}