comparison dwtx/draw2d/AbstractLabeledBorder.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.draw2d.AbstractLabeledBorder;
14
15 import dwt.dwthelper.utils;
16
17
18
19 import dwt.graphics.Color;
20 import dwt.graphics.Font;
21 import dwtx.draw2d.geometry.Dimension;
22 import dwtx.draw2d.geometry.Insets;
23 import dwtx.draw2d.AbstractBorder;
24 import dwtx.draw2d.LabeledBorder;
25 import dwtx.draw2d.IFigure;
26 import dwtx.draw2d.ColorConstants;
27 import dwtx.draw2d.FigureUtilities;
28
29 /**
30 * Provides support for a border with a label describing the contents of which it is
31 * surrounding.
32 */
33 public abstract class AbstractLabeledBorder
34 : AbstractBorder
35 , LabeledBorder
36 {
37
38 private Dimension textExtents;
39 private String label;
40 private Insets insets;
41 private Color textColor;
42 private Font font;
43
44 /**
45 * Constructs a default AbstractLabeledBorder with the name of this class set as its
46 * label.
47 *
48 * @since 2.0
49 */
50 public this() {
51 textColor = ColorConstants.black;
52 String className = this.classinfo.name;
53 setLabel(className.substring(className.lastIndexOf('.') + 1, className.length));
54 }
55
56 /**
57 * Constructs a border with the label set to the String passed in as input.
58 *
59 * @param s Label to be set on the border
60 * @since 2.0
61 */
62 public this(String s) {
63 textColor = ColorConstants.black;
64 setLabel(s);
65 }
66
67 /**
68 * Calculates insets based on the current font and other attributes. This value will be
69 * cached until {@link #invalidate()} is called.
70 * @param figure The figure to which the border is being applied
71 * @return The Insets
72 */
73 protected abstract Insets calculateInsets(IFigure figure);
74
75 /**
76 * Returns the font that this border will use. If no Font has been specified, the font
77 * associated with the input Figure will be used.
78 * @param f Figure used to get a default font
79 * @return The font for this border
80 */
81 protected Font getFont(IFigure f) {
82 if (font is null)
83 return f.getFont();
84 return font;
85 }
86
87 /**
88 * Returns the insets, or space associated for this border. Returns any previously set
89 * value if present, else calculates it from the Figure provided in as input.
90 * @param fig Figure used to calculate insets
91 * @return The insets
92 */
93 public Insets getInsets(IFigure fig) {
94 if (insets is null)
95 insets = calculateInsets(fig);
96 return insets;
97 }
98
99 /**
100 * @see dwtx.draw2d.LabeledBorder#getLabel()
101 */
102 public String getLabel() {
103 return label;
104 }
105
106 /**
107 * @see dwtx.draw2d.Border#getPreferredSize(IFigure)
108 */
109 public Dimension getPreferredSize(IFigure fig) {
110 return new Dimension(getTextExtents(fig));
111 }
112
113 /**
114 * Returns the text Color of this AbstractLabeledBorder's label.
115 *
116 * @return The text color
117 * @since 2.0
118 */
119 public Color getTextColor() {
120 return textColor;
121 }
122
123 /**
124 * Calculates and returns the size required by this border's label.
125 *
126 * @param f IFigure on which the calculations are to be made
127 * @return Dimensions required by the text of this border's label
128 * @since 2.0
129 */
130 protected Dimension getTextExtents(IFigure f) {
131 if (textExtents is null)
132 textExtents = FigureUtilities.getTextExtents(label, getFont(f));
133 return textExtents;
134 }
135
136 /**
137 * Resets the internal values and state so that they can be recalculated. Called whenever
138 * a state change has occurred that effects the insets or text extents of this border.
139 */
140 protected void invalidate() {
141 insets = null;
142 textExtents = null;
143 }
144
145 /**
146 * Sets the Font of this border to the input value, and invalidates the border forcing an
147 * update of internal parameters of insets and text extents.
148 * @param font The font
149 */
150 public void setFont(Font font) {
151 this.font = font;
152 invalidate();
153 }
154
155 /**
156 * @see dwtx.draw2d.LabeledBorder#setLabel(String)
157 */
158 public void setLabel(String s) {
159 label = ((s is null) ? "" : s); //$NON-NLS-1$
160 invalidate();
161 }
162
163 /**
164 * Sets the color for this border's text.
165 *
166 * @param color Color to be set for this border's text
167 * @since 2.0
168 */
169 public void setTextColor(Color color) {
170 textColor = color;
171 }
172
173 }