Project

General

Profile

Download (4.38 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
import org.eclipse.swt.graphics.Font;
15

    
16
import org.eclipse.draw2d.geometry.Dimension;
17
import org.eclipse.draw2d.geometry.Insets;
18

    
19
/**
20
 * Provides support for a border with a label describing the contents of which
21
 * it is surrounding.
22
 */
23
public abstract class AbstractLabeledBorder extends AbstractBorder implements
24
		LabeledBorder {
25

    
26
	private Dimension textExtents;
27
	private String label;
28
	private Insets insets;
29
	private Color textColor = ColorConstants.black;
30
	private Font font;
31

    
32
	/**
33
	 * Constructs a default AbstractLabeledBorder with the name of this class
34
	 * set as its label.
35
	 * 
36
	 * @since 2.0
37
	 */
38
	public AbstractLabeledBorder() {
39
		String className = getClass().getName();
40
		setLabel(className.substring(className.lastIndexOf('.') + 1,
41
				className.length()));
42
	}
43

    
44
	/**
45
	 * Constructs a border with the label set to the String passed in as input.
46
	 * 
47
	 * @param s
48
	 *            Label to be set on the border
49
	 * @since 2.0
50
	 */
51
	public AbstractLabeledBorder(String s) {
52
		setLabel(s);
53
	}
54

    
55
	/**
56
	 * Calculates insets based on the current font and other attributes. This
57
	 * value will be cached until {@link #invalidate()} is called.
58
	 * 
59
	 * @param figure
60
	 *            The figure to which the border is being applied
61
	 * @return The Insets
62
	 */
63
	protected abstract Insets calculateInsets(IFigure figure);
64

    
65
	/**
66
	 * Returns the font that this border will use. If no Font has been
67
	 * specified, the font associated with the input Figure will be used.
68
	 * 
69
	 * @param f
70
	 *            Figure used to get a default font
71
	 * @return The font for this border
72
	 */
73
	protected Font getFont(IFigure f) {
74
		if (font == null)
75
			return f.getFont();
76
		return font;
77
	}
78

    
79
	/**
80
	 * Returns the insets, or space associated for this border. Returns any
81
	 * previously set value if present, else calculates it from the Figure
82
	 * provided in as input.
83
	 * 
84
	 * @param fig
85
	 *            Figure used to calculate insets
86
	 * @return The insets
87
	 */
88
	public Insets getInsets(IFigure fig) {
89
		if (insets == null)
90
			insets = calculateInsets(fig);
91
		return insets;
92
	}
93

    
94
	/**
95
	 * @see org.eclipse.draw2d.LabeledBorder#getLabel()
96
	 */
97
	public String getLabel() {
98
		return label;
99
	}
100

    
101
	/**
102
	 * @see org.eclipse.draw2d.Border#getPreferredSize(IFigure)
103
	 */
104
	public Dimension getPreferredSize(IFigure fig) {
105
		return new Dimension(getTextExtents(fig));
106
	}
107

    
108
	/**
109
	 * Returns the text Color of this AbstractLabeledBorder's label.
110
	 * 
111
	 * @return The text color
112
	 * @since 2.0
113
	 */
114
	public Color getTextColor() {
115
		return textColor;
116
	}
117

    
118
	/**
119
	 * Calculates and returns the size required by this border's label.
120
	 * 
121
	 * @param f
122
	 *            IFigure on which the calculations are to be made
123
	 * @return Dimensions required by the text of this border's label
124
	 * @since 2.0
125
	 */
126
	protected Dimension getTextExtents(IFigure f) {
127
		if (textExtents == null)
128
			textExtents = FigureUtilities.getTextExtents(label, getFont(f));
129
		return textExtents;
130
	}
131

    
132
	/**
133
	 * Resets the internal values and state so that they can be recalculated.
134
	 * Called whenever a state change has occurred that effects the insets or
135
	 * text extents of this border.
136
	 */
137
	protected void invalidate() {
138
		insets = null;
139
		textExtents = null;
140
	}
141

    
142
	/**
143
	 * Sets the Font of this border to the input value, and invalidates the
144
	 * border forcing an update of internal parameters of insets and text
145
	 * extents.
146
	 * 
147
	 * @param font
148
	 *            The font
149
	 */
150
	public void setFont(Font font) {
151
		this.font = font;
152
		invalidate();
153
	}
154

    
155
	/**
156
	 * @see org.eclipse.draw2d.LabeledBorder#setLabel(String)
157
	 */
158
	public void setLabel(String s) {
159
		label = ((s == null) ? "" : s); //$NON-NLS-1$
160
		invalidate();
161
	}
162

    
163
	/**
164
	 * Sets the color for this border's text.
165
	 * 
166
	 * @param color
167
	 *            Color to be set for this border's text
168
	 * @since 2.0
169
	 */
170
	public void setTextColor(Color color) {
171
		textColor = color;
172
	}
173

    
174
}
(7-7/171)