Skip to main content
summaryrefslogtreecommitdiffstats
blob: e5101d655253d9c0f64056bc149d1b768d4e408e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * Creates a hyperlink label that when enabled, shows as blue and changes cursor when hover over and when disabled,
 * shows in black with no cursor change. To add listener, use: link.addListener(SWT.MouseUp, new Listener() { public
 * void handleEvent(org.eclipse.swt.widgets.Event event) { System.out.println("Link Selected"); } });
 * 
 * @author Donald G. Dunne
 */

public class HyperLinkLabel extends Label {

   private boolean hyperEnabled = true;
   /**
    * Amount of the margin width around the hyperlink (default is 1).
    */
   protected int marginWidth = 1;

   /**
    * Amount of the margin height around the hyperlink (default is 1).
    */
   protected int marginHeight = 1;

   public HyperLinkLabel(Composite parent, int style) {
      this(null, parent, style, null);
   }

   public HyperLinkLabel(FormToolkit toolkit, Composite parent, int style) {
      this(toolkit, parent, style, null);
   }

   public HyperLinkLabel(FormToolkit toolkit, Composite parent, int style, String text) {
      super(parent, style);
      if (text != null) {
         setText(text);
      }
      if (toolkit != null) {
         toolkit.adapt(this, true, true);
      }
      refresh();
   }

   public static void adapt(final Label label) {
      label.setForeground(Displays.getSystemColor(SWT.COLOR_BLUE));
      label.addMouseTrackListener(new MouseTrackAdapter() {

         @Override
         public void mouseEnter(MouseEvent e) {
            label.setCursor(CursorManager.getCursor(SWT.CURSOR_HAND));
         }

         @Override
         public void mouseExit(MouseEvent e) {
            label.setCursor(null);
         };

      });
   }

   MouseTrackListener listener = new MouseTrackAdapter() {

      @Override
      public void mouseEnter(MouseEvent e) {
         setCursor(CursorManager.getCursor(SWT.CURSOR_HAND));
      }

      @Override
      public void mouseExit(MouseEvent e) {
         setCursor(null);
      };

   };

   public void refresh() {
      if (hyperEnabled) {
         setForeground(Displays.getSystemColor(SWT.COLOR_BLUE));
         removeMouseTrackListener(listener);
         addMouseTrackListener(listener);
      } else {
         setForeground(Displays.getSystemColor(SWT.COLOR_BLACK));
         removeMouseTrackListener(listener);
      }
   }

   public boolean isHyperEnabled() {
      return hyperEnabled;
   }

   public void setHyperEnabled(boolean hyperEnabled) {
      this.hyperEnabled = hyperEnabled;
      refresh();
   }

}

Back to the top