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