<commit_before>/*
 *  RapidMiner
 *
 *  Copyright (C) 2001-2013 by Rapid-I and the contributors
 *
 *  Complete list of developers available at our web site:
 *
 *       http://rapid-i.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */
package com.rapidminer.gui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.logging.Level;

import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToggleButton;

import com.rapidminer.gui.actions.WorkspaceAction;
import com.rapidminer.gui.docking.RapidDockingToolbar;
import com.rapidminer.gui.processeditor.ProcessLogTab;
import com.rapidminer.gui.processeditor.results.ResultTab;
import com.rapidminer.gui.tools.ResourceAction;
import com.rapidminer.gui.tools.ResourceMenu;
import com.rapidminer.gui.tools.SwingTools;
import com.rapidminer.tools.FileSystemService;
import com.rapidminer.tools.I18N;
import com.rapidminer.tools.LogService;
import com.vlsolutions.swing.docking.Dockable;
import com.vlsolutions.swing.docking.DockableResolver;
import com.vlsolutions.swing.docking.DockableState;
import com.vlsolutions.swing.docking.DockingContext;
import com.vlsolutions.swing.docking.DockingDesktop;
import com.vlsolutions.swing.docking.ws.WSDockKey;

/** Collection of {@link Perspective}s that can be applied, saved, created.
 * 
 * @author <NAME>
 *
 */
public abstract class ApplicationPerspectives {

    public final Action RESTORE_DEFAULT_ACTION = new ResourceAction("restore_predefined_perspective_default") {
        private static final long serialVersionUID = 1L;
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!current.isUserDefined()) {
                restoreDefault(current.getName());
                current.apply(context);
            }
        }
    };

    private final ButtonGroup workspaceButtonGroup = new ButtonGroup();

    private final JMenu workspaceMenu = new ResourceMenu("perspectives");

    private final ButtonGroup workspaceMenuGroup = new ButtonGroup();

    private Perspective current;

    private final DockingContext context;

    private final Map<String,Perspective> perspectives = new LinkedHashMap<String,Perspective>();

    private RapidDockingToolbar workspaceToolBar;

    public ApplicationPerspectives(DockingContext context) {
        this.context = context;
        context.setDockableResolver(new DockableResolver() {
            @Override
            public Dockable resolveDockable(String key) {
                if (key.startsWith(ResultTab.DOCKKEY_PREFIX)) {
                    ResultTab tab = new ResultTab(key);
                    tab.showResult(null);
                    return tab;
                } else if (key.startsWith(ProcessLogTab.DOCKKEY_PREFIX)) {
                    ProcessLogTab tab = new ProcessLogTab(key);
                    tab.setDataTableViewer(null);
                    return tab;
                } else {
                    return null;
                }
            }
        });
        this.makePredefined();
    }

    public void showPerspective(Perspective perspective) {
        if (current == perspective) {
            return;
        }
        if (current != null) {
            current.store(context);
        }
        perspective.apply(context);
        current = perspective;
        RESTORE_DEFAULT_ACTION.setEnabled(!current.isUserDefined());
        
        
        //TODO: change to listener mechanism
        MainFrame mainFrame = RapidMinerGUI.getMainFrame();
        if(mainFrame != null) {
        	// check all ConditionalActions on perspective switch
        	mainFrame.getActions().enableActions();
        	
        	// try to request focus for the process renderer so actions are enabled after perspective switch and
        	// ProcessRenderer is visible
        	if (mainFrame.getProcessPanel().getProcessRenderer().isShowing()) {
        		mainFrame.getProcessPanel().getProcessRenderer().requestFocusInWindow();
        	}
        	
        	// disable close action on the result dockable when in pre-defined results perspective
        	if (perspective.getName().contains("result")) {
        		mainFrame.getResultDisplay().getDockKey().setCloseEnabled(false);
        	} else {
        		mainFrame.getResultDisplay().getDockKey().setCloseEnabled(true);
        	}
        }
    }

    public JMenu getWorkspaceMenu() {
        return workspaceMenu;
    }

    public RapidDockingToolbar getWorkspaceToolBar() {
        if (workspaceToolBar == null) {
            workspaceToolBar = new RapidDockingToolbar("perspectives");
            Enumeration<AbstractButton> e = workspaceButtonGroup.getElements();
            while (e.hasMoreElements()) {
                AbstractButton b = e.nextElement();
                workspaceToolBar.add(b);
            }
        }
        return workspaceToolBar;
    }

    /**
     * Checks if the given string is valid as name of a new perspective.
     * @param name
     * @return validity
     */
    public boolean isValidName(String name) {
        if (name == null) {<commit_msg>Synchronization<commit_after>/*
 *  RapidMiner
 *
 *  Copyright (C) 2001-2013 by Rapid-I and the contributors
 *
 *  Complete list of developers available at our web site:
 *
 *       http://rapid-i.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */
package com.rapidminer.gui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.logging.Level;

import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToggleButton;

import com.rapidminer.gui.actions.WorkspaceAction;
import com.rapidminer.gui.docking.RapidDockingToolbar;
import com.rapidminer.gui.processeditor.ProcessLogTab;
import com.rapidminer.gui.processeditor.results.ResultTab;
import com.rapidminer.gui.tools.ResourceAction;
import com.rapidminer.gui.tools.ResourceMenu;
import com.rapidminer.gui.tools.SwingTools;
import com.rapidminer.tools.FileSystemService;
import com.rapidminer.tools.I18N;
import com.rapidminer.tools.LogService;
import com.vlsolutions.swing.docking.Dockable;
import com.vlsolutions.swing.docking.DockableResolver;
import com.vlsolutions.swing.docking.DockableState;
import com.vlsolutions.swing.docking.DockingContext;
import com.vlsolutions.swing.docking.DockingDesktop;
import com.vlsolutions.swing.docking.ws.WSDockKey;

/** Collection of {@link Perspective}s that can be applied, saved, created.
 * 
 * @author <NAME>
 *
 */
public abstract class ApplicationPerspectives {

    public final Action RESTORE_DEFAULT_ACTION = new ResourceAction("restore_predefined_perspective_default") {
        private static final long serialVersionUID = 1L;
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!current.isUserDefined()) {
                restoreDefault(current.getName());
                current.apply(context);
            }
        }
    };

    private final ButtonGroup workspaceButtonGroup = new ButtonGroup();

    private final JMenu workspaceMenu = new ResourceMenu("perspectives");

    private final ButtonGroup workspaceMenuGroup = new ButtonGroup();

    private Perspective current;

    private final DockingContext context;

    private final Map<String,Perspective> perspectives = new LinkedHashMap<String,Perspective>();

    private RapidDockingToolbar workspaceToolBar;

    public ApplicationPerspectives(DockingContext context) {
        this.context = context;
        context.setDockableResolver(new DockableResolver() {
            @Override
            public Dockable resolveDockable(String key) {
                if (key.startsWith(ResultTab.DOCKKEY_PREFIX)) {
                    ResultTab tab = new ResultTab(key);
                    tab.showResult(null);
                    return tab;
                } else if (key.startsWith(ProcessLogTab.DOCKKEY_PREFIX)) {
                    ProcessLogTab tab = new ProcessLogTab(key);
                    tab.setDataTableViewer(null);
                    return tab;
                } else {
                    return null;
                }
            }
        });
        this.makePredefined();
    }

    public void showPerspective(Perspective perspective) {
        if (current == perspective) {
            return;
        }
        if (current != null) {
            current.store(context);
        }
        perspective.apply(context);
        current = perspective;
        RESTORE_DEFAULT_ACTION.setEnabled(!current.isUserDefined());
        
        
        //TODO: change to listener mechanism
        MainFrame mainFrame = RapidMinerGUI.getMainFrame();
        if(mainFrame != null) {
        	
        	// check all ConditionalActions on perspective switch
        	mainFrame.getActions().enableActions();
        	
        	// try to request focus for the process renderer so actions are enabled after perspective switch and
        	// ProcessRenderer is visible
        	if (mainFrame.getProcessPanel().getProcessRenderer().isShowing()) {
        		mainFrame.getProcessPanel().getProcessRenderer().requestFocusInWindow();
        	}
        	
        	// disable close action on the result dockable when in pre-defined results perspective
        	//TODO: commented out because a fast switch between perspectives during this call breaks the Dockable (turns blue)
        	/*
        	if (perspective.getName().contains("result")) {
        		mainFrame.getResultDisplay().getDockKey().setCloseEnabled(false);
        	} else {
        		mainFrame.getResultDisplay().getDockKey().setCloseEnabled(true);
        	}*/
        }
    }

    public JMenu getWorkspaceMenu() {
        return workspaceMenu;
    }

    public RapidDockingToolbar getWorkspaceToolBar() {
        if (workspaceToolBar == null) {
            workspaceToolBar = new RapidDockingToolbar("perspectives");
            Enumeration<AbstractButton> e = workspaceButtonGroup.getElements();
            while (e.hasMoreElements()) {
                AbstractButton b = e.nextElement();
                workspaceToolBar.add(b);
            }
        }
        return workspaceToolBar;
    }

    /**
     * Checks if the given string is valid as name of a new perspective.
     * @param name
     * @return validity
     */
    public boolean isValidName(String name) {
        if (name == null) {
{'fraction_non_alphanumeric': 0.08561975468043899, 'fraction_numerical': 0.001936733376371853, 'mean_word_length': 2.793388429752066, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '7584269', 'n_tokens_mistral': 3398, 'n_tokens_neox': 3305, 'n_words': 859}