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