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 package embeddedobj.test; 28 29 // __________ Imports __________ 30 31 import java.awt.*; 32 import java.lang.*; 33 import java.awt.event.*; 34 35 // __________ Implementation __________ 36 37 /** 38 * Class to pass the system window handle to the OpenOffice.org toolkit. 39 * It use special JNI methods to get the system handle of used java window. 40 * 41 * Attention! 42 * Use JNI functions on already visible canvas objects only! 43 * Otherwise they can make some trouble. 44 * 45 * @author Andreas Schlüns 46 * @created 22.02.2002 08:47 47 */ 48 49 public class NativeView extends java.awt.Canvas 50 { 51 // ____________________ 52 53 /** 54 * ctor 55 * Does nothing realy. 56 * We can use our JNI mechanism for an already visible 57 * canvas only. So we overload the method for showing ( "setVisible()" ) 58 * and make our intialization there. BUt we try to show an empty clean 59 * window till there. 60 */ 61 public NativeView() 62 { 63 maHandle = null; 64 maSystem = 0; 65 this.setBackground( Color.white ); 66 } 67 68 // ____________________ 69 70 /** 71 * Overload this method to make neccessary initializations here. 72 * ( e.g. get the window handle and neccessary system informations ) 73 * 74 * Why here? 75 * Because the handle seams to be available for already visible windows 76 * only. So it's the best place to get it. Special helper method 77 * can be called more then ones - but call native code one times only 78 * and safe the handle and the system type on our members maHandle/maSystem! 79 */ 80 public void setVisible( boolean bState ) 81 { 82 getHWND(); 83 } 84 85 // ____________________ 86 87 /** 88 * to guarantee right resize handling inside a swing container 89 * ( e.g. JSplitPane ) we must provide some informations about our 90 * prefered/minimum and maximum size. 91 */ 92 public Dimension getPreferredSize() 93 { 94 return new Dimension( 800, 600 ); 95 } 96 97 public Dimension getMaximumSize() 98 { 99 return new Dimension( 1024, 768 ); 100 } 101 102 public Dimension getMinimumSize() 103 { 104 return new Dimension( 300, 300 ); 105 } 106 107 // ____________________ 108 109 /** 110 * overload paint routine to show provide against 111 * repaint errors if no office view is realy plugged 112 * into this canvas. 113 * If handle is present - we shouldn't paint anything further. 114 * May the remote window is already plugged. In such case we 115 * shouldn't paint it over. 116 */ 117 public void paint( Graphics aGraphic ) 118 { 119 if( maHandle == null ) 120 { 121 Dimension aSize = getSize(); 122 aGraphic.clearRect( 0, 0, aSize.width, aSize.height ); 123 } 124 } 125 126 // ____________________ 127 128 /** 129 * JNI interface of this class 130 * These two methods are implemented by using JNI mechanismen. 131 * The will be used to get the platform dependent window handle 132 * of a java awt canvas. This handle can be used to create an office 133 * window as direct child of it. So it's possible to plug Office 134 * windows in a java UI container. 135 * 136 * Note: 137 * Native code for windows register special function pointer to handle 138 * window messages ... But if it doesn't check for an already registered 139 * instance of this handler it will do it twice and produce a stack overflow 140 * because such method call herself in a never ending loop ... 141 * So we try to use the JNI code one times only and safe already getted 142 * informations inside this class. 143 */ 144 public native int getNativeWindowSystemType(); 145 private native long getNativeWindow(); // private! => use getHWND() with cache mechanism! 146 147 public Integer getHWND() 148 { 149 if( maHandle == null ) 150 { 151 maHandle = new Integer( (int )getNativeWindow() ); 152 maSystem = getNativeWindowSystemType(); 153 } 154 return maHandle; 155 } 156 157 // ____________________ 158 159 /** 160 * for using of the JNI methods it's neccessary to load 161 * system library which exports it. 162 */ 163 static 164 { 165 System.loadLibrary( "nativeview" ); 166 } 167 168 // ____________________ 169 170 /** 171 * @member maHandle system window handle 172 * @member maSystem info about currently used platform 173 */ 174 public Integer maHandle ; 175 public int maSystem ; 176 } 177 178