Get the style color and number just once
[LibreOffice.git] / odk / examples / DevelopersGuide / OfficeDev / DesktopEnvironment / NativeView.java
blob273290e55f8fdb367a7df630493a7cfbcc287d63
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 // __________ Imports __________
38 import java.awt.*;
40 // __________ Implementation __________
42 /**
43 * Class to pass the system window handle to the OpenOffice.org toolkit.
44 * It use special JNI methods to get the system handle of used java window.
46 * Attention!
47 * Use JNI functions on already visible canvas objects only!
48 * Otherwise they can make some trouble.
52 public class NativeView extends java.awt.Canvas
56 /**
57 * ctor
58 * Does nothing really.
59 * We can use our JNI mechanism for an already visible
60 * canvas only. So we override the method for showing ("setVisible()")
61 * and make our initialization there. But we try to show an empty clean
62 * window till there.
64 public NativeView()
66 maHandle = null;
67 maSystem = 0;
68 this.setBackground(Color.white);
73 /**
74 * Override this method to make necessary initializations here.
75 * (e.g. get the window handle and necessary system information)
77 * Why here?
78 * Because the handle seems to be available for already visible windows
79 * only. So it's the best place to get it. Special helper method
80 * can be called more than ones - but call native code one times only
81 * and safe the handle and the system type on our members maHandle/maSystem!
83 @Override
84 public void setVisible(boolean bState)
86 getHWND();
91 /**
92 * to guarantee right resize handling inside a swing container
93 * (e.g. JSplitPane) we must provide some information about our
94 * preferred/minimum and maximum size.
96 @Override
97 public Dimension getPreferredSize()
99 return new Dimension(500,300);
102 @Override
103 public Dimension getMaximumSize()
105 return new Dimension(1024,768);
108 @Override
109 public Dimension getMinimumSize()
111 return new Dimension(100,100);
117 * Override paint routine to show provide against
118 * repaint errors if no office view is really plugged
119 * into this canvas.
120 * If handle is present - we shouldn't paint anything further.
121 * May the remote window is already plugged. In such case we
122 * shouldn't paint it over.
124 @Override
125 public void paint(Graphics aGraphic)
127 if(maHandle==null)
129 Dimension aSize = getSize();
130 aGraphic.clearRect(0,0,aSize.width,aSize.height);
137 * JNI interface of this class
138 * These two methods are implemented by using JNI mechanismen.
139 * The will be used to get the platform dependent window handle
140 * of a java awt canvas. This handle can be used to create an office
141 * window as direct child of it. So it's possible to plug Office
142 * windows in a java UI container.
144 * Note:
145 * Native code for Windows registers a special function pointer to handle
146 * window messages... But if it doesn't check for an already-registered
147 * instance of this handler it will do it twice and produce a stack overflow
148 * because such method calls itself in a never-ending loop...
149 * So we try to use the JNI code one time only and save already-obtained
150 * information inside this class.
152 public native int getNativeWindowSystemType();
153 private native long getNativeWindow(); // private! => use getHWND() with cache mechanism!
155 public Integer getHWND()
157 if(maHandle==null)
159 maHandle = Integer.valueOf((int)getNativeWindow());
160 maSystem = getNativeWindowSystemType();
162 return maHandle;
168 * for using of the JNI methods it's necessary to load
169 * system library which exports it.
171 static
173 System.loadLibrary("nativeview");
179 * @member maHandle system window handle
180 * @member maSystem info about currently used platform
182 public Integer maHandle ;
183 public int maSystem ;
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */