1 11 package org.eclipse.ui.forms.widgets; 12 import org.eclipse.swt.SWT; 13 import org.eclipse.swt.accessibility.*; 14 import org.eclipse.swt.graphics.*; 15 import org.eclipse.swt.widgets.*; 16 import org.eclipse.ui.forms.events.*; 17 import org.eclipse.ui.internal.forms.Messages; 18 28 public abstract class ToggleHyperlink extends AbstractHyperlink { 29 protected int innerWidth; 30 protected int innerHeight; 31 protected boolean hover; 32 private boolean expanded; 33 private Color decorationColor; 34 private Color hoverColor; 35 43 public ToggleHyperlink(Composite parent, int style) { 44 super(parent, style); 45 Listener listener = new Listener() { 46 public void handleEvent(Event e) { 47 switch (e.type) { 48 case SWT.MouseEnter: 49 hover=true; 50 redraw(); 51 break; 52 case SWT.MouseExit: 53 hover = false; 54 redraw(); 55 break; 56 case SWT.KeyDown: 57 onKeyDown(e); 58 break; 59 } 60 } 61 }; 62 addListener(SWT.MouseEnter, listener); 63 addListener(SWT.MouseExit, listener); 64 addListener(SWT.KeyDown, listener); 65 addHyperlinkListener(new HyperlinkAdapter() { 66 public void linkActivated(HyperlinkEvent e) { 67 setExpanded(!isExpanded()); 68 } 69 }); 70 initAccessible(); 71 } 72 77 public void setDecorationColor(Color decorationColor) { 78 this.decorationColor = decorationColor; 79 } 80 85 public Color getDecorationColor() { 86 return decorationColor; 87 } 88 95 public void setHoverDecorationColor(Color hoverColor) { 96 this.hoverColor = hoverColor; 97 } 98 104 public Color getHoverDecorationColor() { 105 return hoverColor; 106 } 107 108 115 public Color geHoverDecorationColor() { 116 return hoverColor; 117 } 118 128 public Point computeSize(int wHint, int hHint, boolean changed) { 129 int width, height; 130 134 width = innerWidth + 2 * marginWidth; 135 139 height = innerHeight + 2 * marginHeight; 140 return new Point(width, height); 141 } 142 149 public boolean isExpanded() { 150 return expanded; 151 } 152 157 public void setExpanded(boolean expanded) { 158 this.expanded = expanded; 159 getAccessible().selectionChanged(); 160 redraw(); 161 } 162 private void initAccessible() { 163 getAccessible().addAccessibleListener(new AccessibleAdapter() { 164 public void getHelp(AccessibleEvent e) { 165 e.result = getToolTipText(); 166 } 167 public void getName(AccessibleEvent e) { 168 String name=Messages.ToggleHyperlink_accessibleName; 169 if (getParent() instanceof ExpandableComposite) { 170 name += Messages.ToggleHyperlink_accessibleColumn+((ExpandableComposite)getParent()).getText(); 171 int index = name.indexOf('&'); 172 if (index != -1) { 173 name = name.substring(0, index) + name.substring(index + 1); 174 } 175 } 176 e.result = name; 177 } 178 public void getDescription(AccessibleEvent e) { 179 getName(e); 180 } 181 }); 182 getAccessible().addAccessibleControlListener( 183 new AccessibleControlAdapter() { 184 public void getChildAtPoint(AccessibleControlEvent e) { 185 Point testPoint = toControl(new Point(e.x, e.y)); 186 if (getBounds().contains(testPoint)) { 187 e.childID = ACC.CHILDID_SELF; 188 } 189 } 190 public void getLocation(AccessibleControlEvent e) { 191 Rectangle location = getBounds(); 192 Point pt = toDisplay(new Point(location.x, location.y)); 193 e.x = pt.x; 194 e.y = pt.y; 195 e.width = location.width; 196 e.height = location.height; 197 } 198 public void getSelection (AccessibleControlEvent e) { 199 if (ToggleHyperlink.this.getSelection()) 200 e.childID = ACC.CHILDID_SELF; 201 } 202 203 public void getFocus (AccessibleControlEvent e) { 204 if (ToggleHyperlink.this.getSelection()) 205 e.childID = ACC.CHILDID_SELF; 206 } 207 public void getChildCount(AccessibleControlEvent e) { 208 e.detail = 0; 209 } 210 public void getRole(AccessibleControlEvent e) { 211 e.detail = ACC.ROLE_TREE; 212 } 213 public void getState(AccessibleControlEvent e) { 214 int state = ACC.STATE_FOCUSABLE; 215 if (ToggleHyperlink.this.getSelection()) 216 state |= ACC.STATE_FOCUSED; 217 state |= ToggleHyperlink.this.isExpanded() 218 ? ACC.STATE_EXPANDED 219 : ACC.STATE_COLLAPSED; 220 e.detail = state; 221 } 222 }); 223 } 224 private void onKeyDown(Event e) { 225 if (e.keyCode==SWT.ARROW_RIGHT) { 226 if (!isExpanded()) { 228 handleActivate(e); 229 } 230 e.doit=false; 231 } 232 else if (e.keyCode==SWT.ARROW_LEFT) { 233 if (isExpanded()) { 235 handleActivate(e); 236 } 237 e.doit=false; 238 } 239 } 240 } 241 | Popular Tags |