1 11 package org.eclipse.ui.forms; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.graphics.Color; 17 import org.eclipse.swt.widgets.Display; 18 import org.eclipse.swt.widgets.Event; 19 import org.eclipse.swt.widgets.Listener; 20 import org.eclipse.ui.forms.events.HyperlinkEvent; 21 import org.eclipse.ui.forms.events.IHyperlinkListener; 22 import org.eclipse.ui.forms.widgets.Hyperlink; 23 24 32 33 public final class HyperlinkGroup extends HyperlinkSettings { 34 private ArrayList links = new ArrayList (); 35 private Hyperlink lastActivated; 36 private Hyperlink lastEntered; 37 private GroupListener listener; 38 private boolean isActiveBackgroundSet; 39 private boolean isActiveForegroundSet; 40 private boolean isBackgroundSet; 41 private boolean isForegroundSet; 42 43 private class GroupListener implements Listener, IHyperlinkListener { 44 45 private Color previousBackground; 46 private Color previousForeground; 47 48 public void handleEvent(Event e) { 49 switch (e.type) { 50 case SWT.MouseEnter : 51 onMouseEnter(e); 52 break; 53 case SWT.MouseExit : 54 onMouseExit(e); 55 break; 56 case SWT.MouseDown : 57 onMouseDown(e); 58 break; 59 case SWT.Dispose : 60 unhook((Hyperlink) e.widget); 61 break; 62 } 63 } 64 private void onMouseEnter(Event e) { 65 Hyperlink link = (Hyperlink) e.widget; 66 previousBackground = link.getBackground(); 67 previousForeground = link.getForeground(); 68 if (isActiveBackgroundSet) 69 link.setBackground(getActiveBackground()); 70 if (isActiveForegroundSet) 71 link.setForeground(getActiveForeground()); 72 if (getHyperlinkUnderlineMode() == UNDERLINE_HOVER) 73 link.setUnderlined(true); 74 link.setCursor(getHyperlinkCursor()); 75 } 76 private void onMouseExit(Event e) { 77 Hyperlink link = (Hyperlink) e.widget; 78 if (isActiveBackgroundSet) 79 link.setBackground(previousBackground); 80 if (isActiveForegroundSet) 81 link.setForeground(previousForeground); 82 if (getHyperlinkUnderlineMode() == UNDERLINE_HOVER) 83 link.setUnderlined(false); 84 } 85 public void linkActivated(HyperlinkEvent e) { 86 } 87 88 public void linkEntered(HyperlinkEvent e) { 89 Hyperlink link = (Hyperlink) e.widget; 90 if (lastEntered != null) { 91 linkExited(lastEntered); 92 } 93 lastEntered = link; 94 } 95 96 public void linkExited(HyperlinkEvent e) { 97 linkExited((Hyperlink) e.widget); 98 } 99 private void linkExited(Hyperlink link) { 100 link.setCursor(null); 101 if (lastEntered == link) 102 lastEntered = null; 103 } 104 } 105 106 109 110 public HyperlinkGroup(Display display) { 111 super(display); 112 listener = new GroupListener(); 113 } 114 115 122 public Hyperlink getLastActivated() { 123 return lastActivated; 124 } 125 132 133 public void add(Hyperlink link) { 134 if (isBackgroundSet) 135 link.setBackground(getBackground()); 136 if (isForegroundSet) 137 link.setForeground(getForeground()); 138 if (getHyperlinkUnderlineMode() == UNDERLINE_ALWAYS) 139 link.setUnderlined(true); 140 hook(link); 141 } 142 143 149 public void setActiveBackground(Color newActiveBackground) { 150 super.setActiveBackground(newActiveBackground); 151 isActiveBackgroundSet = true; 152 } 153 154 160 public void setActiveForeground(Color newActiveForeground) { 161 super.setActiveForeground(newActiveForeground); 162 isActiveForegroundSet = true; 163 } 164 165 172 public void setBackground(Color bg) { 173 super.setBackground(bg); 174 isBackgroundSet = true; 175 if (links != null) { 176 for (int i = 0; i < links.size(); i++) { 177 Hyperlink label = (Hyperlink) links.get(i); 178 label.setBackground(bg); 179 } 180 } 181 } 182 189 public void setForeground(Color fg) { 190 super.setForeground(fg); 191 isForegroundSet = true; 192 if (links != null) { 193 for (int i = 0; i < links.size(); i++) { 194 Hyperlink label = (Hyperlink) links.get(i); 195 label.setForeground(fg); 196 } 197 } 198 } 199 206 public void setHyperlinkUnderlineMode(int mode) { 207 super.setHyperlinkUnderlineMode(mode); 208 if (links != null) { 209 for (int i = 0; i < links.size(); i++) { 210 Hyperlink label = (Hyperlink) links.get(i); 211 label.setUnderlined(mode == UNDERLINE_ALWAYS); 212 } 213 } 214 } 215 216 private void hook(Hyperlink link) { 217 link.addListener(SWT.MouseDown, listener); 218 link.addHyperlinkListener(listener); 219 link.addListener(SWT.Dispose, listener); 220 link.addListener(SWT.MouseEnter, listener); 221 link.addListener(SWT.MouseExit, listener); 222 links.add(link); 223 } 224 225 private void unhook(Hyperlink link) { 226 link.removeListener(SWT.MouseDown, listener); 227 link.removeHyperlinkListener(listener); 228 link.removeListener(SWT.MouseEnter, listener); 229 link.removeListener(SWT.MouseExit, listener); 230 if (lastActivated == link) 231 lastActivated = null; 232 if (lastEntered == link) 233 lastEntered = null; 234 links.remove(link); 235 } 236 237 private void onMouseDown(Event e) { 238 if (e.button == 1) 239 return; 240 lastActivated = (Hyperlink) e.widget; 241 } 242 } 243 | Popular Tags |