[Solved] How to handle Calc Save As Dialog (Filters + Pwd)

The Application Programming Interface and the OASIS Open Document Format

[Solved] How to handle Calc Save As Dialog (Filters + Pwd)

Postby bcris » Thu Sep 15, 2011 10:51 am

I use the following code to open a "Save As" Dialog programmatically:

Code: Select all   Expand viewCollapse view
   public static String raiseSaveAsDialog(final XComponentContext xContext) {
      String sStorePath = "";
      XComponent xComponent = null;
      try {
         XMultiComponentFactory xMCF = xContext.getServiceManager();
         Object oFilePicker = xMCF.createInstanceWithContext("com.sun.star.ui.dialogs.FilePicker", xContext);
         XFilePicker xFilePicker = (XFilePicker) UnoRuntime.queryInterface(XFilePicker.class, oFilePicker);

         xFilePicker.setDefaultName("");

         Object oPathSettings = xMCF.createInstanceWithContext("com.sun.star.util.PathSettings", xContext);
         XPropertySet xPropertySet = (XPropertySet) com.sun.star.uno.UnoRuntime.queryInterface(XPropertySet.class, oPathSettings);
         String sTemplateUrl = (String) xPropertySet.getPropertyValue("Work");
         xFilePicker.setDisplayDirectory(sTemplateUrl);

         XInitialization xInitialize = (XInitialization) UnoRuntime.queryInterface(XInitialization.class, oFilePicker);
         Short[] listAny = new Short[] {new Short(com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION_PASSWORD) };
         xInitialize.initialize(listAny);

         XFilePickerControlAccess xFilePickerControlAccess = (XFilePickerControlAccess) UnoRuntime.queryInterface(XFilePickerControlAccess.class, oFilePicker);
         xFilePickerControlAccess.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION, (short) 0, new Boolean(true));

         xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xFilePicker);

         XFilterManager xFilterManager = (XFilterManager) UnoRuntime.queryInterface(XFilterManager.class, oFilePicker);
         xFilterManager.appendFilter("OpenOffice.org 1.0 Spreadsheet Template", "*.odt");
         xFilterManager.appendFilter("OpenOffice.org 1.0 Spreadsheet", "*.ods");
         xFilterManager.setCurrentFilter("OpenOffice.org 1.0 Spreadsheet");         
         
         XExecutableDialog xExecutable = (XExecutableDialog) UnoRuntime.queryInterface(XExecutableDialog.class, oFilePicker);
         short nResult = xExecutable.execute();

         if (nResult == com.sun.star.ui.dialogs.ExecutableDialogResults.OK) {
            String[] sPathList = xFilePicker.getFiles();
            if (sPathList.length > 0) {
               sStorePath = sPathList[0];
            }
         }

      } catch (com.sun.star.uno.Exception exception) {
         exception.printStackTrace();
      } finally {
         if (xComponent != null) {
            xComponent.dispose();
         }
      }

      return sStorePath;
   }


My questions are:

1. In the above code there are 2 hardcoded filters for "ods" and "odt". How can I dynamically retrieve the list of all available filters for Calc (so those filters which are normally displayed by the standard Save As functionality in Calc) ?

Until now I found the following code but this returns a list of all filters available in Open Office, so writer etc included, not only those which are Calc specific:

Code: Select all   Expand viewCollapse view
         XMultiComponentFactory m_xMCF = xContext.getServiceManager();
                        Object factory =  m_xMCF.createInstanceWithContext("com.sun.star.document.FilterFactory", xContext);

         XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, factory);

                        String[] filterNames = xNameAccess.getElementNames();


Is there a way to retrieve only the Calc specific filters ?


2. I use the FILESAVE_AUTOEXTENSION_PASSWORD template description and therefore a checkbox is displayed in the Save As dialog for specifying if I want to save the file with a password. With the normal Save As functionality from Calc, if that checkbox is checked, a new dialog apeears asking the user to give a password. With the code above nothing happens. Is there a way to activate that additional dialog if the password checkbox is checked ? Or maybe there is a way to find out in code, after executing the dialog, if that checkbox was checked ?


The Code should work in Open Office 3.2 and 3.3.

Thanks in advance !
Last edited by Hagar Delest on Sun Oct 02, 2011 8:59 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.3 on Windows XP SP3
bcris
 
Posts: 21
Joined: Wed Aug 17, 2011 3:51 pm

Re: Programmatically handle Calc Save As Dialog (Filters + P

Postby B Marcelly » Thu Sep 15, 2011 11:57 am

Hi,
A much simpler solution: run the dispatcher with command ".uno:SaveAs"
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Programmatically handle Calc Save As Dialog (Filters + P

Postby bcris » Thu Sep 15, 2011 1:19 pm

Thank you for your reply !

One problem I have is that I need some additional processing before and after the actual file saving. For example after saving the file I need the file's path and name. Is it possible to retrieve these infos after dispatching the ".uno:SaveAs" command and the standard Save As functionality from Open Office is completed ?

Another problem is that I also need some additional processing between the showing of the Save as dialog and the actual saving: if the user chooses an already existing file (and wants to overwrite it) than an error message should appear informing the user that this is not accepted (a requirement of the OOo extension I'm working on). Is it possible to request this constraint when dispathcing the command ".uno:SaveAs" so that Calc itself checks this and doesn't allow the overwriting of existing files ?

Thanks in advance !
OpenOffice 3.3 on Windows XP SP3
bcris
 
Posts: 21
Joined: Wed Aug 17, 2011 3:51 pm

Re: Programmatically handle Calc Save As Dialog (Filters + P

Postby B Marcelly » Thu Sep 15, 2011 2:24 pm

For the first question : use method getURL from the document model.
For the second question I don't know if there is an argument for this.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Programmatically handle Calc Save As Dialog (Filters + P

Postby bcris » Sun Oct 02, 2011 6:54 pm

Thank you for your help. I implemented it by dispatching the command ".uno:SaveAs" as you suggested.

Best regards !
OpenOffice 3.3 on Windows XP SP3
bcris
 
Posts: 21
Joined: Wed Aug 17, 2011 3:51 pm


Return to UNO API and ODF

Who is online

Users browsing this forum: No registered users and 2 guests