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