1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
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
.XFolderPicker2
;
43 import com
.sun
.star
.uno
.UnoRuntime
;
44 import com
.sun
.star
.uno
.XComponentContext
;
45 import com
.sun
.star
.util
.thePathSettings
;
48 public class SystemDialog
{
50 protected XComponentContext m_xContext
= null;
51 protected com
.sun
.star
.lang
.XMultiComponentFactory m_xMCF
;
53 /** Creates a new instance of MessageBox */
54 public SystemDialog(XComponentContext _xContext
, XMultiComponentFactory _xMCF
){
55 m_xContext
= _xContext
;
59 public static void main(String args
[]){
61 XComponentContext xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
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());
77 public String
raiseSaveAsDialog() {
78 String sStorePath
= "";
79 XComponent xComponent
= null;
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
= UnoRuntime
.queryInterface(XFilePicker
.class, oFilePicker
);
85 // the defaultname is the initially proposed filename..
86 xFilePicker
.setDefaultName("MyExampleDocument");
88 // set the initial displaydirectory. In this example the user template directory is used
89 Object oPathSettings
= thePathSettings
.get(m_xContext
);
90 XPropertySet xPropertySet
= com
.sun
.star
.uno
.UnoRuntime
.queryInterface(XPropertySet
.class, oPathSettings
);
91 String sTemplateUrl
= (String
) xPropertySet
.getPropertyValue("Template_writable");
92 xFilePicker
.setDisplayDirectory(sTemplateUrl
);
94 // set the filters of the dialog. The filternames may be retrieved from
95 // http://wiki.openoffice.org/wiki/Framework/Article/Filter
96 XFilterManager xFilterManager
= UnoRuntime
.queryInterface(XFilterManager
.class, xFilePicker
);
97 xFilterManager
.appendFilter("OpenDocument Text Template", "writer8_template");
98 xFilterManager
.appendFilter("OpenDocument Text", "writer8");
100 // choose the template that defines the capabilities of the filepicker dialog
101 XInitialization xInitialize
= UnoRuntime
.queryInterface(XInitialization
.class, xFilePicker
);
102 Short
[] listAny
= new Short
[] { Short
.valueOf(com
.sun
.star
.ui
.dialogs
.TemplateDescription
.FILESAVE_AUTOEXTENSION
)};
103 xInitialize
.initialize(listAny
);
105 // add a control to the dialog to add the extension automatically to the filename...
106 XFilePickerControlAccess xFilePickerControlAccess
= UnoRuntime
.queryInterface(XFilePickerControlAccess
.class, xFilePicker
);
107 xFilePickerControlAccess
.setValue(com
.sun
.star
.ui
.dialogs
.ExtendedFilePickerElementIds
.CHECKBOX_AUTOEXTENSION
, (short) 0, Boolean
.TRUE
);
109 xComponent
= UnoRuntime
.queryInterface(XComponent
.class, xFilePicker
);
111 // execute the dialog...
112 XExecutableDialog xExecutable
= UnoRuntime
.queryInterface(XExecutableDialog
.class, xFilePicker
);
113 short nResult
= xExecutable
.execute();
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];
122 } catch (com
.sun
.star
.uno
.Exception exception
) {
123 exception
.printStackTrace();
125 //make sure always to dispose the component and free the memory!
126 if (xComponent
!= null){
127 xComponent
.dispose();
133 public String
getWorkPath(){
134 String sWorkUrl
= "";
136 // retrieve the configured Work path...
137 Object oPathSettings
= thePathSettings
.get(m_xContext
);
138 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();
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
154 public String
raiseFolderPicker(String _sDisplayDirectory
, String _sTitle
) {
155 String sReturnFolder
= "";
156 XComponent xComponent
= null;
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 XFolderPicker2 xFolderPicker
= UnoRuntime
.queryInterface(XFolderPicker2
.class, oFolderPicker
);
161 XExecutableDialog xExecutable
= UnoRuntime
.queryInterface(XExecutableDialog
.class, oFolderPicker
);
162 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();
169 // User has clicked "Select" button...
170 if (nResult
== com
.sun
.star
.ui
.dialogs
.ExecutableDialogResults
.OK
){
171 sReturnFolder
= xFolderPicker
.getDirectory();
174 }catch( Exception exception
) {
175 exception
.printStackTrace(System
.err
);
177 //make sure always to dispose the component and free the memory!
178 if (xComponent
!= null){
179 xComponent
.dispose();
182 // return the selected path. If the user has clicked cancel an empty string is
183 return sReturnFolder
;