<commit_before>package de.fxdiagram.swtfx;

import static de.fxdiagram.swtfx.PrivateFieldAccessor.getPrivateField;

import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.GestureEvent;
import org.eclipse.swt.events.GestureListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.sun.javafx.tk.TKSceneListener;

import javafx.application.Platform;
import javafx.embed.swt.FXCanvas;
import javafx.event.EventType;
import javafx.scene.input.RotateEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.input.SwipeEvent;
import javafx.scene.input.ZoomEvent;

/**
 * A gesture listener that converts and transfers SWT {@link GestureEvent}s to an {@link FXCanvas}.
 *  
 * @author <NAME>
 */
public class SwtToFXGestureConverter implements GestureListener {

	enum StateType {
		IDLE, SCROLLING, ROTATING, ZOOMING;
	}
	
	protected class State {
		StateType type;
		
		double totalScrollX = 0;
		double totalScrollY = 0;
		
		double lastZoomFactor = 1;
		double lastRotation = 0;
		
		public State(StateType type) {
			this.type = type;
		}
	}
	
	private Listener mouseWheelEmulatedEventFilter;

	private FXCanvas canvas;
	
	private State currentState;

	public SwtToFXGestureConverter(final FXCanvas canvas) {
		this.canvas = canvas;
		this.currentState = new State(StateType.IDLE);
		canvas.addGestureListener(this);
		Display display = canvas.getDisplay();
		if (display.getTouchEnabled()) {
			// register a filter to suppress emulated scroll events that
			// originate from mouse wheel events on devices that support touch
			// events (see #430940)
			mouseWheelEmulatedEventFilter = new Listener() {
				@Override
				public void handleEvent(Event event) {
					if (event.widget == canvas) {
						event.type = SWT.None;
					}
				}
			};
			display.addFilter(SWT.MouseVerticalWheel,
					mouseWheelEmulatedEventFilter);
			display.addFilter(SWT.MouseHorizontalWheel,
					mouseWheelEmulatedEventFilter);
		}
	}	
	
	public void dispose() {
		if(!canvas.isDisposed()) {
			canvas.removeGestureListener(this);
			canvas.dispose();
		}
		Display display = Display.getDefault();
		if (mouseWheelEmulatedEventFilter != null) {
			display.removeFilter(SWT.MouseVerticalWheel,
					mouseWheelEmulatedEventFilter);
			display.removeFilter(SWT.MouseHorizontalWheel,
					mouseWheelEmulatedEventFilter);
		}
	}

	@Override
	public void gesture(GestureEvent event) {
		sendGestureEventToFX(event);
	}

	protected void sendGestureEventToFX(final GestureEvent event) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
            	final Object scenePeer = getPrivateField(canvas, "scenePeer");
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
                    @Override
                    public Void run() {
                    	TKSceneListener sceneListener = getPrivateField(scenePeer, "sceneListener");
						if (sceneListener == null) {
                            return null;
                        }
						switch(event.detail) {
							case SWT.GESTURE_BEGIN:
								break;
							case SWT.GESTURE_END:
								changeState(StateType.IDLE, event, sceneListener);<commit_msg>transform mouse wheel events to scroll events<commit_after>package de.fxdiagram.swtfx;

import static de.fxdiagram.swtfx.PrivateFieldAccessor.getPrivateField;

import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.GestureEvent;
import org.eclipse.swt.events.GestureListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseWheelListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.sun.javafx.tk.TKSceneListener;

import javafx.application.Platform;
import javafx.embed.swt.FXCanvas;
import javafx.event.EventType;
import javafx.scene.input.RotateEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.input.SwipeEvent;
import javafx.scene.input.ZoomEvent;

/**
 * A gesture listener that converts and transfers SWT {@link GestureEvent}s to an {@link FXCanvas}.
 *  
 * @author <NAME>
 */
public class SwtToFXGestureConverter implements GestureListener, MouseWheelListener {

	enum StateType {
		IDLE, SCROLLING, ROTATING, ZOOMING;
	}
	
	protected class State {
		StateType type;
		
		double totalScrollX = 0;
		double totalScrollY = 0;
		
		double lastZoomFactor = 1;
		double lastRotation = 0;
		
		public State(StateType type) {
			this.type = type;
		}
	}
	
	private Listener mouseWheelEmulatedEventFilter;

	private FXCanvas canvas;
	
	private State currentState;

	public SwtToFXGestureConverter(final FXCanvas canvas) {
		this.canvas = canvas;
		this.currentState = new State(StateType.IDLE);
		canvas.addGestureListener(this);
		canvas.addMouseWheelListener(this);
		Display display = canvas.getDisplay();
		if (display.getTouchEnabled()) {
			// register a filter to suppress emulated scroll events that
			// originate from mouse wheel events on devices that support touch
			// events (see #430940)
			mouseWheelEmulatedEventFilter = new Listener() {
				@Override
				public void handleEvent(Event event) {
					if (event.widget == canvas) {
						event.type = SWT.None;
					}
				}
			};
			display.addFilter(SWT.MouseVerticalWheel,
					mouseWheelEmulatedEventFilter);
			display.addFilter(SWT.MouseHorizontalWheel,
					mouseWheelEmulatedEventFilter);
		}
	}	
	
	public void dispose() {
		if(!canvas.isDisposed()) {
			canvas.removeGestureListener(this);
			canvas.removeMouseWheelListener(this);
			canvas.dispose();
		}
		Display display = Display.getDefault();
		if (mouseWheelEmulatedEventFilter != null) {
			display.removeFilter(SWT.MouseVerticalWheel,
					mouseWheelEmulatedEventFilter);
			display.removeFilter(SWT.MouseHorizontalWheel,
					mouseWheelEmulatedEventFilter);
		}
	}

	@Override
	public void gesture(GestureEvent event) {
		sendGestureEventToFX(event);
	}

	@Override
	public void mouseScrolled(final MouseEvent e) {
		Platform.runLater(new Runnable() {
            @Override
            public void run() {
            	final Object scenePeer = getPrivateField(canvas, "scenePeer");
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
                    @Override
                    public Void run() {
                    	TKSceneListener sceneListener = getPrivateField(scenePeer, "sceneListener");
						if (sceneListener == null) {
                            return null;
                        }
						Event mockEvent = new Event();
						mockEvent.stateMask = e.stateMask;
						mockEvent.x = e.x;
						mockEvent.y = e.y;
						mockEvent.xDirection = 0;
						mockEvent.yDirection = e.count;
						mockEvent.widget = e.widget;
						mockEvent.display = e.display;
						mockEvent.time = e.time;
						mockEvent.data = e.data;
						GestureEvent mockGestureEvent = new GestureEvent(mockEvent);
						mockGestureEvent.stateMask = e.stateMask;
						sendScrollEvent(ScrollEvent.SCROLL_STARTED, mockGestureEvent, sceneListener);
						sendScrollEvent(ScrollEvent.SCROLL_FINISHED, mockGestureEvent, sceneListener);
                        return null;
                    }

                }, (AccessControlContext) getPrivateField(scenePeer, "accessCtrlCtx"));
            }
        });
	}
	
	protected void sendGestureEventToFX(final GestureEvent event) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
            	final Object scenePeer = getPrivateField(canvas, "scenePeer");
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
                    @Override
                    public Void run() {
                    	TKSceneListener sceneListener = getPrivateField(scenePeer, "sceneListener");
						if (sceneListener == null) {
                            return null;
                        }
						switch(event.detail) {
							case SWT.GESTURE_BEGIN:
								break;
							case SWT.GESTURE_END:
								changeState(StateType.IDLE, event, sceneListener);
{'fraction_non_alphanumeric': 0.09114676173499703, 'fraction_numerical': 0.0024955436720142605, 'mean_word_length': 3.1787487586891756, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '679993', 'n_tokens_mistral': 2889, 'n_tokens_neox': 2437, 'n_words': 513}