Skip to main content
summaryrefslogtreecommitdiffstats
blob: a497dcc132d84abcaf36a940296e7fd816f33555 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*******************************************************************************
 * Copyright (c) 2006, 2008 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.css2.widget;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jst.pagedesigner.css2.ICSSStyle;
import org.eclipse.jst.pagedesigner.css2.font.ICSSFont;
import org.eclipse.jst.pagedesigner.css2.provider.DimensionInfo;
import org.eclipse.jst.pagedesigner.css2.style.DefaultStyle;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

/**
 * @author mengbo
 * @version 1.5
 */
public class ComboWidgetProvider extends AbstractWidgetProvider {
	private static final int VERTICAL_PADDING = 6;

	private static final int HORIZONTAL_PADDING = 12;

	private static int ARRAWWIDTH = 16;

	//private static int ARROWHEIGHT = 16;

	private String _firstString;

	private String _longestString;

	private String _label;

	/**
	 * @param style
	 */
	public ComboWidgetProvider(ICSSStyle style) {
		super(style);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.provider.ICSSWidgetProvider#getPreferredDimension(int,
	 *      int)
	 */
	public DimensionInfo getPreferredDimension(int width, int height) {
		if (width <= 0) {
			width = getDefaultWidth();

		}
		if (height <= 0) {
			height = getDefaultHeight();
		}
		return new DimensionInfo(new Dimension(width, height), -1);
	}

	/**
	 * by default, the combo's width will be calculated from the longest option
	 * value.
	 * 
	 * @return the default width
	 */
	private int getDefaultWidth() {
		int textareaWidth;
		if (this._longestString == null || this._longestString.length() == 0) {
			textareaWidth = 20;
		} else {
			ICSSStyle style = this.getCSSStyle();
			if (style == null) {
				style = DefaultStyle.getInstance();
			}
			ICSSFont font = style.getCSSFont();

			textareaWidth = FigureUtilities.getTextWidth(_longestString, font
					.getSwtFont());
		}
		return textareaWidth + ARRAWWIDTH + HORIZONTAL_PADDING;
	}

	/**
	 * 
	 * @return the default height
	 * @see TextInputWidgetProvider#getDefaultHeight()
	 */
	public int getDefaultHeight() {
		ICSSStyle style = this.getCSSStyle();
		if (style == null) {
			style = DefaultStyle.getInstance();
		}
		ICSSFont font = style.getCSSFont();
		Font swtfont = font.getSwtFont();
		int fontSize = FigureUtilities.getFontMetrics(swtfont).getHeight();
		return fontSize + VERTICAL_PADDING;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.provider.ICSSWidgetProvider#paintFigure(org.eclipse.draw2d.Graphics,
	 *      org.eclipse.draw2d.geometry.Rectangle)
	 */
	public void paintFigure(Graphics g, Rectangle rect) {
		if (this._firstString != null) {
			ICSSStyle style = this.getCSSStyle();
			if (style == null) {
				style = DefaultStyle.getInstance();
			}
			ICSSFont font = style.getCSSFont();
			g.setFont(font.getSwtFont());

			Color newColor = null;
			Object color = style.getColor();
			if (color instanceof Color) {
				g.setForegroundColor((Color) color);
			} else if (color instanceof RGB) {
				newColor = new Color(Display.getCurrent(), (RGB) color);
				g.setForegroundColor(newColor);
			} else {
				g.setForegroundColor(ColorConstants.black);
			}
			g.clipRect(rect);
			String label = _label != null ? _label : _firstString;
			label = label.replaceAll("[ \r\n]+", " "); //$NON-NLS-1$ //$NON-NLS-2$
			if (label.endsWith(" ")) { //$NON-NLS-1$
				label = label.substring(0, label.length() - 1);
			}
			g.drawString(label, rect.x + HORIZONTAL_PADDING / 2, rect.y
					+ VERTICAL_PADDING / 2);
			if (newColor != null) {
				newColor.dispose();
			}

		}

		BorderUtil.drawBorder(g, rect.x, rect.y, rect.width, rect.height,
				BORDERTHICK, true);
		// next the drop down button
		int width = ARRAWWIDTH;
		int left = rect.x + rect.width - width - BORDERTHICK;
		int top = rect.y + BORDERTHICK;
		int height = rect.height - BORDERTHICK * 2;
		g.setBackgroundColor(ColorConstants.button);
		g.fillRectangle(left, top, width, height);

		Rectangle borderRect = new Rectangle(left, top, width, height);
		BorderUtil.drawBorder(g, borderRect.x, borderRect.y, borderRect.width,
				borderRect.height, BORDERTHICK, false);

		g.setForegroundColor(ColorConstants.black);

		int decoratorWidth = (width - BORDERTHICK * 2) / 2;
		int length = decoratorWidth / 2 + 1;
		int leftX = left + (width - decoratorWidth) / 2 - 1;
		int topY = top + (height - length) / 2 + 1;
		for (int i = 0; i < length; i++) {
			g.drawLine(leftX + i, topY + i, leftX - i + decoratorWidth, topY
					+ i);
		}
	}

	/**
	 * set the options to be displayed in this combo box. Will calculate out the
	 * first string and the longest string.
	 * 
	 * @param options
	 */
	public void setOptions(String[] options) {
		if (options == null || options.length == 0) {
			this._firstString = null;
			this._longestString = null;
		} else {
			this._firstString = options[0];
			this._longestString = (options[0] == null ? "" : options[0]); //$NON-NLS-1$
			for (int i = 1; i < options.length; i++) {
				if (options[i] == null) {
					continue;
				}
				if (options[i].length() > this._longestString.length()) {
					this._longestString = options[i];
				}
			}
		}
	}

	/**
	 * @param label
	 */
	public void setSelectedLabel(String label) {
		this._label = label;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.css2.provider.ICSSWidgetProvider#isHandlingBorder()
	 */
	public boolean isHandlingBorder() {
		return false;
	}
}

Back to the top