1 /*
2  * Created on Jan 9, 2012
3  * Created by Paul Gardner
4  *
5  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19 
20 
21 package org.gudy.azureus2.ui.swt.components;
22 
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.PaintEvent;
25 import org.eclipse.swt.events.PaintListener;
26 import org.eclipse.swt.graphics.GC;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.widgets.Canvas;
31 import org.eclipse.swt.widgets.Composite;
32 import org.gudy.azureus2.core3.util.Debug;
33 import org.gudy.azureus2.ui.swt.shells.GCStringPrinter;
34 
35 public class
36 DoubleBufferedLabel
37 	extends Canvas implements PaintListener
38 {
39 	private String text = "";
40 
DoubleBufferedLabel( Composite parent, int style )41 	public DoubleBufferedLabel(
42 		Composite 	parent,
43 		int 		style )
44 	{
45 		super( parent, style | SWT.DOUBLE_BUFFERED );
46 
47 			// only support GridLayout I'm afraid...
48 
49 		GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER | GridData.VERTICAL_ALIGN_FILL);
50 
51 		setLayoutData(gridData);
52 
53 		addPaintListener(this);
54 	}
55 
56 	public void
setLayoutData( Object ld )57 	setLayoutData(
58 		Object	ld )
59 	{
60 		if ( ld instanceof GridData ){
61 
62 			GridData gd = (GridData)ld;
63 
64 				// need to ensure we have the right vertical align
65 
66 			gd.verticalAlignment	= 4;
67 		}
68 
69 		super.setLayoutData( ld );
70 	}
71 
72 	public void
paintControl( PaintEvent e)73 	paintControl(
74 		PaintEvent e)
75 	{
76 		e.gc.setAdvanced(true);
77 
78 		Rectangle clientArea = getClientArea();
79 
80 		GCStringPrinter sp =
81 			new GCStringPrinter(e.gc, getText(), clientArea, true, true, SWT.LEFT);
82 
83 		sp.printString(e.gc, clientArea, SWT.LEFT);
84 	}
85 
86 
87 	public Point
computeSize( int wHint, int hHint)88 	computeSize(
89 		int wHint,
90 		int hHint)
91 	{
92 		return computeSize(wHint, hHint, true);
93 	}
94 
95 	public Point
computeSize( int wHint, int hHint, boolean changed )96 	computeSize(
97 		int 	wHint,
98 		int 	hHint,
99 		boolean changed )
100 	{
101 		try {
102 			Point pt = computeSize(wHint, hHint, changed, false);
103 
104 			return pt;
105 
106 		} catch (Throwable t){
107 
108 			Debug.out("Error while computing size for DoubleBufferedLabel with text:"
109 					+ getText() + "; " + t.toString());
110 
111 			return new Point(0, 0);
112 		}
113 	}
114 
115 	public Point
computeSize( int wHint, int hHint, boolean changed, boolean realWidth )116 	computeSize(
117 		int 	wHint,
118 		int 	hHint,
119 		boolean changed,
120 		boolean realWidth )
121 	{
122 		/* Can't do this as things are often layed out when invisible and
123 		 * don't get redone on visibility change :(
124 
125 		if (!isVisible()){
126 
127 			return (new Point(0, 0));
128 		}
129 		*/
130 
131 		if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
132 			return new Point(wHint, hHint);
133 		}
134 		Point pt = new Point(wHint, hHint);
135 
136 		Point lastSize = new Point(0, 0);
137 
138 		GC gc = new GC(this);
139 
140 		GCStringPrinter sp = new GCStringPrinter(gc, getText(), new Rectangle(0,
141 				0, 10000, 20), true, true, SWT.LEFT);
142 
143 		sp.calculateMetrics();
144 
145 		Point lastTextSize = sp.getCalculatedSize();
146 
147 		gc.dispose();
148 
149 		lastSize.x += lastTextSize.x + 10;
150 		lastSize.y = Math.max(lastSize.y, lastTextSize.y);
151 
152 		if (wHint == SWT.DEFAULT) {
153 			pt.x = lastSize.x;
154 		}
155 		if (hHint == SWT.DEFAULT) {
156 			pt.y = lastSize.y;
157 		}
158 
159 		return pt;
160 	}
161 
162 	public String
getText()163 	getText()
164 	{
165 		return text;
166 	}
167 
168 	public void
setText( String text)169 	setText(
170 		String text)
171 	{
172 		if (text == null){
173 			text = "";
174 		}
175 
176 		if (text.equals(getText())){
177 			return;
178 		}
179 
180 		this.text = text;
181 
182 		redraw();
183 	}
184 }
185