2
* AccessODF - Accessibility checker for OpenOffice.org and LibreOffice Writer.
4
* Copyright (c) 2011 by DocArch <http://www.docarch.be>.
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation, either version 3 of the
9
* License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20
package be.docarch.accessodf.ooo;
22
import com.sun.star.awt.MessageBoxButtons;
23
import com.sun.star.awt.Rectangle;
24
import com.sun.star.awt.XMessageBox;
25
import com.sun.star.awt.XMessageBoxFactory;
26
import com.sun.star.awt.XWindowPeer;
27
import com.sun.star.beans.XPropertySet;
28
import com.sun.star.lang.XComponent;
29
import com.sun.star.lang.XInitialization;
30
import com.sun.star.lang.XMultiComponentFactory;
31
import com.sun.star.ui.dialogs.XExecutableDialog;
32
import com.sun.star.ui.dialogs.XFilePicker;
33
import com.sun.star.ui.dialogs.XFilterManager;
34
import com.sun.star.uno.UnoRuntime;
35
import com.sun.star.uno.XComponentContext;
39
* This class was taken from com.versusoft.packages.ooo
41
* @author Vincent Spiewak
43
public class UnoAwtUtils {
45
public static String showSaveAsDialog(String filename, String filterName, String filterPattern, XComponentContext m_xContext) {
46
String sStorePath = "";
47
XComponent xComponent = null;
48
XMultiComponentFactory m_xMCF = m_xContext.getServiceManager();
51
// the filepicker is instantiated with the global Multicomponentfactory...
52
Object oFilePicker = m_xMCF.createInstanceWithContext("com.sun.star.ui.dialogs.FilePicker", m_xContext);
53
XFilePicker xFilePicker = (XFilePicker) UnoRuntime.queryInterface(XFilePicker.class, oFilePicker);
55
// choose the template that defines the capabilities of the filepicker dialog
56
XInitialization xInitialize = (XInitialization) UnoRuntime.queryInterface(XInitialization.class, xFilePicker);
57
Short[] listAny = new Short[]{new Short(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION)};
58
xInitialize.initialize(listAny);
60
// add a control to the dialog to add the extension automatically to the filename...
61
// CRASH ON OOo Beta 3 MACOSX
62
//XFilePickerControlAccess xFilePickerControlAccess = (XFilePickerControlAccess) UnoRuntime.queryInterface(XFilePickerControlAccess.class, xFilePicker);
63
//xFilePickerControlAccess.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION, (short) 0, new Boolean(true));
65
xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xFilePicker);
67
// execute the dialog...
68
XExecutableDialog xExecutable = (XExecutableDialog) UnoRuntime.queryInterface(XExecutableDialog.class, xFilePicker);
70
// set the filters of the dialog. The filternames may be retrieved from
71
// http://wiki.services.openoffice.org/wiki/Framework/Article/Filter
72
XFilterManager xFilterManager = (XFilterManager) UnoRuntime.queryInterface(XFilterManager.class, xFilePicker);
73
xFilterManager.appendFilter(filterName, filterPattern);
75
// set the initial displaydirectory.
76
Object oPathSettings = m_xMCF.createInstanceWithContext("com.sun.star.util.PathSettings", m_xContext);
77
XPropertySet xPropertySet = (XPropertySet) com.sun.star.uno.UnoRuntime.queryInterface(XPropertySet.class, oPathSettings);
78
String sTemplateUrl = (String) xPropertySet.getPropertyValue("Work_writable");
79
xFilePicker.setDisplayDirectory(sTemplateUrl);
81
//set the initial filename
82
xFilePicker.setDefaultName(filename);
84
short nResult = xExecutable.execute();
86
// query the resulting path of the dialog...
87
if (nResult == com.sun.star.ui.dialogs.ExecutableDialogResults.OK) {
88
String[] sPathList = xFilePicker.getFiles();
89
if (sPathList.length > 0) {
90
sStorePath = sPathList[0];
94
} catch (com.sun.star.uno.Exception exception) {
95
exception.printStackTrace();
98
//make sure always to dispose the component and free the memory!
99
if (xComponent != null) {
100
xComponent.dispose();
106
public static short showMessageBox(XWindowPeer parentWindowPeer, String messageBoxType, int messageBoxButtons, String messageBoxTitle, String message) {
107
if (parentWindowPeer == null || messageBoxType == null || messageBoxTitle == null || message == null) {
111
// Initialize the message box factory
112
XMessageBoxFactory messageBoxFactory = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, parentWindowPeer.getToolkit());
114
Rectangle messageBoxRectangle = new Rectangle();
116
XMessageBox box = messageBoxFactory.createMessageBox(parentWindowPeer, messageBoxRectangle, messageBoxType, messageBoxButtons, messageBoxTitle, message);
117
return box.execute();
120
public static short showInfoMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
121
if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
125
return showMessageBox(parentWindowPeer, "infobox", MessageBoxButtons.BUTTONS_OK, messageBoxTitle, message);
128
public static short showErrorMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
129
if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
133
return showMessageBox(parentWindowPeer, "errorbox", MessageBoxButtons.BUTTONS_OK, messageBoxTitle, message);
136
public static short showYesNoWarningMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
137
if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
141
return showMessageBox(parentWindowPeer, "warningbox", MessageBoxButtons.BUTTONS_YES_NO + MessageBoxButtons.DEFAULT_BUTTON_NO, messageBoxTitle, message);
144
public static short showOkCancelWarningMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
145
if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
149
return showMessageBox(parentWindowPeer, "warningbox", MessageBoxButtons.BUTTONS_OK_CANCEL + MessageBoxButtons.DEFAULT_BUTTON_OK, messageBoxTitle, message);
152
public static short showQuestionMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
153
if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
157
return showMessageBox(parentWindowPeer, "querybox", MessageBoxButtons.BUTTONS_YES_NO_CANCEL + MessageBoxButtons.DEFAULT_BUTTON_YES, messageBoxTitle, message);
160
public static short showAbortRetryIgnoreErrorMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
161
if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
165
return showMessageBox(parentWindowPeer, "errorbox", MessageBoxButtons.BUTTONS_ABORT_IGNORE_RETRY + MessageBoxButtons.DEFAULT_BUTTON_RETRY, messageBoxTitle, message);
168
public static short showRetryCancelErrorMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
169
if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
173
return showMessageBox(parentWindowPeer, "errorbox", MessageBoxButtons.BUTTONS_RETRY_CANCEL + MessageBoxButtons.DEFAULT_BUTTON_CANCEL, messageBoxTitle, message);