merge the formfield patch from ooo-build
[ooovba.git] / avmedia / source / java / WindowAdapter.java
blobb06c4bb4cc20508ab92084bda728bd379e01fb90
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: WindowAdapter.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 sun.awt.*;
32 import com.sun.star.awt.*;
33 import com.sun.star.lang.*;
34 import java.util.*;
35 import javax.swing.*;
37 public class WindowAdapter
39 private java.awt.Frame maFrame;
40 private LinkedList maEventListeners = new LinkedList();
41 private LinkedList maWindowListeners = new LinkedList();
42 private LinkedList maFocusListeners = new LinkedList();
43 private LinkedList maKeyListeners = new LinkedList();
44 private LinkedList maMouseListeners = new LinkedList();
45 private LinkedList maMouseMotionListeners = new LinkedList();
46 private LinkedList maPaintListeners = new LinkedList();
47 private boolean mbShift = false, mbMod1 = false, mbMod2 = false;
49 // -----------------
50 // - WindowAdapter -
51 // -----------------
53 public WindowAdapter( int windowHandle )
55 maFrame = SystemWindowAdapter.createFrame( windowHandle );
58 //----------------------------------------------------------------------------------
60 public java.awt.Frame getJavaFrame()
62 return maFrame;
65 //----------------------------------------------------------------------------------
67 private short implGetUNOKeyCode( int nJavaKeyCode )
69 short nRet = 0;
71 switch( nJavaKeyCode )
73 case( java.awt.event.KeyEvent.VK_NUMPAD0 ):
74 case( java.awt.event.KeyEvent.VK_0 ): nRet = com.sun.star.awt.Key.NUM0; break;
75 case( java.awt.event.KeyEvent.VK_NUMPAD1 ):
76 case( java.awt.event.KeyEvent.VK_1 ): nRet = com.sun.star.awt.Key.NUM1; break;
77 case( java.awt.event.KeyEvent.VK_NUMPAD2 ):
78 case( java.awt.event.KeyEvent.VK_2 ): nRet = com.sun.star.awt.Key.NUM2; break;
79 case( java.awt.event.KeyEvent.VK_NUMPAD3 ):
80 case( java.awt.event.KeyEvent.VK_3 ): nRet = com.sun.star.awt.Key.NUM3; break;
81 case( java.awt.event.KeyEvent.VK_NUMPAD4 ):
82 case( java.awt.event.KeyEvent.VK_4 ): nRet = com.sun.star.awt.Key.NUM4; break;
83 case( java.awt.event.KeyEvent.VK_NUMPAD5 ):
84 case( java.awt.event.KeyEvent.VK_5 ): nRet = com.sun.star.awt.Key.NUM5; break;
85 case( java.awt.event.KeyEvent.VK_NUMPAD6 ):
86 case( java.awt.event.KeyEvent.VK_6 ): nRet = com.sun.star.awt.Key.NUM6; break;
87 case( java.awt.event.KeyEvent.VK_NUMPAD7 ):
88 case( java.awt.event.KeyEvent.VK_7 ): nRet = com.sun.star.awt.Key.NUM7; break;
89 case( java.awt.event.KeyEvent.VK_NUMPAD8 ):
90 case( java.awt.event.KeyEvent.VK_8 ): nRet = com.sun.star.awt.Key.NUM8; break;
91 case( java.awt.event.KeyEvent.VK_NUMPAD9 ):
92 case( java.awt.event.KeyEvent.VK_9 ): nRet = com.sun.star.awt.Key.NUM9; break;
94 case( java.awt.event.KeyEvent.VK_A ): nRet = com.sun.star.awt.Key.A; break;
95 case( java.awt.event.KeyEvent.VK_B ): nRet = com.sun.star.awt.Key.B; break;
96 case( java.awt.event.KeyEvent.VK_C ): nRet = com.sun.star.awt.Key.C; break;
97 case( java.awt.event.KeyEvent.VK_D ): nRet = com.sun.star.awt.Key.D; break;
98 case( java.awt.event.KeyEvent.VK_E ): nRet = com.sun.star.awt.Key.E; break;
99 case( java.awt.event.KeyEvent.VK_F ): nRet = com.sun.star.awt.Key.F; break;
100 case( java.awt.event.KeyEvent.VK_G ): nRet = com.sun.star.awt.Key.G; break;
101 case( java.awt.event.KeyEvent.VK_H ): nRet = com.sun.star.awt.Key.H; break;
102 case( java.awt.event.KeyEvent.VK_I ): nRet = com.sun.star.awt.Key.I; break;
103 case( java.awt.event.KeyEvent.VK_J ): nRet = com.sun.star.awt.Key.J; break;
104 case( java.awt.event.KeyEvent.VK_K ): nRet = com.sun.star.awt.Key.K; break;
105 case( java.awt.event.KeyEvent.VK_L ): nRet = com.sun.star.awt.Key.L; break;
106 case( java.awt.event.KeyEvent.VK_M ): nRet = com.sun.star.awt.Key.M; break;
107 case( java.awt.event.KeyEvent.VK_N ): nRet = com.sun.star.awt.Key.N; break;
108 case( java.awt.event.KeyEvent.VK_O ): nRet = com.sun.star.awt.Key.O; break;
109 case( java.awt.event.KeyEvent.VK_P ): nRet = com.sun.star.awt.Key.P; break;
110 case( java.awt.event.KeyEvent.VK_Q ): nRet = com.sun.star.awt.Key.Q; break;
111 case( java.awt.event.KeyEvent.VK_R ): nRet = com.sun.star.awt.Key.R; break;
112 case( java.awt.event.KeyEvent.VK_S ): nRet = com.sun.star.awt.Key.S; break;
113 case( java.awt.event.KeyEvent.VK_T ): nRet = com.sun.star.awt.Key.T; break;
114 case( java.awt.event.KeyEvent.VK_U ): nRet = com.sun.star.awt.Key.U; break;
115 case( java.awt.event.KeyEvent.VK_V ): nRet = com.sun.star.awt.Key.V; break;
116 case( java.awt.event.KeyEvent.VK_W ): nRet = com.sun.star.awt.Key.W; break;
117 case( java.awt.event.KeyEvent.VK_X ): nRet = com.sun.star.awt.Key.X; break;
118 case( java.awt.event.KeyEvent.VK_Y ): nRet = com.sun.star.awt.Key.Y; break;
119 case( java.awt.event.KeyEvent.VK_Z ): nRet = com.sun.star.awt.Key.Z; break;
121 case( java.awt.event.KeyEvent.VK_F1 ): nRet = com.sun.star.awt.Key.F1; break;
122 case( java.awt.event.KeyEvent.VK_F2 ): nRet = com.sun.star.awt.Key.F2; break;
123 case( java.awt.event.KeyEvent.VK_F3 ): nRet = com.sun.star.awt.Key.F3; break;
124 case( java.awt.event.KeyEvent.VK_F4 ): nRet = com.sun.star.awt.Key.F4; break;
125 case( java.awt.event.KeyEvent.VK_F5 ): nRet = com.sun.star.awt.Key.F5; break;
126 case( java.awt.event.KeyEvent.VK_F6 ): nRet = com.sun.star.awt.Key.F6; break;
127 case( java.awt.event.KeyEvent.VK_F7 ): nRet = com.sun.star.awt.Key.F7; break;
128 case( java.awt.event.KeyEvent.VK_F8 ): nRet = com.sun.star.awt.Key.F8; break;
129 case( java.awt.event.KeyEvent.VK_F9 ): nRet = com.sun.star.awt.Key.F9; break;
130 case( java.awt.event.KeyEvent.VK_F10 ): nRet = com.sun.star.awt.Key.F10; break;
131 case( java.awt.event.KeyEvent.VK_F11 ): nRet = com.sun.star.awt.Key.F11; break;
132 case( java.awt.event.KeyEvent.VK_F12 ): nRet = com.sun.star.awt.Key.F12; break;
133 case( java.awt.event.KeyEvent.VK_F13 ): nRet = com.sun.star.awt.Key.F13; break;
134 case( java.awt.event.KeyEvent.VK_F14 ): nRet = com.sun.star.awt.Key.F14; break;
135 case( java.awt.event.KeyEvent.VK_F15 ): nRet = com.sun.star.awt.Key.F15; break;
136 case( java.awt.event.KeyEvent.VK_F16 ): nRet = com.sun.star.awt.Key.F16; break;
137 case( java.awt.event.KeyEvent.VK_F17 ): nRet = com.sun.star.awt.Key.F17; break;
138 case( java.awt.event.KeyEvent.VK_F18 ): nRet = com.sun.star.awt.Key.F18; break;
139 case( java.awt.event.KeyEvent.VK_F19 ): nRet = com.sun.star.awt.Key.F19; break;
140 case( java.awt.event.KeyEvent.VK_F20 ): nRet = com.sun.star.awt.Key.F20; break;
141 case( java.awt.event.KeyEvent.VK_F21 ): nRet = com.sun.star.awt.Key.F21; break;
142 case( java.awt.event.KeyEvent.VK_F22 ): nRet = com.sun.star.awt.Key.F22; break;
143 case( java.awt.event.KeyEvent.VK_F23 ): nRet = com.sun.star.awt.Key.F23; break;
144 case( java.awt.event.KeyEvent.VK_F24 ): nRet = com.sun.star.awt.Key.F24; break;
146 case( java.awt.event.KeyEvent.VK_UP ): nRet = com.sun.star.awt.Key.UP; break;
147 case( java.awt.event.KeyEvent.VK_DOWN): nRet = com.sun.star.awt.Key.DOWN; break;
148 case( java.awt.event.KeyEvent.VK_LEFT ): nRet = com.sun.star.awt.Key.LEFT; break;
149 case( java.awt.event.KeyEvent.VK_RIGHT ): nRet = com.sun.star.awt.Key.RIGHT; break;
151 case( java.awt.event.KeyEvent.VK_HOME ): nRet = com.sun.star.awt.Key.HOME; break;
152 case( java.awt.event.KeyEvent.VK_END ): nRet = com.sun.star.awt.Key.END; break;
154 case( java.awt.event.KeyEvent.VK_PAGE_UP ): nRet = com.sun.star.awt.Key.PAGEUP; break;
155 case( java.awt.event.KeyEvent.VK_PAGE_DOWN ): nRet = com.sun.star.awt.Key.PAGEDOWN; break;
157 case( java.awt.event.KeyEvent.VK_ENTER ): nRet = com.sun.star.awt.Key.RETURN; break;
158 case( java.awt.event.KeyEvent.VK_ESCAPE ): nRet = com.sun.star.awt.Key.ESCAPE; break;
160 case( java.awt.event.KeyEvent.VK_TAB ): nRet = com.sun.star.awt.Key.TAB; break;
161 case( java.awt.event.KeyEvent.VK_BACK_SPACE ): nRet = com.sun.star.awt.Key.BACKSPACE; break;
162 case( java.awt.event.KeyEvent.VK_SPACE ): nRet = com.sun.star.awt.Key.SPACE; break;
163 case( java.awt.event.KeyEvent.VK_INSERT): nRet = com.sun.star.awt.Key.INSERT; break;
164 case( java.awt.event.KeyEvent.VK_DELETE): nRet = com.sun.star.awt.Key.DELETE; break;
165 case( java.awt.event.KeyEvent.VK_ADD ): nRet = com.sun.star.awt.Key.ADD; break;
166 case( java.awt.event.KeyEvent.VK_SUBTRACT ): nRet = com.sun.star.awt.Key.SUBTRACT; break;
167 case( java.awt.event.KeyEvent.VK_MULTIPLY ): nRet = com.sun.star.awt.Key.MULTIPLY; break;
168 case( java.awt.event.KeyEvent.VK_DIVIDE ): nRet = com.sun.star.awt.Key.DIVIDE; break;
169 case( java.awt.event.KeyEvent.VK_DECIMAL ): nRet = com.sun.star.awt.Key.POINT; break;
170 // case( java.awt.event.KeyEvent.VK_ COMMA; break;
171 case( java.awt.event.KeyEvent.VK_LESS ): nRet = com.sun.star.awt.Key.LESS; break;
172 case( java.awt.event.KeyEvent.VK_GREATER ): nRet = com.sun.star.awt.Key.GREATER; break;
173 case( java.awt.event.KeyEvent.VK_EQUALS ): nRet = com.sun.star.awt.Key.EQUAL; break;
174 // case( java.awt.event.KeyEvent.VK_ OPEN; break;
175 // case( java.awt.event.KeyEvent.VK_ CUT; break;
176 // case( java.awt.event.KeyEvent.VK_ COPY; break;
177 // case( java.awt.event.KeyEvent.VK_ PASTE; break;
178 // case( java.awt.event.KeyEvent.VK_ UNDO; break;
179 // case( java.awt.event.KeyEvent.VK_ REPEAT; break;
180 // case( java.awt.event.KeyEvent.VK_ FIND; break;
181 // case( java.awt.event.KeyEvent.VK_ PROPERTIES; break;
182 // case( java.awt.event.KeyEvent.VK_ FRONT; break;
183 // case( java.awt.event.KeyEvent.VK_ CONTEXTMENU; break;
184 // case( java.awt.event.KeyEvent.VK_ HELP; break;
186 default:
187 break;
190 return nRet;
193 //----------------------------------------------------------------------------------
195 public void fireKeyEvent( java.awt.event.KeyEvent aEvt )
197 if( maKeyListeners.size() > 0 )
199 boolean bProcess = false, bPressed = false;
201 if( java.awt.event.KeyEvent.KEY_PRESSED == aEvt.getID() )
203 switch( aEvt.getKeyCode() )
205 case( java.awt.event.KeyEvent.VK_SHIFT ): mbShift = true; break;
206 case( java.awt.event.KeyEvent.VK_CONTROL ): mbMod1 = true; break;
207 case( java.awt.event.KeyEvent.VK_ALT ): mbMod2 = true; break;
209 default:
211 bProcess = bPressed = true;
213 break;
216 else if( java.awt.event.KeyEvent.KEY_RELEASED == aEvt.getID() )
218 switch( aEvt.getKeyCode() )
220 case( java.awt.event.KeyEvent.VK_SHIFT ): mbShift = false; break;
221 case( java.awt.event.KeyEvent.VK_CONTROL ): mbMod1 = false; break;
222 case( java.awt.event.KeyEvent.VK_ALT ): mbMod2 = false; break;
224 default:
226 bProcess = true;
228 break;
232 if( bProcess )
234 KeyEvent aUNOEvt = new KeyEvent();
236 aUNOEvt.Modifiers = 0;
238 if( mbShift )
239 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.SHIFT;
241 if( mbMod1 )
242 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.MOD1;
244 if( mbMod2 )
245 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.MOD2;
247 aUNOEvt.KeyCode = implGetUNOKeyCode( aEvt.getKeyCode() );
248 aUNOEvt.KeyChar = aEvt.getKeyChar();
249 aUNOEvt.KeyFunc = com.sun.star.awt.KeyFunction.DONTKNOW;
251 ListIterator aIter = maKeyListeners.listIterator( 0 );
253 while( aIter.hasNext() )
255 if( bPressed )
256 ( (XKeyListener) aIter.next() ).keyPressed( aUNOEvt );
257 else
258 ( (XKeyListener) aIter.next() ).keyReleased( aUNOEvt );
264 //----------------------------------------------------------------------------------
266 public void fireMouseEvent( java.awt.event.MouseEvent aEvt )
268 MouseEvent aUNOEvt = new MouseEvent();
270 aUNOEvt.Modifiers = 0;
271 aUNOEvt.Buttons = 0;
272 aUNOEvt.X = aEvt.getX();
273 aUNOEvt.Y = aEvt.getY();
274 aUNOEvt.PopupTrigger = false;
276 // Modifiers
277 if( aEvt.isShiftDown() )
278 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.SHIFT;
280 if( aEvt.isControlDown() )
281 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.MOD1;
283 if( aEvt.isAltDown() )
284 aUNOEvt.Modifiers |= com.sun.star.awt.KeyModifier.MOD2;
286 // Buttons
287 if( SwingUtilities.isLeftMouseButton( aEvt ) )
288 aUNOEvt.Buttons |= com.sun.star.awt.MouseButton.LEFT;
290 if( SwingUtilities.isMiddleMouseButton( aEvt ) )
291 aUNOEvt.Buttons |= com.sun.star.awt.MouseButton.MIDDLE;
293 if( SwingUtilities.isRightMouseButton( aEvt ) )
294 aUNOEvt.Buttons |= com.sun.star.awt.MouseButton.RIGHT;
296 // event type
297 if( java.awt.event.MouseEvent.MOUSE_PRESSED == aEvt.getID() )
299 ListIterator aIter = maMouseListeners.listIterator( 0 );
301 aUNOEvt.ClickCount = 1;
303 while( aIter.hasNext() )
304 ( (XMouseListener) aIter.next() ).mousePressed( aUNOEvt );
306 else if( java.awt.event.MouseEvent.MOUSE_RELEASED == aEvt.getID() )
308 ListIterator aIter = maMouseListeners.listIterator( 0 );
310 aUNOEvt.ClickCount = 1;
312 while( aIter.hasNext() )
313 ( (XMouseListener) aIter.next() ).mouseReleased( aUNOEvt );
315 else if( java.awt.event.MouseEvent.MOUSE_DRAGGED == aEvt.getID() )
317 ListIterator aIter = maMouseMotionListeners.listIterator( 0 );
319 aUNOEvt.ClickCount = 0;
321 while( aIter.hasNext() )
322 ( (XMouseMotionListener) aIter.next() ).mouseDragged( aUNOEvt );
324 else if( java.awt.event.MouseEvent.MOUSE_MOVED == aEvt.getID() )
326 ListIterator aIter = maMouseMotionListeners.listIterator( 0 );
328 aUNOEvt.ClickCount = 0;
330 while( aIter.hasNext() )
331 ( (XMouseMotionListener) aIter.next() ).mouseMoved( aUNOEvt );
335 //----------------------------------------------------------------------------------
337 public void fireFocusEvent( java.awt.event.FocusEvent aEvt )
339 if( java.awt.event.FocusEvent.FOCUS_GAINED == aEvt.getID() )
341 ListIterator aIter = maFocusListeners.listIterator( 0 );
342 FocusEvent aUNOEvt = new FocusEvent();
344 while( aIter.hasNext() )
346 ( (XFocusListener) aIter.next() ).focusGained( aUNOEvt );
351 //----------------------------------------------------------------------------------
353 public void fireDisposingEvent()
355 ListIterator aIter = maEventListeners.listIterator( 0 );
357 while( aIter.hasNext() )
359 ( (XEventListener) aIter.next() ).disposing( new com.sun.star.lang.EventObject() );
363 // --------------------
364 // - XWindow methods -
365 // --------------------
367 public void setPosSize( int X, int Y, int Width, int Height, short Flags )
369 maFrame.setBounds( X, Y, Width, Height );
372 //----------------------------------------------------------------------------------
374 public com.sun.star.awt.Rectangle getPosSize( )
376 java.awt.Rectangle bounds = maFrame.getBounds();
377 return new com.sun.star.awt.Rectangle( bounds.x, bounds.y, bounds.width, bounds.height );
380 //----------------------------------------------------------------------------------
382 public void setVisible( boolean visible )
384 maFrame.setVisible( visible );
387 //----------------------------------------------------------------------------------
389 public void setEnable( boolean enable )
391 maFrame.setEnabled( enable );
394 //----------------------------------------------------------------------------------
396 public void setFocus()
400 //----------------------------------------------------------------------------------
402 public void addEventListener( XEventListener xListener )
404 if( xListener != null )
405 maEventListeners.add( xListener );
408 //----------------------------------------------------------------------------------
410 public void removeEventListener( XEventListener xListener )
412 if( xListener != null )
413 maEventListeners.remove( xListener );
416 //----------------------------------------------------------------------------------
418 public void addWindowListener( XWindowListener xListener )
420 if( xListener != null )
421 maWindowListeners.add( xListener );
424 //----------------------------------------------------------------------------------
426 public void removeWindowListener( XWindowListener xListener )
428 if( xListener != null )
429 maWindowListeners.remove( xListener );
432 //----------------------------------------------------------------------------------
434 public void addFocusListener( XFocusListener xListener )
436 if( xListener != null )
437 maFocusListeners.add( xListener );
440 //----------------------------------------------------------------------------------
442 public void removeFocusListener( XFocusListener xListener )
444 if( xListener != null )
445 maFocusListeners.remove( xListener );
448 //----------------------------------------------------------------------------------
450 public void addKeyListener( XKeyListener xListener )
452 if( xListener != null )
453 maKeyListeners.add( xListener );
456 //----------------------------------------------------------------------------------
458 public void removeKeyListener( XKeyListener xListener )
460 if( xListener != null )
461 maKeyListeners.remove( xListener );
464 //----------------------------------------------------------------------------------
466 public void addMouseListener( XMouseListener xListener )
468 if( xListener != null )
469 maMouseListeners.add( xListener );
472 //----------------------------------------------------------------------------------
474 public void removeMouseListener( XMouseListener xListener )
476 if( xListener != null )
477 maMouseListeners.remove( xListener );
480 //----------------------------------------------------------------------------------
482 public void addMouseMotionListener( XMouseMotionListener xListener )
484 if( xListener != null )
485 maMouseMotionListeners.add( xListener );
488 //----------------------------------------------------------------------------------
490 public void removeMouseMotionListener( XMouseMotionListener xListener )
492 if( xListener != null )
493 maMouseMotionListeners.remove( xListener );
496 //----------------------------------------------------------------------------------
498 public void addPaintListener( XPaintListener xListener )
500 if( xListener != null )
501 maPaintListeners.add( xListener );
504 //----------------------------------------------------------------------------------
506 public void removePaintListener( XPaintListener xListener )
508 if( xListener != null )
509 maPaintListeners.remove( xListener );