From 91aabe467dd9166dc47287523cfd960b5ee9b249 Mon Sep 17 00:00:00 2001 From: Manas Ranjan Dikshit Date: Thu, 27 Feb 2025 22:58:47 +0530 Subject: [PATCH] Update NativeView.java --- src/ag/ion/bion/officelayer/NativeView.java | 177 ++++++++------------ 1 file changed, 73 insertions(+), 104 deletions(-) diff --git a/src/ag/ion/bion/officelayer/NativeView.java b/src/ag/ion/bion/officelayer/NativeView.java index 8c58f1e..423435a 100644 --- a/src/ag/ion/bion/officelayer/NativeView.java +++ b/src/ag/ion/bion/officelayer/NativeView.java @@ -33,9 +33,6 @@ * * ****************************************************************************/ -/* - * Last changes made by $Author$, $Date$ - */ package ag.ion.bion.officelayer; import java.awt.Canvas; @@ -48,13 +45,13 @@ /** * Class to pass the system window handle to the OpenOffice.org toolkit. - * It use special JNI methods to get the system handle of used java window. - * + * It uses special JNI methods to get the system handle of the Java window. + * * Attention! * Use JNI functions on already visible canvas objects only! - * Otherwise they can make some trouble. + * Otherwise, they can cause issues. * - * Integrated into NOA by Markus Krüger as it is needed for integration in Swing applications. + * Integrated into NOA by Markus Krüger for Swing integration. * * @author Andreas Schlüns * @author Markus Krüger @@ -62,72 +59,36 @@ */ public class NativeView extends Canvas { - static final long serialVersionUID = 744155902373436284L; + private static final long serialVersionUID = 744155902373436284L; - /** - * @member maHandle system window handle - */ - public Integer maHandle; - /** - * @member maSystem info about currently used platform - */ - public int maSystem; + private Integer maHandle; + private int maSystem; + private Dimension preferredSize = new Dimension(500, 300); + private Dimension minSize = new Dimension(100, 100); + private Dimension maxSize = new Dimension(1024, 768); + private String libPath = null; /** - * JNI interface of this class - * These two methods are implemented by using JNI mechanismen. - * The will be used to get the platform dependent window handle - * of a java awt canvas. This handle can be used to create an office - * window as direct child of it. So it's possible to plug Office - * windows in a java UI container. - * - * Note: - * Native code for windows register special function pointer to handle - * window messages ... But if it doesn't check for an already registered - * instance of this handler it will do it twice and produce a stack overflow - * because such method call herself in a never ending loop ... - * So we try to use the JNI code one times only and safe already getted - * informations inside this class. + * JNI interface for native window handle */ public native int getNativeWindowSystemType(); - - private native long getNativeWindow(); // private! => use getHWND() with cache mechanism! - - private Dimension preferredSize = new Dimension(500, 300); - private Dimension minSize = new Dimension(100, 100); - private Dimension maxSize = new Dimension(1024, 768); - - private String libPath = null; + private native long getNativeWindow(); //---------------------------------------------------------------------------- + /** - * ctor - * Does nothing realy. - * We can use our JNI mechanism for an already visible - * canvas only. So we overload the method for showing ("setVisible()") - * and make our intialization there. BUt we try to show an empty clean - * window till there. + * Default constructor */ public NativeView() { - maHandle = null; - maSystem = 0; - this.setBackground(Color.white); - loadLibrary(); + this(null); } - //---------------------------------------------------------------------------- /** - * ctor - * Does nothing realy. - * We can use our JNI mechanism for an already visible - * canvas only. So we overload the method for showing ("setVisible()") - * and make our intialization there. BUt we try to show an empty clean - * window till there. - * + * Constructor with a custom native library path * @param nativeLibPath path to the native view library */ public NativeView(String nativeLibPath) { - libPath = nativeLibPath; + this.libPath = nativeLibPath; maHandle = null; maSystem = 0; this.setBackground(Color.white); @@ -135,54 +96,45 @@ public NativeView(String nativeLibPath) { } //---------------------------------------------------------------------------- + /** - * Overload this method to make neccessary initializations here. - * (e.g. get the window handle and neccessary system informations) - * - * Why here? - * Because the handle seams to be available for already visible windows - * only. So it's the best place to get it. Special helper method - * can be called more then ones - but call native code one times only - * and safe the handle and the system type on our members maHandle/maSystem! + * Overload this method to perform necessary initializations. + * Native handle should be available only for visible windows. */ + @Override public void setVisible(boolean bState) { getHWND(); } //---------------------------------------------------------------------------- - /** - * to guarantee right resize handling inside a swing container - * (e.g. JSplitPane) we must provide some informations about our - * prefered/minimum and maximum size. - */ + + @Override public Dimension getPreferredSize() { return preferredSize; } - //---------------------------------------------------------------------------- + @Override public void setPreferredSize(Dimension preferredSize) { super.setPreferredSize(preferredSize); this.preferredSize = preferredSize; } + @Override public Dimension getMaximumSize() { return maxSize; } - //---------------------------------------------------------------------------- + @Override public Dimension getMinimumSize() { return minSize; } //---------------------------------------------------------------------------- + /** - * overload paint routine to show provide against - * repaint errors if no office view is realy plugged - * into this canvas. - * If handle is present - we shouldn't paint anything further. - * May the remote window is already plugged. In such case we - * shouldn't paint it over. + * Overload paint routine to avoid repaint errors when no office view is plugged in. */ + @Override public void paint(Graphics aGraphic) { if (maHandle == null) { Dimension aSize = getSize(); @@ -191,51 +143,68 @@ public void paint(Graphics aGraphic) { } //---------------------------------------------------------------------------- + /** * Returns the window handle. - * * @return the window handle */ public Integer getHWND() { if (maHandle == null) { - maHandle = new Integer((int) getNativeWindow()); + maHandle = (int) getNativeWindow(); maSystem = getNativeWindowSystemType(); } return maHandle; } //---------------------------------------------------------------------------- + /** - * for using of the JNI methods it's necessary to load - * system library which exports it. + * Loads the required native system library, if not suppressed. */ private void loadLibrary() { - String libPathFromProps = System.getProperty(IOfficeApplication.NOA_NATIVE_LIB_PATH); - if (libPathFromProps != null) { - libPath = libPathFromProps; + // Check if suppression flag is set + String disableNativeLibs = System.getProperty("noa.native.disable", "false"); + if (Boolean.parseBoolean(disableNativeLibs)) { + System.out.println("Native library loading is disabled."); + return; // Exit without loading } - if (libPath != null) { - String libName = "libnativeview.so"; - String folder64bit = "64bit"; - if (OSHelper.IS_WINDOWS) { - libName = "nativeview.dll"; + + try { + String libPathFromProps = System.getProperty(IOfficeApplication.NOA_NATIVE_LIB_PATH); + if (libPathFromProps != null) { + libPath = libPathFromProps; } - boolean is64Bit = Integer.valueOf(System.getProperties().getProperty("sun.arch.data.model")) == 64; - if (is64Bit) { - if (new File(libPath + File.separator + folder64bit + File.separator + libName).exists()) { - libPath = libPath + File.separator + folder64bit; + + if (libPath != null) { + String libName = "libnativeview.so"; + String folder64bit = "64bit"; + if (OSHelper.IS_WINDOWS) { + libName = "nativeview.dll"; } - } - System.load(libPath + File.separator + libName); - }else if(OSHelper.IS_LINUX || OSHelper.IS_MAC){ - String libName = "libnativeview.so"; - boolean is64Bit = Integer.valueOf(System.getProperties().getProperty("sun.arch.data.model")) == 64; - libPath = "/usr/lib" + (is64Bit?"64":""); - - System.load(libPath + File.separator + libName); - }else{ - //not giving up yet + + boolean is64Bit = Integer.parseInt(System.getProperty("sun.arch.data.model")) == 64; + if (is64Bit) { + File potentialLib = new File(libPath + File.separator + folder64bit + File.separator + libName); + if (potentialLib.exists()) { + libPath = libPath + File.separator + folder64bit; + } + } + + System.load(libPath + File.separator + libName); + + } else if (OSHelper.IS_LINUX || OSHelper.IS_MAC) { + String libName = "libnativeview.so"; + boolean is64Bit = Integer.parseInt(System.getProperty("sun.arch.data.model")) == 64; + libPath = "/usr/lib" + (is64Bit ? "64" : ""); + System.load(libPath + File.separator + libName); + + } else { + // Default fallback System.loadLibrary("nativeview"); + } + + } catch (Exception e) { + System.err.println("Failed to load native library: " + e.getMessage()); } } }