<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers 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
 *******************************************************************************/

package org.eclipse.mylyn.internal.context.ui;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.mylyn.context.ui.InterestFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.navigator.CommonViewer;
import org.eclipse.ui.progress.UIJob;

/**
 * @author <NAME>
 */
public class BrowseFilteredListener implements MouseListener, KeyListener {

	private StructuredViewer viewer;

	public BrowseFilteredListener(StructuredViewer viewer) {
		this.viewer = viewer;
	}

	/**
	 * @param treeViewer
	 *            cannot be null
	 * @param targetSelection
	 *            cannot be null
	 */
	public void unfilterSelection(TreeViewer treeViewer, IStructuredSelection targetSelection) {
		InterestFilter filter = getInterestFilter(treeViewer);
		Object targetObject = targetSelection.getFirstElement();
		if (targetObject != null) {
			filter.setTemporarilyUnfiltered(targetObject);
			if (targetObject instanceof Tree) {
				treeViewer.refresh();
			} else {
				treeViewer.refresh(targetObject, true);
				treeViewer.expandToLevel(targetObject, 1);
			}
		}
	}

	private void unfilter(final InterestFilter filter, final TreeViewer treeViewer, Object targetObject) {
		if (targetObject != null) {
			filter.setTemporarilyUnfiltered(targetObject);
			if (targetObject instanceof Tree) {
				treeViewer.refresh();
			} else {
				treeViewer.refresh(targetObject, true);
				treeViewer.expandToLevel(targetObject, 1);
			}
		}
	}

	public void keyPressed(KeyEvent event) {
		// ignore
	}

	public void keyReleased(KeyEvent event) {
		InterestFilter filter = getInterestFilter(viewer);

		if (event.keyCode == SWT.ARROW_RIGHT) {
			if (filter == null || !(viewer instanceof TreeViewer)) {
				return;
			}

			final TreeViewer treeViewer = (TreeViewer) viewer;
			ISelection selection = treeViewer.getSelection();
			if (selection instanceof IStructuredSelection) {
				Object targetObject = ((IStructuredSelection) selection).getFirstElement();
				unfilter(filter, treeViewer, targetObject);
			}
		}
	}

	public void mouseDown(MouseEvent event) {
		// ignore
	}

	public void mouseDoubleClick(MouseEvent e) {
		// ignore
	}

	public void mouseUp(MouseEvent event) {
		final InterestFilter filter = getInterestFilter(viewer);
		if (filter == null || !(viewer instanceof TreeViewer)) {
			return;
		}

		TreeViewer treeViewer = (TreeViewer) viewer;
		Object selectedObject = null;
		Object clickedObject = getClickedItem(event);
		if (clickedObject != null) {
			selectedObject = clickedObject;
		} else {
			selectedObject = treeViewer.getTree();
		}

		if (isUnfilterEvent(event)) {
			if (treeViewer instanceof CommonViewer) {
				CommonViewer commonViewer = (CommonViewer) treeViewer;
				commonViewer.setSelection(new StructuredSelection(selectedObject), true);
			}
			unfilter(filter, treeViewer, selectedObject);
		} else {
			if (event.button == 1) {
				if ((event.stateMask & SWT.MOD1) != 0) {
					viewer.refresh(selectedObject);
				} else {
					final Object unfiltered = filter.getTemporarilyUnfiltered();
					if (unfiltered != null) {
						// NOTE: delaying refresh to ensure double click is handled, see bug 208702
						new UIJob("") {
							@Override
							public IStatus runInUIThread(IProgressMonitor monitor) {
								filter.resetTemporarilyUnfiltered();
								viewer.refresh(unfiltered);
								return Status.OK_STATUS;
							}
						}.schedule(OS.GetDoubleClickTime() + 50);	
					}
				}
			}<commit_msg>RESOLVED - bug 208702: support Alt+ double click and Ctrl+click https://bugs.eclipse.org/bugs/show_bug.cgi?id=208702<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers 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
 *******************************************************************************/

package org.eclipse.mylyn.internal.context.ui;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.mylyn.context.ui.InterestFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.navigator.CommonViewer;
import org.eclipse.ui.progress.UIJob;

/**
 * @author <NAME>
 */
public class BrowseFilteredListener implements MouseListener, KeyListener {

	private StructuredViewer viewer;

	public BrowseFilteredListener(StructuredViewer viewer) {
		this.viewer = viewer;
	}

	/**
	 * @param treeViewer
	 *            cannot be null
	 * @param targetSelection
	 *            cannot be null
	 */
	public void unfilterSelection(TreeViewer treeViewer, IStructuredSelection targetSelection) {
		InterestFilter filter = getInterestFilter(treeViewer);
		Object targetObject = targetSelection.getFirstElement();
		if (targetObject != null) {
			filter.setTemporarilyUnfiltered(targetObject);
			if (targetObject instanceof Tree) {
				treeViewer.refresh();
			} else {
				treeViewer.refresh(targetObject, true);
				treeViewer.expandToLevel(targetObject, 1);
			}
		}
	}

	private void unfilter(final InterestFilter filter, final TreeViewer treeViewer, Object targetObject) {
		if (targetObject != null) {
			filter.setTemporarilyUnfiltered(targetObject);
			if (targetObject instanceof Tree) {
				treeViewer.refresh();
			} else {
				treeViewer.refresh(targetObject, true);
				treeViewer.expandToLevel(targetObject, 1);
			}
		}
	}

	public void keyPressed(KeyEvent event) {
		// ignore
	}

	public void keyReleased(KeyEvent event) {
		InterestFilter filter = getInterestFilter(viewer);

		if (event.keyCode == SWT.ARROW_RIGHT) {
			if (filter == null || !(viewer instanceof TreeViewer)) {
				return;
			}

			final TreeViewer treeViewer = (TreeViewer) viewer;
			ISelection selection = treeViewer.getSelection();
			if (selection instanceof IStructuredSelection) {
				Object targetObject = ((IStructuredSelection) selection).getFirstElement();
				unfilter(filter, treeViewer, targetObject);
			}
		}
	}

	public void mouseDown(MouseEvent event) {
		// ignore
	}

	public void mouseDoubleClick(MouseEvent e) {
		// ignore
	}

	public void mouseUp(MouseEvent event) {
		final InterestFilter filter = getInterestFilter(viewer);
		if (filter == null || !(viewer instanceof TreeViewer)) {
			return;
		}

		TreeViewer treeViewer = (TreeViewer) viewer;
		Object selectedObject = null;
		Object clickedObject = getClickedItem(event);
		if (clickedObject != null) {
			selectedObject = clickedObject;
		} else {
			selectedObject = treeViewer.getTree();
		}

		if (isUnfilterEvent(event)) {
			if (treeViewer instanceof CommonViewer) {
				CommonViewer commonViewer = (CommonViewer) treeViewer;
				commonViewer.setSelection(new StructuredSelection(selectedObject), true);
			}
			unfilter(filter, treeViewer, selectedObject);
		} else {
			if (event.button == 1) {
				if ((event.stateMask & SWT.MOD1) != 0) {
					viewer.refresh(selectedObject);
				} else {
					final Object unfiltered = filter.getTemporarilyUnfiltered();
					if (unfiltered != null) {
						// NOTE: delaying refresh to ensure double click is handled, see bug 208702
						
						// FIXME this API is windows specific
						//OS.GetDoubleClickTime()
						int doubleClickDelay = 400;
						
						new UIJob("") {
							@Override
							public IStatus runInUIThread(IProgressMonitor monitor) {
								filter.resetTemporarilyUnfiltered();
								viewer.refresh(unfiltered);
								return Status.OK_STATUS;
							}
						}.schedule(doubleClickDelay + 50);	
					}
				}
			}
{'fraction_non_alphanumeric': 0.13187954309449637, 'fraction_numerical': 0.00695742471443406, 'mean_word_length': 4.268599562363239, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '685206', 'n_tokens_mistral': 3345, 'n_tokens_neox': 2793, 'n_words': 633}