Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 39e27b8b958a1986280880c1c5f9b27a00a78199 (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. 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:
 *     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.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 ListWidgetProvider extends AbstractWidgetProvider {
	private static int DEFAULTSIZE = 4;

	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[] _options;

	private int _rows = DEFAULTSIZE;

	/**
	 * @param style
	 */
	public ListWidgetProvider(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(width, height, -1);
	}

	/**
	 * by default, the combo's width will be calculated from the longest option
	 * value.
	 * 
	 * @return the default width
	 */
	public int getDefaultWidth() {
		int longestStringWidth = 0;
		if (_options != null) {
			ICSSStyle style = this.getCSSStyle();
			if (style == null) {
				style = DefaultStyle.getInstance();
			}
			ICSSFont font = style.getCSSFont();
			Font swtFont = font.getSwtFont();
			for (int i = 0; i < _options.length; i++) {
				int width = FigureUtilities.getTextWidth(_options[i], swtFont);
				if (width > longestStringWidth) {
					longestStringWidth = width;
				}
			}
		}
		// text area width + borderWidth + vertical scroll width
		return (longestStringWidth) + HORIZONTAL_PADDING + ARRAWWIDTH;
	}

	/**
	 * 
	 * @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 fontHeight = FigureUtilities.getFontMetrics(swtfont).getHeight();
		return (fontHeight) * _rows + 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) {
		BorderUtil.drawBorder(g, rect.x, rect.y, rect.width, rect.height,
				BORDERTHICK, true);
		if (_options != null) {
			ICSSStyle style = this.getCSSStyle();
			if (style == null) {
				style = DefaultStyle.getInstance();
			}
			ICSSFont font = style.getCSSFont();
			Font swtfont = font.getSwtFont();
			g.setFont(swtfont);

			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);
			}
			int fontHeight = FigureUtilities.getFontMetrics(swtfont)
					.getHeight();
			int x = rect.x + HORIZONTAL_PADDING / 2;
			int y = rect.y + VERTICAL_PADDING / 2;

			g.clipRect(rect);
			for (int i = 0; i < _options.length
					&& (i * fontHeight < rect.height - VERTICAL_PADDING); i++) {
				g.drawString(_options[i], x, y);
				y += fontHeight;
			}
			if (newColor != null) {
				newColor.dispose();
			}

			int borderThick = 2;
			Rectangle barRect = new Rectangle(rect.x, rect.y + borderThick,
					rect.width - borderThick, rect.height - 2 * borderThick);
			BorderUtil.drawVertialBar(g, ARRAWWIDTH, ARROWHEIGHT, borderThick,
					barRect);
		}
	}

	/**
	 * @param string
	 * @param x
	 * @param y
	 * @return
	 */
	private String normalize(String string) {
		if (string == null) {
			return ""; //$NON-NLS-1$
		}
		int index = string.indexOf('\r');
		if (index >= 0) {
			string = string.substring(0, index);
		}
		index = string.indexOf('\n');
		if (index >= 0) {
			string = string.substring(0, index);
		}
		return string;
	}

	/**
	 * set the options to be displayed in this combo box.
	 * 
	 * @param options
	 */
	public void setOptions(String[] options) {
		this._options = options;
		if (_options != null) {
			for (int i = 0; i < _options.length; i++) {
				_options[i] = normalize(_options[i]);
			}
		}
	}

	/**
	 * set the default number of rows to be displayed.
	 * 
	 * @param rows
	 */
	public void setRows(int rows) {
		_rows = (rows > 0 ? rows : DEFAULTSIZE);
	}

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

Back to the top