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.widgets;
|
12
|
|
13
|
import org.eclipse.swt.accessibility.ACC;
|
14
|
import org.eclipse.swt.accessibility.AccessibleAdapter;
|
15
|
import org.eclipse.swt.accessibility.AccessibleControlAdapter;
|
16
|
import org.eclipse.swt.accessibility.AccessibleControlEvent;
|
17
|
import org.eclipse.swt.accessibility.AccessibleEvent;
|
18
|
import org.eclipse.swt.events.KeyAdapter;
|
19
|
import org.eclipse.swt.events.KeyEvent;
|
20
|
import org.eclipse.swt.graphics.Font;
|
21
|
import org.eclipse.swt.graphics.Image;
|
22
|
import org.eclipse.swt.widgets.Composite;
|
23
|
|
24
|
import org.eclipse.draw2d.Border;
|
25
|
import org.eclipse.draw2d.ColorConstants;
|
26
|
import org.eclipse.draw2d.FigureCanvas;
|
27
|
import org.eclipse.draw2d.FocusEvent;
|
28
|
import org.eclipse.draw2d.Graphics;
|
29
|
import org.eclipse.draw2d.MarginBorder;
|
30
|
import org.eclipse.draw2d.Viewport;
|
31
|
import org.eclipse.draw2d.geometry.Point;
|
32
|
import org.eclipse.draw2d.rap.swt.SWT;
|
33
|
import org.eclipse.draw2d.text.FlowPage;
|
34
|
import org.eclipse.draw2d.text.TextFlow;
|
35
|
|
36
|
/**
|
37
|
* A widget for displaying a multi-line string. The label will have a vertical
|
38
|
* or horizontal scrollbar when needed. Unlike the platform Label, this label is
|
39
|
* focusable and accessible to screen-readers.
|
40
|
*
|
41
|
* @author hudsonr
|
42
|
*/
|
43
|
public final class MultiLineLabel extends FigureCanvas {
|
44
|
|
45
|
private TextFlow textFlow;
|
46
|
static final Border MARGIN = new MarginBorder(2);
|
47
|
private Image image;
|
48
|
|
49
|
class FocusableViewport extends Viewport {
|
50
|
FocusableViewport() {
|
51
|
super(true);
|
52
|
setFocusTraversable(true);
|
53
|
setBorder(MARGIN);
|
54
|
}
|
55
|
|
56
|
public void handleFocusGained(FocusEvent event) {
|
57
|
super.handleFocusGained(event);
|
58
|
repaint();
|
59
|
}
|
60
|
|
61
|
public void handleFocusLost(FocusEvent event) {
|
62
|
super.handleFocusLost(event);
|
63
|
repaint();
|
64
|
}
|
65
|
|
66
|
protected void paintBorder(Graphics graphics) {
|
67
|
super.paintBorder(graphics);
|
68
|
if (hasFocus()) {
|
69
|
graphics.setForegroundColor(ColorConstants.black);
|
70
|
graphics.setBackgroundColor(ColorConstants.white);
|
71
|
graphics.drawFocus(getBounds().getResized(-1, -1));
|
72
|
}
|
73
|
}
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Constructs a new MultiLineLabel with the given parent.
|
78
|
*
|
79
|
* @param parent
|
80
|
* the parent
|
81
|
*/
|
82
|
public MultiLineLabel(Composite parent) {
|
83
|
super(parent);
|
84
|
setViewport(new FocusableViewport());
|
85
|
|
86
|
FlowPage page = new FlowPage();
|
87
|
textFlow = new TextFlow();
|
88
|
page.add(textFlow);
|
89
|
|
90
|
setContents(page);
|
91
|
getViewport().setContentsTracksWidth(true);
|
92
|
addAccessibility();
|
93
|
}
|
94
|
|
95
|
private void addAccessibility() {
|
96
|
getAccessible().addAccessibleControlListener(
|
97
|
new AccessibleControlAdapter() {
|
98
|
public void getRole(AccessibleControlEvent e) {
|
99
|
e.detail = ACC.ROLE_LABEL;
|
100
|
}
|
101
|
|
102
|
public void getState(AccessibleControlEvent e) {
|
103
|
e.detail = ACC.STATE_READONLY;
|
104
|
}
|
105
|
});
|
106
|
getAccessible().addAccessibleListener(new AccessibleAdapter() {
|
107
|
public void getName(AccessibleEvent e) {
|
108
|
e.result = getText();
|
109
|
}
|
110
|
});
|
111
|
addKeyListener(new KeyAdapter() {
|
112
|
public void keyPressed(KeyEvent e) {
|
113
|
Point p = getViewport().getViewLocation();
|
114
|
int dy = getFont().getFontData()[0].getHeight();
|
115
|
int dx = dy * 3 / 2;
|
116
|
boolean mirrored = (e.widget.getStyle() & SWT.MIRRORED) != 0;
|
117
|
if (e.keyCode == SWT.ARROW_DOWN) {
|
118
|
scrollToY(p.y + dy / 2);
|
119
|
scrollToY(p.y + dy);
|
120
|
scrollToY(p.y + dy * 3 / 2);
|
121
|
scrollToY(p.y + dy * 2);
|
122
|
} else if (e.keyCode == SWT.ARROW_UP) {
|
123
|
scrollToY(p.y - dy / 2);
|
124
|
scrollToY(p.y - dy);
|
125
|
scrollToY(p.y - dy * 3 / 2);
|
126
|
scrollToY(p.y - dy * 2);
|
127
|
} else if ((!mirrored && e.keyCode == SWT.ARROW_RIGHT)
|
128
|
|| (mirrored && e.keyCode == SWT.ARROW_LEFT)) {
|
129
|
scrollToX(p.x + dx);
|
130
|
scrollToX(p.x + dx * 2);
|
131
|
scrollToX(p.x + dx * 3);
|
132
|
} else if ((!mirrored && e.keyCode == SWT.ARROW_LEFT)
|
133
|
|| (mirrored && e.keyCode == SWT.ARROW_RIGHT)) {
|
134
|
scrollToX(p.x - dx);
|
135
|
scrollToX(p.x - dx * 2);
|
136
|
scrollToX(p.x - dx * 3);
|
137
|
}
|
138
|
}
|
139
|
});
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* @see org.eclipse.swt.widgets.Control#setEnabled(boolean)
|
144
|
*/
|
145
|
public void setEnabled(boolean enabled) {
|
146
|
super.setEnabled(enabled);
|
147
|
textFlow.setEnabled(getEnabled());
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* @return the Image for this label, or <code>null</code> if there is none
|
152
|
* @see #setImage(Image)
|
153
|
*/
|
154
|
public Image getImage() {
|
155
|
return image;
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Returns the text in this label.
|
160
|
*
|
161
|
* @return the text
|
162
|
*/
|
163
|
public String getText() {
|
164
|
return textFlow.getText();
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* @see org.eclipse.swt.widgets.Canvas#setFont(org.eclipse.swt.graphics.Font)
|
169
|
*/
|
170
|
public void setFont(Font font) {
|
171
|
super.setFont(font);
|
172
|
textFlow.revalidate();
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* @param image
|
177
|
* The <code>Image</code> to be used for this label. It can be
|
178
|
* <code>null</code>.
|
179
|
*/
|
180
|
public void setImage(Image image) {
|
181
|
this.image = image;
|
182
|
if (image != null)
|
183
|
setBorder(new ImageBorder(image));
|
184
|
else
|
185
|
setBorder(null);
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Sets the text for this label.
|
190
|
*
|
191
|
* @param text
|
192
|
* the new text
|
193
|
*/
|
194
|
public void setText(String text) {
|
195
|
textFlow.setText(text);
|
196
|
}
|
197
|
|
198
|
}
|