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