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