Project

General

Profile

Download (7.82 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.draw2d;
12

    
13
import org.eclipse.swt.graphics.Color;
14

    
15
import org.eclipse.draw2d.ButtonBorder.ButtonScheme;
16
import org.eclipse.draw2d.geometry.Insets;
17

    
18
/**
19
 * Creates a border for a clickable type of figure, which works in conjunction
20
 * with the Figure and its model. This border adjusts itself to the various
21
 * states the model of the figure could be. This border uses an extended
22
 * {@link SchemeBorder.Scheme Scheme} called {@link ButtonScheme} which provides
23
 * more information required by border to handle the the states of the model.
24
 * 
25
 * @see SchemeBorder.Scheme
26
 * @see ButtonScheme
27
 */
28
public class ButtonBorder extends SchemeBorder {
29

    
30
	/**
31
	 * Default button border.
32
	 * 
33
	 * @see SCHEMES#BUTTON
34
	 */
35
	public static final Border BUTTON = new ButtonBorder(SCHEMES.BUTTON);
36
	/**
37
	 * Inverted hightlight colors from BUTTON.
38
	 * 
39
	 * @see SCHEMES#BUTTON_CONTRAST
40
	 */
41
	public static final Border BUTTON_CONTRAST = new ButtonBorder(
42
			SCHEMES.BUTTON_CONTRAST);
43
	/**
44
	 * Used for scrollbar buttons.
45
	 * 
46
	 * @see SCHEMES#BUTTON_SCROLLBAR
47
	 */
48
	public static final Border BUTTON_SCROLLBAR = new ButtonBorder(
49
			SCHEMES.BUTTON_SCROLLBAR);
50
	/**
51
	 * Used for toolbar buttons.
52
	 * 
53
	 * @see SCHEMES#TOOLBAR
54
	 */
55
	public static final Border TOOLBAR = new ButtonBorder(SCHEMES.TOOLBAR);
56

    
57
	/**
58
	 * Provides for a scheme to represent the borders of clickable figures like
59
	 * buttons. Though similar to the {@link SchemeBorder.Scheme Scheme} it
60
	 * supports an extra set of borders for the pressed states.
61
	 */
62
	public static class ButtonScheme extends Scheme {
63
		private Color highlightPressed[] = null, shadowPressed[] = null;
64

    
65
		/**
66
		 * Constructs a new button scheme where the input colors are the colors
67
		 * for the top-left and bottom-right sides of the border. These colors
68
		 * serve as the colors when the border is in a pressed state too. The
69
		 * width of each side is determined by the number of colors passed in as
70
		 * input.
71
		 * 
72
		 * @param highlight
73
		 *            Colors for the top-left sides of the border
74
		 * @param shadow
75
		 *            Colors for the bottom-right sides of the border
76
		 * @since 2.0
77
		 */
78
		public ButtonScheme(Color[] highlight, Color[] shadow) {
79
			highlightPressed = this.highlight = highlight;
80
			shadowPressed = this.shadow = shadow;
81
			init();
82
		}
83

    
84
		/**
85
		 * Constructs a new button scheme where the input colors are the colors
86
		 * for the top-left and bottom-right sides of the border, for the normal
87
		 * and pressed states. The width of each side is determined by the
88
		 * number of colors passed in as input.
89
		 * 
90
		 * @param hl
91
		 *            Colors for the top-left sides of the border
92
		 * @param sh
93
		 *            Colors for the bottom-right sides of the border
94
		 * @param hlp
95
		 *            Colors for the top-left sides of the border when figure is
96
		 *            pressed
97
		 * @param shp
98
		 *            Colors for the bottom-right sides of the border when
99
		 *            figure is pressed
100
		 * @since 2.0
101
		 */
102
		public ButtonScheme(Color[] hl, Color[] sh, Color[] hlp, Color[] shp) {
103
			highlight = hl;
104
			shadow = sh;
105
			highlightPressed = hlp;
106
			shadowPressed = shp;
107
			init();
108
		}
109

    
110
		/**
111
		 * Calculates and returns the Insets for this border. The calculations
112
		 * are based on the number of normal and pressed, highlight and shadow
113
		 * colors.
114
		 * 
115
		 * @return The insets for this border
116
		 * @since 2.0
117
		 */
118
		protected Insets calculateInsets() {
119
			int br = 1 + Math.max(getShadow().length,
120
					getHighlightPressed().length);
121
			int tl = Math.max(getHighlight().length, getShadowPressed().length);
122
			return new Insets(tl, tl, br, br);
123
		}
124

    
125
		/**
126
		 * Calculates and returns the opaque state of this border.
127
		 * <p>
128
		 * Returns false in the following conditions:
129
		 * <ul>
130
		 * <li>The number of highlight colors is different than the the number
131
		 * of shadow colors.
132
		 * <li>The number of pressed highlight colors is different than the
133
		 * number of pressed shadow colors.
134
		 * <li>Any of the highlight and shadow colors are set to
135
		 * <code>null</code>
136
		 * <li>Any of the pressed highlight and shadow colors are set to
137
		 * <code>null</code>
138
		 * </ul>
139
		 * This is done so that the entire region under the figure is properly
140
		 * covered.
141
		 * 
142
		 * @return The opaque state of this border
143
		 * @since 2.0
144
		 */
145
		protected boolean calculateOpaque() {
146
			if (!super.calculateOpaque())
147
				return false;
148
			if (getHighlight().length != getShadowPressed().length)
149
				return false;
150
			if (getShadow().length != getHighlightPressed().length)
151
				return false;
152
			Color[] colors = getHighlightPressed();
153
			for (int i = 0; i < colors.length; i++)
154
				if (colors[i] == null)
155
					return false;
156
			colors = getShadowPressed();
157
			for (int i = 0; i < colors.length; i++)
158
				if (colors[i] == null)
159
					return false;
160
			return true;
161
		}
162

    
163
		/**
164
		 * Returns the pressed highlight colors of this border.
165
		 * 
166
		 * @return Colors as an array of Colors
167
		 * @since 2.0
168
		 */
169
		protected Color[] getHighlightPressed() {
170
			return highlightPressed;
171
		}
172

    
173
		/**
174
		 * Returns the pressed shadow colors of this border.
175
		 * 
176
		 * @return Colors as an array of Colors
177
		 * @since 2.0
178
		 */
179
		protected Color[] getShadowPressed() {
180
			return shadowPressed;
181
		}
182
	}
183

    
184
	/**
185
	 * Interface defining commonly used schemes for the ButtonBorder.
186
	 */
187
	public static interface SCHEMES {
188

    
189
		/**
190
		 * Contrast button scheme
191
		 */
192
		ButtonScheme BUTTON_CONTRAST = new ButtonScheme(new Color[] { button,
193
				buttonLightest }, DARKEST_DARKER);
194
		/**
195
		 * Regular button scheme
196
		 */
197
		ButtonScheme BUTTON = new ButtonScheme(new Color[] { buttonLightest },
198
				DARKEST_DARKER);
199
		/**
200
		 * Toolbar button scheme
201
		 */
202
		ButtonScheme TOOLBAR = new ButtonScheme(new Color[] { buttonLightest },
203
				new Color[] { buttonDarker });
204
		/**
205
		 * Scrollbar button scheme
206
		 */
207
		ButtonScheme BUTTON_SCROLLBAR = new ButtonScheme(new Color[] { button,
208
				buttonLightest }, DARKEST_DARKER, new Color[] { buttonDarker },
209
				new Color[] { buttonDarker });
210
	}
211

    
212
	/**
213
	 * Constructs a ButtonBorder with a predefined button scheme set as its
214
	 * default.
215
	 * 
216
	 * @since 2.0
217
	 */
218
	public ButtonBorder() {
219
		setScheme(SCHEMES.BUTTON);
220
	}
221

    
222
	/**
223
	 * Constructs a ButtonBorder with the input ButtonScheme set as its Scheme.
224
	 * 
225
	 * @param scheme
226
	 *            ButtonScheme for this ButtonBorder.
227
	 * @since 2.0
228
	 */
229
	public ButtonBorder(ButtonScheme scheme) {
230
		setScheme(scheme);
231
	}
232

    
233
	/**
234
	 * Paints this border with the help of the set scheme, the model of the
235
	 * clickable figure, and other inputs. The scheme is used in conjunction
236
	 * with the state of the model to get the appropriate colors for the border.
237
	 * 
238
	 * @param figure
239
	 *            The Clickable that this border belongs to
240
	 * @param graphics
241
	 *            The graphics used for painting
242
	 * @param insets
243
	 *            The insets
244
	 */
245
	public void paint(IFigure figure, Graphics graphics, Insets insets) {
246
		Clickable clickable = (Clickable) figure;
247
		ButtonModel model = clickable.getModel();
248
		ButtonScheme colorScheme = (ButtonScheme) getScheme();
249

    
250
		if (clickable.isRolloverEnabled() && !model.isMouseOver()
251
				&& !model.isSelected())
252
			return;
253

    
254
		Color tl[], br[];
255
		if (model.isSelected() || model.isArmed()) {
256
			tl = colorScheme.getShadowPressed();
257
			br = colorScheme.getHighlightPressed();
258
		} else {
259
			tl = colorScheme.getHighlight();
260
			br = colorScheme.getShadow();
261
		}
262

    
263
		paint(graphics, figure, insets, tl, br);
264
	}
265

    
266
}
(30-30/171)