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
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 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
;
55 public class MessageBox
{
57 protected XComponentContext m_xContext
= null;
58 protected com
.sun
.star
.lang
.XMultiComponentFactory m_xMCF
;
60 /** Creates a new instance of MessageBox */
61 public MessageBox(XComponentContext _xContext
, XMultiComponentFactory _xMCF
){
62 m_xContext
= _xContext
;
66 public static void main(String args
[]) {
67 XComponent xComp
= null;
69 XComponentContext xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
71 System
.out
.println("Connected to a running office ...");
72 XMultiComponentFactory xMCF
= xContext
.getServiceManager();
74 MessageBox oMessageBox
= new MessageBox(xContext
, xMCF
);
76 //load default text document to get an active frame
77 xComp
= oMessageBox
.createDefaultTextDocument();
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
);
85 System
.out
.println("Could not retrieve current frame");
88 } catch( Exception e
) {
89 System
.err
.println( e
+ e
.getMessage());
94 XCloseable xClose
= UnoRuntime
.queryInterface(XCloseable
.class, xComp
);
100 } catch (com
.sun
.star
.util
.CloseVetoException e
) {
101 System
.err
.println( e
+ e
.getMessage());
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
113 public XWindowPeer
getWindowPeerOfFrame(XComponent xComp
) {
115 XFrame xFrame
= null;
118 XModel xModel
= UnoRuntime
.queryInterface(XModel
.class, xComp
);
119 xFrame
= xModel
.getCurrentController().getFrame();
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();
130 XWindow xWindow
= xFrame
.getContainerWindow();
131 if (xWindow
!= null){
132 XWindowPeer xWindowPeer
= UnoRuntime
.queryInterface(XWindowPeer
.class, xWindow
);
136 } catch (com
.sun
.star
.uno
.Exception ex
) {
137 ex
.printStackTrace();
142 XComponent
createDefaultTextDocument() {
144 XComponent xComp
= null;
146 Object oDesktop
= m_xMCF
.createInstanceWithContext(
147 "com.sun.star.frame.Desktop", m_xContext
);
149 // get the component loader from the desktop to create a new
151 com
.sun
.star
.frame
.XComponentLoader xCLoader
=UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XComponentLoader
.class,oDesktop
);
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";
159 xComp
= xCLoader
.loadComponentFromURL(strDoc
, "_blank", 0, args
);
161 } catch(com
.sun
.star
.uno
.Exception ex
) {
162 ex
.printStackTrace();
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
172 public void showErrorMessageBox(XWindowPeer _xParentWindowPeer
, String _sTitle
, String _sMessage
) {
173 XComponent xComponent
= null;
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();
182 } catch (com
.sun
.star
.uno
.Exception ex
) {
183 ex
.printStackTrace(System
.err
);
185 //make sure always to dispose the component and free the memory!
186 if (xComponent
!= null){
187 xComponent
.dispose();
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
196 public boolean isHighContrastModeActivated(XVclWindowPeer _xVclWindowPeer
) {
197 boolean bIsActivated
= false;
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);
211 } catch (IllegalArgumentException e
) {
212 e
.printStackTrace(System
.err
);
217 public static int getRedColorShare(int _nColor
) {
218 int nRed
= _nColor
/65536;
222 public static int getGreenColorShare(int _nColor
) {
223 int nRedModulo
= _nColor
% 65536;
224 int nGreen
= nRedModulo
/ 256;
228 public static int getBlueColorShare(int _nColor
) {
229 int nRedModulo
= _nColor
% 65536;
230 int nGreenModulo
= (nRedModulo
% 256);
231 int nBlue
= nGreenModulo
;
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */