1*34dd1e25SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 24cdf0e10cSrcweir import com.sun.star.lang.XComponent; 25cdf0e10cSrcweir import com.sun.star.lang.XInitialization; 26cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 27cdf0e10cSrcweir import com.sun.star.ui.dialogs.XExecutableDialog; 28cdf0e10cSrcweir import com.sun.star.ui.dialogs.XFilePicker; 29cdf0e10cSrcweir import com.sun.star.ui.dialogs.XFilePickerControlAccess; 30cdf0e10cSrcweir import com.sun.star.ui.dialogs.XFilterManager; 31cdf0e10cSrcweir import com.sun.star.ui.dialogs.XFolderPicker; 32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 33cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 34cdf0e10cSrcweir 35cdf0e10cSrcweir 36cdf0e10cSrcweir 37cdf0e10cSrcweir public class SystemDialog { 38cdf0e10cSrcweir 39cdf0e10cSrcweir protected XComponentContext m_xContext = null; 40cdf0e10cSrcweir protected com.sun.star.lang.XMultiComponentFactory m_xMCF; 41cdf0e10cSrcweir 42cdf0e10cSrcweir /** Creates a new instance of MessageBox */ SystemDialog(XComponentContext _xContext, XMultiComponentFactory _xMCF)43cdf0e10cSrcweir public SystemDialog(XComponentContext _xContext, XMultiComponentFactory _xMCF){ 44cdf0e10cSrcweir m_xContext = _xContext; 45cdf0e10cSrcweir m_xMCF = _xMCF; 46cdf0e10cSrcweir } 47cdf0e10cSrcweir main(String args[])48cdf0e10cSrcweir public static void main(String args[]){ 49cdf0e10cSrcweir try { 50cdf0e10cSrcweir XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 51cdf0e10cSrcweir if(xContext != null ) 52cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 53cdf0e10cSrcweir XMultiComponentFactory xMCF = xContext.getServiceManager(); 54cdf0e10cSrcweir SystemDialog oSystemDialog = new SystemDialog(xContext, xMCF); 55cdf0e10cSrcweir oSystemDialog.raiseSaveAsDialog(); 56cdf0e10cSrcweir oSystemDialog.raiseFolderPicker(oSystemDialog.getWorkPath(), "My Title"); 57cdf0e10cSrcweir }catch( Exception e ) { 58cdf0e10cSrcweir System.err.println( e + e.getMessage()); 59cdf0e10cSrcweir e.printStackTrace(); 60cdf0e10cSrcweir } 61cdf0e10cSrcweir 62cdf0e10cSrcweir System.exit( 0 ); 63cdf0e10cSrcweir } 64cdf0e10cSrcweir 65cdf0e10cSrcweir raiseSaveAsDialog()66cdf0e10cSrcweir public String raiseSaveAsDialog() { 67cdf0e10cSrcweir String sStorePath = ""; 68cdf0e10cSrcweir XComponent xComponent = null; 69cdf0e10cSrcweir try { 70cdf0e10cSrcweir // the filepicker is instantiated with the global Multicomponentfactory... 71cdf0e10cSrcweir Object oFilePicker = m_xMCF.createInstanceWithContext("com.sun.star.ui.dialogs.FilePicker", m_xContext); 72cdf0e10cSrcweir XFilePicker xFilePicker = (XFilePicker) UnoRuntime.queryInterface(XFilePicker.class, oFilePicker); 73cdf0e10cSrcweir 74cdf0e10cSrcweir // the defaultname is the initially proposed filename.. 75cdf0e10cSrcweir xFilePicker.setDefaultName("MyExampleDocument"); 76cdf0e10cSrcweir 77cdf0e10cSrcweir // set the initial displaydirectory. In this example the user template directory is used 78cdf0e10cSrcweir Object oPathSettings = m_xMCF.createInstanceWithContext("com.sun.star.util.PathSettings",m_xContext); 79cdf0e10cSrcweir XPropertySet xPropertySet = (XPropertySet) com.sun.star.uno.UnoRuntime.queryInterface(XPropertySet.class, oPathSettings); 80cdf0e10cSrcweir String sTemplateUrl = (String) xPropertySet.getPropertyValue("Template_writable"); 81cdf0e10cSrcweir xFilePicker.setDisplayDirectory(sTemplateUrl); 82cdf0e10cSrcweir 83cdf0e10cSrcweir // set the filters of the dialog. The filternames may be retrieved from 84cdf0e10cSrcweir // http://wiki.services.openoffice.org/wiki/Framework/Article/Filter 85cdf0e10cSrcweir XFilterManager xFilterManager = (XFilterManager) UnoRuntime.queryInterface(XFilterManager.class, xFilePicker); 86cdf0e10cSrcweir xFilterManager.appendFilter("OpenDocument Text Template", "writer8_template"); 87cdf0e10cSrcweir xFilterManager.appendFilter("OpenDocument Text", "writer8"); 88cdf0e10cSrcweir 89cdf0e10cSrcweir // choose the template that defines the capabilities of the filepicker dialog 90cdf0e10cSrcweir XInitialization xInitialize = (XInitialization) UnoRuntime.queryInterface(XInitialization.class, xFilePicker); 91cdf0e10cSrcweir Short[] listAny = new Short[] { new Short(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION)}; 92cdf0e10cSrcweir xInitialize.initialize(listAny); 93cdf0e10cSrcweir 94cdf0e10cSrcweir // add a control to the dialog to add the extension automatically to the filename... 95cdf0e10cSrcweir XFilePickerControlAccess xFilePickerControlAccess = (XFilePickerControlAccess) UnoRuntime.queryInterface(XFilePickerControlAccess.class, xFilePicker); 96cdf0e10cSrcweir xFilePickerControlAccess.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION, (short) 0, new Boolean(true)); 97cdf0e10cSrcweir 98cdf0e10cSrcweir xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xFilePicker); 99cdf0e10cSrcweir 100cdf0e10cSrcweir // execute the dialog... 101cdf0e10cSrcweir XExecutableDialog xExecutable = (XExecutableDialog) UnoRuntime.queryInterface(XExecutableDialog.class, xFilePicker); 102cdf0e10cSrcweir short nResult = xExecutable.execute(); 103cdf0e10cSrcweir 104cdf0e10cSrcweir // query the resulting path of the dialog... 105cdf0e10cSrcweir if (nResult == com.sun.star.ui.dialogs.ExecutableDialogResults.OK){ 106cdf0e10cSrcweir String[] sPathList = xFilePicker.getFiles(); 107cdf0e10cSrcweir if (sPathList.length > 0){ 108cdf0e10cSrcweir sStorePath = sPathList[0]; 109cdf0e10cSrcweir } 110cdf0e10cSrcweir } 111cdf0e10cSrcweir } catch (com.sun.star.uno.Exception exception) { 112cdf0e10cSrcweir exception.printStackTrace(); 113cdf0e10cSrcweir } finally{ 114cdf0e10cSrcweir //make sure always to dispose the component and free the memory! 115cdf0e10cSrcweir if (xComponent != null){ 116cdf0e10cSrcweir xComponent.dispose(); 117cdf0e10cSrcweir } 118cdf0e10cSrcweir } 119cdf0e10cSrcweir return sStorePath; 120cdf0e10cSrcweir } 121cdf0e10cSrcweir getWorkPath()122cdf0e10cSrcweir public String getWorkPath(){ 123cdf0e10cSrcweir String sWorkUrl = ""; 124cdf0e10cSrcweir try{ 125cdf0e10cSrcweir // retrieve the configured Work path... 126cdf0e10cSrcweir Object oPathSettings = m_xMCF.createInstanceWithContext("com.sun.star.util.PathSettings",m_xContext); 127cdf0e10cSrcweir XPropertySet xPropertySet = (XPropertySet) com.sun.star.uno.UnoRuntime.queryInterface(XPropertySet.class, oPathSettings); 128cdf0e10cSrcweir sWorkUrl = (String) xPropertySet.getPropertyValue("Work"); 129cdf0e10cSrcweir } catch (com.sun.star.uno.Exception exception) { 130cdf0e10cSrcweir exception.printStackTrace(); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir return sWorkUrl; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir /** raises a folderpicker in which the user can browse and select a path 136cdf0e10cSrcweir * @param _sDisplayDirectory the path to the directory that is initially displayed 137cdf0e10cSrcweir * @param _sTitle the title of the folderpicker 138cdf0e10cSrcweir * @return the path to the folder that the user has selected. if the user has closed 139cdf0e10cSrcweir * the folderpicker by clicking the "Cancel" button 140cdf0e10cSrcweir * an empty string is returned 141cdf0e10cSrcweir * @see com.sun.star.ui.dialogs.FolderPicker 142cdf0e10cSrcweir */ raiseFolderPicker(String _sDisplayDirectory, String _sTitle)143cdf0e10cSrcweir public String raiseFolderPicker(String _sDisplayDirectory, String _sTitle) { 144cdf0e10cSrcweir String sReturnFolder = ""; 145cdf0e10cSrcweir XComponent xComponent = null; 146cdf0e10cSrcweir try { 147cdf0e10cSrcweir // instantiate the folder picker and retrieve the necessary interfaces... 148cdf0e10cSrcweir Object oFolderPicker = m_xMCF.createInstanceWithContext("com.sun.star.ui.dialogs.FolderPicker", m_xContext); 149cdf0e10cSrcweir XFolderPicker xFolderPicker = (XFolderPicker) UnoRuntime.queryInterface(XFolderPicker.class, oFolderPicker); 150cdf0e10cSrcweir XExecutableDialog xExecutable = (XExecutableDialog) UnoRuntime.queryInterface(XExecutableDialog.class, oFolderPicker); 151cdf0e10cSrcweir xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, oFolderPicker); 152cdf0e10cSrcweir xFolderPicker.setDisplayDirectory(_sDisplayDirectory); 153cdf0e10cSrcweir // set the dialog title... 154cdf0e10cSrcweir xFolderPicker.setTitle(_sTitle); 155cdf0e10cSrcweir // show the dialog... 156cdf0e10cSrcweir short nResult = xExecutable.execute(); 157cdf0e10cSrcweir 158cdf0e10cSrcweir // User has clicked "Select" button... 159cdf0e10cSrcweir if (nResult == com.sun.star.ui.dialogs.ExecutableDialogResults.OK){ 160cdf0e10cSrcweir sReturnFolder = xFolderPicker.getDirectory(); 161cdf0e10cSrcweir } 162cdf0e10cSrcweir 163cdf0e10cSrcweir }catch( Exception exception ) { 164cdf0e10cSrcweir exception.printStackTrace(System.out); 165cdf0e10cSrcweir } finally{ 166cdf0e10cSrcweir //make sure always to dispose the component and free the memory! 167cdf0e10cSrcweir if (xComponent != null){ 168cdf0e10cSrcweir xComponent.dispose(); 169cdf0e10cSrcweir } 170cdf0e10cSrcweir } 171cdf0e10cSrcweir // return the selected path. If the user has clicked cancel an empty string is 172cdf0e10cSrcweir return sReturnFolder; 173cdf0e10cSrcweir } 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176