All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.diffplug.common.swt.widgets.RadioGroup Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 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
 *
 *     https://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.widgets;


import com.diffplug.common.base.Preconditions;
import com.diffplug.common.collect.BiMap;
import com.diffplug.common.collect.HashBiMap;
import com.diffplug.common.collect.Iterables;
import com.diffplug.common.collect.Maps;
import com.diffplug.common.rx.RxBox;
import com.diffplug.common.swt.Coat;
import com.diffplug.common.swt.Layouts;
import com.diffplug.common.swt.SwtExec;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

public class RadioGroup {
	private final Optional> source;
	private LinkedHashMap options = Maps.newLinkedHashMap();

	/** Guava-style constructor. */
	public static  RadioGroup create() {
		return new RadioGroup(Optional.empty());
	}

	/** Guava-style constructor. */
	public static  RadioGroup create(RxBox source) {
		return new RadioGroup(Optional.of(source));
	}

	protected RadioGroup(Optional> source) {
		this.source = source;
	}

	public RadioGroup addOption(T value, String name) {
		options.put(name, value);
		return this;
	}

	public RadioGroup addOptions(Iterable values, Function labelProvider) {
		for (T value : values) {
			addOption(value, labelProvider.apply(value));
		}
		return this;
	}

	public RxBox buildOn(Composite cmp) {
		// object which will hold the current selection
		RxBox selection = source.orElseGet(() -> RxBox.of(Iterables.get(options.values(), 0)));

		// mapping from button to objects
		BiMap mapping = HashBiMap.create();

		for (Map.Entry entry : options.entrySet()) {
			// create a button
			Button btn = new Button(cmp, SWT.RADIO);
			btn.setText(entry.getKey());
			// update the mapping
			mapping.put(btn, entry.getValue());
			// update selection when a button is clicked
			btn.addListener(SWT.Selection, e -> {
				selection.set(mapping.get(btn));
			});
		}

		// when the selection is changed, set the button
		Button firstBtn = Iterables.get(mapping.keySet(), 0);
		SwtExec.immediate().guardOn(firstBtn).subscribe(selection.asObservable(), obj -> {
			Button selectedBtn = mapping.inverse().get(obj);
			for (Button btn : mapping.keySet()) {
				btn.setSelection(btn == selectedBtn);
			}
		});
		return selection;

	}

	public Coat.Returning> getCoat() {
		Preconditions.checkArgument(options.size() > 0, "Must be at least one option!");
		return new Coat.Returning>() {
			@Override
			public RxBox putOn(Composite cmp) {
				Layouts.setFill(cmp).vertical().margin(0);
				return buildOn(cmp);
			}
		};
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy