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