KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > widgets > Hyperlink


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.ui.forms.widgets;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.accessibility.*;
15 import org.eclipse.swt.graphics.*;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.ui.internal.forms.widgets.*;
18
19 /**
20  * Hyperlink is a concrete implementation of the abstract base class that draws
21  * text in the client area. Text can be wrapped and underlined. Hyperlink is
22  * typically added to the hyperlink group so that certain properties are managed
23  * for all the hyperlinks that belong to it.
24  * <p>
25  * Hyperlink can be extended.
26  *
27  * @see org.eclipse.ui.forms.HyperlinkGroup
28  * @since 3.0
29  */

30 public class Hyperlink extends AbstractHyperlink {
31     private String JavaDoc text;
32     private static final String JavaDoc ELLIPSIS = "..."; //$NON-NLS-1$
33
private boolean underlined;
34     // The tooltip is used for two purposes - the application can set
35
// a tooltip or the tooltip can be used to display the full text when the
36
// the text has been truncated due to the label being too short.
37
// The appToolTip stores the tooltip set by the application. Control.tooltiptext
38
// contains whatever tooltip is currently being displayed.
39
private String JavaDoc appToolTipText;
40
41     /**
42      * Creates a new hyperlink control in the provided parent.
43      *
44      * @param parent
45      * the control parent
46      * @param style
47      * the widget style
48      */

49     public Hyperlink(Composite parent, int style) {
50         super(parent, style);
51         initAccessible();
52     }
53
54     protected void initAccessible() {
55         Accessible accessible = getAccessible();
56         accessible.addAccessibleListener(new AccessibleAdapter() {
57             public void getName(AccessibleEvent e) {
58                 e.result = getText();
59                 if (e.result == null)
60                     getHelp(e);
61             }
62
63             public void getHelp(AccessibleEvent e) {
64                 e.result = getToolTipText();
65             }
66         });
67         accessible.addAccessibleControlListener(new AccessibleControlAdapter() {
68             public void getChildAtPoint(AccessibleControlEvent e) {
69                 Point pt = toControl(new Point(e.x, e.y));
70                 e.childID = (getBounds().contains(pt)) ? ACC.CHILDID_SELF
71                         : ACC.CHILDID_NONE;
72             }
73
74             public void getLocation(AccessibleControlEvent e) {
75                 Rectangle location = getBounds();
76                 Point pt = toDisplay(new Point(location.x, location.y));
77                 e.x = pt.x;
78                 e.y = pt.y;
79                 e.width = location.width;
80                 e.height = location.height;
81             }
82
83             public void getChildCount(AccessibleControlEvent e) {
84                 e.detail = 0;
85             }
86
87             public void getRole(AccessibleControlEvent e) {
88                 e.detail = ACC.ROLE_LINK;
89             }
90                         
91             public void getDefaultAction (AccessibleControlEvent e) {
92                 e.result = SWT.getMessage ("SWT_Press"); //$NON-NLS-1$
93
}
94
95             public void getState(AccessibleControlEvent e) {
96                 int state = ACC.STATE_NORMAL;
97                 if (Hyperlink.this.getSelection())
98                     state = ACC.STATE_SELECTED | ACC.STATE_FOCUSED;
99                 e.detail = state;
100             }
101         });
102     }
103
104     /**
105      * Sets the underlined state. It is not necessary to call this method when
106      * in a hyperlink group.
107      *
108      * @param underlined
109      * if <samp>true </samp>, a line will be drawn below the text for
110      * each wrapped line.
111      */

112     public void setUnderlined(boolean underlined) {
113         this.underlined = underlined;
114         redraw();
115     }
116
117     /**
118      * Returns the underline state of the hyperlink.
119      *
120      * @return <samp>true </samp> if text is underlined, <samp>false </samp>
121      * otherwise.
122      */

123     public boolean isUnderlined() {
124         return underlined;
125     }
126
127     /**
128      * Overrides the parent by incorporating the margin.
129      */

130     public Point computeSize(int wHint, int hHint, boolean changed) {
131         checkWidget();
132         int innerWidth = wHint;
133         if (innerWidth != SWT.DEFAULT)
134             innerWidth -= marginWidth * 2;
135         Point textSize = computeTextSize(innerWidth, hHint);
136         int textWidth = textSize.x + 2 * marginWidth;
137         int textHeight = textSize.y + 2 * marginHeight;
138         return new Point(textWidth, textHeight);
139     }
140
141     /**
142      * Returns the current hyperlink text.
143      *
144      * @return hyperlink text
145      */

146     public String JavaDoc getText() {
147         return text;
148     }
149     
150     /* (non-Javadoc)
151      * @see org.eclipse.swt.widgets.Control#getToolTipText()
152      */

153     public String JavaDoc getToolTipText () {
154         checkWidget();
155         return appToolTipText;
156     }
157     
158     /* (non-Javadoc)
159      * @see org.eclipse.swt.widgets.Control#setToolTipText(java.lang.String)
160      */

161     public void setToolTipText (String JavaDoc string) {
162         super.setToolTipText (string);
163         appToolTipText = super.getToolTipText();
164     }
165
166     /**
167      * Sets the text of this hyperlink.
168      *
169      * @param text
170      * the hyperlink text
171      */

172     public void setText(String JavaDoc text) {
173         if (text != null)
174             this.text = text;
175         else
176             this.text = ""; //$NON-NLS-1$
177
redraw();
178     }
179
180     /**
181      * Paints the hyperlink text.
182      *
183      * @param gc
184      * graphic context
185      */

186     protected void paintHyperlink(GC gc) {
187         Rectangle carea = getClientArea();
188         Rectangle bounds = new Rectangle(marginWidth, marginHeight, carea.width
189                 - marginWidth - marginWidth, carea.height - marginHeight
190                 - marginHeight);
191         paintText(gc, bounds);
192     }
193
194     /**
195      * Paints the hyperlink text in provided bounding rectangle.
196      *
197      * @param gc
198      * graphic context
199      * @param bounds
200      * the bounding rectangle in which to paint the text
201      */

202     protected void paintText(GC gc, Rectangle bounds) {
203         gc.setFont(getFont());
204         gc.setForeground(getForeground());
205         if ((getStyle() & SWT.WRAP) != 0) {
206             FormUtil.paintWrapText(gc, text, bounds, underlined);
207         } else {
208             Point totalSize = computeTextSize(SWT.DEFAULT, SWT.DEFAULT);
209             boolean shortenText =false;
210             if (bounds.width<totalSize.x) {
211                 // shorten
212
shortenText=true;
213             }
214             int textWidth = Math.min(bounds.width, totalSize.x);
215             int textHeight = totalSize.y;
216             String JavaDoc textToDraw = getText();
217             if (shortenText) {
218                 textToDraw = shortenText(gc, getText(), bounds.width);
219                 if (appToolTipText == null) {
220                     super.setToolTipText(getText());
221                 }
222             }
223             else {
224                 super.setToolTipText(appToolTipText);
225             }
226             gc.drawText(textToDraw, bounds.x, bounds.y, true);
227             if (underlined) {
228                 int descent = gc.getFontMetrics().getDescent();
229                 int lineY = bounds.y + textHeight - descent + 1;
230                 gc.drawLine(bounds.x, lineY, bounds.x + textWidth, lineY);
231             }
232         }
233     }
234     
235     protected String JavaDoc shortenText(GC gc, String JavaDoc t, int width) {
236         if (t == null) return null;
237         int w = gc.textExtent(ELLIPSIS).x;
238         if (width<=w) return t;
239         int l = t.length();
240         int max = l/2;
241         int min = 0;
242         int mid = (max+min)/2 - 1;
243         if (mid <= 0) return t;
244         while (min < mid && mid < max) {
245             String JavaDoc s1 = t.substring(0, mid);
246             String JavaDoc s2 = t.substring(l-mid, l);
247             int l1 = gc.textExtent(s1).x;
248             int l2 = gc.textExtent(s2).x;
249             if (l1+w+l2 > width) {
250                 max = mid;
251                 mid = (max+min)/2;
252             } else if (l1+w+l2 < width) {
253                 min = mid;
254                 mid = (max+min)/2;
255             } else {
256                 min = max;
257             }
258         }
259         if (mid == 0) return t;
260         return t.substring(0, mid)+ELLIPSIS+t.substring(l-mid, l);
261     }
262
263     protected Point computeTextSize(int wHint, int hHint) {
264         Point extent;
265         GC gc = new GC(this);
266         gc.setFont(getFont());
267         if ((getStyle() & SWT.WRAP) != 0 && wHint != SWT.DEFAULT) {
268             extent = FormUtil.computeWrapSize(gc, getText(), wHint);
269         } else {
270             extent = gc.textExtent(getText());
271             if ((getStyle() & SWT.WRAP)==0 && wHint!=SWT.DEFAULT)
272                 extent.x = wHint;
273         }
274         gc.dispose();
275         return extent;
276     }
277 }
278
Popular Tags