<commit_before>package org.jboss.reddeer.swt.lookup;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import org.jboss.reddeer.junit.logging.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchSite;
import org.hamcrest.Matcher;
import org.jboss.reddeer.swt.exception.SWTLayerException;
import org.jboss.reddeer.swt.lookup.WidgetResolver;
import org.jboss.reddeer.swt.reference.ReferencedComposite;
import org.jboss.reddeer.swt.matcher.AndMatcher;
import org.jboss.reddeer.swt.matcher.ClassMatcher;
import org.jboss.reddeer.swt.matcher.MatcherBuilder;
import org.jboss.reddeer.swt.util.Display;
import org.jboss.reddeer.swt.util.ObjectUtil;
import org.jboss.reddeer.swt.util.ResultRunnable;

/**
 * Widget Lookup methods contains core lookup and resolving widgets
 * @author <NAME>
 * @author <NAME>
 *
 */
public class WidgetLookup {
	
	private static WidgetLookup instance = null;
	protected final Logger logger = Logger.getLogger(this.getClass());
	
	private WidgetLookup() {
	}
	
	public static WidgetLookup getInstance() {
		if (instance == null) instance = new WidgetLookup();
		return instance;
	}
	
	/**
	 * Checks if widget is enabled
	 * @param widget
	 * @return
	 */
	public boolean isEnabled(Widget widget) {
		boolean ret = true;
		Object o = null;
		try {
			o = ObjectUtil.invokeMethod(widget, "isEnabled");
		} catch (RuntimeException e) {
			return true;
		}
		if (o == null) return ret;
		if (o instanceof Boolean) {
			ret = ((Boolean)o).booleanValue();
		}
		return ret;
	}
	
	/**
	 * Checks if widget is visible
	 * @param widget given widget
	 * @return true if wideget is visible, false otherwise
	 */
	public boolean isVisible(Widget widget) {
		boolean ret = true;
		Object o = null;
		try {
			o = ObjectUtil.invokeMethod(widget, "isVisible");
		} catch (RuntimeException e) {
			throw new SWTLayerException("Runtime error during checking widget visibility");
		}
		if (o == null) return ret;
		if (o instanceof Boolean) {
			ret = ((Boolean)o).booleanValue();
		}
		return ret;
	}
	
	
	/**
	 * Send click notification to a widget
	 * @param widget
	 */
	public void sendClickNotifications(Widget widget) {
		notify(SWT.Selection,widget);
	}

	public void notify(int eventType, Widget widget) {
		Event event = createEvent(widget);
		notify(eventType, event, widget);
		
	}
	
	public void notifyHyperlink(int eventType, Widget widget) {
		Event event = createHyperlinkEvent(widget);
		notify(eventType, event, widget);
		
	}
	
	public void notifyItem(int eventType, int detail, Widget widget, Widget widgetItem) {
		Event event = createEventItem(eventType, detail, widget, widgetItem);
		notify(eventType, event, widget);
		
	}

	private Event createEvent(Widget widget) {
		Event event = new Event();
		event.time = (int) System.currentTimeMillis();
		event.widget = widget;
		event.display = Display.getDisplay();
		return event;
	}
	
	private Event createHyperlinkEvent(Widget widget){
		Event event = new Event();
		event.time = (int) System.currentTimeMillis();
		event.widget = widget;
		event.display = Display.getDisplay();
		event.button=1;
		event.x=0;
		event.y=0;
		return event;
	}
	
	private Event createEventItem(int eventType, int detail, Widget widget, Widget widgetItem) {
		Event event = new Event();
		event.display = Display.getDisplay();
		event.time = (int) System.currentTimeMillis();
		event.item = widgetItem;
		event.widget = widget;
		event.detail = detail;
		event.type = eventType;
		return event;
	}
	
	
	
	private void notify(final int eventType, final Event createEvent, final Widget widget) {
		createEvent.type = eventType;
		
		Display.asyncExec(new Runnable() {
			public void run() {
				if ((widget == null) || widget.isDisposed()) {
					return;
				}
								
				widget.notifyListeners(eventType, createEvent);
			}
		});

		// Wait for synchronization
		Display.syncExec(new Runnable() {
			public void run() {
				// do nothing here
			}
		});
	}
	
	/**
	 * @deprecated As of release 0.4, replaced by {@link #activeWidget(Class, int, Matcher...)}
	 */
	@Deprecated
	public Widget activeShellWidget(Matcher<? extends Widget> matcher, int index) {
		List<? extends Widget> widgets = activeWidgets(new ShellLookup().getActiveShell(), matcher);
		return getProperWidget(widgets, index);
	}

	/**
	 * @deprecated As of release 0.4, replaced by {@link #activeWidget(Class, int, Matcher...)}
	 */
	@Deprecated
	public Widget activeViewWidget(Matcher<? extends Widget> matcher, int index) {
		List<? extends Widget> widgets = activeWidgets(getFocusControl(), matcher);
		return getProperWidget(widgets, index);
	}
	
	/**
	 * @deprecated As of release 0.4, replaced by {@link #activeWidget(Class, int, Matcher...)}
	 */
	@Deprecated
	public Widget activeWidget(Matcher<? extends Widget> matcher, Control activeControl, int index) {
		List<? extends Widget> widgets = activeWidgets(activeControl, matcher);
		return getProperWidget(widgets, index);
	}
	
	/**
	 * @deprecated As of release 0.4, replaced by {@link #activeWidget(Class, int, Matcher...)}}
	 */
	@Deprecated
	public Widget activeWidget(Matcher<? extends Widget> matcher, int index) {
		return getProperWidget(activeWidgets(null, matcher), index);
	}
	
	@SuppressWarnings({ "rawtypes","unchecked" })
	public <T extends Widget> T activeWidget(ReferencedComposite refComposite, Class<T> clazz, int index, Matcher... matchers) {
		ClassMatcher cm = new ClassMatcher(clazz);
		Matcher[] allMatchers = MatcherBuilder.getInstance().addMatcher(matchers, cm);
		AndMatcher am  = new AndMatcher(allMatchers);
		logger.debug("Search for activeWidget of class: " + clazz.getName()
				+  "\n  index: " + index);
		for (int ind = 0 ; ind < matchers.length ; ind++ ){
			logger.debug("Matcher: " + matchers[ind].getClass());
		}
		if(refComposite == null){
			return (T)getProperWidget(activeWidgets(null, am), index);
		}
		return (T)getProperWidget(activeWidgets(refComposite.getControl(), am), index);
	}
<commit_msg>Deprecated methods removed from WidgetLookup (fixes #229)<commit_after>package org.jboss.reddeer.swt.lookup;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import org.jboss.reddeer.junit.logging.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchSite;
import org.hamcrest.Matcher;
import org.jboss.reddeer.swt.exception.SWTLayerException;
import org.jboss.reddeer.swt.lookup.WidgetResolver;
import org.jboss.reddeer.swt.reference.ReferencedComposite;
import org.jboss.reddeer.swt.matcher.AndMatcher;
import org.jboss.reddeer.swt.matcher.ClassMatcher;
import org.jboss.reddeer.swt.matcher.MatcherBuilder;
import org.jboss.reddeer.swt.util.Display;
import org.jboss.reddeer.swt.util.ObjectUtil;
import org.jboss.reddeer.swt.util.ResultRunnable;

/**
 * Widget Lookup methods contains core lookup and resolving widgets
 * @author <NAME>
 * @author <NAME>
 *
 */
public class WidgetLookup {
	
	private static WidgetLookup instance = null;
	protected final Logger logger = Logger.getLogger(this.getClass());
	
	private WidgetLookup() {
	}
	
	public static WidgetLookup getInstance() {
		if (instance == null) instance = new WidgetLookup();
		return instance;
	}
	
	/**
	 * Checks if widget is enabled
	 * @param widget
	 * @return
	 */
	public boolean isEnabled(Widget widget) {
		boolean ret = true;
		Object o = null;
		try {
			o = ObjectUtil.invokeMethod(widget, "isEnabled");
		} catch (RuntimeException e) {
			return true;
		}
		if (o == null) return ret;
		if (o instanceof Boolean) {
			ret = ((Boolean)o).booleanValue();
		}
		return ret;
	}
	
	/**
	 * Checks if widget is visible
	 * @param widget given widget
	 * @return true if wideget is visible, false otherwise
	 */
	public boolean isVisible(Widget widget) {
		boolean ret = true;
		Object o = null;
		try {
			o = ObjectUtil.invokeMethod(widget, "isVisible");
		} catch (RuntimeException e) {
			throw new SWTLayerException("Runtime error during checking widget visibility");
		}
		if (o == null) return ret;
		if (o instanceof Boolean) {
			ret = ((Boolean)o).booleanValue();
		}
		return ret;
	}
	
	
	/**
	 * Send click notification to a widget
	 * @param widget
	 */
	public void sendClickNotifications(Widget widget) {
		notify(SWT.Selection,widget);
	}

	public void notify(int eventType, Widget widget) {
		Event event = createEvent(widget);
		notify(eventType, event, widget);
		
	}
	
	public void notifyHyperlink(int eventType, Widget widget) {
		Event event = createHyperlinkEvent(widget);
		notify(eventType, event, widget);
		
	}
	
	public void notifyItem(int eventType, int detail, Widget widget, Widget widgetItem) {
		Event event = createEventItem(eventType, detail, widget, widgetItem);
		notify(eventType, event, widget);
		
	}

	private Event createEvent(Widget widget) {
		Event event = new Event();
		event.time = (int) System.currentTimeMillis();
		event.widget = widget;
		event.display = Display.getDisplay();
		return event;
	}
	
	private Event createHyperlinkEvent(Widget widget){
		Event event = new Event();
		event.time = (int) System.currentTimeMillis();
		event.widget = widget;
		event.display = Display.getDisplay();
		event.button=1;
		event.x=0;
		event.y=0;
		return event;
	}
	
	private Event createEventItem(int eventType, int detail, Widget widget, Widget widgetItem) {
		Event event = new Event();
		event.display = Display.getDisplay();
		event.time = (int) System.currentTimeMillis();
		event.item = widgetItem;
		event.widget = widget;
		event.detail = detail;
		event.type = eventType;
		return event;
	}
	
	
	
	private void notify(final int eventType, final Event createEvent, final Widget widget) {
		createEvent.type = eventType;
		
		Display.asyncExec(new Runnable() {
			public void run() {
				if ((widget == null) || widget.isDisposed()) {
					return;
				}
								
				widget.notifyListeners(eventType, createEvent);
			}
		});

		// Wait for synchronization
		Display.syncExec(new Runnable() {
			public void run() {
				// do nothing here
			}
		});
	}
	
	
	@SuppressWarnings({ "rawtypes","unchecked" })
	public <T extends Widget> T activeWidget(ReferencedComposite refComposite, Class<T> clazz, int index, Matcher... matchers) {
		ClassMatcher cm = new ClassMatcher(clazz);
		Matcher[] allMatchers = MatcherBuilder.getInstance().addMatcher(matchers, cm);
		AndMatcher am  = new AndMatcher(allMatchers);
		logger.debug("Search for activeWidget of class: " + clazz.getName()
				+  "\n  index: " + index);
		for (int ind = 0 ; ind < matchers.length ; ind++ ){
			logger.debug("Matcher: " + matchers[ind].getClass());
		}
		if(refComposite == null){
			return (T)getProperWidget(activeWidgets(null, am), index);
		}
		return (T)getProperWidget(activeWidgets(refComposite.getControl(), am), index);
	}

{'fraction_non_alphanumeric': 0.12170584045584046, 'fraction_numerical': 0.0016915954415954416, 'mean_word_length': 4.1860572483841185, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3958195', 'n_tokens_mistral': 3885, 'n_tokens_neox': 3526, 'n_words': 908}