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