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 com.sun.star.uno.UnoRuntime; 29 import com.sun.star.uno.XComponentContext; 30 import com.sun.star.uno.AnyConverter; 31 import com.sun.star.uno.IQueryInterface; 32 import com.sun.star.lang.XInitialization; 33 import com.sun.star.lang.XEventListener; 34 import com.sun.star.awt.*; 35 import com.sun.star.media.*; 36 37 // ----------------- 38 // - Player Window - 39 // ----------------- 40 41 public class PlayerWindow implements java.awt.event.KeyListener, 42 java.awt.event.MouseListener, 43 java.awt.event.MouseMotionListener, 44 java.awt.event.FocusListener, 45 com.sun.star.lang.XServiceInfo, 46 com.sun.star.media.XPlayerWindow 47 { 48 private com.sun.star.lang.XMultiServiceFactory maFactory; 49 private WindowAdapter maFrame; 50 private javax.media.Player maPlayer; 51 private com.sun.star.media.ZoomLevel meZoomLevel = com.sun.star.media.ZoomLevel.ORIGINAL; 52 private boolean mbShowControls = false; 53 54 55 // ------------------------------------------------------------------------- 56 57 public PlayerWindow( com.sun.star.lang.XMultiServiceFactory aFactory, 58 java.lang.Object[] aArgs, javax.media.Player aPlayer ) 59 { 60 maFactory = aFactory; 61 62 try 63 { 64 if( aArgs.length > 1 ) 65 { 66 com.sun.star.awt.Rectangle aBoundRect = (com.sun.star.awt.Rectangle) aArgs[ 1 ]; 67 68 maFrame = new WindowAdapter( AnyConverter.toInt( aArgs[ 0 ] ) ); 69 maFrame.setPosSize( aBoundRect.X, aBoundRect.Y, aBoundRect.Width, aBoundRect.Height, (short) 0 ); 70 mbShowControls = false; 71 72 java.awt.Panel aPanel = new java.awt.Panel( new java.awt.BorderLayout() ); 73 74 aPanel.setLayout( null ); 75 aPanel.setBackground( java.awt.Color.black ); 76 aPanel.addKeyListener( this ); 77 aPanel.addMouseListener( this ); 78 aPanel.addMouseMotionListener( this ); 79 80 if( mbShowControls ) 81 { 82 java.awt.Component aControlComponent = aPlayer.getControlPanelComponent(); 83 84 if( aControlComponent != null ) 85 aPanel.add( aControlComponent ); 86 else 87 mbShowControls = false; 88 } 89 90 java.awt.Component aVisualComponent = aPlayer.getVisualComponent(); 91 92 if( aVisualComponent != null ) 93 { 94 aVisualComponent.addKeyListener( this ); 95 aVisualComponent.addMouseListener( this ); 96 aVisualComponent.addMouseMotionListener( this ); 97 aVisualComponent.addFocusListener( this ); 98 aPanel.add( aVisualComponent ); 99 } 100 else 101 meZoomLevel = com.sun.star.media.ZoomLevel.NOT_AVAILABLE; 102 103 if( maFrame.getJavaFrame() != null ) 104 maFrame.getJavaFrame().add( aPanel ); 105 106 LayoutComponents(); 107 } 108 } 109 catch( com.sun.star.lang.IllegalArgumentException e ) 110 { 111 } 112 } 113 114 // ------------------------------------------------------------------------- 115 116 protected synchronized void LayoutComponents() 117 { 118 if( maFrame.getJavaFrame() != null ) 119 { 120 java.awt.Panel aPanel = (java.awt.Panel) maFrame.getJavaFrame().getComponent( 0 ); 121 int nW = maFrame.getJavaFrame().getWidth(); 122 int nH = maFrame.getJavaFrame().getHeight(); 123 int nControlH = 0; 124 125 aPanel.setBounds( 0, 0, nW, nH ); 126 127 if( mbShowControls ) 128 { 129 java.awt.Component aControlComponent = aPanel.getComponent( 0 ); 130 131 if( aControlComponent != null ) 132 { 133 java.awt.Dimension aControlDimension = aControlComponent.getPreferredSize(); 134 135 nControlH = Math.min( nH, aControlDimension.height ); 136 aControlComponent.setBounds( 0, nH - nControlH, nW, nControlH ); 137 } 138 } 139 140 if( com.sun.star.media.ZoomLevel.NOT_AVAILABLE != meZoomLevel ) 141 { 142 java.awt.Component aVisualComponent = aPanel.getComponent( mbShowControls ? 1 : 0 ); 143 144 if( aVisualComponent != null ) 145 { 146 java.awt.Dimension aPrefDim = aVisualComponent.getPreferredSize(); 147 int nVideoW = nW, nVideoH = ( nH - nControlH ); 148 int nX = 0, nY = 0, nWidth = 0, nHeight = 0; 149 boolean bDone = false, bZoom = false; 150 151 if( com.sun.star.media.ZoomLevel.ORIGINAL == meZoomLevel ) 152 { 153 bZoom = true; 154 } 155 else if( com.sun.star.media.ZoomLevel.ZOOM_1_TO_4 == meZoomLevel ) 156 { 157 aPrefDim.width >>= 2; 158 aPrefDim.height >>= 2; 159 bZoom = true; 160 } 161 else if( com.sun.star.media.ZoomLevel.ZOOM_1_TO_2 == meZoomLevel ) 162 { 163 aPrefDim.width >>= 1; 164 aPrefDim.height >>= 1; 165 bZoom = true; 166 } 167 else if( com.sun.star.media.ZoomLevel.ZOOM_2_TO_1 == meZoomLevel ) 168 { 169 aPrefDim.width <<= 1; 170 aPrefDim.height <<= 1; 171 bZoom = true; 172 } 173 else if( com.sun.star.media.ZoomLevel.ZOOM_4_TO_1 == meZoomLevel ) 174 { 175 aPrefDim.width <<= 2; 176 aPrefDim.height <<= 2; 177 bZoom = true; 178 } 179 else if( com.sun.star.media.ZoomLevel.FIT_TO_WINDOW == meZoomLevel ) 180 { 181 nWidth = nVideoW; 182 nHeight = nVideoH; 183 bDone = true; 184 } 185 186 if( bZoom ) 187 { 188 if( ( aPrefDim.width <= nVideoW ) && ( aPrefDim.height <= nVideoH ) ) 189 { 190 nX = ( nVideoW - aPrefDim.width ) >> 1; 191 nY = ( nVideoH - aPrefDim.height ) >> 1; 192 nWidth = aPrefDim.width; 193 nHeight = aPrefDim.height; 194 bDone = true; 195 } 196 } 197 198 if( !bDone ) 199 { 200 if( aPrefDim.width > 0 && aPrefDim.height > 0 && nVideoW > 0 && nVideoH > 0 ) 201 { 202 double fPrefWH = (double) aPrefDim.width / aPrefDim.height; 203 204 if( fPrefWH < ( (double) nVideoW / nVideoH ) ) 205 nVideoW = (int)( nVideoH * fPrefWH ); 206 else 207 nVideoH = (int)( nVideoW / fPrefWH ); 208 209 nX = ( nW - nVideoW ) >> 1; 210 nY = ( nH - nControlH - nVideoH ) >> 1; 211 nWidth = nVideoW; 212 nHeight = nVideoH; 213 } 214 else 215 nX = nY = nWidth = nHeight = 0; 216 } 217 218 aVisualComponent.setBounds( nX, nY, nWidth, nHeight ); 219 aVisualComponent.requestFocus(); 220 } 221 else 222 aPanel.requestFocus(); 223 } 224 else 225 aPanel.requestFocus(); 226 } 227 } 228 229 // ------------------------------------------------------------------------- 230 231 private void implFireMouseEvent( java.awt.event.MouseEvent aEvt ) 232 { 233 if( aEvt.getSource() != null && 234 aEvt.getSource() instanceof java.awt.Component ) 235 { 236 aEvt.translatePoint( ( (java.awt.Component) aEvt.getSource() ).getX(), 237 ( (java.awt.Component) aEvt.getSource() ).getY() ); 238 } 239 240 maFrame.fireMouseEvent( aEvt ); 241 } 242 243 // --------------- 244 // - KeyListener - 245 // --------------- 246 247 public void keyPressed( java.awt.event.KeyEvent aEvt ) 248 { 249 maFrame.fireKeyEvent( aEvt ); 250 } 251 252 // ------------------------------------------------------------------------- 253 254 public void keyReleased( java.awt.event.KeyEvent aEvt ) 255 { 256 maFrame.fireKeyEvent( aEvt ); 257 } 258 259 // ------------------------------------------------------------------------- 260 261 public void keyTyped( java.awt.event.KeyEvent aEvt ) 262 { 263 maFrame.fireKeyEvent( aEvt ); 264 } 265 266 // ----------------- 267 // - MouseListener - 268 // ----------------- 269 270 public void mousePressed( java.awt.event.MouseEvent aEvt ) 271 { 272 implFireMouseEvent( aEvt ); 273 } 274 275 // ------------------------------------------------------------------------- 276 277 public void mouseClicked( java.awt.event.MouseEvent aEvt ) 278 { 279 implFireMouseEvent( aEvt ); 280 } 281 282 // ------------------------------------------------------------------------- 283 284 public void mouseEntered( java.awt.event.MouseEvent aEvt ) 285 { 286 implFireMouseEvent( aEvt ); 287 } 288 289 // ------------------------------------------------------------------------- 290 291 public void mouseExited( java.awt.event.MouseEvent aEvt ) 292 { 293 implFireMouseEvent( aEvt ); 294 } 295 296 // ------------------------------------------------------------------------- 297 298 public void mouseReleased( java.awt.event.MouseEvent aEvt ) 299 { 300 implFireMouseEvent( aEvt ); 301 } 302 303 // ----------------------- 304 // - MouseMotionListener - 305 // ----------------------- 306 307 public void mouseDragged( java.awt.event.MouseEvent aEvt ) 308 { 309 implFireMouseEvent( aEvt ); 310 } 311 312 // ------------------------------------------------------------------------- 313 314 public void mouseMoved( java.awt.event.MouseEvent aEvt ) 315 { 316 implFireMouseEvent( aEvt ); 317 } 318 319 // ----------------------- 320 // - FocusListener - 321 // ----------------------- 322 323 public void focusGained( java.awt.event.FocusEvent aEvt ) 324 { 325 if( maFrame.getJavaFrame() != null ) 326 maFrame.fireFocusEvent( aEvt ); 327 } 328 329 // ------------------------------------------------------------------------- 330 331 public void focusLost( java.awt.event.FocusEvent aEvt ) 332 { 333 if( maFrame.getJavaFrame() != null ) 334 maFrame.fireFocusEvent( aEvt ); 335 } 336 337 // ----------------- 338 // - XPlayerWindow - 339 // ----------------- 340 341 public synchronized void update() 342 { 343 if( maFrame.getJavaFrame() != null ) 344 maFrame.getJavaFrame().repaint(); 345 } 346 347 // ------------------------------------------------------------------------- 348 349 public synchronized boolean setZoomLevel( com.sun.star.media.ZoomLevel eZoomLevel ) 350 { 351 boolean bRet = false; 352 353 if( com.sun.star.media.ZoomLevel.NOT_AVAILABLE != meZoomLevel && 354 com.sun.star.media.ZoomLevel.NOT_AVAILABLE != eZoomLevel ) 355 { 356 if( eZoomLevel != meZoomLevel ) 357 { 358 meZoomLevel = eZoomLevel; 359 LayoutComponents(); 360 } 361 362 bRet = true; 363 } 364 365 return bRet; 366 } 367 368 // ------------------------------------------------------------------------- 369 370 public synchronized com.sun.star.media.ZoomLevel getZoomLevel() 371 { 372 return meZoomLevel; 373 } 374 375 // ------------------------------------------------------------------------- 376 377 public synchronized void setPointerType( int nPointerType ) 378 { 379 if( maFrame.getJavaFrame() != null ) 380 { 381 int nCursor; 382 383 switch( nPointerType ) 384 { 385 case( com.sun.star.awt.SystemPointer.CROSS ): nCursor = java.awt.Cursor.CROSSHAIR_CURSOR; break; 386 case( com.sun.star.awt.SystemPointer.HAND ): nCursor = java.awt.Cursor.HAND_CURSOR; break; 387 case( com.sun.star.awt.SystemPointer.MOVE ): nCursor = java.awt.Cursor.MOVE_CURSOR; break; 388 case( com.sun.star.awt.SystemPointer.WAIT ): nCursor = java.awt.Cursor.WAIT_CURSOR; break; 389 390 default: nCursor = java.awt.Cursor.DEFAULT_CURSOR; break; 391 } 392 393 maFrame.getJavaFrame().setCursor( java.awt.Cursor.getPredefinedCursor( nCursor ) ); 394 } 395 } 396 397 // -------------- 398 // - XComponent - 399 // -------------- 400 401 public synchronized void dispose() 402 { 403 if( maFrame != null ) 404 { 405 java.awt.Panel aPanel = (java.awt.Panel) maFrame.getJavaFrame().getComponent( 0 ); 406 407 if( aPanel != null && aPanel.getComponent( 0 ) != null ) 408 aPanel.getComponent( 0 ).removeFocusListener( this ); 409 410 if( maFrame.getJavaFrame() != null ) 411 maFrame.getJavaFrame().dispose(); 412 413 maFrame.fireDisposingEvent(); 414 } 415 416 maFrame = null; 417 } 418 419 // ----------- 420 // - XWindow - 421 // ----------- 422 423 public synchronized void setPosSize( int X, int Y, int Width, int Height, short Flags ) 424 { 425 if( maFrame != null ) 426 { 427 maFrame.setPosSize( X, Y, Width, Height, Flags ); 428 LayoutComponents(); 429 } 430 } 431 432 // ------------------------------------------------------------------------- 433 434 public synchronized com.sun.star.awt.Rectangle getPosSize() 435 { 436 return( ( maFrame != null ) ? maFrame.getPosSize() : new com.sun.star.awt.Rectangle() ); 437 } 438 439 // ------------------------------------------------------------------------- 440 441 public synchronized void setVisible( boolean visible ) 442 { 443 if( maFrame != null ) 444 maFrame.setVisible( visible ); 445 } 446 447 // ------------------------------------------------------------------------- 448 449 public synchronized void setEnable( boolean enable ) 450 { 451 if( maFrame != null ) 452 maFrame.setEnable( enable ); 453 } 454 455 // ------------------------------------------------------------------------- 456 457 public synchronized void setFocus() 458 { 459 if( maFrame != null ) 460 maFrame.setFocus(); 461 } 462 463 // ------------------------------------------------------------------------- 464 465 public synchronized void addEventListener( com.sun.star.lang.XEventListener xListener ) 466 { 467 if( maFrame != null ) 468 maFrame.addEventListener( xListener ); 469 } 470 471 // ------------------------------------------------------------------------- 472 473 public synchronized void removeEventListener( com.sun.star.lang.XEventListener xListener ) 474 { 475 if( maFrame != null ) 476 maFrame.removeEventListener( xListener ); 477 } 478 479 // ------------------------------------------------------------------------- 480 481 public synchronized void addWindowListener( XWindowListener xListener ) 482 { 483 if( maFrame != null ) 484 maFrame.addWindowListener( xListener ); 485 } 486 487 // ------------------------------------------------------------------------- 488 489 public synchronized void removeWindowListener( XWindowListener xListener ) 490 { 491 if( maFrame != null ) 492 maFrame.removeWindowListener( xListener ); 493 } 494 495 // ------------------------------------------------------------------------- 496 497 public synchronized void addFocusListener( XFocusListener xListener ) 498 { 499 if( maFrame != null ) 500 maFrame.addFocusListener( xListener ); 501 } 502 503 // ------------------------------------------------------------------------- 504 505 public synchronized void removeFocusListener( XFocusListener xListener ) 506 { 507 if( maFrame != null ) 508 maFrame.removeFocusListener( xListener ); 509 } 510 511 // ------------------------------------------------------------------------- 512 513 public synchronized void addKeyListener( XKeyListener xListener ) 514 { 515 if( maFrame != null ) 516 maFrame.addKeyListener( xListener ); 517 } 518 519 // ------------------------------------------------------------------------- 520 521 public synchronized void removeKeyListener( XKeyListener xListener ) 522 { 523 if( maFrame != null ) 524 maFrame.removeKeyListener( xListener ); 525 } 526 527 // ------------------------------------------------------------------------- 528 529 public synchronized void addMouseListener( XMouseListener xListener ) 530 { 531 if( maFrame != null ) 532 maFrame.addMouseListener( xListener ); 533 } 534 535 // ------------------------------------------------------------------------- 536 537 public synchronized void removeMouseListener( XMouseListener xListener ) 538 { 539 if( maFrame != null ) 540 maFrame.removeMouseListener( xListener ); 541 } 542 543 // ------------------------------------------------------------------------- 544 545 public synchronized void addMouseMotionListener( XMouseMotionListener xListener ) 546 { 547 if( maFrame != null ) 548 maFrame.addMouseMotionListener( xListener ); 549 } 550 551 // ------------------------------------------------------------------------- 552 553 public synchronized void removeMouseMotionListener( XMouseMotionListener xListener ) 554 { 555 if( maFrame != null ) 556 maFrame.removeMouseMotionListener( xListener ); 557 } 558 559 // ------------------------------------------------------------------------- 560 561 public synchronized void addPaintListener( XPaintListener xListener ) 562 { 563 if( maFrame != null ) 564 maFrame.addPaintListener( xListener ); 565 } 566 567 // ------------------------------------------------------------------------- 568 569 public synchronized void removePaintListener( XPaintListener xListener ) 570 { 571 if( maFrame != null ) 572 maFrame.removePaintListener( xListener ); 573 } 574 575 // ---------------- 576 // - XServiceInfo - 577 // ---------------- 578 579 private static final String s_implName = "com.sun.star.comp.PlayerWindow_Java"; 580 private static final String s_serviceName = "com.sun.star.media.PlayerWindow_Java"; 581 582 public synchronized String getImplementationName() 583 { 584 return s_implName; 585 } 586 587 // ------------------------------------------------------------------------- 588 589 public synchronized String [] getSupportedServiceNames() 590 { 591 return new String [] { s_serviceName }; 592 } 593 594 // ------------------------------------------------------------------------- 595 596 public synchronized boolean supportsService( String serviceName ) 597 { 598 return serviceName.equals( s_serviceName ); 599 } 600 } 601