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