merge the formfield patch from ooo-build
[ooovba.git] / avmedia / source / java / PlayerWindow.java
blob590228f59c6dd076c9b4f854813e30b6b0943416
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PlayerWindow.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.uno.XComponentContext;
33 import com.sun.star.uno.AnyConverter;
34 import com.sun.star.uno.IQueryInterface;
35 import com.sun.star.lang.XInitialization;
36 import com.sun.star.lang.XEventListener;
37 import com.sun.star.awt.*;
38 import com.sun.star.media.*;
40 // -----------------
41 // - Player Window -
42 // -----------------
44 public class PlayerWindow implements java.awt.event.KeyListener,
45 java.awt.event.MouseListener,
46 java.awt.event.MouseMotionListener,
47 java.awt.event.FocusListener,
48 com.sun.star.lang.XServiceInfo,
49 com.sun.star.media.XPlayerWindow
51 private com.sun.star.lang.XMultiServiceFactory maFactory;
52 private WindowAdapter maFrame;
53 private javax.media.Player maPlayer;
54 private com.sun.star.media.ZoomLevel meZoomLevel = com.sun.star.media.ZoomLevel.ORIGINAL;
55 private boolean mbShowControls = false;
58 // -------------------------------------------------------------------------
60 public PlayerWindow( com.sun.star.lang.XMultiServiceFactory aFactory,
61 java.lang.Object[] aArgs, javax.media.Player aPlayer )
63 maFactory = aFactory;
65 try
67 if( aArgs.length > 1 )
69 com.sun.star.awt.Rectangle aBoundRect = (com.sun.star.awt.Rectangle) aArgs[ 1 ];
71 maFrame = new WindowAdapter( AnyConverter.toInt( aArgs[ 0 ] ) );
72 maFrame.setPosSize( aBoundRect.X, aBoundRect.Y, aBoundRect.Width, aBoundRect.Height, (short) 0 );
74 if( aArgs.length > 2 )
75 mbShowControls = AnyConverter.toBoolean( aArgs[ 2 ] );
77 java.awt.Panel aPanel = new java.awt.Panel( new java.awt.BorderLayout() );
79 aPanel.setLayout( null );
80 aPanel.setBackground( java.awt.Color.black );
81 aPanel.addKeyListener( this );
82 aPanel.addMouseListener( this );
83 aPanel.addMouseMotionListener( this );
85 if( mbShowControls )
87 java.awt.Component aControlComponent = aPlayer.getControlPanelComponent();
89 if( aControlComponent != null )
90 aPanel.add( aControlComponent );
91 else
92 mbShowControls = false;
95 java.awt.Component aVisualComponent = aPlayer.getVisualComponent();
97 if( aVisualComponent != null )
99 aVisualComponent.addKeyListener( this );
100 aVisualComponent.addMouseListener( this );
101 aVisualComponent.addMouseMotionListener( this );
102 aVisualComponent.addFocusListener( this );
103 aPanel.add( aVisualComponent );
105 else
106 meZoomLevel = com.sun.star.media.ZoomLevel.NOT_AVAILABLE;
108 if( maFrame.getJavaFrame() != null )
109 maFrame.getJavaFrame().add( aPanel );
111 LayoutComponents();
114 catch( com.sun.star.lang.IllegalArgumentException e )
119 // -------------------------------------------------------------------------
121 protected synchronized void LayoutComponents()
123 if( maFrame.getJavaFrame() != null )
125 java.awt.Panel aPanel = (java.awt.Panel) maFrame.getJavaFrame().getComponent( 0 );
126 int nW = maFrame.getJavaFrame().getWidth();
127 int nH = maFrame.getJavaFrame().getHeight();
128 int nControlH = 0;
130 aPanel.setBounds( 0, 0, nW, nH );
132 if( mbShowControls )
134 java.awt.Component aControlComponent = aPanel.getComponent( 0 );
136 if( aControlComponent != null )
138 java.awt.Dimension aControlDimension = aControlComponent.getPreferredSize();
140 nControlH = Math.min( nH, aControlDimension.height );
141 aControlComponent.setBounds( 0, nH - nControlH, nW, nControlH );
145 if( com.sun.star.media.ZoomLevel.NOT_AVAILABLE != meZoomLevel )
147 java.awt.Component aVisualComponent = aPanel.getComponent( mbShowControls ? 1 : 0 );
149 if( aVisualComponent != null )
151 java.awt.Dimension aPrefDim = aVisualComponent.getPreferredSize();
152 int nVideoW = nW, nVideoH = ( nH - nControlH );
153 int nX = 0, nY = 0, nWidth = 0, nHeight = 0;
154 boolean bDone = false, bZoom = false;
156 if( com.sun.star.media.ZoomLevel.ORIGINAL == meZoomLevel )
158 bZoom = true;
160 else if( com.sun.star.media.ZoomLevel.ZOOM_1_TO_4 == meZoomLevel )
162 aPrefDim.width >>= 2;
163 aPrefDim.height >>= 2;
164 bZoom = true;
166 else if( com.sun.star.media.ZoomLevel.ZOOM_1_TO_2 == meZoomLevel )
168 aPrefDim.width >>= 1;
169 aPrefDim.height >>= 1;
170 bZoom = true;
172 else if( com.sun.star.media.ZoomLevel.ZOOM_2_TO_1 == meZoomLevel )
174 aPrefDim.width <<= 1;
175 aPrefDim.height <<= 1;
176 bZoom = true;
178 else if( com.sun.star.media.ZoomLevel.ZOOM_4_TO_1 == meZoomLevel )
180 aPrefDim.width <<= 2;
181 aPrefDim.height <<= 2;
182 bZoom = true;
184 else if( com.sun.star.media.ZoomLevel.FIT_TO_WINDOW == meZoomLevel )
186 nWidth = nVideoW;
187 nHeight = nVideoH;
188 bDone = true;
191 if( bZoom )
193 if( ( aPrefDim.width <= nVideoW ) && ( aPrefDim.height <= nVideoH ) )
195 nX = ( nVideoW - aPrefDim.width ) >> 1;
196 nY = ( nVideoH - aPrefDim.height ) >> 1;
197 nWidth = aPrefDim.width;
198 nHeight = aPrefDim.height;
199 bDone = true;
203 if( !bDone )
205 if( aPrefDim.width > 0 && aPrefDim.height > 0 && nVideoW > 0 && nVideoH > 0 )
207 double fPrefWH = (double) aPrefDim.width / aPrefDim.height;
209 if( fPrefWH < ( (double) nVideoW / nVideoH ) )
210 nVideoW = (int)( nVideoH * fPrefWH );
211 else
212 nVideoH = (int)( nVideoW / fPrefWH );
214 nX = ( nW - nVideoW ) >> 1;
215 nY = ( nH - nControlH - nVideoH ) >> 1;
216 nWidth = nVideoW;
217 nHeight = nVideoH;
219 else
220 nX = nY = nWidth = nHeight = 0;
223 aVisualComponent.setBounds( nX, nY, nWidth, nHeight );
224 aVisualComponent.requestFocus();
226 else
227 aPanel.requestFocus();
229 else
230 aPanel.requestFocus();
234 // -------------------------------------------------------------------------
236 private void implFireMouseEvent( java.awt.event.MouseEvent aEvt )
238 if( aEvt.getSource() != null &&
239 aEvt.getSource() instanceof java.awt.Component )
241 aEvt.translatePoint( ( (java.awt.Component) aEvt.getSource() ).getX(),
242 ( (java.awt.Component) aEvt.getSource() ).getY() );
245 maFrame.fireMouseEvent( aEvt );
248 // ---------------
249 // - KeyListener -
250 // ---------------
252 public void keyPressed( java.awt.event.KeyEvent aEvt )
254 maFrame.fireKeyEvent( aEvt );
257 // -------------------------------------------------------------------------
259 public void keyReleased( java.awt.event.KeyEvent aEvt )
261 maFrame.fireKeyEvent( aEvt );
264 // -------------------------------------------------------------------------
266 public void keyTyped( java.awt.event.KeyEvent aEvt )
268 maFrame.fireKeyEvent( aEvt );
271 // -----------------
272 // - MouseListener -
273 // -----------------
275 public void mousePressed( java.awt.event.MouseEvent aEvt )
277 implFireMouseEvent( aEvt );
280 // -------------------------------------------------------------------------
282 public void mouseClicked( java.awt.event.MouseEvent aEvt )
284 implFireMouseEvent( aEvt );
287 // -------------------------------------------------------------------------
289 public void mouseEntered( java.awt.event.MouseEvent aEvt )
291 implFireMouseEvent( aEvt );
294 // -------------------------------------------------------------------------
296 public void mouseExited( java.awt.event.MouseEvent aEvt )
298 implFireMouseEvent( aEvt );
301 // -------------------------------------------------------------------------
303 public void mouseReleased( java.awt.event.MouseEvent aEvt )
305 implFireMouseEvent( aEvt );
308 // -----------------------
309 // - MouseMotionListener -
310 // -----------------------
312 public void mouseDragged( java.awt.event.MouseEvent aEvt )
314 implFireMouseEvent( aEvt );
317 // -------------------------------------------------------------------------
319 public void mouseMoved( java.awt.event.MouseEvent aEvt )
321 implFireMouseEvent( aEvt );
324 // -----------------------
325 // - FocusListener -
326 // -----------------------
328 public void focusGained( java.awt.event.FocusEvent aEvt )
330 if( maFrame.getJavaFrame() != null )
331 maFrame.fireFocusEvent( aEvt );
334 // -------------------------------------------------------------------------
336 public void focusLost( java.awt.event.FocusEvent aEvt )
338 if( maFrame.getJavaFrame() != null )
339 maFrame.fireFocusEvent( aEvt );
342 // -----------------
343 // - XPlayerWindow -
344 // -----------------
346 public synchronized void update()
348 if( maFrame.getJavaFrame() != null )
349 maFrame.getJavaFrame().repaint();
352 // -------------------------------------------------------------------------
354 public synchronized boolean setZoomLevel( com.sun.star.media.ZoomLevel eZoomLevel )
356 boolean bRet = false;
358 if( com.sun.star.media.ZoomLevel.NOT_AVAILABLE != meZoomLevel &&
359 com.sun.star.media.ZoomLevel.NOT_AVAILABLE != eZoomLevel )
361 if( eZoomLevel != meZoomLevel )
363 meZoomLevel = eZoomLevel;
364 LayoutComponents();
367 bRet = true;
370 return bRet;
373 // -------------------------------------------------------------------------
375 public synchronized com.sun.star.media.ZoomLevel getZoomLevel()
377 return meZoomLevel;
380 // -------------------------------------------------------------------------
382 public synchronized void setPointerType( int nPointerType )
384 if( maFrame.getJavaFrame() != null )
386 int nCursor;
388 switch( nPointerType )
390 case( com.sun.star.awt.SystemPointer.CROSS ): nCursor = java.awt.Cursor.CROSSHAIR_CURSOR; break;
391 case( com.sun.star.awt.SystemPointer.HAND ): nCursor = java.awt.Cursor.HAND_CURSOR; break;
392 case( com.sun.star.awt.SystemPointer.MOVE ): nCursor = java.awt.Cursor.MOVE_CURSOR; break;
393 case( com.sun.star.awt.SystemPointer.WAIT ): nCursor = java.awt.Cursor.WAIT_CURSOR; break;
395 default: nCursor = java.awt.Cursor.DEFAULT_CURSOR; break;
398 maFrame.getJavaFrame().setCursor( java.awt.Cursor.getPredefinedCursor( nCursor ) );
402 // --------------
403 // - XComponent -
404 // --------------
406 public synchronized void dispose()
408 if( maFrame != null )
410 java.awt.Panel aPanel = (java.awt.Panel) maFrame.getJavaFrame().getComponent( 0 );
412 if( aPanel != null && aPanel.getComponent( 0 ) != null )
413 aPanel.getComponent( 0 ).removeFocusListener( this );
415 if( maFrame.getJavaFrame() != null )
416 maFrame.getJavaFrame().dispose();
418 maFrame.fireDisposingEvent();
421 maFrame = null;
424 // -----------
425 // - XWindow -
426 // -----------
428 public synchronized void setPosSize( int X, int Y, int Width, int Height, short Flags )
430 if( maFrame != null )
432 maFrame.setPosSize( X, Y, Width, Height, Flags );
433 LayoutComponents();
437 // -------------------------------------------------------------------------
439 public synchronized com.sun.star.awt.Rectangle getPosSize()
441 return( ( maFrame != null ) ? maFrame.getPosSize() : new com.sun.star.awt.Rectangle() );
444 // -------------------------------------------------------------------------
446 public synchronized void setVisible( boolean visible )
448 if( maFrame != null )
449 maFrame.setVisible( visible );
452 // -------------------------------------------------------------------------
454 public synchronized void setEnable( boolean enable )
456 if( maFrame != null )
457 maFrame.setEnable( enable );
460 // -------------------------------------------------------------------------
462 public synchronized void setFocus()
464 if( maFrame != null )
465 maFrame.setFocus();
468 // -------------------------------------------------------------------------
470 public synchronized void addEventListener( com.sun.star.lang.XEventListener xListener )
472 if( maFrame != null )
473 maFrame.addEventListener( xListener );
476 // -------------------------------------------------------------------------
478 public synchronized void removeEventListener( com.sun.star.lang.XEventListener xListener )
480 if( maFrame != null )
481 maFrame.removeEventListener( xListener );
484 // -------------------------------------------------------------------------
486 public synchronized void addWindowListener( XWindowListener xListener )
488 if( maFrame != null )
489 maFrame.addWindowListener( xListener );
492 // -------------------------------------------------------------------------
494 public synchronized void removeWindowListener( XWindowListener xListener )
496 if( maFrame != null )
497 maFrame.removeWindowListener( xListener );
500 // -------------------------------------------------------------------------
502 public synchronized void addFocusListener( XFocusListener xListener )
504 if( maFrame != null )
505 maFrame.addFocusListener( xListener );
508 // -------------------------------------------------------------------------
510 public synchronized void removeFocusListener( XFocusListener xListener )
512 if( maFrame != null )
513 maFrame.removeFocusListener( xListener );
516 // -------------------------------------------------------------------------
518 public synchronized void addKeyListener( XKeyListener xListener )
520 if( maFrame != null )
521 maFrame.addKeyListener( xListener );
524 // -------------------------------------------------------------------------
526 public synchronized void removeKeyListener( XKeyListener xListener )
528 if( maFrame != null )
529 maFrame.removeKeyListener( xListener );
532 // -------------------------------------------------------------------------
534 public synchronized void addMouseListener( XMouseListener xListener )
536 if( maFrame != null )
537 maFrame.addMouseListener( xListener );
540 // -------------------------------------------------------------------------
542 public synchronized void removeMouseListener( XMouseListener xListener )
544 if( maFrame != null )
545 maFrame.removeMouseListener( xListener );
548 // -------------------------------------------------------------------------
550 public synchronized void addMouseMotionListener( XMouseMotionListener xListener )
552 if( maFrame != null )
553 maFrame.addMouseMotionListener( xListener );
556 // -------------------------------------------------------------------------
558 public synchronized void removeMouseMotionListener( XMouseMotionListener xListener )
560 if( maFrame != null )
561 maFrame.removeMouseMotionListener( xListener );
564 // -------------------------------------------------------------------------
566 public synchronized void addPaintListener( XPaintListener xListener )
568 if( maFrame != null )
569 maFrame.addPaintListener( xListener );
572 // -------------------------------------------------------------------------
574 public synchronized void removePaintListener( XPaintListener xListener )
576 if( maFrame != null )
577 maFrame.removePaintListener( xListener );
580 // ----------------
581 // - XServiceInfo -
582 // ----------------
584 private static final String s_implName = "com.sun.star.comp.PlayerWindow_Java";
585 private static final String s_serviceName = "com.sun.star.media.PlayerWindow_Java";
587 public synchronized String getImplementationName()
589 return s_implName;
592 // -------------------------------------------------------------------------
594 public synchronized String [] getSupportedServiceNames()
596 return new String [] { s_serviceName };
599 // -------------------------------------------------------------------------
601 public synchronized boolean supportsService( String serviceName )
603 return serviceName.equals( s_serviceName );