~ubuntu-branches/ubuntu/raring/accessodf/raring

« back to all changes in this revision

Viewing changes to addon/be/docarch/accessodf/ooo/UnoAwtUtils.java

  • Committer: Package Import Robot
  • Author(s): Sebastian Humenda
  • Date: 2012-04-09 11:21:13 UTC
  • Revision ID: package-import@ubuntu.com-20120409112113-v0kmfdj1ks80xoj8
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  AccessODF - Accessibility checker for OpenOffice.org and LibreOffice Writer.
 
3
 *
 
4
 *  Copyright (c) 2011 by DocArch <http://www.docarch.be>.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
package be.docarch.accessodf.ooo;
 
21
 
 
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;
 
36
 
 
37
 
 
38
/**
 
39
 * This class was taken from com.versusoft.packages.ooo
 
40
 *
 
41
 * @author Vincent Spiewak
 
42
 */
 
43
public class UnoAwtUtils {
 
44
 
 
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();
 
49
 
 
50
        try {
 
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);
 
54
 
 
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);
 
59
 
 
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));
 
64
 
 
65
            xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xFilePicker);
 
66
 
 
67
            // execute the dialog...
 
68
            XExecutableDialog xExecutable = (XExecutableDialog) UnoRuntime.queryInterface(XExecutableDialog.class, xFilePicker);
 
69
 
 
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);
 
74
 
 
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);
 
80
 
 
81
            //set the initial filename
 
82
                xFilePicker.setDefaultName(filename);
 
83
 
 
84
            short nResult = xExecutable.execute();
 
85
 
 
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];
 
91
                }
 
92
            }
 
93
 
 
94
        } catch (com.sun.star.uno.Exception exception) {
 
95
            exception.printStackTrace();
 
96
 
 
97
        } finally {
 
98
            //make sure always to dispose the component and free the memory!
 
99
            if (xComponent != null) {
 
100
                xComponent.dispose();
 
101
            }
 
102
        }
 
103
        return sStorePath;
 
104
    }
 
105
 
 
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) {
 
108
            return 0;
 
109
        }
 
110
 
 
111
        // Initialize the message box factory
 
112
        XMessageBoxFactory messageBoxFactory = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, parentWindowPeer.getToolkit());
 
113
 
 
114
        Rectangle messageBoxRectangle = new Rectangle();
 
115
 
 
116
        XMessageBox box = messageBoxFactory.createMessageBox(parentWindowPeer, messageBoxRectangle, messageBoxType, messageBoxButtons, messageBoxTitle, message);
 
117
        return box.execute();
 
118
    }
 
119
 
 
120
    public static short showInfoMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
 
121
        if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
 
122
            return 0;
 
123
        }
 
124
 
 
125
        return showMessageBox(parentWindowPeer, "infobox", MessageBoxButtons.BUTTONS_OK, messageBoxTitle, message);
 
126
    }
 
127
 
 
128
    public static short showErrorMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
 
129
        if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
 
130
            return 0;
 
131
        }
 
132
 
 
133
        return showMessageBox(parentWindowPeer, "errorbox", MessageBoxButtons.BUTTONS_OK, messageBoxTitle, message);
 
134
    }
 
135
 
 
136
    public static short showYesNoWarningMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
 
137
        if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
 
138
            return 0;
 
139
        }
 
140
 
 
141
        return showMessageBox(parentWindowPeer, "warningbox", MessageBoxButtons.BUTTONS_YES_NO + MessageBoxButtons.DEFAULT_BUTTON_NO, messageBoxTitle, message);
 
142
    }
 
143
 
 
144
    public static short showOkCancelWarningMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
 
145
        if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
 
146
            return 0;
 
147
        }
 
148
 
 
149
        return showMessageBox(parentWindowPeer, "warningbox", MessageBoxButtons.BUTTONS_OK_CANCEL + MessageBoxButtons.DEFAULT_BUTTON_OK, messageBoxTitle, message);
 
150
    }
 
151
 
 
152
    public static short showQuestionMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
 
153
        if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
 
154
            return 0;
 
155
        }
 
156
 
 
157
        return showMessageBox(parentWindowPeer, "querybox", MessageBoxButtons.BUTTONS_YES_NO_CANCEL + MessageBoxButtons.DEFAULT_BUTTON_YES, messageBoxTitle, message);
 
158
    }
 
159
 
 
160
    public static short showAbortRetryIgnoreErrorMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
 
161
        if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
 
162
            return 0;
 
163
        }
 
164
 
 
165
        return showMessageBox(parentWindowPeer, "errorbox", MessageBoxButtons.BUTTONS_ABORT_IGNORE_RETRY + MessageBoxButtons.DEFAULT_BUTTON_RETRY, messageBoxTitle, message);
 
166
    }
 
167
 
 
168
    public static short showRetryCancelErrorMessageBox(XWindowPeer parentWindowPeer, String messageBoxTitle, String message) {
 
169
        if (parentWindowPeer == null || messageBoxTitle == null || message == null) {
 
170
            return 0;
 
171
        }
 
172
 
 
173
        return showMessageBox(parentWindowPeer, "errorbox", MessageBoxButtons.BUTTONS_RETRY_CANCEL + MessageBoxButtons.DEFAULT_BUTTON_CANCEL, messageBoxTitle, message);
 
174
    }
 
175
}