KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > forms > widgets > HyperlinkSegment


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.forms.widgets;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.swt.graphics.*;
16 import org.eclipse.ui.forms.HyperlinkSettings;
17
18 /**
19  * @version 1.0
20  * @author
21  */

22 public class HyperlinkSegment
23     extends TextSegment {
24     private String JavaDoc href;
25     private static final String JavaDoc LINK_FG = "c.___link_fg";
26     private HyperlinkSettings settings;
27     
28     public HyperlinkSegment(String JavaDoc text, HyperlinkSettings settings, String JavaDoc fontId) {
29         super(text, fontId);
30         this.settings = settings;
31         underline = settings.getHyperlinkUnderlineMode()==HyperlinkSettings.UNDERLINE_ALWAYS;
32     }
33
34     /*
35      * @see IObjectReference#getObjectId()
36      */

37     public String JavaDoc getHref() {
38         return href;
39     }
40     
41     void setHref(String JavaDoc href) {
42         this.href = href;
43     }
44     public void paint(GC gc, int width, Locator locator, Hashtable JavaDoc resourceTable, boolean selected) {
45         resourceTable.put(LINK_FG, settings.getForeground());
46         setColorId(LINK_FG);
47         super.paint(gc, width, locator, resourceTable, selected);
48     }
49     
50     public void repaint(GC gc, boolean hover) {
51         FontMetrics fm = gc.getFontMetrics();
52         int lineHeight = fm.getHeight();
53         int descent = fm.getDescent();
54         boolean rolloverMode = settings.getHyperlinkUnderlineMode()==HyperlinkSettings.UNDERLINE_HOVER;
55         for (int i=0; i<areaRectangles.size(); i++) {
56             AreaRectangle areaRectangle = (AreaRectangle)areaRectangles.get(i);
57             Rectangle rect = areaRectangle.rect;
58             String JavaDoc text = areaRectangle.getText();
59             Point extent = gc.textExtent(text);
60             int textX = rect.x + 1;
61             gc.drawString(text, textX, rect.y, false);
62             if (underline || hover || rolloverMode) {
63                 int lineY = rect.y + lineHeight - descent + 1;
64                 Color saved=null;
65                 if (rolloverMode && !hover) {
66                     saved = gc.getForeground();
67                     gc.setForeground(gc.getBackground());
68                 }
69                 gc.drawLine(textX, lineY, textX+extent.x, lineY);
70                 if (saved!=null)
71                     gc.setForeground(saved);
72             }
73         }
74     }
75 }
76
Popular Tags