|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 EclipseSource and others. All rights reserved. |
| 3 |
* This program and the accompanying materials are made available under the |
| 4 |
* terms of the Eclipse Public License v1.0 which accompanies this distribution, |
| 5 |
* and is available at http://www.eclipse.org/legal/epl-v10.html |
| 6 |
* |
| 7 |
* Contributors: |
| 8 |
* EclipseSource - initial API and implementation |
| 9 |
******************************************************************************/ |
| 10 |
|
| 11 |
/** |
| 12 |
* This class provides the client-side implementation for |
| 13 |
* org.eclipse.swt.widgets.Link |
| 14 |
*/ |
| 15 |
qx.Class.define( "org.eclipse.swt.widgets.Link", { |
| 16 |
extend : qx.ui.layout.CanvasLayout, |
| 17 |
|
| 18 |
construct : function() { |
| 19 |
this.base( arguments ); |
| 20 |
this.setAppearance( "link" ); |
| 21 |
// Default values |
| 22 |
this._text = ""; |
| 23 |
this._hasSelectionListener = false; |
| 24 |
this._hyperlinksHaveListeners = false; |
| 25 |
this._linkColor; |
| 26 |
// innerTab handling |
| 27 |
this._currentLinkFocused = -1; |
| 28 |
this._linksCount = 0; |
| 29 |
// |
| 30 |
this._link = new qx.ui.embed.HtmlEmbed(); |
| 31 |
this._link.setAppearance( "link-text" ); |
| 32 |
this.add( this._link ); |
| 33 |
// |
| 34 |
this.setSelectable( false ); |
| 35 |
this.setHideFocus( true ); |
| 36 |
// |
| 37 |
this.__onMouseDown = qx.lang.Function.bindEvent( this._onMouseDown, this ); |
| 38 |
this.__onKeyDown = qx.lang.Function.bindEvent( this._onKeyDown, this ); |
| 39 |
// |
| 40 |
this.addEventListener( "appear", this._onAppear, this ); |
| 41 |
this.addEventListener( "changeEnabled", this._onChangeEnabled, this ); |
| 42 |
this.addEventListener( "contextmenu", this._onContextMenu, this ); |
| 43 |
// Event listener used for inner TabIndex change |
| 44 |
this.addEventListener( "keypress", this._onKeyPress ); |
| 45 |
this.addEventListener( "focusout", this._onFocusOut ); |
| 46 |
// |
| 47 |
this._link.addEventListener( "changeHtml", this._onChangeHtml, this ); |
| 48 |
}, |
| 49 |
|
| 50 |
destruct : function() { |
| 51 |
this._removeEventListeners(); |
| 52 |
delete this.__onMouseDown; |
| 53 |
delete this.__onKeyDown; |
| 54 |
this.removeEventListener( "appear", this._onAppear, this ); |
| 55 |
this.removeEventListener( "contextmenu", this._onContextMenu, this ); |
| 56 |
this.removeEventListener( "changeEnabled", this._onChangeEnabled, this ); |
| 57 |
this.removeEventListener( "keypress", this._onKeyPress ); |
| 58 |
this.removeEventListener( "focusout", this._onFocusOut ); |
| 59 |
this._link.removeEventListener( "changeHtml", this._onChangeHtml, this ); |
| 60 |
this._link.dispose(); |
| 61 |
}, |
| 62 |
|
| 63 |
members : { |
| 64 |
_onContextMenu : function( evt ) { |
| 65 |
var menu = this.getContextMenu(); |
| 66 |
if( menu != null ) { |
| 67 |
menu.setLocation( evt.getPageX(), evt.getPageY() ); |
| 68 |
menu.setOpener( this ); |
| 69 |
menu.show(); |
| 70 |
evt.stopPropagation(); |
| 71 |
} |
| 72 |
}, |
| 73 |
|
| 74 |
_onAppear : function( evt ) { |
| 75 |
this._link.setTabIndex( -1 ); |
| 76 |
this._link.setHideFocus( true ); |
| 77 |
this._applyHyperlinksStyleProperties(); |
| 78 |
this._addEventListeners(); |
| 79 |
}, |
| 80 |
|
| 81 |
_onChangeHtml : function( evt ) { |
| 82 |
this._applyHyperlinksStyleProperties(); |
| 83 |
this._addEventListeners(); |
| 84 |
}, |
| 85 |
|
| 86 |
_applyTextColor : function( value, old ) { |
| 87 |
this.base( arguments, value, old ); |
| 88 |
var themeValues |
| 89 |
= new org.eclipse.swt.theme.ThemeValues( this._getStates() ); |
| 90 |
this._linkColor = themeValues.getCssColor( "Link-Hyperlink", "color" ); |
| 91 |
themeValues.dispose(); |
| 92 |
this._applyHyperlinksStyleProperties(); |
| 93 |
}, |
| 94 |
|
| 95 |
_onChangeEnabled : function( evt ) { |
| 96 |
this._applyHyperlinksStyleProperties(); |
| 97 |
this._changeHyperlinksTabIndexProperty(); |
| 98 |
}, |
| 99 |
|
| 100 |
_getStates : function() { |
| 101 |
if( !this.__states ) { |
| 102 |
this.__states = {}; |
| 103 |
} |
| 104 |
return this.__states; |
| 105 |
}, |
| 106 |
|
| 107 |
addState : function( state ) { |
| 108 |
this.base( arguments, state ); |
| 109 |
this._link.addState( state ); |
| 110 |
}, |
| 111 |
|
| 112 |
removeState : function( state ) { |
| 113 |
this.base( arguments, state ); |
| 114 |
this._link.removeState( state ); |
| 115 |
}, |
| 116 |
|
| 117 |
setHasSelectionListener : function( value ) { |
| 118 |
this._hasSelectionListener = value; |
| 119 |
}, |
| 120 |
|
| 121 |
addText : function( text ) { |
| 122 |
this._text += text; |
| 123 |
}, |
| 124 |
|
| 125 |
addLink : function( text, index ) { |
| 126 |
this._text += "<span tabIndex=\"1\" "; |
| 127 |
this._text += "style=\""; |
| 128 |
this._text += "text-decoration:underline; "; |
| 129 |
this._text += "\" "; |
| 130 |
this._text += "id=\"" + index + "\""; |
| 131 |
this._text += ">"; |
| 132 |
this._text += text; |
| 133 |
this._text += "</span>"; |
| 134 |
this._linksCount++; |
| 135 |
}, |
| 136 |
|
| 137 |
applyText : function() { |
| 138 |
this._link.setHtml( this._text ); |
| 139 |
if ( this._linksCount == 0 ) { |
| 140 |
this.setTabIndex( -1 ); |
| 141 |
} else { |
| 142 |
this.setTabIndex( 1 ); |
| 143 |
} |
| 144 |
}, |
| 145 |
|
| 146 |
clear : function() { |
| 147 |
this._removeEventListeners(); |
| 148 |
this._text = ""; |
| 149 |
this._linksCount = 0; |
| 150 |
}, |
| 151 |
|
| 152 |
_applyHyperlinksStyleProperties : function() { |
| 153 |
var linkElement = this.getElement(); |
| 154 |
if( linkElement ) { |
| 155 |
var hyperlinks = linkElement.getElementsByTagName( "span" ); |
| 156 |
for( i = 0; i < hyperlinks.length; i++ ) { |
| 157 |
if( this._linkColor ) { |
| 158 |
if( this.isEnabled() ) { |
| 159 |
hyperlinks[ i ].style.color = this._linkColor; |
| 160 |
} else { |
| 161 |
hyperlinks[ i ].style.color = ""; |
| 162 |
} |
| 163 |
} |
| 164 |
if( this.isEnabled() ) { |
| 165 |
hyperlinks[ i ].style.cursor = "pointer"; |
| 166 |
} else { |
| 167 |
hyperlinks[ i ].style.cursor = "default"; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
}, |
| 172 |
|
| 173 |
_changeHyperlinksTabIndexProperty : function() { |
| 174 |
var linkElement = this.getElement(); |
| 175 |
if( linkElement ) { |
| 176 |
var hyperlinks = linkElement.getElementsByTagName( "span" ); |
| 177 |
for( i = 0; i < hyperlinks.length; i++ ) { |
| 178 |
if( this.isEnabled() ) { |
| 179 |
hyperlinks[ i ].tabIndex = "1"; |
| 180 |
} else { |
| 181 |
hyperlinks[ i ].tabIndex = "-1"; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
}, |
| 186 |
|
| 187 |
_addEventListeners : function() { |
| 188 |
var linkElement = this.getElement(); |
| 189 |
if( linkElement && !this._hyperlinksHaveListeners ) { |
| 190 |
var hyperlinks = linkElement.getElementsByTagName( "span" ); |
| 191 |
for( i = 0; i < hyperlinks.length; i++ ) { |
| 192 |
qx.html.EventRegistration.addEventListener( hyperlinks[ i ], |
| 193 |
"mousedown", |
| 194 |
this.__onMouseDown ); |
| 195 |
qx.html.EventRegistration.addEventListener( hyperlinks[ i ], |
| 196 |
"keydown", |
| 197 |
this.__onKeyDown ); |
| 198 |
} |
| 199 |
this._hyperlinksHaveListeners = true; |
| 200 |
} |
| 201 |
}, |
| 202 |
|
| 203 |
_removeEventListeners : function() { |
| 204 |
var linkElement = this.getElement(); |
| 205 |
if( linkElement && this._hyperlinksHaveListeners ) { |
| 206 |
var hyperlinks = linkElement.getElementsByTagName( "span" ); |
| 207 |
for( i = 0; i < hyperlinks.length; i++ ) { |
| 208 |
qx.html.EventRegistration.removeEventListener( hyperlinks[ i ], |
| 209 |
"mousedown", |
| 210 |
this.__onMouseDown ); |
| 211 |
qx.html.EventRegistration.removeEventListener( hyperlinks[ i ], |
| 212 |
"keydown", |
| 213 |
this.__onKeyDown ); |
| 214 |
} |
| 215 |
this._hyperlinksHaveListeners = false; |
| 216 |
} |
| 217 |
}, |
| 218 |
|
| 219 |
_onMouseDown : function( e ) { |
| 220 |
var target = this._getEventTarget( e ); |
| 221 |
var index = target.id; |
| 222 |
this._currentLinkFocused = index; |
| 223 |
target.focus(); |
| 224 |
var leftBtnPressed = this._isLeftMouseButtonPressed( e ); |
| 225 |
if( this.isEnabled() && leftBtnPressed ) { |
| 226 |
this._sendChanges( index ); |
| 227 |
} |
| 228 |
}, |
| 229 |
|
| 230 |
_isLeftMouseButtonPressed : function( e ) { |
| 231 |
var leftBtnPressed; |
| 232 |
if( e.which ) { |
| 233 |
leftBtnPressed = ( e.which == 1 ); |
| 234 |
} else if ( e.button ) { |
| 235 |
if( qx.core.Variant.isSet( "qx.client", "mshtml" ) ) { |
| 236 |
leftBtnPressed = ( e.button == 1 ); |
| 237 |
} else { |
| 238 |
leftBtnPressed = ( e.button == 0 ); |
| 239 |
} |
| 240 |
} |
| 241 |
return leftBtnPressed; |
| 242 |
}, |
| 243 |
|
| 244 |
_onKeyDown : function( e ) { |
| 245 |
if( this.isEnabled() && e.keyCode == 13 ) { |
| 246 |
var target = this._getEventTarget( e ); |
| 247 |
var index = target.id; |
| 248 |
this._sendChanges( index ); |
| 249 |
} |
| 250 |
}, |
| 251 |
|
| 252 |
_getEventTarget : function( e ) { |
| 253 |
var target; |
| 254 |
if( qx.core.Variant.isSet( "qx.client", "mshtml" ) ) { |
| 255 |
target = window.event.srcElement; |
| 256 |
} else { |
| 257 |
target = e.target; |
| 258 |
} |
| 259 |
return target; |
| 260 |
}, |
| 261 |
|
| 262 |
// Override of the _ontabfocus method from qx.ui.core.Widget |
| 263 |
_ontabfocus : function() { |
| 264 |
if( this._currentLinkFocused == -1 && this._linksCount > 0 ) { |
| 265 |
var linkElement = this.getElement(); |
| 266 |
if( linkElement ) { |
| 267 |
var hyperlinks = linkElement.getElementsByTagName( "span" ); |
| 268 |
hyperlinks[ 0 ].focus(); |
| 269 |
this._currentLinkFocused = 0; |
| 270 |
} |
| 271 |
} |
| 272 |
}, |
| 273 |
|
| 274 |
_onKeyPress : function( evt ) { |
| 275 |
if( this.isFocused() |
| 276 |
&& evt.getKeyIdentifier() == "Tab" |
| 277 |
&& this._linksCount > 0 ) |
| 278 |
{ |
| 279 |
if( !evt.isShiftPressed() |
| 280 |
&& this._currentLinkFocused >= 0 |
| 281 |
&& this._currentLinkFocused < ( this._linksCount - 1 ) ) |
| 282 |
{ |
| 283 |
evt.stopPropagation(); |
| 284 |
evt.preventDefault(); |
| 285 |
this._currentLinkFocused++; |
| 286 |
this._focusLinkByID( this._currentLinkFocused ); |
| 287 |
} else if( !evt.isShiftPressed() |
| 288 |
&& this._currentLinkFocused == -1 ) |
| 289 |
{ |
| 290 |
evt.stopPropagation(); |
| 291 |
evt.preventDefault(); |
| 292 |
var linkElement = this.getElement(); |
| 293 |
if( linkElement ) { |
| 294 |
var hyperlinks = linkElement.getElementsByTagName( "span" ); |
| 295 |
hyperlinks[ 0 ].focus(); |
| 296 |
this._currentLinkFocused = 0; |
| 297 |
} |
| 298 |
} else if( evt.isShiftPressed() |
| 299 |
&& this._currentLinkFocused > 0 |
| 300 |
&& this._currentLinkFocused <= ( this._linksCount - 1 ) ) |
| 301 |
{ |
| 302 |
evt.stopPropagation(); |
| 303 |
evt.preventDefault(); |
| 304 |
this._currentLinkFocused--; |
| 305 |
this._focusLinkByID( this._currentLinkFocused ); |
| 306 |
} |
| 307 |
} |
| 308 |
}, |
| 309 |
|
| 310 |
_focusLinkByID : function( id ) { |
| 311 |
var linkElement = this.getElement(); |
| 312 |
if( linkElement ) { |
| 313 |
var hyperlinks = linkElement.getElementsByTagName( "span" ); |
| 314 |
hyperlinks[ id ].focus(); |
| 315 |
} |
| 316 |
}, |
| 317 |
|
| 318 |
_onFocusOut : function( evt ) { |
| 319 |
var linkElement = this.getElement(); |
| 320 |
if( linkElement ) { |
| 321 |
var hyperlinks = linkElement.getElementsByTagName( "span" ); |
| 322 |
if( this._currentLinkFocused >= 0 ) { |
| 323 |
hyperlinks[ this._currentLinkFocused ].blur(); |
| 324 |
} |
| 325 |
} |
| 326 |
this._currentLinkFocused = -1; |
| 327 |
}, |
| 328 |
|
| 329 |
_sendChanges : function( index ) { |
| 330 |
if( !org_eclipse_rap_rwt_EventUtil_suspend ) { |
| 331 |
var widgetManager = org.eclipse.swt.WidgetManager.getInstance(); |
| 332 |
var id = widgetManager.findIdByWidget( this ); |
| 333 |
var req = org.eclipse.swt.Request.getInstance(); |
| 334 |
if( this._hasSelectionListener ) { |
| 335 |
req.addEvent( "org.eclipse.swt.events.widgetSelected", id ); |
| 336 |
req.addEvent( "org.eclipse.swt.events.widgetSelected.index", index ); |
| 337 |
req.send(); |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
} ); |