<commit_before>/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.monitor.ui;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.mylar.context.core.ContextCorePlugin;
import org.eclipse.mylar.core.MylarStatusHandler;
import org.eclipse.mylar.monitor.core.IInteractionEventListener;
import org.eclipse.mylar.monitor.core.InteractionEvent;
import org.eclipse.ui.IPageListener;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPerspectiveListener;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * @author <NAME>
 */
public class MylarMonitorUiPlugin extends AbstractUIPlugin {

	private static final int TIMEOUT_INACTIVITY_MILLIS = 2 * 60 * 1000;

	private int inactivityTimeout = TIMEOUT_INACTIVITY_MILLIS;

	private static MylarMonitorUiPlugin INSTANCE;

	private ShellLifecycleListener shellLifecycleListener;

	private List<AbstractUserInteractionMonitor> selectionMonitors = new ArrayList<AbstractUserInteractionMonitor>();

	/**
	 * TODO: this could be merged with context interaction events rather than
	 * requiring update from the monitor.
	 */
	private List<IInteractionEventListener> interactionListeners = new ArrayList<IInteractionEventListener>();

	private ActivityContextManager activityContextManager;

	private AbstractUserActivityTimer osActivityTimer = null;

	protected Set<IPartListener> partListeners = new HashSet<IPartListener>();

	protected Set<IPageListener> pageListeners = new HashSet<IPageListener>();

	protected Set<IPerspectiveListener> perspectiveListeners = new HashSet<IPerspectiveListener>();

	protected Set<ISelectionListener> postSelectionListeners = new HashSet<ISelectionListener>();

	public static final String OBFUSCATED_LABEL = "[obfuscated]";

	protected IWindowListener WINDOW_LISTENER = new IWindowListener() {
		public void windowActivated(IWorkbenchWindow window) {
			// ignore
		}

		public void windowDeactivated(IWorkbenchWindow window) {
			// ignore
		}

		public void windowOpened(IWorkbenchWindow window) {
			if (getWorkbench().isClosing()) {
				return;
			}
			for (IPageListener listener : pageListeners) {
				window.addPageListener(listener);
			}
			for (IPartListener listener : partListeners) {
				window.getPartService().addPartListener(listener);
			}
			for (IPerspectiveListener listener : perspectiveListeners) {
				window.addPerspectiveListener(listener);
			}
			for (ISelectionListener listener : postSelectionListeners) {
				window.getSelectionService().addPostSelectionListener(listener);
			}

		}

		public void windowClosed(IWorkbenchWindow window) {
			for (IPageListener listener : pageListeners) {
				window.removePageListener(listener);
			}
			for (IPartListener listener : partListeners) {
				window.getPartService().removePartListener(listener);
			}
			for (IPerspectiveListener listener : perspectiveListeners) {
				window.removePerspectiveListener(listener);
			}
			for (ISelectionListener listener : postSelectionListeners) {
				window.getSelectionService().removePostSelectionListener(listener);
			}
		}
	};

	public MylarMonitorUiPlugin() {
		INSTANCE = this;
	}

	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				try {
					getWorkbench().addWindowListener(WINDOW_LISTENER);
					shellLifecycleListener = new ShellLifecycleListener(ContextCorePlugin.getContextManager());
					getWorkbench().getActiveWorkbenchWindow().getShell().addShellListener(shellLifecycleListener);

					new MonitorUiExtensionPointReader().initExtensions();

					AbstractUserActivityTimer activityTimer = null;
					if (osActivityTimer != null) {
						activityTimer = osActivityTimer;
					} else {
						activityTimer = new WorkbenchUserActivityTimer(TIMEOUT_INACTIVITY_MILLIS);
					}

					activityContextManager = new ActivityContextManager(activityTimer);
					activityContextManager.start();
				} catch (Exception e) {
					MylarStatusHandler.fail(e, "Mylar Monitor start failed", false);
				}
			}
		});
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		try {
			activityContextManager.stop();
			if (getWorkbench() != null && !getWorkbench().isClosing()) {
				getWorkbench().removeWindowListener(WINDOW_LISTENER);
				getWorkbench().getActiveWorkbenchWindow().getShell().removeShellListener(shellLifecycleListener);
			}
		} catch (Exception e) {
			MylarStatusHandler.fail(e, "Mylar Monitor stop failed", false);
		}
		INSTANCE = null;
	}

	public ShellLifecycleListener getShellLifecycleListener() {<commit_msg>RESOLVED - bug 174403: Closing Eclipse cause "Mylar Monitor stop failed" error at times https://bugs.eclipse.org/bugs/show_bug.cgi?id=174403<commit_after>/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.monitor.ui;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.mylar.context.core.ContextCorePlugin;
import org.eclipse.mylar.core.MylarStatusHandler;
import org.eclipse.mylar.monitor.core.IInteractionEventListener;
import org.eclipse.mylar.monitor.core.InteractionEvent;
import org.eclipse.ui.IPageListener;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPerspectiveListener;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * @author <NAME>
 */
public class MylarMonitorUiPlugin extends AbstractUIPlugin {

	private static final int TIMEOUT_INACTIVITY_MILLIS = 2 * 60 * 1000;

	private int inactivityTimeout = TIMEOUT_INACTIVITY_MILLIS;

	private static MylarMonitorUiPlugin INSTANCE;

	private ShellLifecycleListener shellLifecycleListener;

	private List<AbstractUserInteractionMonitor> selectionMonitors = new ArrayList<AbstractUserInteractionMonitor>();

	/**
	 * TODO: this could be merged with context interaction events rather than
	 * requiring update from the monitor.
	 */
	private List<IInteractionEventListener> interactionListeners = new ArrayList<IInteractionEventListener>();

	private ActivityContextManager activityContextManager;

	private AbstractUserActivityTimer osActivityTimer = null;

	protected Set<IPartListener> partListeners = new HashSet<IPartListener>();

	protected Set<IPageListener> pageListeners = new HashSet<IPageListener>();

	protected Set<IPerspectiveListener> perspectiveListeners = new HashSet<IPerspectiveListener>();

	protected Set<ISelectionListener> postSelectionListeners = new HashSet<ISelectionListener>();

	public static final String OBFUSCATED_LABEL = "[obfuscated]";

	protected IWindowListener WINDOW_LISTENER = new IWindowListener() {
		public void windowActivated(IWorkbenchWindow window) {
			// ignore
		}

		public void windowDeactivated(IWorkbenchWindow window) {
			// ignore
		}

		public void windowOpened(IWorkbenchWindow window) {
			if (getWorkbench().isClosing()) {
				return;
			}
			for (IPageListener listener : pageListeners) {
				window.addPageListener(listener);
			}
			for (IPartListener listener : partListeners) {
				window.getPartService().addPartListener(listener);
			}
			for (IPerspectiveListener listener : perspectiveListeners) {
				window.addPerspectiveListener(listener);
			}
			for (ISelectionListener listener : postSelectionListeners) {
				window.getSelectionService().addPostSelectionListener(listener);
			}

		}

		public void windowClosed(IWorkbenchWindow window) {
			for (IPageListener listener : pageListeners) {
				window.removePageListener(listener);
			}
			for (IPartListener listener : partListeners) {
				window.getPartService().removePartListener(listener);
			}
			for (IPerspectiveListener listener : perspectiveListeners) {
				window.removePerspectiveListener(listener);
			}
			for (ISelectionListener listener : postSelectionListeners) {
				window.getSelectionService().removePostSelectionListener(listener);
			}
		}
	};

	public MylarMonitorUiPlugin() {
		INSTANCE = this;
	}

	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				try {
					getWorkbench().addWindowListener(WINDOW_LISTENER);
					shellLifecycleListener = new ShellLifecycleListener(ContextCorePlugin.getContextManager());
					getWorkbench().getActiveWorkbenchWindow().getShell().addShellListener(shellLifecycleListener);

					new MonitorUiExtensionPointReader().initExtensions();

					AbstractUserActivityTimer activityTimer = null;
					if (osActivityTimer != null) {
						activityTimer = osActivityTimer;
					} else {
						activityTimer = new WorkbenchUserActivityTimer(TIMEOUT_INACTIVITY_MILLIS);
					}

					activityContextManager = new ActivityContextManager(activityTimer);
					activityContextManager.start();
				} catch (Exception e) {
					MylarStatusHandler.fail(e, "Mylar Monitor start failed", false);
				}
			}
		});
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		try {
			if (Platform.isRunning()) {
				if (activityContextManager != null) {
					activityContextManager.stop();
				}
				if (getWorkbench() != null && !getWorkbench().isClosing()) {
					getWorkbench().removeWindowListener(WINDOW_LISTENER);
					getWorkbench().getActiveWorkbenchWindow().getShell().removeShellListener(shellLifecycleListener);
				}
			}
		} catch (Exception e) {
			MylarStatusHandler.fail(e, "Mylar Monitor stop failed", false);
		}
		INSTANCE = null;
	}

	public ShellLifecycleListener getShellLifecycleListener() {
{'fraction_non_alphanumeric': 0.11694958628337457, 'fraction_numerical': 0.0042651198498677816, 'mean_word_length': 5.406557377049181, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, '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': '6206981', 'n_tokens_mistral': 3563, 'n_tokens_neox': 3146, 'n_words': 680}