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