1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 import sun.awt.*; 29 import com.sun.star.awt.*; 30 import com.sun.star.lang.*; 31 import java.util.*; 32 import javax.swing.*; 33 34 public class WindowAdapter 35 { 36 private java.awt.Frame maFrame; 37 private LinkedList maEventListeners = new LinkedList(); 38 private LinkedList maWindowListeners = new LinkedList(); 39 private LinkedList maFocusListeners = new LinkedList(); 40 private LinkedList maKeyListeners = new LinkedList(); 41 private LinkedList maMouseListeners = new LinkedList(); 42 private LinkedList maMouseMotionListeners = new LinkedList(); 43 private LinkedList maPaintListeners = new LinkedList(); 44 private boolean mbShift = false, mbMod1 = false, mbMod2 = false; 45 46 // ----------------- 47 // - WindowAdapter - 48 // ----------------- 49 50 public WindowAdapter( int windowHandle ) 51 { 52 maFrame = SystemWindowAdapter.createFrame( windowHandle ); 53 } 54 55 //---------------------------------------------------------------------------------- 56 57 public java.awt.Frame getJavaFrame() 58 { 59 return maFrame; 60 } 61 62 //---------------------------------------------------------------------------------- 63 64 private short implGetUNOKeyCode( int nJavaKeyCode ) 65 { 66 short nRet = 0; 67 68 switch( nJavaKeyCode ) 69 { 70 case( java.awt.event.KeyEvent.VK_NUMPAD0 ): 71 case( java.awt.event.KeyEvent.VK_0 ): nRet = com.sun.star.awt.Key.NUM0; break; 72 case( java.awt.event.KeyEvent.VK_NUMPAD1 ): 73 case( java.awt.event.KeyEvent.VK_1 ): nRet = com.sun.star.awt.Key.NUM1; break; 74 case( java.awt.event.KeyEvent.VK_NUMPAD2 ): 75 case( java.awt.event.KeyEvent.VK_2 ): nRet = com.sun.star.awt.Key.NUM2; break; 76 case( java.awt.event.KeyEvent.VK_NUMPAD3 ): 77 case( java.awt.event.KeyEvent.VK_3 ): nRet = com.sun.star.awt.Key.NUM3; break; 78 case( java.awt.event.KeyEvent.VK_NUMPAD4 ): 79 case( java.awt.event.KeyEvent.VK_4 ): nRet = com.sun.star.awt.Key.NUM4; break; 80 case( java.awt.event.KeyEvent.VK_NUMPAD5 ): 81 case( java.awt.event.KeyEvent.VK_5 ): nRet = com.sun.star.awt.Key.NUM5; break; 82 case( java.awt.event.KeyEvent.VK_NUMPAD6 ): 83 case( java.awt.event.KeyEvent.VK_6 ): nRet = com.sun.star.awt.Key.NUM6; break; 84 case( java.awt.event.KeyEvent.VK_NUMPAD7 ): 85 case( java.awt.event.KeyEvent.VK_7 ): nRet = com.sun.star.awt.Key.NUM7; break; 86 case( java.awt.event.KeyEvent.VK_NUMPAD8 ): 87 case( java.awt.event.KeyEvent.VK_8 ): nRet = com.sun.star.awt.Key.NUM8; break; 88 case( java.awt.event.KeyEvent.VK_NUMPAD9 ): 89 case( java.awt.event.KeyEvent.VK_9 ): nRet = com.sun.star.awt.Key.NUM9; break; 90 91 case( java.awt.event.KeyEvent.VK_A ): nRet = com.sun.star.awt.Key.A; break; 92 case( java.awt.event.KeyEvent.VK_B ): nRet = com.sun.star.awt.Key.B; break; 93 case( java.awt.event.KeyEvent.VK_C ): nRet = com.sun.star.awt.Key.C; break; 94 case( java.awt.event.KeyEvent.VK_D ): nRet = com.sun.star.awt.Key.D; break; 95 case( java.awt.event.KeyEvent.VK_E ): nRet = com.sun.star.awt.Key.E; break; 96 case( java.awt.event.KeyEvent.VK_F ): nRet = com.sun.star.awt.Key.F; break; 97 case( java.awt.event.KeyEvent.VK_G ): nRet = com.sun.star.awt.Key.G; break; 98 case( java.awt.event.KeyEvent.VK_H ): nRet = com.sun.star.awt.Key.H; break; 99 case( java.awt.event.KeyEvent.VK_I ): nRet = com.sun.star.awt.Key.I; break; 100 case( java.awt.event.KeyEvent.VK_J ): nRet = com.sun.star.awt.Key.J; break; 101 case( java.awt.event.KeyEvent.VK_K ): nRet = com.sun.star.awt.Key.K; break; 102 case( java.awt.event.KeyEvent.VK_L ): nRet = com.sun.star.awt.Key.L; break; 103 case( java.awt.event.KeyEvent.VK_M ): nRet = com.sun.star.awt.Key.M; break; 104 case( java.awt.event.KeyEvent.VK_N ): nRet = com.sun.star.awt.Key.N; break; 105 case( java.awt.event.KeyEvent.VK_O ): nRet = com.sun.star.awt.Key.O; break; 106 case( java.awt.event.KeyEvent.VK_P ): nRet = com.sun.star.awt.Key.P; break; 107 case( java.awt.event.KeyEvent.VK_Q ): nRet = com.sun.star.awt.Key.Q; break; 108 case( java.awt.event.KeyEvent.VK_R ): nRet = com.sun.star.awt.Key.R; break; 109 case( java.awt.event.KeyEvent.VK_S ): nRet = com.sun.star.awt.Key.S; break; 110 case( java.awt.event.KeyEvent.VK_T ): nRet = com.sun.star.awt.Key.T; break; 111 case( java.awt.event.KeyEvent.VK_U ): nRet = com.sun.star.awt.Key.U; break; 112 case( java.awt.event.KeyEvent.VK_V ): nRet = com.sun.star.awt.Key.V; break; 113 case( java.awt.event.KeyEvent.VK_W ): nRet = com.sun.star.awt.Key.W; break; 114 case( java.awt.event.KeyEvent.VK_X ): nRet = com.sun.star.awt.Key.X; break; 115 case( java.awt.event.KeyEvent.VK_Y ): nRet = com.sun.star.awt.Key.Y; break; 116 case( java.awt.event.KeyEvent.VK_Z ): nRet = com.sun.star.awt.Key.Z; break; 117 118 case( java.awt.event.KeyEvent.VK_F1 ): nRet = com.sun.star.awt.Key.F1; break; 119 case( java.awt.event.KeyEvent.VK_F2 ): nRet = com.sun.star.awt.Key.F2; break; 120 case( java.awt.event.KeyEvent.VK_F3 ): nRet = com.sun.star.awt.Key.F3; break; 121 case( java.awt.event.KeyEvent.VK_F4 ): nRet = com.sun.star.awt.Key.F4; break; 122 case( java.awt.event.KeyEvent.VK_F5 ): nRet = com.sun.star.awt.Key.F5; break; 123 case( java.awt.event.KeyEvent.VK_F6 ): nRet = com.sun.star.awt.Key.F6; break; 124 case( java.awt.event.KeyEvent.VK_F7 ): nRet = com.sun.star.awt.Key.F7; break; 125 case( java.awt.event.KeyEvent.VK_F8 ): nRet = com.sun.star.awt.Key.F8; break; 126 case( java.awt.event.KeyEvent.VK_F9 ): nRet = com.sun.star.awt.Key.F9; break; 127 case( java.awt.event.KeyEvent.VK_F10 ): nRet = com.sun.star.awt.Key.F10; break; 128 case( java.awt.event.KeyEvent.VK_F11 ): nRet = com.sun.star.awt.Key.F11; break; 129 case( java.awt.event.KeyEvent.VK_F12 ): nRet = com.sun.star.awt.Key.F12; break; 130 case( java.awt.event.KeyEvent.VK_F13 ): nRet = com.sun.star.awt.Key.F13; break; 131 case( java.awt.event.KeyEvent.VK_F14 ): nRet = com.sun.star.awt.Key.F14; break; 132 case( java.awt.event.KeyEvent.VK_F15 ): nRet = com.sun.star.awt.Key.F15; break; 133 case( java.awt.event.KeyEvent.VK_F16 ): nRet = com.sun.star.awt.Key.F16; break; 134 case( java.awt.event.KeyEvent.VK_F17 ): nRet = com.sun.star.awt.Key.F17; break; 135 case( java.awt.event.KeyEvent.VK_F18 ): nRet = com.sun.star.awt.Key.F18; break; 136 case( java.awt.event.KeyEvent.VK_F19 ): nRet = com.sun.star.awt.Key.F19; break; 137 case( java.awt.event.KeyEvent.VK_F20 ): nRet = com.sun.star.awt.Key.F20; break; 138 case( java.awt.event.KeyEvent.VK_F21 ): nRet = com.sun.star.awt.Key.F21; break; 139 case( java.awt.event.KeyEvent.VK_F22 ): nRet = com.sun.star.awt.Key.F22; break; 140 case( java.awt.event.KeyEvent.VK_F23 ): nRet = com.sun.star.awt.Key.F23; break; 141 case( java.awt.event.KeyEvent.VK_F24 ): nRet = com.sun.star.awt.Key.F24; break; 142 143 case( java.awt.event.KeyEvent.VK_UP ): nRet = com.sun.star.awt.Key.UP; break; 144 case( java.awt.event.KeyEvent.VK_DOWN): nRet = com.sun.star.awt.Key.DOWN; break; 145 case( java.awt.event.KeyEvent.VK_LEFT ): nRet = com.sun.star.awt.Key.LEFT; break; 146 case( java.awt.event.KeyEvent.VK_RIGHT ): nRet = com.sun.star.awt.Key.RIGHT; break; 147 148 case( java.awt.event.KeyEvent.VK_HOME ): nRet = com.sun.star.awt.Key.HOME; break; 149 case( java.awt.event.KeyEvent.VK_END ): nRet = com.sun.star.awt.Key.END; break; 150 151 case( java.awt.event.KeyEvent.VK_PAGE_UP ): nRet = com.sun.star.awt.Key.PAGEUP; break; 152 case( java.awt.event.KeyEvent.VK_PAGE_DOWN ): nRet = com.sun.star.awt.Key.PAGEDOWN; break; 153 154 case( java.awt.event.KeyEvent.VK_ENTER ): nRet = com.sun.star.awt.Key.RETURN; break; 155 case( java.awt.event.KeyEvent.VK_ESCAPE ): nRet = com.sun.star.awt.Key.ESCAPE; break; 156 157 case( java.awt.event.KeyEvent.VK_TAB ): nRet = com.sun.star.awt.Key.TAB; break; 158 case( java.awt.event.KeyEvent.VK_BACK_SPACE ): nRet = com.sun.star.awt.Key.BACKSPACE; break; 159 case( java.awt.event.KeyEvent.VK_SPACE ): nRet = com.sun.star.awt.Key.SPACE; break; 160 case( java.awt.event.KeyEvent.VK_INSERT): nRet = com.sun.star.awt.Key.INSERT; break; 161 case( java.awt.event.KeyEvent.VK_DELETE): nRet = com.sun.star.awt.Key.DELETE; break; 162 case( java.awt.event.KeyEvent.VK_ADD ): nRet = com.sun.star.awt.Key.ADD; break; 163 case( java.awt.event.KeyEvent.VK_SUBTRACT ): nRet = com.sun.star.awt.Key.SUBTRACT; break; 164 case( java.awt.event.KeyEvent.VK_MULTIPLY ): nRet = com.sun.star.awt.Key.MULTIPLY; break; 165 case( java.awt.event.KeyEvent.VK_DIVIDE ): nRet = com.sun.star.awt.Key.DIVIDE; break; 166 case( java.awt.event.KeyEvent.VK_DECIMAL ): nRet = com.sun.star.awt.Key.POINT; break; 167 // case( java.awt.event.KeyEvent.VK_ COMMA; break; 168 case( java.awt.event.KeyEvent.VK_LESS ): nRet = com.sun.star.awt.Key.LESS; break; 169 case( java.awt.event.KeyEvent.VK_GREATER ): nRet = com.sun.star.awt.Key.GREATER; break; 170 case( java.awt.event.KeyEvent.VK_EQUALS ): nRet = com.sun.star.awt.Key.EQUAL; break; 171 // case( java.awt.event.KeyEvent.VK_ OPEN; break; 172 // case( java.awt.event.KeyEvent.VK_ CUT; break; 173 // case( java.awt.event.KeyEvent.VK_ COPY; break; 174 // case( java.awt.event.KeyEvent.VK_ PASTE; break; 175 // case( java.awt.event.KeyEvent.VK_ UNDO; break; 176 // case( java.awt.event.KeyEvent.VK_ REPEAT; break; 177 // case( java.awt.event.KeyEvent.VK_ FIND; break; 178 // case( java.awt.event.KeyEvent.VK_ PROPERTIES; break; 179 // case( java.awt.event.KeyEvent.VK_ FRONT; break; 180 // case( java.awt.event.KeyEvent.VK_ CONTEXTMENU; break; 181 // case( java.awt.event.KeyEvent.VK_ HELP; break; 182 183 default: 184 break; 185 } 186 187 return nRet; 188 } 189 190 //---------------------------------------------------------------------------------- 191 192 public void fireKeyEvent( java.awt.event.KeyEvent aEvt ) 193 { 194 if( maKeyListeners.size() > 0 ) 195 { 196 boolean bProcess = false, bPressed = false; 197 198 if( java.awt.event.KeyEvent.KEY_PRESSED == aEvt.getID() ) 199 { 200 switch( aEvt.getKeyCode() ) 201 { 202 case( java.awt.event.KeyEvent.VK_SHIFT ): mbShift = true; break; 203 case( java.awt.event.KeyEvent.VK_CONTROL ): mbMod1 = true; break; 204 case( java.awt.event.KeyEvent.VK_ALT ): mbMod2 = true; break; 205 206 default: 207 { 208 bProcess = bPressed = true; 209 } 210 break; 211 } 212 } 213 else if( java.awt.event.KeyEvent.KEY_RELEASED == aEvt.getID() ) 214 { 215 switch( aEvt.getKeyCode() ) 216 { 217 case( java.awt.event.KeyEvent.VK_SHIFT ): mbShift = false; break; 218 case( java.awt.event.KeyEvent.VK_CONTROL ): mbMod1 = false; break; 219 case( java.awt.event.KeyEvent.VK_ALT ): mbMod2 = false; break; 220 221 default: 222 { 223 bProcess = true; 224 } 225 break; 226 } 227 } 228 229 if( bProcess ) 230 { 231 KeyEvent aUNOEvt = new KeyEvent(); 232 233 aUNOEvt.Modifiers = 0; 234 235 if( mbShift ) 236 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.SHIFT; 237 238 if( mbMod1 ) 239 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.MOD1; 240 241 if( mbMod2 ) 242 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.MOD2; 243 244 aUNOEvt.KeyCode = implGetUNOKeyCode( aEvt.getKeyCode() ); 245 aUNOEvt.KeyChar = aEvt.getKeyChar(); 246 aUNOEvt.KeyFunc = com.sun.star.awt.KeyFunction.DONTKNOW; 247 248 ListIterator aIter = maKeyListeners.listIterator( 0 ); 249 250 while( aIter.hasNext() ) 251 { 252 if( bPressed ) 253 ( (XKeyListener) aIter.next() ).keyPressed( aUNOEvt ); 254 else 255 ( (XKeyListener) aIter.next() ).keyReleased( aUNOEvt ); 256 } 257 } 258 } 259 } 260 261 //---------------------------------------------------------------------------------- 262 263 public void fireMouseEvent( java.awt.event.MouseEvent aEvt ) 264 { 265 MouseEvent aUNOEvt = new MouseEvent(); 266 267 aUNOEvt.Modifiers = 0; 268 aUNOEvt.Buttons = 0; 269 aUNOEvt.X = aEvt.getX(); 270 aUNOEvt.Y = aEvt.getY(); 271 aUNOEvt.PopupTrigger = false; 272 273 // Modifiers 274 if( aEvt.isShiftDown() ) 275 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.SHIFT; 276 277 if( aEvt.isControlDown() ) 278 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.MOD1; 279 280 if( aEvt.isAltDown() ) 281 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.MOD2; 282 283 // Buttons 284 if( SwingUtilities.isLeftMouseButton( aEvt ) ) 285 aUNOEvt.Buttons |= com.sun.star.awt.MouseButton.LEFT; 286 287 if( SwingUtilities.isMiddleMouseButton( aEvt ) ) 288 aUNOEvt.Buttons |= com.sun.star.awt.MouseButton.MIDDLE; 289 290 if( SwingUtilities.isRightMouseButton( aEvt ) ) 291 aUNOEvt.Buttons |= com.sun.star.awt.MouseButton.RIGHT; 292 293 // event type 294 if( java.awt.event.MouseEvent.MOUSE_PRESSED == aEvt.getID() ) 295 { 296 ListIterator aIter = maMouseListeners.listIterator( 0 ); 297 298 aUNOEvt.ClickCount = 1; 299 300 while( aIter.hasNext() ) 301 ( (XMouseListener) aIter.next() ).mousePressed( aUNOEvt ); 302 } 303 else if( java.awt.event.MouseEvent.MOUSE_RELEASED == aEvt.getID() ) 304 { 305 ListIterator aIter = maMouseListeners.listIterator( 0 ); 306 307 aUNOEvt.ClickCount = 1; 308 309 while( aIter.hasNext() ) 310 ( (XMouseListener) aIter.next() ).mouseReleased( aUNOEvt ); 311 } 312 else if( java.awt.event.MouseEvent.MOUSE_DRAGGED == aEvt.getID() ) 313 { 314 ListIterator aIter = maMouseMotionListeners.listIterator( 0 ); 315 316 aUNOEvt.ClickCount = 0; 317 318 while( aIter.hasNext() ) 319 ( (XMouseMotionListener) aIter.next() ).mouseDragged( aUNOEvt ); 320 } 321 else if( java.awt.event.MouseEvent.MOUSE_MOVED == aEvt.getID() ) 322 { 323 ListIterator aIter = maMouseMotionListeners.listIterator( 0 ); 324 325 aUNOEvt.ClickCount = 0; 326 327 while( aIter.hasNext() ) 328 ( (XMouseMotionListener) aIter.next() ).mouseMoved( aUNOEvt ); 329 } 330 } 331 332 //---------------------------------------------------------------------------------- 333 334 public void fireFocusEvent( java.awt.event.FocusEvent aEvt ) 335 { 336 if( java.awt.event.FocusEvent.FOCUS_GAINED == aEvt.getID() ) 337 { 338 ListIterator aIter = maFocusListeners.listIterator( 0 ); 339 FocusEvent aUNOEvt = new FocusEvent(); 340 341 while( aIter.hasNext() ) 342 { 343 ( (XFocusListener) aIter.next() ).focusGained( aUNOEvt ); 344 } 345 } 346 } 347 348 //---------------------------------------------------------------------------------- 349 350 public void fireDisposingEvent() 351 { 352 ListIterator aIter = maEventListeners.listIterator( 0 ); 353 354 while( aIter.hasNext() ) 355 { 356 ( (XEventListener) aIter.next() ).disposing( new com.sun.star.lang.EventObject() ); 357 } 358 } 359 360 // -------------------- 361 // - XWindow methods - 362 // -------------------- 363 364 public void setPosSize( int X, int Y, int Width, int Height, short Flags ) 365 { 366 maFrame.setBounds( X, Y, Width, Height ); 367 } 368 369 //---------------------------------------------------------------------------------- 370 371 public com.sun.star.awt.Rectangle getPosSize( ) 372 { 373 java.awt.Rectangle bounds = maFrame.getBounds(); 374 return new com.sun.star.awt.Rectangle( bounds.x, bounds.y, bounds.width, bounds.height ); 375 } 376 377 //---------------------------------------------------------------------------------- 378 379 public void setVisible( boolean visible ) 380 { 381 maFrame.setVisible( visible ); 382 } 383 384 //---------------------------------------------------------------------------------- 385 386 public void setEnable( boolean enable ) 387 { 388 maFrame.setEnabled( enable ); 389 } 390 391 //---------------------------------------------------------------------------------- 392 393 public void setFocus() 394 { 395 } 396 397 //---------------------------------------------------------------------------------- 398 399 public void addEventListener( XEventListener xListener ) 400 { 401 if( xListener != null ) 402 maEventListeners.add( xListener ); 403 } 404 405 //---------------------------------------------------------------------------------- 406 407 public void removeEventListener( XEventListener xListener ) 408 { 409 if( xListener != null ) 410 maEventListeners.remove( xListener ); 411 } 412 413 //---------------------------------------------------------------------------------- 414 415 public void addWindowListener( XWindowListener xListener ) 416 { 417 if( xListener != null ) 418 maWindowListeners.add( xListener ); 419 } 420 421 //---------------------------------------------------------------------------------- 422 423 public void removeWindowListener( XWindowListener xListener ) 424 { 425 if( xListener != null ) 426 maWindowListeners.remove( xListener ); 427 } 428 429 //---------------------------------------------------------------------------------- 430 431 public void addFocusListener( XFocusListener xListener ) 432 { 433 if( xListener != null ) 434 maFocusListeners.add( xListener ); 435 } 436 437 //---------------------------------------------------------------------------------- 438 439 public void removeFocusListener( XFocusListener xListener ) 440 { 441 if( xListener != null ) 442 maFocusListeners.remove( xListener ); 443 } 444 445 //---------------------------------------------------------------------------------- 446 447 public void addKeyListener( XKeyListener xListener ) 448 { 449 if( xListener != null ) 450 maKeyListeners.add( xListener ); 451 } 452 453 //---------------------------------------------------------------------------------- 454 455 public void removeKeyListener( XKeyListener xListener ) 456 { 457 if( xListener != null ) 458 maKeyListeners.remove( xListener ); 459 } 460 461 //---------------------------------------------------------------------------------- 462 463 public void addMouseListener( XMouseListener xListener ) 464 { 465 if( xListener != null ) 466 maMouseListeners.add( xListener ); 467 } 468 469 //---------------------------------------------------------------------------------- 470 471 public void removeMouseListener( XMouseListener xListener ) 472 { 473 if( xListener != null ) 474 maMouseListeners.remove( xListener ); 475 } 476 477 //---------------------------------------------------------------------------------- 478 479 public void addMouseMotionListener( XMouseMotionListener xListener ) 480 { 481 if( xListener != null ) 482 maMouseMotionListeners.add( xListener ); 483 } 484 485 //---------------------------------------------------------------------------------- 486 487 public void removeMouseMotionListener( XMouseMotionListener xListener ) 488 { 489 if( xListener != null ) 490 maMouseMotionListeners.remove( xListener ); 491 } 492 493 //---------------------------------------------------------------------------------- 494 495 public void addPaintListener( XPaintListener xListener ) 496 { 497 if( xListener != null ) 498 maPaintListeners.add( xListener ); 499 } 500 501 //---------------------------------------------------------------------------------- 502 503 public void removePaintListener( XPaintListener xListener ) 504 { 505 if( xListener != null ) 506 maPaintListeners.remove( xListener ); 507 } 508 } 509