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