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