1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /************************************************************************* 3 * 4 * The Contents of this file are made available subject to the terms of 5 * the BSD license. 6 * 7 * Copyright 2000, 2010 Oracle and/or its affiliates. 8 * All rights reserved. 9 * 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. 21 * 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. 33 * 34 *************************************************************************/ 35 36 import com.sun.star.awt.MessageBoxType; 37 import com.sun.star.awt.XMessageBox; 38 import com.sun.star.awt.XMessageBoxFactory; 39 import com.sun.star.awt.XVclWindowPeer; 40 import com.sun.star.awt.XWindow; 41 import com.sun.star.awt.XWindowPeer; 42 import com.sun.star.frame.XFrame; 43 import com.sun.star.frame.XModel; 44 import com.sun.star.util.XCloseable; 45 import com.sun.star.frame.XFramesSupplier; 46 import com.sun.star.lang.IllegalArgumentException; 47 import com.sun.star.lang.XComponent; 48 import com.sun.star.lang.XMultiComponentFactory; 49 import com.sun.star.uno.AnyConverter; 50 import com.sun.star.uno.UnoRuntime; 51 import com.sun.star.uno.XComponentContext; 52 53 54 55 public class MessageBox { 56 57 protected XComponentContext m_xContext = null; 58 protected com.sun.star.lang.XMultiComponentFactory m_xMCF; 59 60 /** Creates a new instance of MessageBox */ MessageBox(XComponentContext _xContext, XMultiComponentFactory _xMCF)61 public MessageBox(XComponentContext _xContext, XMultiComponentFactory _xMCF){ 62 m_xContext = _xContext; 63 m_xMCF = _xMCF; 64 } 65 main(String args[])66 public static void main(String args[]) { 67 XComponent xComp = null; 68 try { 69 XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 70 if(xContext != null ) 71 System.out.println("Connected to a running office ..."); 72 XMultiComponentFactory xMCF = xContext.getServiceManager(); 73 74 MessageBox oMessageBox = new MessageBox(xContext, xMCF); 75 76 //load default text document to get an active frame 77 xComp = oMessageBox.createDefaultTextDocument(); 78 79 XWindowPeer xWindowPeer = oMessageBox.getWindowPeerOfFrame(xComp); 80 if (xWindowPeer != null) { 81 XVclWindowPeer xVclWindowPeer = UnoRuntime.queryInterface(XVclWindowPeer.class, xWindowPeer); 82 boolean bisHighContrast = oMessageBox.isHighContrastModeActivated(xVclWindowPeer); 83 oMessageBox.showErrorMessageBox(xWindowPeer, "My Sampletitle", "HighContrastMode is enabled: " + bisHighContrast); 84 } else{ 85 System.out.println("Could not retrieve current frame"); 86 } 87 88 } catch( Exception e ) { 89 System.err.println( e + e.getMessage()); 90 e.printStackTrace(); 91 } finally { 92 if (xComp != null) { 93 try { 94 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xComp); 95 if (xClose != null) { 96 xClose.close(false); 97 } else { 98 xComp.dispose(); 99 } 100 } catch (com.sun.star.util.CloseVetoException e) { 101 System.err.println( e + e.getMessage()); 102 e.printStackTrace(); 103 } 104 } 105 } 106 107 System.exit( 0 ); 108 } 109 110 // helper method to get the window peer of a document or if no 111 // document is specified it tries to get the active frame 112 // which is potentially dangerous getWindowPeerOfFrame(XComponent xComp)113 public XWindowPeer getWindowPeerOfFrame(XComponent xComp) { 114 try { 115 XFrame xFrame = null; 116 117 if (xComp != null) { 118 XModel xModel = UnoRuntime.queryInterface(XModel.class, xComp); 119 xFrame = xModel.getCurrentController().getFrame(); 120 121 } else { 122 // Note: This method is potentially dangerous and should only be used for debugging 123 // purposes as it relies on the platform dependent window handler... 124 Object oDesktop = m_xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext); 125 XFramesSupplier xFramesSupplier = UnoRuntime.queryInterface(XFramesSupplier.class, oDesktop); 126 xFrame = xFramesSupplier.getActiveFrame(); 127 } 128 129 if (xFrame != null){ 130 XWindow xWindow = xFrame.getContainerWindow(); 131 if (xWindow != null){ 132 XWindowPeer xWindowPeer = UnoRuntime.queryInterface(XWindowPeer.class, xWindow); 133 return xWindowPeer; 134 } 135 } 136 } catch (com.sun.star.uno.Exception ex) { 137 ex.printStackTrace(); 138 } 139 return null; 140 } 141 createDefaultTextDocument()142 XComponent createDefaultTextDocument() { 143 144 XComponent xComp = null; 145 try { 146 Object oDesktop = m_xMCF.createInstanceWithContext( 147 "com.sun.star.frame.Desktop", m_xContext); 148 149 // get the component laoder from the desktop to create a new 150 // text document 151 com.sun.star.frame.XComponentLoader xCLoader =UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,oDesktop); 152 153 com.sun.star.beans.PropertyValue[] args = new com.sun.star.beans.PropertyValue [1]; 154 args[0] = new com.sun.star.beans.PropertyValue(); 155 args[0].Name = "Hidden"; 156 args[0].Value = Boolean.TRUE; 157 String strDoc = "private:factory/swriter"; 158 159 xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, args); 160 161 } catch(com.sun.star.uno.Exception ex) { 162 ex.printStackTrace(); 163 } 164 return xComp; 165 } 166 167 /** shows an error messagebox 168 * @param _xParentWindowPeer the windowpeer of the parent window 169 * @param _sTitle the title of the messagebox 170 * @param _sMessage the message of the messagebox 171 */ showErrorMessageBox(XWindowPeer _xParentWindowPeer, String _sTitle, String _sMessage)172 public void showErrorMessageBox(XWindowPeer _xParentWindowPeer, String _sTitle, String _sMessage) { 173 XComponent xComponent = null; 174 try { 175 Object oToolkit = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext); 176 XMessageBoxFactory xMessageBoxFactory = UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit); 177 XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xParentWindowPeer, MessageBoxType.ERRORBOX, com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage); 178 xComponent = UnoRuntime.queryInterface(XComponent.class, xMessageBox); 179 if (xMessageBox != null){ 180 xMessageBox.execute(); 181 } 182 } catch (com.sun.star.uno.Exception ex) { 183 ex.printStackTrace(System.err); 184 } finally{ 185 //make sure always to dispose the component and free the memory! 186 if (xComponent != null){ 187 xComponent.dispose(); 188 } 189 } 190 } 191 192 193 /** @param _xVclWindowPeer the windowpeer of a dialog control or the dialog itself 194 * @return true if HighContrastMode is activated or false if HighContrastMode is deactivated 195 */ isHighContrastModeActivated(XVclWindowPeer _xVclWindowPeer)196 public boolean isHighContrastModeActivated(XVclWindowPeer _xVclWindowPeer) { 197 boolean bIsActivated = false; 198 199 try { 200 if (_xVclWindowPeer != null){ 201 int nUIColor = AnyConverter.toInt(_xVclWindowPeer.getProperty("DisplayBackgroundColor")); 202 int nRed = getRedColorShare(nUIColor); 203 int nGreen = getGreenColorShare(nUIColor); 204 int nBlue = getBlueColorShare(nUIColor); 205 int nLuminance = (( nBlue*28 + nGreen*151 + nRed*77 ) / 256 ); 206 boolean bisactivated = (nLuminance <= 25); 207 return bisactivated; 208 } else{ 209 return false; 210 } 211 } catch (IllegalArgumentException e) { 212 e.printStackTrace(System.err); 213 } 214 return bIsActivated; 215 } 216 getRedColorShare(int _nColor)217 public static int getRedColorShare(int _nColor) { 218 int nRed = _nColor/65536; 219 return nRed; 220 } 221 getGreenColorShare(int _nColor)222 public static int getGreenColorShare(int _nColor) { 223 int nRedModulo = _nColor % 65536; 224 int nGreen = nRedModulo / 256; 225 return nGreen; 226 } 227 getBlueColorShare(int _nColor)228 public static int getBlueColorShare(int _nColor) { 229 int nRedModulo = _nColor % 65536; 230 int nGreenModulo = (nRedModulo % 256); 231 int nBlue = nGreenModulo; 232 return nBlue; 233 } 234 235 } 236 237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 238