Skip to main content
summaryrefslogtreecommitdiffstats
blob: f0f897158a040298d105642b90775d1aa4d07cda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * Copyright (c) 2014 IBM Corporation, BestSolution.at and others.
 * 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
 *
 * Contributors:
 * 	   IBM Corporation - initial API
 *     Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.widgets;

import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Separator;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Region;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Device.NoOpDrawableGC;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.Util;

public class Label extends Control {
	private javafx.scene.control.Label control;
	private Separator separator;
	private Image image;
	
	public Label(Composite parent, int style) {
		super(parent,style);
	}

	@Override
	protected Node createWidget() {
		if( (style & SWT.SEPARATOR) != 0 ) {
			separator = new Separator();
			separator.setOrientation((style & SWT.VERTICAL) == SWT.VERTICAL ? Orientation.VERTICAL : Orientation.HORIZONTAL);
			return separator;
		} else {
			control = new javafx.scene.control.Label();
			control.setWrapText((style & SWT.WRAP) == SWT.WRAP);
			return control;
		}
	}
	
	public Point computeSize(int wHint, int hHint, boolean flushCache) {
		checkWidget ();
		forceSizeProcessing();
		int width = (int) Math.ceil(internal_getNativeObject().prefWidth(javafx.scene.control.Control.USE_COMPUTED_SIZE));
		int height = (int) Math.ceil(internal_getNativeObject().prefHeight(javafx.scene.control.Control.USE_COMPUTED_SIZE));

		if (wHint != SWT.DEFAULT)
			width = wHint;
		if (hHint != SWT.DEFAULT)
			height = hHint;

		Point p = new Point(width, height);
		return p;
	}

	@Override
	public Region internal_getNativeObject() {
		return control == null ? separator : control;
	}

	public int getAlignment () {
		checkWidget ();
		if ((style & SWT.SEPARATOR) != 0) return 0;
		if ((style & SWT.LEFT) != 0) return SWT.LEFT;
		if ((style & SWT.CENTER) != 0) return SWT.CENTER;
		if ((style & SWT.RIGHT) != 0) return SWT.RIGHT;
		return SWT.LEFT;
	}
	
	public String getText () {
		checkWidget ();
		if ((style & SWT.SEPARATOR) != 0) return "";
		return notNullString(control.getText());
	}
	
	public void setAlignment (int alignment) {
		checkWidget ();
		if ((style & SWT.SEPARATOR) != 0) return;
		if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) == 0) return;
		style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER);
		style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER);
		
		Pos p;
		switch (alignment) {
		case SWT.RIGHT:
			p = Pos.CENTER_RIGHT;
			break;
		case SWT.CENTER:
			p = Pos.CENTER;
			break;
		default:
			p = Pos.CENTER_LEFT;
			break;
		}
		control.setAlignment(p);
	}
	
	public void setText (String string) {
		checkWidget ();
		if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
		if ((style & SWT.SEPARATOR) != 0) return;
		control.setText(string);
	}
	
	public Image getImage () {
		return image;
	}
	
	public void setImage (Image image) {
		if ((style & SWT.SEPARATOR) != 0) return;
		control.setGraphic(image == null ? null : new ImageView(image.internal_getImage()));
	}
	
	@Override
	protected Font getDefaultFont() {
		if( control.getFont() != null ) {
			return new Font(getDisplay(), control.getFont());	
		}
		return super.getDefaultFont();
	}
	
	@Override
	public void internal_dispose_GC(DrawableGC gc) {
		
	}
	
}

Back to the top