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