|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package javax.microedition.lcdui; |
|
18 |
|
19 import org.eclipse.swt.SWT; |
|
20 import org.eclipse.swt.graphics.Point; |
|
21 import org.eclipse.swt.graphics.Rectangle; |
|
22 import org.eclipse.swt.widgets.Composite; |
|
23 import org.eclipse.swt.widgets.Event; |
|
24 import org.eclipse.swt.widgets.Label; |
|
25 import org.eclipse.swt.widgets.Layout; |
|
26 import org.eclipse.swt.widgets.Listener; |
|
27 import org.eclipse.swt.widgets.ScrollBar; |
|
28 |
|
29 /** |
|
30 * Composite control which is able to scroll text with a vertical scroll bar. |
|
31 */ |
|
32 class ScrolledTextComposite extends Composite { |
|
33 |
|
34 private Label iESWTTextLabel; |
|
35 private ScrollBar vBar; |
|
36 private boolean resizing; |
|
37 private boolean iTextScrolling; |
|
38 |
|
39 /** |
|
40 * Constructor. |
|
41 */ |
|
42 public ScrolledTextComposite(Composite parent, ScrollBar vertBar) { |
|
43 super(parent, 0); |
|
44 vBar = vertBar; |
|
45 vBar.setVisible(false); |
|
46 vBar.setMaximum(0); |
|
47 vBar.setThumb(0); |
|
48 vBar.setSelection(0); |
|
49 |
|
50 vBar.addListener(SWT.Selection, new Listener() { |
|
51 public void handleEvent(Event e) { |
|
52 vScroll(); |
|
53 } |
|
54 }); |
|
55 |
|
56 addListener(SWT.Resize, new Listener() { |
|
57 public void handleEvent(Event e) { |
|
58 resize(); |
|
59 } |
|
60 }); |
|
61 |
|
62 iESWTTextLabel = new Label(this, SWT.WRAP); |
|
63 iESWTTextLabel.setLocation(0, 0); |
|
64 layout(); |
|
65 |
|
66 iESWTTextLabel.addListener(SWT.Resize, new Listener() { |
|
67 public void handleEvent(Event e) { |
|
68 if (e.type == SWT.Resize) { |
|
69 resize(); |
|
70 } |
|
71 } |
|
72 }); |
|
73 } |
|
74 |
|
75 public void setText(String aText) { |
|
76 iESWTTextLabel.setText(aText); |
|
77 iESWTTextLabel.setSize(calculateTextSize()); |
|
78 } |
|
79 |
|
80 private Point calculateTextSize() { |
|
81 iTextScrolling = false; |
|
82 vBar.setVisible(false); |
|
83 Point nextSize = iESWTTextLabel.computeSize(getClientArea().width, |
|
84 SWT.DEFAULT); |
|
85 if (nextSize.y > getClientArea().height) { |
|
86 iTextScrolling = true; |
|
87 vBar.setVisible(true); |
|
88 nextSize = iESWTTextLabel.computeSize(getClientArea().width, |
|
89 SWT.DEFAULT); |
|
90 } |
|
91 return nextSize; |
|
92 } |
|
93 |
|
94 /** |
|
95 * Return if the text is scrolling. The vBar.isVisible() is not good, |
|
96 * because it depends on parent's visibility. |
|
97 */ |
|
98 public boolean isTextScrolling() { |
|
99 return iTextScrolling; |
|
100 } |
|
101 |
|
102 public void setLayout(Layout layout) { |
|
103 // do not allow a layout to be set |
|
104 } |
|
105 |
|
106 public void layout(boolean changed) { |
|
107 iESWTTextLabel.setSize(calculateTextSize()); |
|
108 |
|
109 Rectangle contentRect = iESWTTextLabel.getBounds(); |
|
110 Rectangle hostRect = getClientArea(); |
|
111 vBar.setMaximum(contentRect.height); |
|
112 vBar.setThumb(Math.min(contentRect.height, hostRect.height)); |
|
113 int vPage = contentRect.height - hostRect.height; |
|
114 int vSelection = vBar.getSelection(); |
|
115 if (vSelection >= vPage) { |
|
116 if (vPage <= 0) { |
|
117 vSelection = 0; |
|
118 vBar.setSelection(0); |
|
119 } |
|
120 contentRect.y = -vSelection; |
|
121 } |
|
122 iESWTTextLabel.setLocation(contentRect.x, contentRect.y); |
|
123 } |
|
124 |
|
125 public Point computeSize(int wHint, int hHint, boolean changed) { |
|
126 Point size = iESWTTextLabel.computeSize(wHint, hHint, changed); |
|
127 Rectangle trim = computeTrim(0, 0, size.x, size.y); |
|
128 return new Point(trim.width, trim.height); |
|
129 } |
|
130 |
|
131 public Point getOrigin() { |
|
132 Point location = iESWTTextLabel.getLocation(); |
|
133 return new Point(-location.x, -location.y); |
|
134 } |
|
135 |
|
136 public void setOrigin(Point origin) { |
|
137 setOrigin(origin.x, origin.y); |
|
138 } |
|
139 |
|
140 public void setOrigin(int x, int y) { |
|
141 if (iESWTTextLabel != null) { |
|
142 vBar.setSelection(y); |
|
143 iESWTTextLabel.setLocation(0, -vBar.getSelection()); |
|
144 } |
|
145 } |
|
146 |
|
147 private void resize() { |
|
148 if (!resizing) { |
|
149 resizing = true; |
|
150 layout(); |
|
151 resizing = false; |
|
152 } |
|
153 } |
|
154 |
|
155 private void vScroll() { |
|
156 Point location = iESWTTextLabel.getLocation(); |
|
157 int vSelection = vBar.getSelection(); |
|
158 iESWTTextLabel.setLocation(location.x, -vSelection); |
|
159 } |
|
160 } |