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

com.diffplug.common.swt.jface.LabelProviders 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.jface;


import com.diffplug.common.base.Functions;
import java.util.function.Function;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;

/** Utilities for creating JFace {@link CellLabelProvider} (which is also appropriate for plain-jane {@link ILabelProvider}). */
public class LabelProviders {
	/** Returns a fluent builder for creating a {@link ColumnLabelProvider}. */
	public static  Builder builder() {
		return new Builder();
	}

	/** Creates a {@link ColumnLabelProvider} for text. */
	public static  ColumnLabelProvider createWithText(Function text) {
		Builder builder = builder();
		builder.setText(text);
		return builder.build();
	}

	/** Creates a {@link ColumnLabelProvider} for images. */
	public static  ColumnLabelProvider createWithImage(Function image) {
		Builder builder = builder();
		builder.setImage(image);
		return builder.build();
	}

	/** Creates a {@link ColumnLabelProvider} for text and images. */
	public static  ColumnLabelProvider createWithTextAndImage(Function text, Function image) {
		Builder builder = builder();
		builder.setText(text);
		builder.setImage(image);
		return builder.build();
	}

	/** An override of {@link ColumnLabelProvider}. */
	@SuppressWarnings("unchecked")
	private static class Imp extends ColumnLabelProvider {
		private final Function text;
		private final Function image;
		private final Function background;
		private final Function foreground;
		private final Function font;

		private Imp(Function text, Function image, Function background, Function foreground, Function font) {
			this.text = text;
			this.image = image;
			this.background = background;
			this.foreground = foreground;
			this.font = font;
		}

		@Override
		public String getText(Object element) {
			return text.apply((T) element);
		}

		@Override
		public Image getImage(Object element) {
			return image.apply((T) element);
		}

		@Override
		public Color getBackground(Object element) {
			return background.apply((T) element);
		}

		@Override
		public Color getForeground(Object element) {
			return foreground.apply((T) element);
		}

		@Override
		public Font getFont(Object element) {
			return font.apply((T) element);
		}
	}

	/** A fluent API for creating a {@link ColumnLabelProvider}. */
	public static class Builder extends ColumnLabelProvider {
		private Function text = Functions.constant(null);
		private Function image = Functions.constant(null);
		private Function background = Functions.constant(null);
		private Function foreground = Functions.constant(null);
		private Function font = Functions.constant(null);

		private Builder() {}

		/** Sets the function used to determine the text. */
		public Builder setText(Function text) {
			this.text = text;
			return this;
		}

		/** Sets the function used to determine the image. */
		public Builder setImage(Function image) {
			this.image = image;
			return this;
		}

		/** Sets the function used to determine the background color. */
		public Builder setBackground(Function background) {
			this.background = background;
			return this;
		}

		/** Sets the function used to determine the foreground color. */
		public Builder setForeground(Function foreground) {
			this.foreground = foreground;
			return this;
		}

		/** Sets the function used to determine the font. */
		public Builder setFont(Function font) {
			this.font = font;
			return this;
		}

		/** Returns a new ColumnLabelProvider. */
		public ColumnLabelProvider build() {
			return new Imp(text, image, background, foreground, font);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy