<commit_before>/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.airavata.xbaya.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.airavata.common.utils.SwingUtil;
import org.apache.airavata.xbaya.XBayaConfiguration;
import org.apache.airavata.xbaya.XBayaConfiguration.XBayaExecutionMode;
import org.apache.airavata.xbaya.XBayaConstants;
import org.apache.airavata.xbaya.XBayaEngine;
import org.apache.airavata.xbaya.XBayaException;
import org.apache.airavata.xbaya.XBayaRuntimeException;
import org.apache.airavata.xbaya.component.Component;
import org.apache.airavata.xbaya.component.gui.ComponentSelector;
import org.apache.airavata.xbaya.component.gui.ComponentViewer;
import org.apache.airavata.xbaya.event.Event;
import org.apache.airavata.xbaya.event.Event.Type;
import org.apache.airavata.xbaya.event.EventListener;
import org.apache.airavata.xbaya.graph.Node;
import org.apache.airavata.xbaya.graph.Port;
import org.apache.airavata.xbaya.graph.gui.GraphCanvas;
import org.apache.airavata.xbaya.graph.gui.GraphCanvasEvent;
import org.apache.airavata.xbaya.graph.gui.GraphCanvasEvent.GraphCanvasEventType;
import org.apache.airavata.xbaya.graph.gui.GraphCanvasListener;
import org.apache.airavata.xbaya.menues.xbaya.WorkflowFiler;
import org.apache.airavata.xbaya.monitor.MonitorException;
import org.apache.airavata.xbaya.monitor.gui.MonitorPanel;
import org.apache.airavata.xbaya.registrybrowser.JCRBrowserPanel;
import org.apache.airavata.xbaya.wf.Workflow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XBayaGUI implements EventListener, XBayaExecutionModeListener {

    private static final Logger logger = LoggerFactory.getLogger(XBayaGUI.class);

    private static final int STATIC_MENU_ITEMS = 4;

    private XBayaEngine engine;

    private JFrame frame;

    private XBayaMenu menu;

    private List<GraphCanvas> graphCanvases = new LinkedList<GraphCanvas>();

    private PortViewer portViewer;

    private ComponentViewer componentViewer;

    private ComponentSelector componentSelector;

    private MonitorPanel monitorPane;

    private XBayaToolBar toolbar;

    private ErrorWindow errorWindow;

    private JTabbedPane rightBottomTabbedPane;

    private JTabbedPane graphTabbedPane;

    private boolean graphPanelMaximized;

    private int previousMainDividerLocation;

    private int previousRightDividerLocation;

    private JSplitPane mainSplitPane;

    private JSplitPane leftSplitPane;

    private JSplitPane rightSplitPane;

    private JTabbedPane componentTabbedPane;

    private ScrollPanel compTreeXBayapanel;

	private WorkflowFiler graphFiler;

    /**
     * Constructs an XBayaEngine.
     * 
     * @param engine
     */
    public XBayaGUI(XBayaEngine engine) {
        this.engine = engine;
        this.engine.getMonitor().addEventListener(this);
        graphFiler = new WorkflowFiler(engine);
        engine.getConfiguration().registerExecutionModeChangeListener(this);

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    init();
                }
            });
        } catch (InterruptedException e) {
            // Shouldn't happen.
            throw new XBayaRuntimeException(e);
        } catch (InvocationTargetException e) {
            // Shouldn't happen.
            throw new XBayaRuntimeException(e);
        }
        
        // Following suppsed to jump in the middle to save unsaved workflows when exiting xbaya
        // but its not working because the UI is already disposed it seems :(
//        Runtime.getRuntime().addShutdownHook(new Thread(){<commit_msg>comment on a exception<commit_after>/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.airavata.xbaya.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.airavata.common.utils.SwingUtil;
import org.apache.airavata.xbaya.XBayaConfiguration;
import org.apache.airavata.xbaya.XBayaConfiguration.XBayaExecutionMode;
import org.apache.airavata.xbaya.XBayaConstants;
import org.apache.airavata.xbaya.XBayaEngine;
import org.apache.airavata.xbaya.XBayaException;
import org.apache.airavata.xbaya.XBayaRuntimeException;
import org.apache.airavata.xbaya.component.Component;
import org.apache.airavata.xbaya.component.gui.ComponentSelector;
import org.apache.airavata.xbaya.component.gui.ComponentViewer;
import org.apache.airavata.xbaya.event.Event;
import org.apache.airavata.xbaya.event.Event.Type;
import org.apache.airavata.xbaya.event.EventListener;
import org.apache.airavata.xbaya.graph.Node;
import org.apache.airavata.xbaya.graph.Port;
import org.apache.airavata.xbaya.graph.gui.GraphCanvas;
import org.apache.airavata.xbaya.graph.gui.GraphCanvasEvent;
import org.apache.airavata.xbaya.graph.gui.GraphCanvasEvent.GraphCanvasEventType;
import org.apache.airavata.xbaya.graph.gui.GraphCanvasListener;
import org.apache.airavata.xbaya.menues.xbaya.WorkflowFiler;
import org.apache.airavata.xbaya.monitor.MonitorException;
import org.apache.airavata.xbaya.monitor.gui.MonitorPanel;
import org.apache.airavata.xbaya.registrybrowser.JCRBrowserPanel;
import org.apache.airavata.xbaya.wf.Workflow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class XBayaGUI implements EventListener, XBayaExecutionModeListener {

    private static final Logger logger = LoggerFactory.getLogger(XBayaGUI.class);

    private static final int STATIC_MENU_ITEMS = 4;

    private XBayaEngine engine;

    private JFrame frame;

    private XBayaMenu menu;

    private List<GraphCanvas> graphCanvases = new LinkedList<GraphCanvas>();

    private PortViewer portViewer;

    private ComponentViewer componentViewer;

    private ComponentSelector componentSelector;

    private MonitorPanel monitorPane;

    private XBayaToolBar toolbar;

    private ErrorWindow errorWindow;

    private JTabbedPane rightBottomTabbedPane;

    private JTabbedPane graphTabbedPane;

    private boolean graphPanelMaximized;

    private int previousMainDividerLocation;

    private int previousRightDividerLocation;

    private JSplitPane mainSplitPane;

    private JSplitPane leftSplitPane;

    private JSplitPane rightSplitPane;

    private JTabbedPane componentTabbedPane;

    private ScrollPanel compTreeXBayapanel;

	private WorkflowFiler graphFiler;

    /**
     * Constructs an XBayaEngine.
     * 
     * @param engine
     */
    public XBayaGUI(XBayaEngine engine) {
        this.engine = engine;
        this.engine.getMonitor().addEventListener(this);
        graphFiler = new WorkflowFiler(engine);
        engine.getConfiguration().registerExecutionModeChangeListener(this);

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    init();
                }
            });
        } catch (InterruptedException e) {
            // Shouldn't happen.
            throw new XBayaRuntimeException(e);
        } catch (InvocationTargetException e) {
            // Shouldn't happen.
        	//It happened
        	/* exception occurs when xbaya is opened twice from the jvm
        	 * org.apache.airavata.xbaya.XBayaRuntimeException: java.lang.reflect.InvocationTargetException
				at org.apache.airavata.xbaya.gui.XBayaGUI.<init>(XBayaGUI.java:148)
				at org.apache.airavata.xbaya.XBayaEngine.<init>(XBayaEngine.java:106)
				at org.apache.airavata.xbaya.XBaya.<init>(XBaya.java:51)
				at org.ogce.paramchem.XBayaLauncher.run(XBayaLauncher.java:44)
				at java.lang.Thread.run(Thread.java:662)
			Caused by: java.lang.reflect.InvocationTargetException
				at java.awt.EventQueue.invokeAndWait(EventQueue.java:1042)
				at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1326)
				at org.apache.airavata.xbaya.gui.XBayaGUI.<init>(XBayaGUI.java:138)
				... 4 more
        	 */
            throw new XBayaRuntimeException(e);
        }
        
        // Following suppsed to jump in the middle to save unsaved workflows when exiting xbaya
        // but its not working because the UI is already disposed it seems :(
//        Runtime.getRuntime().addShutdownHook(new Thread(){
{'fraction_non_alphanumeric': 0.08417419012214551, 'fraction_numerical': 0.0034519383961763143, 'mean_word_length': 3.995137046861185, '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': '6649814', 'n_tokens_mistral': 3390, 'n_tokens_neox': 3274, 'n_words': 769}