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