<commit_before>/*
 * Copyright 2015 DiffPlug
 *
 * Licensed 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 com.diffplug.common.swt;

import java.util.Collection;
import java.util.stream.Stream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;

import rx.Observable;
import rx.subjects.PublishSubject;

import com.google.common.base.Preconditions;
import com.google.common.primitives.Ints;

import com.diffplug.common.rx.RxBox;

/** Utilities that convert SWT events into Rx-friendly Observables. */
public class SwtRx {
	/** Subscribes to the given widget and pipes the events to an {@link Observable}<{@link Event}>. */
	public static Observable<Event> addListener(Widget widget, int... events) {
		return addListener(widget, Ints.asList(events));
	}

	/** Subscribes to the given widget and pipes the events to an {@link Observable}<{@link Event}>. */
	public static Observable<Event> addListener(Widget widget, Collection<Integer> events) {
		return addListener(widget, events.stream());
	}

	/** Subscribes to the given widget and pipes the events to an {@link Observable}<{@link Event}>. */
	public static Observable<Event> addListener(Widget widget, Stream<Integer> events) {
		PublishSubject<Event> subject = PublishSubject.create();
		events.forEach(event -> widget.addListener(event, subject::onNext));
		return subject.asObservable();
	}

	/** Returns an {@link Observable}<{@link Point}> of the right-click mouse-up on the given control, in global co-ordinates. */
	public static Observable<Point> rightClick(Control ctl) {
		PublishSubject<Point> observable = PublishSubject.create();
		ctl.addListener(SWT.MouseUp, e -> {
			if (e.button == 3) {
				Point globalCoord = ctl.toDisplay(e.x, e.y);
				observable.onNext(globalCoord);
			}
		});
		return observable;
	}

	/** Returns an RxBox<String> which contains the content of the text box. */
	public static RxBox<String> textImmediate(Text text) {
		return textImp(text, SWT.Modify);
	}

	/**
	 * Returns an RxBox<String> which contains the content of the text box
	 * only when it has been confirmed by:
	 * <ul>
	 * <li>programmer setting the RxBox</li>
	 * <li>user hitting enter</li>
	 * <li>focus leaving the text</li>
	 * </ul>
	 */
	public static RxBox<String> textConfirmed(Text text) {
		return textImp(text, SWT.DefaultSelection, SWT.FocusOut);
	}

	private static RxBox<String> textImp(Text text, int... events) {
		RxBox<String> box = RxBox.of(text.getText());
		// set the text when the box changes
		SwtExec.immediate().guardOn(text).subscribe(box, str -> {
			Point selection = text.getSelection();
			text.setText(str);
			text.setSelection(selection);
		});
		// set the box when the text changes
		Listener listener = e -> box.set(text.getText());
		for (int event : events) {
			text.addListener(event, listener);
		}
		return box;
	}

	/** Returns an {@code RxBox<Boolean>} for the toggle state of the given button as an RxBox. */
	public static RxBox<Boolean> toggle(Button btn) {
		Preconditions.checkArgument(SwtMisc.flagIsSet(SWT.TOGGLE, btn));
		RxBox<Boolean> box = RxBox.of(btn.getSelection());
		// update the box when a click happens
		btn.addListener(SWT.Selection, e -> {
			box.set(!box.get());
		});
		// update the button when the box happens
		SwtExec.immediate().guardOn(btn).subscribe(box, btn::setSelection);
		return box;
	}
}<commit_msg>SwtRx.toggle() now works for TOGGLE and CHECK.<commit_after>/*
 * Copyright 2015 DiffPlug
 *
 * Licensed 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 com.diffplug.common.swt;

import java.util.Collection;
import java.util.stream.Stream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;

import rx.Observable;
import rx.subjects.PublishSubject;

import com.google.common.base.Preconditions;
import com.google.common.primitives.Ints;

import com.diffplug.common.rx.RxBox;

/** Utilities that convert SWT events into Rx-friendly Observables. */
public class SwtRx {
	/** Subscribes to the given widget and pipes the events to an {@link Observable}<{@link Event}>. */
	public static Observable<Event> addListener(Widget widget, int... events) {
		return addListener(widget, Ints.asList(events));
	}

	/** Subscribes to the given widget and pipes the events to an {@link Observable}<{@link Event}>. */
	public static Observable<Event> addListener(Widget widget, Collection<Integer> events) {
		return addListener(widget, events.stream());
	}

	/** Subscribes to the given widget and pipes the events to an {@link Observable}<{@link Event}>. */
	public static Observable<Event> addListener(Widget widget, Stream<Integer> events) {
		PublishSubject<Event> subject = PublishSubject.create();
		events.forEach(event -> widget.addListener(event, subject::onNext));
		return subject.asObservable();
	}

	/** Returns an {@link Observable}<{@link Point}> of the right-click mouse-up on the given control, in global co-ordinates. */
	public static Observable<Point> rightClick(Control ctl) {
		PublishSubject<Point> observable = PublishSubject.create();
		ctl.addListener(SWT.MouseUp, e -> {
			if (e.button == 3) {
				Point globalCoord = ctl.toDisplay(e.x, e.y);
				observable.onNext(globalCoord);
			}
		});
		return observable;
	}

	/** Returns an RxBox<String> which contains the content of the text box. */
	public static RxBox<String> textImmediate(Text text) {
		return textImp(text, SWT.Modify);
	}

	/**
	 * Returns an RxBox<String> which contains the content of the text box
	 * only when it has been confirmed by:
	 * <ul>
	 * <li>programmer setting the RxBox</li>
	 * <li>user hitting enter</li>
	 * <li>focus leaving the text</li>
	 * </ul>
	 */
	public static RxBox<String> textConfirmed(Text text) {
		return textImp(text, SWT.DefaultSelection, SWT.FocusOut);
	}

	private static RxBox<String> textImp(Text text, int... events) {
		RxBox<String> box = RxBox.of(text.getText());
		// set the text when the box changes
		SwtExec.immediate().guardOn(text).subscribe(box, str -> {
			Point selection = text.getSelection();
			text.setText(str);
			text.setSelection(selection);
		});
		// set the box when the text changes
		Listener listener = e -> box.set(text.getText());
		for (int event : events) {
			text.addListener(event, listener);
		}
		return box;
	}

	/** Returns an {@code RxBox<Boolean>} for the toggle state of the given button as an RxBox. */
	public static RxBox<Boolean> toggle(Button btn) {
		Preconditions.checkArgument(SwtMisc.flagIsSet(SWT.TOGGLE, btn) || SwtMisc.flagIsSet(SWT.CHECK, btn));
		RxBox<Boolean> box = RxBox.of(btn.getSelection());
		// update the box when a click happens
		btn.addListener(SWT.Selection, e -> {
			box.set(!box.get());
		});
		// update the button when the box happens
		SwtExec.immediate().guardOn(btn).subscribe(box, btn::setSelection);
		return box;
	}
}
{'fraction_non_alphanumeric': 0.12288545190913484, 'fraction_numerical': 0.0021749637506041568, 'mean_word_length': 4.251903553299492, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 71, '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': '1198862', 'n_tokens_mistral': 2683, 'n_tokens_neox': 2489, 'n_words': 780}