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