xref: /AOO41X/main/bean/com/sun/star/comp/beans/LocalOfficeWindow.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.comp.beans;
29 
30 import java.awt.Component;
31 
32 import com.sun.star.lang.EventObject;
33 import com.sun.star.lang.SystemDependent;
34 import com.sun.star.lang.XEventListener;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.lang.XMultiComponentFactory;
37 import com.sun.star.awt.Rectangle;
38 import com.sun.star.awt.XWindow;
39 import com.sun.star.awt.XWindowPeer;
40 import com.sun.star.awt.XVclWindowPeer;
41 import com.sun.star.awt.XToolkit;
42 import com.sun.star.awt.WindowDescriptor;
43 import com.sun.star.awt.WindowAttribute;
44 import com.sun.star.awt.WindowClass;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XComponentContext;
47 import com.sun.star.uno.Any;
48 import com.sun.star.uno.Type;
49 import com.sun.star.beans.NamedValue;
50 
51 /**
52  * This class represents a local office window.
53  *
54  * @since OOo 2.0.0
55  */
56 public class LocalOfficeWindow
57 	extends java.awt.Canvas
58 	implements OfficeWindow, XEventListener
59 {
60 	private transient OfficeConnection	mConnection;
61 	private transient XWindowPeer		mParentProxy;
62 	private transient XWindowPeer		mWindow;
63 	private boolean 			bPeer = false;
64 
65 	/**
66 	 * Construnctor.
67 	 *
68 	 * @param connection The office connection object the window
69 	 *	belongs to.
70 	 */
71 	protected LocalOfficeWindow(OfficeConnection connection)
72 	{
73 		mConnection	= connection;
74 		mConnection.addEventListener((XEventListener)this);
75 	}
76 
77 	/**
78 	 * Retrives an AWT component object associated with the OfficeWindow.
79 	 *
80 	 * @return The AWT component object associated with the OfficeWindow.
81 	 */
82 	public Component getAWTComponent()
83 	{
84 		return this;
85 	}
86 
87 	/**
88 	 * Retrives an UNO XWindowPeer object associated with the OfficeWindow.
89 	 *
90 	 * @return The UNO XWindowPeer object associated with the OfficeWindow.
91 	 */
92 	public XWindowPeer getUNOWindowPeer()
93 	{
94 		if (mWindow == null)
95 			createUNOWindowPeer();
96 		return mWindow;
97 	}
98 
99 	/**
100 	 * Receives a notification about the connection has been closed.
101 	 * This method has to set the connection to <code>null</code>.
102 	 *
103 	 * @source The event object.
104 	 */
105 	public void disposing(EventObject source)
106 	{
107 		// the window will be disposed by the framework
108 		mWindow = null;
109 		mConnection	= null;
110 	}
111 
112 	/**
113 	* Returns an AWT toolkit.
114         */
115        private XToolkit queryAWTToolkit()
116                throws com.sun.star.uno.Exception
117        {
118 			// Create a UNO toolkit.
119 			XMultiComponentFactory  compfactory;
120 			XComponentContext xContext = mConnection.getComponentContext();
121 			if ( xContext != null )
122 			{
123 				compfactory     = mConnection.getComponentContext().getServiceManager();
124 				XMultiServiceFactory    factory;
125 				factory = (XMultiServiceFactory)UnoRuntime.queryInterface(
126 						XMultiServiceFactory.class, compfactory);
127 				Object          object  = factory.createInstance( "com.sun.star.awt.Toolkit");
128 				return (XToolkit)UnoRuntime.queryInterface(XToolkit.class, object);
129 			}
130 			else
131 				return null;
132        }
133 
134        	/// called when system parent is available, reparents the bean window
135 	private synchronized void aquireSystemWindow()
136 	{
137 		if ( !bPeer )
138 		{
139 			// set real parent
140 			XVclWindowPeer xVclWindowPeer = (XVclWindowPeer)UnoRuntime.queryInterface(
141                                XVclWindowPeer.class, mWindow);
142 
143 			xVclWindowPeer.setProperty( "PluginParent", getWrappedWindowHandle());
144 			bPeer = true;
145             // show document window
146 			XWindow aWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, mWindow);
147 			aWindow.setVisible( true );
148 		}
149 	}
150 
151        	/// called when system parent is about to die, reparents the bean window
152 	private synchronized void releaseSystemWindow()
153 	{
154 		if ( bPeer )
155 		{
156 		       	// hide document window
157 			XWindow aWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, mWindow);
158 			aWindow.setVisible( false );
159 
160 			// set null parent
161 			XVclWindowPeer xVclWindowPeer = (XVclWindowPeer)UnoRuntime.queryInterface(
162                                XVclWindowPeer.class, mWindow);
163 			xVclWindowPeer.setProperty( "PluginParent", new Long(0) );
164 			bPeer = false;
165 		}
166 	}
167 
168 
169 	/// Overriding java.awt.Component.setVisible() due to Java bug (no showing event).
170 	public void setVisible( boolean b )
171 	{
172 		super.setVisible(b);
173 
174 		// Java-Bug: componentShown() is never called :-(
175 		// is still at least in Java 1.4.1_02
176 		if ( b )
177 			aquireSystemWindow();
178 		else
179 			releaseSystemWindow();
180 	}
181 
182        /** Factory method for a UNO AWT toolkit window as a child of this Java window.
183 	*
184 	*/
185        private synchronized XWindowPeer createUNOWindowPeer()
186        {
187 		try
188 		{
189 			// get this windows native window type
190             int type = getNativeWindowSystemType();
191 
192             // Java AWT windows only have a system window when showing.
193             XWindowPeer parentPeer;
194             if ( isShowing() )
195             {
196 				// create direct parent relationship
197 				//setVisible( true );
198                 parentPeer = new JavaWindowPeerFake(getWrappedWindowHandle(), type);
199 				bPeer = true;
200                         }
201                         else
202 			{
203 				// no parent yet
204 				parentPeer = null;
205 				bPeer = false;
206 			}
207 
208 			// create native window (mWindow)
209 			Rectangle aRect = new Rectangle( 0, 0, 20, 20 );
210 			WindowDescriptor desc = new WindowDescriptor();
211 			desc.Type = WindowClass.TOP;
212 			desc.Parent = parentPeer;
213 			desc.Bounds = aRect;
214 			desc.WindowServiceName = "workwindow";
215 			desc.WindowAttributes = (type == SystemDependent.SYSTEM_WIN32)
216 				? WindowAttribute.SHOW : 0;
217 			mWindow	= queryAWTToolkit().createWindow(desc);
218 
219 
220 			// set initial visibility
221             XWindow aWindow = (XWindow)UnoRuntime.queryInterface(XWindow.class, mWindow);
222 			aWindow.setVisible( bPeer );
223 		}
224 		catch (com.sun.star.uno.Exception exp) {
225 		}
226 
227 		return mWindow;
228 	}
229     /** We make sure that the office window is notified that the parent
230      *  will be removed.
231      */
232     public void removeNotify()
233     {
234         try {
235             releaseSystemWindow();
236         } catch (java.lang.Exception e) {
237             System.err.println("LocaleOfficeWindow.removeNotify: Exception in releaseSystemWindow.");
238             System.err.println(e.getMessage());
239             e.printStackTrace(System.err);
240         }
241         super.removeNotify();
242     }
243 
244 	/**
245 	 * Retrives a platform dependant system window identifier.
246 	 *
247 	 * @return The system window identifier.
248 	 */
249     private native long getNativeWindow();
250 
251 	/**
252 	 * Retrives a platform dependant system window type.
253 	 *
254 	 * @return The system window type.
255 	 */
256 	private native int getNativeWindowSystemType();
257 
258     /**
259     Returns an Any containing a sequences of com.sun.star.beans.NamedValue. One NamedValue
260     contains the name "WINDOW" and the value is a Long representing the window handle.
261     The second NamedValue  has the name "XEMBED" and the value is true, when the XEmbed
262     protocol shall be used fore embedding the native Window.
263     */
264     protected Any getWrappedWindowHandle()
265     {
266 
267         NamedValue window = new NamedValue(
268             "WINDOW", new Any(new Type(Long.class), new Long(getNativeWindow())));
269         NamedValue xembed = new NamedValue(
270             "XEMBED", new Any(new Type(Boolean.class), new Boolean(false)));
271 
272         if (getNativeWindowSystemType() == SystemDependent.SYSTEM_XWINDOW )
273         {
274             String vendor = System.getProperty("java.vendor");
275             if (vendor.equals("Sun Microsystems Inc.")
276                 && Boolean.valueOf(System.getProperty("sun.awt.xembedserver")).booleanValue())
277             {
278                 xembed = new NamedValue(
279                     "XEMBED",
280                     new Any(new Type(Boolean.class), new Boolean(true)));
281             }
282         }
283         return new Any(
284             new Type("[]com.sun.star.beans.NamedValue"),
285             new NamedValue[] {window, xembed});
286     }
287 
288 }
289