<commit_before>/**
 * Copyright (c) 2016 by <NAME>
 *
 * 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.greip.color;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.greip.common.Util;
import org.greip.nls.Messages;
import org.greip.separator.LineStyle;
import org.greip.separator.Separator;

public abstract class AbstractColorChooser extends Composite {

	private final ColorResolution colorResolution;
	private RGB newRGB;
	private RGB rgb;

	private Spinner spiRed;
	private Spinner spiGreen;
	private Spinner spiBlue;
	private Composite previewPanel;
	private final boolean showInfo;

	protected AbstractColorChooser(final Composite parent, final ColorResolution colorResolution, final boolean showInfo,
			final boolean showHistory) {
		super(parent, SWT.NO_FOCUS);

		this.colorResolution = colorResolution;
		this.showInfo = showInfo;
		this.newRGB = new RGB(0, 0, 0);

		final int columns = 1 + (showHistory ? 1 : 0) + (showInfo ? 1 : 0);
		setLayout(GridLayoutFactory.fillDefaults().numColumns(columns).spacing(10, 0).create());
		setBackgroundMode(SWT.INHERIT_FORCE);

		addListener(SWT.Selection, e -> ColorHistoryList.INSTANCE.add(getRGB()));
		addListener(SWT.Traverse, e -> Util.when(e.detail == SWT.TRAVERSE_RETURN, () -> ColorHistoryList.INSTANCE.add(getRGB())));

		if (showHistory) createHistoryPanel();
		createColorChooserPanel();
		if (showInfo) createInfoPanel();
	}

	protected abstract Composite createColorChooserPanel();

	public Point getMargins() {
		return new Point(10, 10);
	}

	private void createInfoPanel() {
		final Composite infoPanel = new Composite(this, SWT.NONE);
		infoPanel.setLayout(GridLayoutFactory.fillDefaults().numColumns(3).spacing(3, 5).create());
		infoPanel.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());

		final Separator line = new Separator(infoPanel, SWT.VERTICAL);
		line.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 4));
		line.setLineColor(getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
		line.setLineStyle(LineStyle.Dot);
		line.setMarginWidth(0);

		spiRed = createRGBSpinner(infoPanel, Messages.Red);
		spiGreen = createRGBSpinner(infoPanel, Messages.Green);
		spiBlue = createRGBSpinner(infoPanel, Messages.Blue);

		previewPanel = new Composite(infoPanel, SWT.NO_FOCUS);
		previewPanel.setLayoutData(GridDataFactory.fillDefaults().hint(60, 20).span(2, 1).align(SWT.RIGHT, SWT.BOTTOM).create());

		previewPanel.addListener(SWT.Paint, e -> {
			final Rectangle bounds = previewPanel.getClientArea();
			final Color oldColor = new Color(e.display, rgb);
			final Color newColor = new Color(e.display, getRGB());

			e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GRAY));
			e.gc.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1);
			e.gc.setBackground(newColor);
			e.gc.fillRectangle(1, 1, bounds.width / 2, bounds.height - 2);
			e.gc.setBackground(oldColor);
			e.gc.fillRectangle(bounds.width / 2, 1, (bounds.width - 2) / 2, bounds.height - 2);

			oldColor.dispose();
			newColor.dispose();
		});

		previewPanel.addListener(SWT.MouseDown, e -> {
			final Rectangle bounds = previewPanel.getClientArea();
			Util.when(e.x > bounds.width / 2, () -> setRGB(rgb));
		});

		previewPanel.addListener(SWT.MouseDoubleClick, e -> {
			final Rectangle bounds = previewPanel.getClientArea();
			Util.when(e.x < bounds.width / 2, () -> notifyListeners(SWT.Selection, new Event()));
		});
	}

	private void createHistoryPanel() {
		GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).applyTo(new ColorHistory(this));
	}

	private static Spinner createRGBSpinner(final Composite parent, final String label) {
		final Label lbl = new Label(parent, SWT.NONE);
		lbl.setText(label + ":"); //$NON-NLS-1$
		lbl.setLayoutData(GridDataFactory.swtDefaults().indent(8, 0).create());

		final Spinner spi = new Spinner(parent, SWT.RIGHT | SWT.BORDER);
		spi.setMaximum(255);
		spi.setEnabled(false);

		return spi;
	}

	protected final ColorResolution getColorResolution() {
		return colorResolution;
	}

	protected final void setNewRGB(final RGB rgb) {
		newRGB = rgb;

		if (showInfo) {
			spiRed.setSelection(rgb.red);
			spiGreen.setSelection(rgb.green);
			spiBlue.setSelection(rgb.blue);
			previewPanel.redraw();
		}
	}

	/**
	 * Set the initial color. When the exact color is not available in the color
	 * chooser, the nearest color is selected.
	 *
	 * @param rgb
	 *        The colors RGB value.
	 *
	 * @exception IllegalArgumentException
	 *            <ul>
	 *            <li>ERROR_NULL_ARGUMENT - if the RGB value is null</li>
	 *            </ul>
	 */
	public void setRGB(final RGB rgb) {
		if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
		this.rgb = rgb;
		setNewRGB(rgb);
		redraw();
	}

	/**
	 * Gets the current selected color.
	 *
	 * @return The colors RGB value.
	 */
	public final RGB getRGB() {
		return newRGB;
	}
}<commit_msg>#9 AbstractColorChooser: add add/remove typed selection listener<commit_after>/**
 * Copyright (c) 2016 by <NAME>
 *
 * 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.greip.color;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.TypedListener;
import org.greip.common.Util;
import org.greip.nls.Messages;
import org.greip.separator.LineStyle;
import org.greip.separator.Separator;

public abstract class AbstractColorChooser extends Composite {

	private final ColorResolution colorResolution;
	private RGB newRGB;
	private RGB rgb;

	private Spinner spiRed;
	private Spinner spiGreen;
	private Spinner spiBlue;
	private Composite previewPanel;
	private final boolean showInfo;

	protected AbstractColorChooser(final Composite parent, final ColorResolution colorResolution, final boolean showInfo,
			final boolean showHistory) {
		super(parent, SWT.NO_FOCUS);

		this.colorResolution = colorResolution;
		this.showInfo = showInfo;
		this.newRGB = new RGB(0, 0, 0);

		final int columns = 1 + (showHistory ? 1 : 0) + (showInfo ? 1 : 0);
		setLayout(GridLayoutFactory.fillDefaults().numColumns(columns).spacing(10, 0).create());
		setBackgroundMode(SWT.INHERIT_FORCE);

		addListener(SWT.Selection, e -> ColorHistoryList.INSTANCE.add(getRGB()));
		addListener(SWT.Traverse, e -> Util.when(e.detail == SWT.TRAVERSE_RETURN, () -> ColorHistoryList.INSTANCE.add(getRGB())));

		if (showHistory) createHistoryPanel();
		createColorChooserPanel();
		if (showInfo) createInfoPanel();
	}

	protected abstract Composite createColorChooserPanel();

	public Point getMargins() {
		return new Point(10, 10);
	}

	private void createInfoPanel() {
		final Composite infoPanel = new Composite(this, SWT.NONE);
		infoPanel.setLayout(GridLayoutFactory.fillDefaults().numColumns(3).spacing(3, 5).create());
		infoPanel.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());

		final Separator line = new Separator(infoPanel, SWT.VERTICAL);
		line.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 4));
		line.setLineColor(getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
		line.setLineStyle(LineStyle.Dot);
		line.setMarginWidth(0);

		spiRed = createRGBSpinner(infoPanel, Messages.Red);
		spiGreen = createRGBSpinner(infoPanel, Messages.Green);
		spiBlue = createRGBSpinner(infoPanel, Messages.Blue);

		previewPanel = new Composite(infoPanel, SWT.NO_FOCUS);
		previewPanel.setLayoutData(GridDataFactory.fillDefaults().hint(60, 20).span(2, 1).align(SWT.RIGHT, SWT.BOTTOM).create());

		previewPanel.addListener(SWT.Paint, e -> {
			final Rectangle bounds = previewPanel.getClientArea();
			final Color oldColor = new Color(e.display, rgb);
			final Color newColor = new Color(e.display, getRGB());

			e.gc.setForeground(e.display.getSystemColor(SWT.COLOR_GRAY));
			e.gc.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1);
			e.gc.setBackground(newColor);
			e.gc.fillRectangle(1, 1, bounds.width / 2, bounds.height - 2);
			e.gc.setBackground(oldColor);
			e.gc.fillRectangle(bounds.width / 2, 1, (bounds.width - 2) / 2, bounds.height - 2);

			oldColor.dispose();
			newColor.dispose();
		});

		previewPanel.addListener(SWT.MouseDown, e -> {
			final Rectangle bounds = previewPanel.getClientArea();
			Util.when(e.x > bounds.width / 2, () -> setRGB(rgb));
		});

		previewPanel.addListener(SWT.MouseDoubleClick, e -> {
			final Rectangle bounds = previewPanel.getClientArea();
			Util.when(e.x < bounds.width / 2, () -> notifyListeners(SWT.Selection, new Event()));
		});
	}

	private void createHistoryPanel() {
		GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).applyTo(new ColorHistory(this));
	}

	private static Spinner createRGBSpinner(final Composite parent, final String label) {
		final Label lbl = new Label(parent, SWT.NONE);
		lbl.setText(label + ":"); //$NON-NLS-1$
		lbl.setLayoutData(GridDataFactory.swtDefaults().indent(8, 0).create());

		final Spinner spi = new Spinner(parent, SWT.RIGHT | SWT.BORDER);
		spi.setMaximum(255);
		spi.setEnabled(false);

		return spi;
	}

	protected final ColorResolution getColorResolution() {
		return colorResolution;
	}

	protected final void setNewRGB(final RGB rgb) {
		newRGB = rgb;

		if (showInfo) {
			spiRed.setSelection(rgb.red);
			spiGreen.setSelection(rgb.green);
			spiBlue.setSelection(rgb.blue);
			previewPanel.redraw();
		}
	}

	/**
	 * Set the initial color. When the exact color is not available in the color
	 * chooser, the nearest color is selected.
	 *
	 * @param rgb
	 *        The colors RGB value.
	 *
	 * @exception IllegalArgumentException
	 *            <ul>
	 *            <li>ERROR_NULL_ARGUMENT - if the RGB value is null</li>
	 *            </ul>
	 */
	public void setRGB(final RGB rgb) {
		if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
		this.rgb = rgb;
		setNewRGB(rgb);
		redraw();
	}

	/**
	 * Gets the current selected color.
	 *
	 * @return The colors RGB value.
	 */
	public final RGB getRGB() {
		return newRGB;
	}

	/**
	 * Adds the listener to the collection of listeners who will be notified when
	 * a color is selected by the user, by sending it one of the messages defined
	 * in the <code>SelectionListener</code> interface.
	 *
	 * @param listener
	 *        the listener which should be notified
	 *
	 * @exception IllegalArgumentException
	 *            <ul>
	 *            <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
	 *            </ul>
	 * @exception SWTException
	 *            <ul>
	 *            <li>ERROR_WIDGET_DISPOSED - if the receiver has been
	 *            disposed</li>
	 *            <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread
	 *            that created the receiver</li>
	 *            </ul>
	 *
	 * @see SelectionListener
	 * @see #removeSelectionListener
	 * @see SelectionEvent
	 */
	public void addSelectionListener(final SelectionListener listener) {
		if (listener == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
		addListener(SWT.Selection, new TypedListener(listener));
	}

	/**
	 * Removes the listener from the collection of listeners who will be notified
	 * when a color is selected by the user.
	 *
	 * @param listener
	 *        the listener which should no longer be notified
	 *
	 * @exception IllegalArgumentException
	 *            <ul>
	 *            <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
	 *            </ul>
	 * @exception SWTException
	 *            <ul>
	 *            <li>ERROR_WIDGET_DISPOSED - if the receiver has been
	 *            disposed</li>
	 *            <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread
	 *            that created the receiver</li>
	 *            </ul>
	 *
	 * @see SelectionListener
	 * @see #addSelectionListener
	 */
	public void removeSelectionListener(final SelectionListener listener) {
		if (listener == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
		removeListener(SWT.Selection, listener);
	}
}
{'fraction_non_alphanumeric': 0.12117318013455287, 'fraction_numerical': 0.008541839897195555, 'mean_word_length': 4.001890359168242, 'pattern_counts': {'":': 2, '<': 37, '<?xml version=': 0, '>': 53, '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': '673567', 'n_tokens_mistral': 4614, 'n_tokens_neox': 4169, 'n_words': 977}