Update ooo320-m1
[ooovba.git] / bean / com / sun / star / comp / beans / LocalOfficeWindow.java
blob2d2a0948d2739e11ac4c4eacaca61b944ccb7983
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.8 $
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.comp.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;
50 import com.sun.star.uno.Any;
51 import com.sun.star.uno.Type;
52 import com.sun.star.beans.NamedValue;
54 /**
55 * This class represents a local office window.
57 * @since OOo 2.0.0
59 public class LocalOfficeWindow
60 extends java.awt.Canvas
61 implements OfficeWindow, XEventListener
63 private transient OfficeConnection mConnection;
64 private transient XWindowPeer mParentProxy;
65 private transient XWindowPeer mWindow;
66 private boolean bPeer = false;
68 /**
69 * Construnctor.
71 * @param connection The office connection object the window
72 * belongs to.
74 protected LocalOfficeWindow(OfficeConnection connection)
76 mConnection = connection;
77 mConnection.addEventListener((XEventListener)this);
80 /**
81 * Retrives an AWT component object associated with the OfficeWindow.
83 * @return The AWT component object associated with the OfficeWindow.
85 public Component getAWTComponent()
87 return this;
90 /**
91 * Retrives an UNO XWindowPeer object associated with the OfficeWindow.
93 * @return The UNO XWindowPeer object associated with the OfficeWindow.
95 public XWindowPeer getUNOWindowPeer()
97 if (mWindow == null)
98 createUNOWindowPeer();
99 return mWindow;
103 * Receives a notification about the connection has been closed.
104 * This method has to set the connection to <code>null</code>.
106 * @source The event object.
108 public void disposing(EventObject source)
110 // the window will be disposed by the framework
111 mWindow = null;
112 mConnection = null;
116 * Returns an AWT toolkit.
118 private XToolkit queryAWTToolkit()
119 throws com.sun.star.uno.Exception
121 // Create a UNO toolkit.
122 XMultiComponentFactory compfactory;
123 XComponentContext xContext = mConnection.getComponentContext();
124 if ( xContext != null )
126 compfactory = mConnection.getComponentContext().getServiceManager();
127 XMultiServiceFactory factory;
128 factory = (XMultiServiceFactory)UnoRuntime.queryInterface(
129 XMultiServiceFactory.class, compfactory);
130 Object object = factory.createInstance( "com.sun.star.awt.Toolkit");
131 return (XToolkit)UnoRuntime.queryInterface(XToolkit.class, object);
133 else
134 return null;
137 /// called when system parent is available, reparents the bean window
138 private synchronized void aquireSystemWindow()
140 if ( !bPeer )
142 // set real parent
143 XVclWindowPeer xVclWindowPeer = (XVclWindowPeer)UnoRuntime.queryInterface(
144 XVclWindowPeer.class, mWindow);
146 xVclWindowPeer.setProperty( "PluginParent", getWrappedWindowHandle());
147 bPeer = true;
148 // show document window
149 XWindow aWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, mWindow);
150 aWindow.setVisible( true );
154 /// called when system parent is about to die, reparents the bean window
155 private synchronized void releaseSystemWindow()
157 if ( bPeer )
159 // hide document window
160 XWindow aWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, mWindow);
161 aWindow.setVisible( false );
163 // set null parent
164 XVclWindowPeer xVclWindowPeer = (XVclWindowPeer)UnoRuntime.queryInterface(
165 XVclWindowPeer.class, mWindow);
166 xVclWindowPeer.setProperty( "PluginParent", new Long(0) );
167 bPeer = false;
172 /// Overriding java.awt.Component.setVisible() due to Java bug (no showing event).
173 public void setVisible( boolean b )
175 super.setVisible(b);
177 // Java-Bug: componentShown() is never called :-(
178 // is still at least in Java 1.4.1_02
179 if ( b )
180 aquireSystemWindow();
181 else
182 releaseSystemWindow();
185 /** Factory method for a UNO AWT toolkit window as a child of this Java window.
188 private synchronized XWindowPeer createUNOWindowPeer()
190 try
192 // get this windows native window type
193 int type = getNativeWindowSystemType();
195 // Java AWT windows only have a system window when showing.
196 XWindowPeer parentPeer;
197 if ( isShowing() )
199 // create direct parent relationship
200 //setVisible( true );
201 parentPeer = new JavaWindowPeerFake(getWrappedWindowHandle(), type);
202 bPeer = true;
204 else
206 // no parent yet
207 parentPeer = null;
208 bPeer = false;
211 // create native window (mWindow)
212 Rectangle aRect = new Rectangle( 0, 0, 20, 20 );
213 WindowDescriptor desc = new WindowDescriptor();
214 desc.Type = WindowClass.TOP;
215 desc.Parent = parentPeer;
216 desc.Bounds = aRect;
217 desc.WindowServiceName = "workwindow";
218 desc.WindowAttributes = (type == SystemDependent.SYSTEM_WIN32)
219 ? WindowAttribute.SHOW : 0;
220 mWindow = queryAWTToolkit().createWindow(desc);
223 // set initial visibility
224 XWindow aWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, mWindow);
225 aWindow.setVisible( bPeer );
227 catch (com.sun.star.uno.Exception exp) {
230 return mWindow;
232 /** We make sure that the office window is notified that the parent
233 * will be removed.
235 public void removeNotify()
237 try {
238 releaseSystemWindow();
239 } catch (java.lang.Exception e) {
240 System.err.println("LocaleOfficeWindow.removeNotify: Exception in releaseSystemWindow.");
241 System.err.println(e.getMessage());
242 e.printStackTrace(System.err);
244 super.removeNotify();
248 * Retrives a platform dependant system window identifier.
250 * @return The system window identifier.
252 private native long getNativeWindow();
255 * Retrives a platform dependant system window type.
257 * @return The system window type.
259 private native int getNativeWindowSystemType();
262 Returns an Any containing a sequences of com.sun.star.beans.NamedValue. One NamedValue
263 contains the name "WINDOW" and the value is a Long representing the window handle.
264 The second NamedValue has the name "XEMBED" and the value is true, when the XEmbed
265 protocol shall be used fore embedding the native Window.
267 protected Any getWrappedWindowHandle()
270 NamedValue window = new NamedValue(
271 "WINDOW", new Any(new Type(Long.class), new Long(getNativeWindow())));
272 NamedValue xembed = new NamedValue(
273 "XEMBED", new Any(new Type(Boolean.class), new Boolean(false)));
275 if (getNativeWindowSystemType() == SystemDependent.SYSTEM_XWINDOW )
277 String vendor = System.getProperty("java.vendor");
278 if (vendor.equals("Sun Microsystems Inc.")
279 && Boolean.valueOf(System.getProperty("sun.awt.xembedserver")).booleanValue())
281 xembed = new NamedValue(
282 "XEMBED",
283 new Any(new Type(Boolean.class), new Boolean(true)));
286 return new Any(
287 new Type("[]com.sun.star.beans.NamedValue"),
288 new NamedValue[] {window, xembed});