tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / odk / examples / DevelopersGuide / GUI / SystemDialog.java
blob62c6e3e03f2db60fd18e38d8602487cc62b91983
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XInitialization;
38 import com.sun.star.lang.XMultiComponentFactory;
39 import com.sun.star.ui.dialogs.XExecutableDialog;
40 import com.sun.star.ui.dialogs.XFilePicker;
41 import com.sun.star.ui.dialogs.XFilePickerControlAccess;
42 import com.sun.star.ui.dialogs.XFilterManager;
43 import com.sun.star.ui.dialogs.XFolderPicker2;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XComponentContext;
46 import com.sun.star.util.thePathSettings;
49 public class SystemDialog {
51 protected XComponentContext m_xContext = null;
52 protected com.sun.star.lang.XMultiComponentFactory m_xMCF;
54 /** Creates a new instance of MessageBox */
55 public SystemDialog(XComponentContext _xContext, XMultiComponentFactory _xMCF){
56 m_xContext = _xContext;
57 m_xMCF = _xMCF;
60 public static void main(String args[]){
61 try {
62 XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
63 if(xContext != null )
64 System.out.println("Connected to a running office ...");
65 XMultiComponentFactory xMCF = xContext.getServiceManager();
66 SystemDialog oSystemDialog = new SystemDialog(xContext, xMCF);
67 oSystemDialog.raiseSaveAsDialog();
68 oSystemDialog.raiseFolderPicker(oSystemDialog.getWorkPath(), "My Title");
69 }catch( Exception e ) {
70 System.err.println( e + e.getMessage());
71 e.printStackTrace();
74 System.exit( 0 );
78 public String raiseSaveAsDialog() {
79 String sStorePath = "";
80 XComponent xComponent = null;
81 try {
82 // the filepicker is instantiated with the global Multicomponentfactory...
83 Object oFilePicker = m_xMCF.createInstanceWithContext("com.sun.star.ui.dialogs.FilePicker", m_xContext);
84 XFilePicker xFilePicker = UnoRuntime.queryInterface(XFilePicker.class, oFilePicker);
86 // the defaultname is the initially proposed filename...
87 xFilePicker.setDefaultName("MyExampleDocument");
89 // set the initial displaydirectory. In this example the user template directory is used
90 Object oPathSettings = thePathSettings.get(m_xContext);
91 XPropertySet xPropertySet = com.sun.star.uno.UnoRuntime.queryInterface(XPropertySet.class, oPathSettings);
92 String sTemplateUrl = (String) xPropertySet.getPropertyValue("Template_writable");
93 xFilePicker.setDisplayDirectory(sTemplateUrl);
95 // set the filters of the dialog. The filternames may be retrieved from
96 // http://wiki.openoffice.org/wiki/Framework/Article/Filter
97 XFilterManager xFilterManager = UnoRuntime.queryInterface(XFilterManager.class, xFilePicker);
98 xFilterManager.appendFilter("OpenDocument Text Template", "writer8_template");
99 xFilterManager.appendFilter("OpenDocument Text", "writer8");
101 // choose the template that defines the capabilities of the filepicker dialog
102 XInitialization xInitialize = UnoRuntime.queryInterface(XInitialization.class, xFilePicker);
103 short[] listAny = new short[] { com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION };
104 xInitialize.initialize(listAny);
106 // add a control to the dialog to add the extension automatically to the filename...
107 XFilePickerControlAccess xFilePickerControlAccess = UnoRuntime.queryInterface(XFilePickerControlAccess.class, xFilePicker);
108 xFilePickerControlAccess.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION, (short) 0, Boolean.TRUE);
110 xComponent = UnoRuntime.queryInterface(XComponent.class, xFilePicker);
112 // execute the dialog...
113 XExecutableDialog xExecutable = UnoRuntime.queryInterface(XExecutableDialog.class, xFilePicker);
114 short nResult = xExecutable.execute();
116 // query the resulting path of the dialog...
117 if (nResult == com.sun.star.ui.dialogs.ExecutableDialogResults.OK){
118 String[] sPathList = xFilePicker.getFiles();
119 if (sPathList.length > 0){
120 sStorePath = sPathList[0];
123 } catch (com.sun.star.uno.Exception exception) {
124 exception.printStackTrace();
125 } finally{
126 //make sure always to dispose the component and free the memory!
127 if (xComponent != null){
128 xComponent.dispose();
131 return sStorePath;
134 public String getWorkPath(){
135 String sWorkUrl = "";
136 try{
137 // retrieve the configured Work path...
138 Object oPathSettings = thePathSettings.get(m_xContext);
139 XPropertySet xPropertySet = com.sun.star.uno.UnoRuntime.queryInterface(XPropertySet.class, oPathSettings);
140 sWorkUrl = (String) xPropertySet.getPropertyValue("Work");
141 } catch (com.sun.star.uno.Exception exception) {
142 exception.printStackTrace();
144 return sWorkUrl;
147 /** raises a folderpicker in which the user can browse and select a path
148 * @param _sDisplayDirectory the path to the directory that is initially displayed
149 * @param _sTitle the title of the folderpicker
150 * @return the path to the folder that the user has selected. if the user has closed
151 * the folderpicker by clicking the "Cancel" button
152 * an empty string is returned
153 * @see com.sun.star.ui.dialogs.FolderPicker
155 public String raiseFolderPicker(String _sDisplayDirectory, String _sTitle) {
156 String sReturnFolder = "";
157 XComponent xComponent = null;
158 try {
159 // instantiate the folder picker and retrieve the necessary interfaces...
160 Object oFolderPicker = m_xMCF.createInstanceWithContext("com.sun.star.ui.dialogs.FolderPicker", m_xContext);
161 XFolderPicker2 xFolderPicker = UnoRuntime.queryInterface(XFolderPicker2.class, oFolderPicker);
162 XExecutableDialog xExecutable = UnoRuntime.queryInterface(XExecutableDialog.class, oFolderPicker);
163 xComponent = UnoRuntime.queryInterface(XComponent.class, oFolderPicker);
164 xFolderPicker.setDisplayDirectory(_sDisplayDirectory);
165 // set the dialog title...
166 xFolderPicker.setTitle(_sTitle);
167 // show the dialog...
168 short nResult = xExecutable.execute();
170 // User has clicked "Select" button...
171 if (nResult == com.sun.star.ui.dialogs.ExecutableDialogResults.OK){
172 sReturnFolder = xFolderPicker.getDirectory();
175 }catch( Exception exception ) {
176 exception.printStackTrace(System.err);
177 } finally{
178 //make sure always to dispose the component and free the memory!
179 if (xComponent != null){
180 xComponent.dispose();
183 // return the selected path. If the user has clicked cancel an empty string is
184 return sReturnFolder;
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */