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