Update ooo320-m1
[ooovba.git] / bean / com / sun / star / beans / LocalOfficeWindow.java
blobf60d218a3f0301ed970c9c765cea8741fbdd9cd1
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: LocalOfficeWindow.java,v $
10 * $Revision: 1.5 $
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 package com.sun.star.beans;
33 import java.awt.Component;
35 import com.sun.star.lang.EventObject;
36 import com.sun.star.lang.SystemDependent;
37 import com.sun.star.lang.XEventListener;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.lang.XMultiComponentFactory;
40 import com.sun.star.awt.Rectangle;
41 import com.sun.star.awt.XWindow;
42 import com.sun.star.awt.XWindowPeer;
43 import com.sun.star.awt.XVclWindowPeer;
44 import com.sun.star.awt.XToolkit;
45 import com.sun.star.awt.WindowDescriptor;
46 import com.sun.star.awt.WindowAttribute;
47 import com.sun.star.awt.WindowClass;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XComponentContext;
51 /**
52 * This class represents a local office window.
53 * @deprecated
55 public class LocalOfficeWindow
56 extends java.awt.Canvas
57 implements OfficeWindow, XEventListener
59 private transient OfficeConnection mConnection;
60 private transient XWindowPeer mParentProxy;
61 private transient XWindowPeer mWindow;
62 private boolean bPeer = false;
64 /**
65 * Construnctor.
67 * @param connection The office connection object the window
68 * belongs to.
70 /* package */ LocalOfficeWindow(OfficeConnection connection)
72 mConnection = connection;
73 mConnection.addEventListener((XEventListener)this);
76 /**
77 * Retrives an AWT component object associated with the OfficeWindow.
79 * @return The AWT component object associated with the OfficeWindow.
81 public Component getAWTComponent()
83 return this;
86 /**
87 * Retrives an UNO XWindowPeer object associated with the OfficeWindow.
89 * @return The UNO XWindowPeer object associated with the OfficeWindow.
91 public XWindowPeer getUNOWindowPeer()
93 if (mWindow == null)
94 createUNOWindowPeer();
95 return mWindow;
98 /**
99 * Receives a notification about the connection has been closed.
100 * This method has to set the connection to <code>null</code>.
102 * @source The event object.
104 public void disposing(EventObject source)
106 // the window will be disposed by the framework
107 mWindow = null;
108 mConnection = null;
112 * Returns an AWT toolkit.
114 private XToolkit queryAWTToolkit()
115 throws com.sun.star.uno.Exception
117 // Create a UNO toolkit.
118 XMultiComponentFactory compfactory;
119 XComponentContext xContext = mConnection.getComponentContext();
120 if ( xContext != null )
122 compfactory = mConnection.getComponentContext().getServiceManager();
123 XMultiServiceFactory factory;
124 factory = (XMultiServiceFactory)UnoRuntime.queryInterface(
125 XMultiServiceFactory.class, compfactory);
126 Object object = factory.createInstance( "com.sun.star.awt.Toolkit");
127 return (XToolkit)UnoRuntime.queryInterface(XToolkit.class, object);
129 else
130 return null;
133 /// called when system parent is available, reparents the bean window
134 private void aquireSystemWindow()
136 if ( !bPeer )
138 // set real parent
139 XVclWindowPeer xVclWindowPeer = (XVclWindowPeer)UnoRuntime.queryInterface(
140 XVclWindowPeer.class, mWindow);
141 xVclWindowPeer.setProperty( "PluginParent", new Long(getNativeWindow()) );
142 bPeer = true;
144 // show document window
145 XWindow aWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, mWindow);
146 aWindow.setVisible( true );
150 /// called when system parent is about to die, reparents the bean window
151 private void releaseSystemWindow()
153 if ( bPeer )
155 // hide document window
156 XWindow aWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, mWindow);
157 aWindow.setVisible( false );
159 // set null parent
160 XVclWindowPeer xVclWindowPeer = (XVclWindowPeer)UnoRuntime.queryInterface(
161 XVclWindowPeer.class, mWindow);
162 xVclWindowPeer.setProperty( "PluginParent", new Long(0) );
163 bPeer = false;
167 /// callback handler to get to know when we become visible
168 //@deprecated
169 class ComponentEventHandler
170 extends java.awt.event.ComponentAdapter
172 public void componentHidden( java.awt.event.ComponentEvent e)
174 // only when we become invisible, we might lose our system window
175 CallWatchThread aCallWatchThread = new CallWatchThread( 500 );
176 setVisible(false);
177 try { aCallWatchThread.cancel(); }
178 catch ( java.lang.InterruptedException aExc )
179 {} // ignore
182 public void componentShown( java.awt.event.ComponentEvent e)
184 // only when we become visible, we get a system window
185 aquireSystemWindow();
189 /// Overriding java.awt.Component.setVisible() due to Java bug (no showing event).
190 public void setVisible( boolean b )
192 super.setVisible(b);
194 // Java-Bug: componentShown() is never called :-(
195 // is still at least in Java 1.4.1_02
196 if ( b )
197 aquireSystemWindow();
198 else
199 releaseSystemWindow();
202 /** Factory method for a UNO AWT toolkit window as a child of this Java window.
205 private XWindowPeer createUNOWindowPeer()
207 try
209 // get this windows native window type
210 int type = getNativeWindowSystemType();
212 // Java AWT windows only have a system window when showing.
213 XWindowPeer parentPeer;
214 if ( isShowing() )
216 // create direct parent relationship
217 //setVisible( true );
218 parentPeer = new JavaWindowPeerFake( getNativeWindow(), type);
219 bPeer = true;
221 else
223 // no parent yet
224 parentPeer = null;
225 bPeer = false;
228 // create native window (mWindow)
229 Rectangle aRect = new Rectangle( 0, 0, 20, 20 );
230 WindowDescriptor desc = new WindowDescriptor();
231 desc.Type = WindowClass.TOP;
232 desc.Parent = parentPeer;
233 desc.Bounds = aRect;
234 desc.WindowServiceName = "workwindow";
235 desc.WindowAttributes = (type == SystemDependent.SYSTEM_WIN32)
236 ? WindowAttribute.SHOW : 0;
237 mWindow = queryAWTToolkit().createWindow(desc);
239 // to get notified when we become visible
240 addComponentListener( new ComponentEventHandler() );
242 // set initial visibility
243 XWindow aWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, mWindow);
244 aWindow.setVisible( bPeer );
246 catch (com.sun.star.uno.Exception exp) {
249 return mWindow;
253 * Retrives a platform dependant system window identifier.
255 * @return The system window identifier.
257 private native long getNativeWindow();
260 * Retrives a platform dependant system window type.
262 * @return The system window type.
264 private native int getNativeWindowSystemType();
266 //---------------------------------------------------------------------------
267 /** Helper class to watch calls into OOo with a timeout.
268 * @deprecated
270 class CallWatchThread extends Thread
272 Thread aWatchedThread;
273 long nTimeout;
275 CallWatchThread( long nTimeout )
277 this.aWatchedThread = Thread.currentThread();
278 this.nTimeout = nTimeout;
279 start();
282 void cancel()
283 throws java.lang.InterruptedException
285 Thread aThread = aWatchedThread;
286 aWatchedThread = null;
287 stop();
289 if ( aThread.interrupted() )
290 throw new InterruptedException();
293 public void run()
295 while ( aWatchedThread != null )
297 try { sleep( nTimeout ); }
298 catch ( java.lang.InterruptedException aExc )
301 //synchronized
303 if ( aWatchedThread != null )
305 aWatchedThread.interrupt();