1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * The Contents of this file are made available subject to the terms of 4*cdf0e10cSrcweir * the BSD license. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 7*cdf0e10cSrcweir * All rights reserved. 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * Redistribution and use in source and binary forms, with or without 10*cdf0e10cSrcweir * modification, are permitted provided that the following conditions 11*cdf0e10cSrcweir * are met: 12*cdf0e10cSrcweir * 1. Redistributions of source code must retain the above copyright 13*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer. 14*cdf0e10cSrcweir * 2. Redistributions in binary form must reproduce the above copyright 15*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer in the 16*cdf0e10cSrcweir * documentation and/or other materials provided with the distribution. 17*cdf0e10cSrcweir * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18*cdf0e10cSrcweir * contributors may be used to endorse or promote products derived 19*cdf0e10cSrcweir * from this software without specific prior written permission. 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*cdf0e10cSrcweir * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*cdf0e10cSrcweir * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*cdf0e10cSrcweir * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*cdf0e10cSrcweir * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*cdf0e10cSrcweir * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*cdf0e10cSrcweir * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28*cdf0e10cSrcweir * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29*cdf0e10cSrcweir * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30*cdf0e10cSrcweir * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31*cdf0e10cSrcweir * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*cdf0e10cSrcweir * 33*cdf0e10cSrcweir *************************************************************************/ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir import com.sun.star.awt.Rectangle; 36*cdf0e10cSrcweir import com.sun.star.awt.XMessageBox; 37*cdf0e10cSrcweir import com.sun.star.awt.XMessageBoxFactory; 38*cdf0e10cSrcweir import com.sun.star.awt.XVclWindowPeer; 39*cdf0e10cSrcweir import com.sun.star.awt.XWindow; 40*cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer; 41*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 42*cdf0e10cSrcweir import com.sun.star.frame.XFrame; 43*cdf0e10cSrcweir import com.sun.star.frame.XModel; 44*cdf0e10cSrcweir import com.sun.star.util.XCloseable; 45*cdf0e10cSrcweir import com.sun.star.frame.XFramesSupplier; 46*cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException; 47*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 48*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 49*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 50*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 51*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir public class MessageBox { 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir protected XComponentContext m_xContext = null; 58*cdf0e10cSrcweir protected com.sun.star.lang.XMultiComponentFactory m_xMCF; 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir /** Creates a new instance of MessageBox */ 61*cdf0e10cSrcweir public MessageBox(XComponentContext _xContext, XMultiComponentFactory _xMCF){ 62*cdf0e10cSrcweir m_xContext = _xContext; 63*cdf0e10cSrcweir m_xMCF = _xMCF; 64*cdf0e10cSrcweir } 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir public static void main(String args[]) { 67*cdf0e10cSrcweir XComponent xComp = null; 68*cdf0e10cSrcweir try { 69*cdf0e10cSrcweir XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 70*cdf0e10cSrcweir if(xContext != null ) 71*cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 72*cdf0e10cSrcweir XMultiComponentFactory xMCF = xContext.getServiceManager(); 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir MessageBox oMessageBox = new MessageBox(xContext, xMCF); 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir //load default text document to get an active frame 77*cdf0e10cSrcweir xComp = oMessageBox.createDefaultTextDocument(); 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir XWindowPeer xWindowPeer = oMessageBox.getWindowPeerOfFrame(xComp); 80*cdf0e10cSrcweir if (xWindowPeer != null) { 81*cdf0e10cSrcweir XVclWindowPeer xVclWindowPeer = (XVclWindowPeer) UnoRuntime.queryInterface(XVclWindowPeer.class, xWindowPeer); 82*cdf0e10cSrcweir boolean bisHighContrast = oMessageBox.isHighContrastModeActivated(xVclWindowPeer); 83*cdf0e10cSrcweir oMessageBox.showErrorMessageBox(xWindowPeer, "My Sampletitle", "HighContrastMode is enabled: " + bisHighContrast); 84*cdf0e10cSrcweir } else{ 85*cdf0e10cSrcweir System.out.println("Could not retrieve current frame"); 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir } catch( Exception e ) { 89*cdf0e10cSrcweir System.err.println( e + e.getMessage()); 90*cdf0e10cSrcweir e.printStackTrace(); 91*cdf0e10cSrcweir } finally { 92*cdf0e10cSrcweir if (xComp != null) { 93*cdf0e10cSrcweir try { 94*cdf0e10cSrcweir XCloseable xClose = (XCloseable)UnoRuntime.queryInterface(XCloseable.class, xComp); 95*cdf0e10cSrcweir if (xClose != null) { 96*cdf0e10cSrcweir xClose.close(false); 97*cdf0e10cSrcweir } else { 98*cdf0e10cSrcweir xComp.dispose(); 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir } catch (com.sun.star.util.CloseVetoException e) { 101*cdf0e10cSrcweir System.err.println( e + e.getMessage()); 102*cdf0e10cSrcweir e.printStackTrace(); 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir } 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir System.exit( 0 ); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir // helper method to get the window peer of a document or if no 111*cdf0e10cSrcweir // document is specified it tries to get the avtive frame 112*cdf0e10cSrcweir // which is potentially dangerous 113*cdf0e10cSrcweir public XWindowPeer getWindowPeerOfFrame(XComponent xComp) { 114*cdf0e10cSrcweir try { 115*cdf0e10cSrcweir XFrame xFrame = null; 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir if (xComp != null) { 118*cdf0e10cSrcweir XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, xComp); 119*cdf0e10cSrcweir xFrame = xModel.getCurrentController().getFrame(); 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir } else { 122*cdf0e10cSrcweir // Note: This method is potentially dangerous and should only be used for debugging 123*cdf0e10cSrcweir // purposes as it relies on the platform dependent window handler.. 124*cdf0e10cSrcweir Object oDesktop = m_xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext); 125*cdf0e10cSrcweir XFramesSupplier xFramesSupplier = (XFramesSupplier) UnoRuntime.queryInterface(XFramesSupplier.class, oDesktop); 126*cdf0e10cSrcweir xFrame = xFramesSupplier.getActiveFrame(); 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir if (xFrame != null){ 130*cdf0e10cSrcweir XWindow xWindow = xFrame.getContainerWindow(); 131*cdf0e10cSrcweir if (xWindow != null){ 132*cdf0e10cSrcweir XWindowPeer xWindowPeer = (XWindowPeer) UnoRuntime.queryInterface(XWindowPeer.class, xWindow); 133*cdf0e10cSrcweir return xWindowPeer; 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception ex) { 137*cdf0e10cSrcweir ex.printStackTrace(); 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir return null; 140*cdf0e10cSrcweir } 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir XComponent createDefaultTextDocument() { 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir XComponent xComp = null; 145*cdf0e10cSrcweir try { 146*cdf0e10cSrcweir Object oDesktop = m_xMCF.createInstanceWithContext( 147*cdf0e10cSrcweir "com.sun.star.frame.Desktop", m_xContext); 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir // get the component laoder from the desktop to create a new 150*cdf0e10cSrcweir // text document 151*cdf0e10cSrcweir com.sun.star.frame.XComponentLoader xCLoader =(com.sun.star.frame.XComponentLoader) 152*cdf0e10cSrcweir UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,oDesktop); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir com.sun.star.beans.PropertyValue[] args = new com.sun.star.beans.PropertyValue [1]; 155*cdf0e10cSrcweir args[0] = new com.sun.star.beans.PropertyValue(); 156*cdf0e10cSrcweir args[0].Name = "Hidden"; 157*cdf0e10cSrcweir args[0].Value = new Boolean(true); 158*cdf0e10cSrcweir String strDoc = "private:factory/swriter"; 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, args); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir } catch(com.sun.star.uno.Exception ex) { 163*cdf0e10cSrcweir ex.printStackTrace(); 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir return xComp; 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir /** shows an error messagebox 169*cdf0e10cSrcweir * @param _xParentWindowPeer the windowpeer of the parent window 170*cdf0e10cSrcweir * @param _sTitle the title of the messagebox 171*cdf0e10cSrcweir * @param _sMessage the message of the messagebox 172*cdf0e10cSrcweir */ 173*cdf0e10cSrcweir public void showErrorMessageBox(XWindowPeer _xParentWindowPeer, String _sTitle, String _sMessage) { 174*cdf0e10cSrcweir XComponent xComponent = null; 175*cdf0e10cSrcweir try { 176*cdf0e10cSrcweir Object oToolkit = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext); 177*cdf0e10cSrcweir XMessageBoxFactory xMessageBoxFactory = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit); 178*cdf0e10cSrcweir // rectangle may be empty if position is in the center of the parent peer 179*cdf0e10cSrcweir Rectangle aRectangle = new Rectangle(); 180*cdf0e10cSrcweir XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xParentWindowPeer, aRectangle, "errorbox", com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage); 181*cdf0e10cSrcweir xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xMessageBox); 182*cdf0e10cSrcweir if (xMessageBox != null){ 183*cdf0e10cSrcweir short nResult = xMessageBox.execute(); 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception ex) { 186*cdf0e10cSrcweir ex.printStackTrace(System.out); 187*cdf0e10cSrcweir } finally{ 188*cdf0e10cSrcweir //make sure always to dispose the component and free the memory! 189*cdf0e10cSrcweir if (xComponent != null){ 190*cdf0e10cSrcweir xComponent.dispose(); 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir } 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir /** @param _xVclWindowPeer the windowpeer of a dialog control or the dialog itself 197*cdf0e10cSrcweir * @return true if HighContrastMode is activated or false if HighContrastMode is deactivated 198*cdf0e10cSrcweir */ 199*cdf0e10cSrcweir public boolean isHighContrastModeActivated(XVclWindowPeer _xVclWindowPeer) { 200*cdf0e10cSrcweir boolean bIsActivated = false; 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir try { 203*cdf0e10cSrcweir if (_xVclWindowPeer != null){ 204*cdf0e10cSrcweir int nUIColor = AnyConverter.toInt(_xVclWindowPeer.getProperty("DisplayBackgroundColor")); 205*cdf0e10cSrcweir int nRed = getRedColorShare(nUIColor); 206*cdf0e10cSrcweir int nGreen = getGreenColorShare(nUIColor); 207*cdf0e10cSrcweir int nBlue = getBlueColorShare(nUIColor); 208*cdf0e10cSrcweir int nLuminance = (( nBlue*28 + nGreen*151 + nRed*77 ) / 256 ); 209*cdf0e10cSrcweir boolean bisactivated = (nLuminance <= 25); 210*cdf0e10cSrcweir return bisactivated; 211*cdf0e10cSrcweir } else{ 212*cdf0e10cSrcweir return false; 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir } catch (IllegalArgumentException e) { 215*cdf0e10cSrcweir e.printStackTrace(System.out); 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir return bIsActivated; 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir public static int getRedColorShare(int _nColor) { 221*cdf0e10cSrcweir int nRed = (int) _nColor/65536; 222*cdf0e10cSrcweir int nRedModulo = _nColor % 65536; 223*cdf0e10cSrcweir int nGreen = (int) (nRedModulo / 256); 224*cdf0e10cSrcweir int nGreenModulo = (nRedModulo % 256); 225*cdf0e10cSrcweir int nBlue = nGreenModulo; 226*cdf0e10cSrcweir return nRed; 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir public static int getGreenColorShare(int _nColor) { 230*cdf0e10cSrcweir int nRed = (int) _nColor/65536; 231*cdf0e10cSrcweir int nRedModulo = _nColor % 65536; 232*cdf0e10cSrcweir int nGreen = (int) (nRedModulo / 256); 233*cdf0e10cSrcweir return nGreen; 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir public static int getBlueColorShare(int _nColor) { 237*cdf0e10cSrcweir int nRed = (int) _nColor/65536; 238*cdf0e10cSrcweir int nRedModulo = _nColor % 65536; 239*cdf0e10cSrcweir int nGreen = (int) (nRedModulo / 256); 240*cdf0e10cSrcweir int nGreenModulo = (nRedModulo % 256); 241*cdf0e10cSrcweir int nBlue = nGreenModulo; 242*cdf0e10cSrcweir return nBlue; 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir } 246