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