1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 import com.sun.star.awt.Rectangle;
36 import com.sun.star.awt.XMessageBox;
37 import com.sun.star.awt.XMessageBoxFactory;
38 import com.sun.star.awt.XVclWindowPeer;
39 import com.sun.star.awt.XWindow;
40 import com.sun.star.awt.XWindowPeer;
41 import com.sun.star.beans.PropertyValue;
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 */
61     public MessageBox(XComponentContext _xContext, XMultiComponentFactory _xMCF){
62         m_xContext = _xContext;
63         m_xMCF = _xMCF;
64     }
65 
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 = (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 = (XCloseable)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 avtive frame
112     // which is potentially dangerous
113     public XWindowPeer getWindowPeerOfFrame(XComponent xComp) {
114         try {
115             XFrame xFrame = null;
116 
117             if (xComp != null) {
118                 XModel 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 = (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 =  (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 
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 =(com.sun.star.frame.XComponentLoader)
152                 UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,oDesktop);
153 
154             com.sun.star.beans.PropertyValue[] args = new com.sun.star.beans.PropertyValue [1];
155             args[0] = new com.sun.star.beans.PropertyValue();
156             args[0].Name = "Hidden";
157             args[0].Value = new Boolean(true);
158             String strDoc = "private:factory/swriter";
159 
160             xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, args);
161 
162         } catch(com.sun.star.uno.Exception ex) {
163             ex.printStackTrace();
164         }
165         return xComp;
166     }
167 
168     /** shows an error messagebox
169      *  @param _xParentWindowPeer the windowpeer of the parent window
170      *  @param _sTitle the title of the messagebox
171      *  @param _sMessage the message of the messagebox
172      */
173     public void showErrorMessageBox(XWindowPeer _xParentWindowPeer, String _sTitle, String _sMessage) {
174         XComponent xComponent = null;
175         try {
176             Object oToolkit = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext);
177             XMessageBoxFactory xMessageBoxFactory = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit);
178             // rectangle may be empty if position is in the center of the parent peer
179             Rectangle aRectangle = new Rectangle();
180             XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xParentWindowPeer, aRectangle, "errorbox", com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage);
181             xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xMessageBox);
182             if (xMessageBox != null){
183                 short nResult = xMessageBox.execute();
184             }
185         } catch (com.sun.star.uno.Exception ex) {
186             ex.printStackTrace(System.out);
187         } finally{
188             //make sure always to dispose the component and free the memory!
189             if (xComponent != null){
190                 xComponent.dispose();
191             }
192         }
193     }
194 
195 
196     /** @param _xVclWindowPeer the windowpeer of a dialog control or the dialog itself
197      *  @return true if HighContrastMode is activated or false if HighContrastMode is deactivated
198      */
199     public boolean isHighContrastModeActivated(XVclWindowPeer _xVclWindowPeer) {
200         boolean bIsActivated = false;
201 
202         try {
203             if (_xVclWindowPeer != null){
204                 int nUIColor = AnyConverter.toInt(_xVclWindowPeer.getProperty("DisplayBackgroundColor"));
205                 int nRed = getRedColorShare(nUIColor);
206                 int nGreen = getGreenColorShare(nUIColor);
207                 int nBlue = getBlueColorShare(nUIColor);
208                 int nLuminance = (( nBlue*28 + nGreen*151 + nRed*77 ) / 256 );
209                 boolean bisactivated = (nLuminance <= 25);
210                 return bisactivated;
211             } else{
212                 return false;
213             }
214         } catch (IllegalArgumentException e) {
215             e.printStackTrace(System.out);
216         }
217         return bIsActivated;
218     }
219 
220     public static int getRedColorShare(int _nColor) {
221         int nRed = (int) _nColor/65536;
222         int nRedModulo = _nColor % 65536;
223         int nGreen = (int) (nRedModulo / 256);
224         int nGreenModulo = (nRedModulo % 256);
225         int nBlue = nGreenModulo;
226         return nRed;
227     }
228 
229     public static int getGreenColorShare(int _nColor) {
230         int nRed = (int) _nColor/65536;
231         int nRedModulo = _nColor % 65536;
232         int nGreen = (int) (nRedModulo / 256);
233         return nGreen;
234     }
235 
236     public static int getBlueColorShare(int _nColor) {
237         int nRed = (int) _nColor/65536;
238         int nRedModulo = _nColor % 65536;
239         int nGreen = (int) (nRedModulo / 256);
240         int nGreenModulo = (nRedModulo % 256);
241         int nBlue = nGreenModulo;
242         return nBlue;
243     }
244 
245 }
246