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