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