1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 *************************************************************************/ 34 35 // __________ Imports __________ 36 37 import java.awt.*; 38 import java.lang.*; 39 import java.awt.event.*; 40 41 // __________ Implementation __________ 42 43 /** 44 * Class to pass the system window handle to the OpenOffice.org toolkit. 45 * It use special JNI methods to get the system handle of used java window. 46 * 47 * Attention! 48 * Use JNI functions on already visible canvas objects only! 49 * Otherwise they can make some trouble. 50 * 51 * @author Andreas Schlüns 52 * @created 22.02.2002 08:47 53 */ 54 55 public class NativeView extends java.awt.Canvas 56 { 57 // ____________________ 58 59 /** 60 * ctor 61 * Does nothing realy. 62 * We can use our JNI mechanism for an already visible 63 * canvas only. So we overload the method for showing ("setVisible()") 64 * and make our intialization there. BUt we try to show an empty clean 65 * window till there. 66 */ 67 public NativeView() 68 { 69 maHandle = null; 70 maSystem = 0; 71 this.setBackground(Color.white); 72 } 73 74 // ____________________ 75 76 /** 77 * Overload this method to make neccessary initializations here. 78 * (e.g. get the window handle and neccessary system informations) 79 * 80 * Why here? 81 * Because the handle seams to be available for already visible windows 82 * only. So it's the best place to get it. Special helper method 83 * can be called more then ones - but call native code one times only 84 * and safe the handle and the system type on our members maHandle/maSystem! 85 */ 86 public void setVisible(boolean bState) 87 { 88 getHWND(); 89 } 90 91 // ____________________ 92 93 /** 94 * to guarantee right resize handling inside a swing container 95 * (e.g. JSplitPane) we must provide some informations about our 96 * prefered/minimum and maximum size. 97 */ 98 public Dimension getPreferredSize() 99 { 100 return new Dimension(500,300); 101 } 102 103 public Dimension getMaximumSize() 104 { 105 return new Dimension(1024,768); 106 } 107 108 public Dimension getMinimumSize() 109 { 110 return new Dimension(100,100); 111 } 112 113 // ____________________ 114 115 /** 116 * overload paint routine to show provide against 117 * repaint errors if no office view is realy plugged 118 * into this canvas. 119 * If handle is present - we shouldn't paint anything further. 120 * May the remote window is already plugged. In such case we 121 * shouldn't paint it over. 122 */ 123 public void paint(Graphics aGraphic) 124 { 125 if(maHandle==null) 126 { 127 Dimension aSize = getSize(); 128 aGraphic.clearRect(0,0,aSize.width,aSize.height); 129 } 130 } 131 132 // ____________________ 133 134 /** 135 * JNI interface of this class 136 * These two methods are implemented by using JNI mechanismen. 137 * The will be used to get the platform dependent window handle 138 * of a java awt canvas. This handle can be used to create an office 139 * window as direct child of it. So it's possible to plug Office 140 * windows in a java UI container. 141 * 142 * Note: 143 * Native code for windows register special function pointer to handle 144 * window messages ... But if it doesn't check for an already registered 145 * instance of this handler it will do it twice and produce a stack overflow 146 * because such method call herself in a never ending loop ... 147 * So we try to use the JNI code one times only and safe already getted 148 * informations inside this class. 149 */ 150 public native int getNativeWindowSystemType(); 151 private native long getNativeWindow(); // private! => use getHWND() with cache mechanism! 152 153 public Integer getHWND() 154 { 155 if(maHandle==null) 156 { 157 maHandle = new Integer((int)getNativeWindow()); 158 maSystem = getNativeWindowSystemType(); 159 } 160 return maHandle; 161 } 162 163 // ____________________ 164 165 /** 166 * for using of the JNI methods it's neccessary to load 167 * system library which exports it. 168 */ 169 static 170 { 171 System.loadLibrary("nativeview"); 172 } 173 174 // ____________________ 175 176 /** 177 * @member maHandle system window handle 178 * @member maSystem info about currently used platform 179 */ 180 public Integer maHandle ; 181 public int maSystem ; 182 } 183