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