1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import com.sun.star.awt.Rectangle;
25 import com.sun.star.awt.MessageBoxType;
26 import com.sun.star.awt.XMessageBox;
27 import com.sun.star.awt.XMessageBoxFactory;
28 import com.sun.star.awt.XVclWindowPeer;
29 import com.sun.star.awt.XWindow;
30 import com.sun.star.awt.XWindowPeer;
31 import com.sun.star.beans.PropertyValue;
32 import com.sun.star.frame.XFrame;
33 import com.sun.star.frame.XModel;
34 import com.sun.star.util.XCloseable;
35 import com.sun.star.frame.XFramesSupplier;
36 import com.sun.star.lang.IllegalArgumentException;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiComponentFactory;
39 import com.sun.star.uno.AnyConverter;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XComponentContext;
42 
43 
44 
45 public class MessageBox  {
46 
47     protected XComponentContext m_xContext = null;
48     protected com.sun.star.lang.XMultiComponentFactory m_xMCF;
49 
50     /** Creates a new instance of MessageBox */
MessageBox(XComponentContext _xContext, XMultiComponentFactory _xMCF)51     public MessageBox(XComponentContext _xContext, XMultiComponentFactory _xMCF){
52         m_xContext = _xContext;
53         m_xMCF = _xMCF;
54     }
55 
main(String args[])56     public static void main(String args[]) {
57         XComponent xComp = null;
58         try {
59             XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
60             if(xContext != null )
61                 System.out.println("Connected to a running office ...");
62             XMultiComponentFactory xMCF = xContext.getServiceManager();
63 
64             MessageBox oMessageBox = new MessageBox(xContext, xMCF);
65 
66             //load default text document to get an active frame
67             xComp = oMessageBox.createDefaultTextDocument();
68 
69             XWindowPeer xWindowPeer = oMessageBox.getWindowPeerOfFrame(xComp);
70             if (xWindowPeer != null) {
71                 XVclWindowPeer xVclWindowPeer = (XVclWindowPeer) UnoRuntime.queryInterface(XVclWindowPeer.class, xWindowPeer);
72                 boolean bisHighContrast = oMessageBox.isHighContrastModeActivated(xVclWindowPeer);
73                 oMessageBox.showErrorMessageBox(xWindowPeer, "My Sampletitle", "HighContrastMode is enabled: " + bisHighContrast);
74             } else{
75                 System.out.println("Could not retrieve current frame");
76             }
77 
78         } catch( Exception e ) {
79             System.err.println( e + e.getMessage());
80             e.printStackTrace();
81         } finally {
82             if (xComp != null) {
83                 try {
84                     XCloseable xClose = (XCloseable)UnoRuntime.queryInterface(XCloseable.class, xComp);
85                     if (xClose != null) {
86                         xClose.close(false);
87                     } else {
88                         xComp.dispose();
89                     }
90                 } catch (com.sun.star.util.CloseVetoException e) {
91                     System.err.println( e + e.getMessage());
92                     e.printStackTrace();
93                 }
94             }
95         }
96 
97         System.exit( 0 );
98     }
99 
100     // helper method to get the window peer of a document or if no
101     // document is specified it tries to get the avtive frame
102     // which is potentially dangerous
getWindowPeerOfFrame(XComponent xComp)103     public XWindowPeer getWindowPeerOfFrame(XComponent xComp) {
104         try {
105             XFrame xFrame = null;
106 
107             if (xComp != null) {
108                 XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, xComp);
109                 xFrame = xModel.getCurrentController().getFrame();
110 
111             } else {
112                 // Note: This method is potentially dangerous and should only be used for debugging
113                 // purposes as it relies on the platform dependent window handler..
114                 Object oDesktop = m_xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
115                 XFramesSupplier xFramesSupplier = (XFramesSupplier) UnoRuntime.queryInterface(XFramesSupplier.class, oDesktop);
116                 xFrame = xFramesSupplier.getActiveFrame();
117             }
118 
119             if (xFrame != null){
120                 XWindow xWindow = xFrame.getContainerWindow();
121                 if (xWindow != null){
122                     XWindowPeer xWindowPeer =  (XWindowPeer) UnoRuntime.queryInterface(XWindowPeer.class, xWindow);
123                     return xWindowPeer;
124                 }
125             }
126         } catch (com.sun.star.uno.Exception ex) {
127             ex.printStackTrace();
128         }
129         return null;
130     }
131 
createDefaultTextDocument()132     XComponent createDefaultTextDocument() {
133 
134         XComponent xComp =  null;
135         try {
136             Object oDesktop = m_xMCF.createInstanceWithContext(
137                                                "com.sun.star.frame.Desktop", m_xContext);
138 
139             // get the component laoder from the desktop to create a new
140             // text document
141             com.sun.star.frame.XComponentLoader xCLoader =(com.sun.star.frame.XComponentLoader)
142                 UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,oDesktop);
143 
144             com.sun.star.beans.PropertyValue[] args = new com.sun.star.beans.PropertyValue [1];
145             args[0] = new com.sun.star.beans.PropertyValue();
146             args[0].Name = "Hidden";
147             args[0].Value = new Boolean(true);
148             String strDoc = "private:factory/swriter";
149 
150             xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, args);
151 
152         } catch(com.sun.star.uno.Exception ex) {
153             ex.printStackTrace();
154         }
155         return xComp;
156     }
157 
158     /** shows an error messagebox
159      *  @param _xParentWindowPeer the windowpeer of the parent window
160      *  @param _sTitle the title of the messagebox
161      *  @param _sMessage the message of the messagebox
162      */
showErrorMessageBox(XWindowPeer _xParentWindowPeer, String _sTitle, String _sMessage)163     public void showErrorMessageBox(XWindowPeer _xParentWindowPeer, String _sTitle, String _sMessage) {
164         XComponent xComponent = null;
165         try {
166             Object oToolkit = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext);
167             XMessageBoxFactory xMessageBoxFactory = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit);
168             XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xParentWindowPeer, MessageBoxType.ERRORBOX, com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage);
169             xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xMessageBox);
170             if (xMessageBox != null){
171                 short nResult = xMessageBox.execute();
172             }
173         } catch (com.sun.star.uno.Exception ex) {
174             ex.printStackTrace(System.out);
175         } finally{
176             //make sure always to dispose the component and free the memory!
177             if (xComponent != null){
178                 xComponent.dispose();
179             }
180         }
181     }
182 
183 
184     /** @param _xVclWindowPeer the windowpeer of a dialog control or the dialog itself
185      *  @return true if HighContrastMode is activated or false if HighContrastMode is deactivated
186      */
isHighContrastModeActivated(XVclWindowPeer _xVclWindowPeer)187     public boolean isHighContrastModeActivated(XVclWindowPeer _xVclWindowPeer) {
188         boolean bIsActivated = false;
189 
190         try {
191             if (_xVclWindowPeer != null){
192                 int nUIColor = AnyConverter.toInt(_xVclWindowPeer.getProperty("DisplayBackgroundColor"));
193                 int nRed = getRedColorShare(nUIColor);
194                 int nGreen = getGreenColorShare(nUIColor);
195                 int nBlue = getBlueColorShare(nUIColor);
196                 int nLuminance = (( nBlue*28 + nGreen*151 + nRed*77 ) / 256 );
197                 boolean bisactivated = (nLuminance <= 25);
198                 return bisactivated;
199             } else{
200                 return false;
201             }
202         } catch (IllegalArgumentException e) {
203             e.printStackTrace(System.out);
204         }
205         return bIsActivated;
206     }
207 
getRedColorShare(int _nColor)208     public static int getRedColorShare(int _nColor) {
209         int nRed = (int) _nColor/65536;
210         int nRedModulo = _nColor % 65536;
211         int nGreen = (int) (nRedModulo / 256);
212         int nGreenModulo = (nRedModulo % 256);
213         int nBlue = nGreenModulo;
214         return nRed;
215     }
216 
getGreenColorShare(int _nColor)217     public static int getGreenColorShare(int _nColor) {
218         int nRed = (int) _nColor/65536;
219         int nRedModulo = _nColor % 65536;
220         int nGreen = (int) (nRedModulo / 256);
221         return nGreen;
222     }
223 
getBlueColorShare(int _nColor)224     public static int getBlueColorShare(int _nColor) {
225         int nRed = (int) _nColor/65536;
226         int nRedModulo = _nColor % 65536;
227         int nGreen = (int) (nRedModulo / 256);
228         int nGreenModulo = (nRedModulo % 256);
229         int nBlue = nGreenModulo;
230         return nBlue;
231     }
232 
233 }
234