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

org.eclipse.swtchart.internal.Util Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2008, 2019 SWTChart project.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 * yoshitaka - initial API and implementation
 * Frank Buloup - Internationalization
 *******************************************************************************/
package org.eclipse.swtchart.internal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swtchart.LineStyle;

/**
 * A utility class providing generic methods.
 */
public final class Util {

	/**
	 * Gets the text extent with given font in GC. If the given font is
	 * null or already disposed, point containing size zero will be
	 * returned.
	 * 
	 * @param font
	 *            the font
	 * @param text
	 *            the text
	 * @return a point containing text extent
	 */
	public static Point getExtentInGC(Font font, String text) {

		if(font == null || font.isDisposed()) {
			return new Point(0, 0);
		}
		// create GC
		GC gc = new GC(Display.getCurrent());
		// get extent of text with given font
		gc.setFont(font);
		Point p;
		if(text == null || "".equals(text.trim())) { //$NON-NLS-1$
			p = new Point(0, gc.getFontMetrics().getHeight());
		} else {
			p = gc.textExtent(text);
		}
		// dispose resources
		gc.dispose();
		return p;
	}

	/**
	 * Gets the index defined in SWT.
	 * 
	 * @param lineStyle
	 *            the line style
	 * @return the index defined in SWT.
	 */
	public static int getIndexDefinedInSWT(LineStyle lineStyle)
	{
		switch(lineStyle) {
			case NONE:
				return SWT.NONE;
			case SOLID:
				return SWT.LINE_SOLID;
			case DASH:
				return SWT.LINE_DASH;
			case DOT:
				return SWT.LINE_DOT;
			case DASHDOT:
				return SWT.LINE_DASHDOT;
			case DASHDOTDOT:
				return SWT.LINE_DASHDOTDOT;
			default:
				return SWT.LINE_SOLID;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy