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