-
Notifications
You must be signed in to change notification settings - Fork 4
/
TextFigure.java
272 lines (247 loc) · 7.18 KB
/
TextFigure.java
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* $Id$
*
* Copyright (C) 2002 Stephane GALLAND, Madhi HANNOUN, Marc BAUMGARTNER.
* Copyright (C) 2012 Stephane GALLAND.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
*/
package org.arakhne.neteditor.fig.figure.decoration;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import org.arakhne.afc.math.continous.object2d.Rectangle2f;
import org.arakhne.afc.ui.vector.Color;
import org.arakhne.afc.ui.vector.Font;
import org.arakhne.afc.ui.vector.VectorToolkit;
import org.arakhne.neteditor.fig.graphics.ViewGraphics2D;
import org.arakhne.vmutil.locale.Locale;
/** A decoration figure that is drawing a text.
*
* @author $Author: galland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public class TextFigure extends BlockDecorationFigure {
private static final long serialVersionUID = -8727208834423982285L;
private String text = null;
/** <code>true</code> if this text area must be filled,
* <code>false</code> otherwise.
*/
private boolean filled = true;
/** <code>true</code> if this text area must be framed,
* <code>false</code> otherwise.
*/
private boolean framed = true;
/** Construct a new figure.
*
* @param viewUUID is the UUID of the view that is enclosing this figure.
*/
public TextFigure(UUID viewUUID) {
this(viewUUID, 0, 0);
}
/** Construct a new figure.
*
* @param viewUUID is the UUID of the view that is enclosing this figure.
* @param x horizontal postition of the upper-left corner of this FigNode.
* @param y vertical postition of the upper-left corner of this FigNode.
*/
public TextFigure(UUID viewUUID, float x, float y) {
this(viewUUID, x, y, DEFAULT_MINIMAL_SIZE, DEFAULT_MINIMAL_SIZE);
}
/** Construct a new figure.
*
* @param viewUUID is the UUID of the view that is enclosing this figure.
* @param x horizontal postition of the upper-left corner of this FigNode.
* @param y vertical postition of the upper-left corner of this FigNode.
* @param width is the width of the figure.
* @param height is the height of the figure.
*/
public TextFigure(UUID viewUUID, float x, float y, float width, float height) {
super(viewUUID, x, y, width, height);
}
/** Set the displayed text.
*
* @param text is the text to display.
*/
public void setText(String text) {
if ((text==null && this.text!=null)
||
(text!=null && !text.equals(this.text))) {
String old = this.text;
this.text = text;
firePropertyChange(PROPERTY_TEXT, old, this.text);
repaint(true);
}
}
/** Replies the text.
*
* @return the text
*/
public String getText() {
return this.text;
}
/** Replies the displayed text.
*
* @return the text or the empty-strign message
* if these is no associated text.
*/
public String getDisplayableText() {
String s = getText();
if (s==null || s.isEmpty()) {
s = Locale.getString("EMPTY_TEXT"); //$NON-NLS-1$
}
return s;
}
/** Change the filling flag.
*
* @param filled <code>true</code> if this FigText
* must be filled, <code>false</code> otherwise.
*/
public void setFilled(boolean filled) {
if (filled!=this.filled) {
boolean old = this.filled;
this.filled = filled;
firePropertyChange(PROPERTY_FILLED, old, this.filled);
repaint(false);
}
}
/** Return the filling flag.
*
* @return <code>true</code> if this FigText must
* be filled, <code>false</code> otherwise.
*/
public boolean isFilled() {
return this.filled;
}
/** Change the framing flag.
*
* @param framed <code>true</code> if this FigText
* must be framed, <code>false</code> otherwise.
*/
public void setFramed(boolean framed) {
if (framed!=this.framed) {
boolean old = this.framed;
this.framed = framed;
firePropertyChange(PROPERTY_FRAMED, old, this.framed);
repaint(false);
}
}
/** Return the framed flag.
*
* @return <code>true</code> if this FigText must
* be framed, <code>false</code> otherwise.
*/
public boolean isFramed() {
return this.framed;
}
/**
* {@inheritDoc}
*/
@Override
public void paint(ViewGraphics2D g) {
boolean isFramed = isFramed();
boolean isFilled = isFilled();
String text = getDisplayableText();
if (isFramed || isFilled || (text!=null && !text.isEmpty())) {
g.setOutlineDrawn(isFramed);
g.setInteriorPainted(isFilled);
g.setInteriorText(text);
g.draw(g.getCurrentViewComponentBounds());
}
}
/**
* {@inheritDoc}
*/
@Override
@Deprecated
public boolean hit(float x, float y, float epsilon) {
Rectangle2f bounds = getBounds();
Rectangle2f rr = new Rectangle2f(
bounds.getMinX() - epsilon,
bounds.getMinY() - epsilon,
bounds.getWidth() + 2*epsilon,
bounds.getHeight() + 2*epsilon);
return rr.contains(x, y);
}
/**
* {@inheritDoc}
*/
@Override
public Map<String,Object> getProperties() {
Map<String,Object> properties = super.getProperties();
if (this.text==null || this.text.isEmpty())
properties.put(PROPERTY_TEXT, null);
else
properties.put(PROPERTY_TEXT, this.text);
properties.put(PROPERTY_FRAMED, this.framed);
properties.put(PROPERTY_FILLED, this.filled);
return properties;
}
/**
* {@inheritDoc}
*/
@Override
public Map<String,Class<?>> getUIEditableProperties() {
Map<String,Class<?>> properties = new TreeMap<String,Class<?>>();
properties.put(PROPERTY_LINECOLOR, Color.class);
properties.put(PROPERTY_FRAMED, Boolean.class);
properties.put(PROPERTY_FILLINGCOLOR, Color.class);
properties.put(PROPERTY_FILLED, Boolean.class);
properties.put(PROPERTY_TEXT, String.class);
return properties;
}
/**
* {@inheritDoc}
*/
@Override
public void setProperties(Map<String, Object> properties) {
super.setProperties(properties);
if (properties!=null) {
setText(propGetString(PROPERTY_TEXT, this.text, false, properties));
setFramed(propGetBoolean(PROPERTY_FRAMED, this.framed, properties));
setFilled(propGetBoolean(PROPERTY_FILLED, this.filled, properties));
}
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
String n = getText();
if (n!=null && !n.isEmpty()) return n;
return super.toString();
}
/** {@inheritDoc}
*/
@Override
public void fitToContent() {
float w, h;
String n = getText();
if (n!=null && !n.isEmpty()) {
Font font = VectorToolkit.font();
Rectangle2f r = font.getStringBounds(n);
w = r.getWidth();
h = r.getHeight();
}
else {
w = getMinimalWidth();
h = getMinimalHeight();
}
setSize(w, h);
}
}