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 package com.sun.star.wizards.common;
24 
25 import com.sun.star.lang.XMultiServiceFactory;
26 import com.sun.star.ui.dialogs.*;
27 import com.sun.star.uno.XInterface;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.AnyConverter;
30 import com.sun.star.util.XStringSubstitution;
31 import com.sun.star.lang.XComponent;
32 import com.sun.star.lang.XInitialization;
33 import com.sun.star.frame.XFrame;
34 import com.sun.star.awt.XWindowPeer;
35 import com.sun.star.awt.XToolkit;
36 import com.sun.star.awt.XMessageBox;
37 import com.sun.star.beans.PropertyValue;
38 
39 public class SystemDialog
40 {
41 
42     Object systemDialog;
43     XFilePicker xFilePicker;
44     XFolderPicker xFolderPicker;
45     XFilterManager xFilterManager;
46     XInitialization xInitialize;
47     XExecutableDialog xExecutable;
48     XComponent xComponent;
49     XFilePickerControlAccess xFilePickerControlAccess;
50     XMultiServiceFactory xMSF;
51     public XStringSubstitution xStringSubstitution;
52     public String sStorePath;
53 
54     /**
55      *
56      * @param xMSF
57      * @param ServiceName
58      * @param type  according to com.sun.star.ui.dialogs.TemplateDescription
59      */
SystemDialog(XMultiServiceFactory xMSF, String ServiceName, short type)60     public SystemDialog(XMultiServiceFactory xMSF, String ServiceName, short type)
61     {
62         try
63         {
64             this.xMSF = xMSF;
65             systemDialog = xMSF.createInstance(ServiceName);
66             xFilePicker = UnoRuntime.queryInterface(XFilePicker.class, systemDialog);
67             xFolderPicker = UnoRuntime.queryInterface(XFolderPicker.class, systemDialog);
68             xFilterManager = UnoRuntime.queryInterface(XFilterManager.class, systemDialog);
69             xInitialize = UnoRuntime.queryInterface(XInitialization.class, systemDialog);
70             xExecutable = UnoRuntime.queryInterface(XExecutableDialog.class, systemDialog);
71             xComponent = UnoRuntime.queryInterface(XComponent.class, systemDialog);
72             xFilePickerControlAccess = UnoRuntime.queryInterface(XFilePickerControlAccess.class, systemDialog);
73             xStringSubstitution = createStringSubstitution(xMSF);
74             Short[] listAny = new Short[]
75             {
76                 new Short(type)
77             };
78             if (xInitialize != null)
79             {
80                 xInitialize.initialize(listAny);
81             }
82         }
83         catch (com.sun.star.uno.Exception exception)
84         {
85             exception.printStackTrace();
86         }
87     }
88 
createStoreDialog(XMultiServiceFactory xmsf)89     public static SystemDialog createStoreDialog(XMultiServiceFactory xmsf)
90     {
91         return new SystemDialog(xmsf, "com.sun.star.ui.dialogs.FilePicker", TemplateDescription.FILESAVE_AUTOEXTENSION);
92     }
93 
createOpenDialog(XMultiServiceFactory xmsf)94     public static SystemDialog createOpenDialog(XMultiServiceFactory xmsf)
95     {
96         return new SystemDialog(xmsf, "com.sun.star.ui.dialogs.FilePicker", TemplateDescription.FILEOPEN_SIMPLE);
97     }
98 
createFolderDialog(XMultiServiceFactory xmsf)99     public static SystemDialog createFolderDialog(XMultiServiceFactory xmsf)
100     {
101         return new SystemDialog(xmsf, "com.sun.star.ui.dialogs.FolderPicker", (short) 0);
102     }
103 
createOfficeFolderDialog(XMultiServiceFactory xmsf)104     public static SystemDialog createOfficeFolderDialog(XMultiServiceFactory xmsf)
105     {
106         return new SystemDialog(xmsf, "com.sun.star.ui.dialogs.OfficeFolderPicker", (short) 0);
107     }
108 
subst(String path)109     private String subst(String path)
110     {
111         try
112         {
113             return xStringSubstitution.substituteVariables(path, false);
114         }
115         catch (Exception ex)
116         {
117             ex.printStackTrace();
118             return path;
119         }
120     }
121 
122     /**
123      * ATTENTION a BUG : The extension calculated
124      * here gives the last 3 chars of the filename - what
125      * if the extension is of 4 or more chars?
126      *
127      * @param DisplayDirectory
128      * @param DefaultName
129      * @param sDocuType
130      * @return
131      */
callStoreDialog(String DisplayDirectory, String DefaultName, String sDocuType)132     public String callStoreDialog(String DisplayDirectory, String DefaultName, String sDocuType)
133     {
134         String sExtension = DefaultName.substring(DefaultName.length() - 3, DefaultName.length());
135         addFilterToDialog(sExtension, sDocuType, true);
136         return callStoreDialog(DisplayDirectory, DefaultName);
137     }
138 
139     /**
140      *
141      * @param displayDir
142      * @param defaultName
143      * given url to a local path.
144      * @return
145      */
callStoreDialog(String displayDir, String defaultName)146     public String callStoreDialog(String displayDir, String defaultName)
147     {
148         sStorePath = null;
149         try
150         {
151             xFilePickerControlAccess.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION, (short) 0, Boolean.TRUE);
152             xFilePicker.setDefaultName(defaultName);
153             xFilePicker.setDisplayDirectory(subst(displayDir));
154             if (execute(xExecutable))
155             {
156                 String[] sPathList = xFilePicker.getFiles();
157                 sStorePath = sPathList[0];
158             }
159         }
160         catch (com.sun.star.uno.Exception exception)
161         {
162             exception.printStackTrace();
163         }
164         return sStorePath;
165     }
166 
callFolderDialog(String title, String description, String displayDir)167     public String callFolderDialog(String title, String description, String displayDir)
168     {
169         try
170         {
171             xFolderPicker.setDisplayDirectory(subst(displayDir));
172         }
173         catch (com.sun.star.lang.IllegalArgumentException iae)
174         {
175             iae.printStackTrace();
176             throw new IllegalArgumentException(iae.getMessage());
177         }
178         xFolderPicker.setTitle(title);
179         xFolderPicker.setDescription(description);
180         if (execute(xFolderPicker))
181         {
182             return xFolderPicker.getDirectory();
183         }
184         else
185         {
186             return null;
187         }
188     }
189 
execute(XExecutableDialog execDialog)190     private boolean execute(XExecutableDialog execDialog)
191     {
192         return execDialog.execute() == 1;
193     }
194 
callOpenDialog(boolean multiSelect, String displayDirectory)195     public String[] callOpenDialog(boolean multiSelect, String displayDirectory)
196     {
197 
198         try
199         {
200             xFilePicker.setMultiSelectionMode(multiSelect);
201             xFilePicker.setDisplayDirectory(subst(displayDirectory));
202             if (execute(xExecutable))
203             {
204                 return xFilePicker.getFiles();
205             }
206         }
207         catch (com.sun.star.uno.Exception exception)
208         {
209             exception.printStackTrace();
210         }
211         return null;
212     }
213 
214     //("writer_StarOffice_XML_Writer_Template")    'StarOffice XML (Writer)
addFilterToDialog(String sExtension, String filterName, boolean setToDefault)215     public void addFilterToDialog(String sExtension, String filterName, boolean setToDefault)
216     {
217         try
218         {
219             //get the localized filtername
220             String uiName = getFilterUIName(filterName);
221             String pattern = "*." + sExtension;
222 
223             //add the filter
224             addFilter(uiName, pattern, setToDefault);
225         }
226         catch (Exception exception)
227         {
228             exception.printStackTrace(System.out);
229         }
230     }
231 
addFilter(String uiName, String pattern, boolean setToDefault)232     public void addFilter(String uiName, String pattern, boolean setToDefault)
233     {
234         try
235         {
236             xFilterManager.appendFilter(uiName, pattern);
237             if (setToDefault)
238             {
239                 xFilterManager.setCurrentFilter(uiName);
240             }
241         }
242         catch (Exception ex)
243         {
244             ex.printStackTrace();
245         }
246     }
247 
248     /**
249      * converts the name returned from getFilterUIName_(...) so the
250      * product name is correct.
251      * @param filterName
252      * @return
253      */
getFilterUIName(String filterName)254     private String getFilterUIName(String filterName)
255     {
256         String prodName = Configuration.getProductName(xMSF);
257         String[][] s = new String[][]
258         {
259             {
260                 getFilterUIName_(filterName)
261             }
262         };
263         s[0][0] = JavaTools.replaceSubString(s[0][0], prodName, "%productname%");
264         return s[0][0];
265     }
266 
267     /**
268      * note the result should go through conversion of the product name.
269      * @param filterName
270      * @return the UI localized name of the given filter name.
271      */
getFilterUIName_(String filterName)272     private String getFilterUIName_(String filterName)
273     {
274         try
275         {
276             Object oFactory = xMSF.createInstance("com.sun.star.document.FilterFactory");
277             Object oObject = Helper.getUnoObjectbyName(oFactory, filterName);
278             Object oArrayObject = AnyConverter.toArray(oObject);
279             PropertyValue[] xPropertyValue = (PropertyValue[]) oArrayObject; //UnoRuntime.queryInterface(XPropertyValue.class, oObject);
280             int MaxCount = xPropertyValue.length;
281             for (int i = 0; i < MaxCount; i++)
282             {
283                 PropertyValue aValue = xPropertyValue[i];
284                 if (aValue != null && aValue.Name.equals("UIName"))
285                 {
286                     return AnyConverter.toString(aValue.Value);
287                 }
288             }
289             throw new NullPointerException("UIName property not found for Filter " + filterName);
290         }
291         catch (com.sun.star.uno.Exception exception)
292         {
293             exception.printStackTrace(System.out);
294             return null;
295         }
296     }
297 
showErrorBox(XMultiServiceFactory xMSF, String ResName, String ResPrefix, int ResID, String AddTag, String AddString)298     public static int showErrorBox(XMultiServiceFactory xMSF, String ResName, String ResPrefix, int ResID, String AddTag, String AddString)
299     {
300         Resource oResource;
301         String ProductName = Configuration.getProductName(xMSF);
302         oResource = new Resource(xMSF, ResName, ResPrefix);
303         String sErrorMessage = oResource.getResText(ResID);
304         sErrorMessage = JavaTools.replaceSubString(sErrorMessage, ProductName, "%PRODUCTNAME");
305         sErrorMessage = JavaTools.replaceSubString(sErrorMessage, String.valueOf((char) 13), "<BR>");
306         sErrorMessage = JavaTools.replaceSubString(sErrorMessage, AddString, AddTag);
307         return SystemDialog.showMessageBox(xMSF, "ErrorBox", com.sun.star.awt.VclWindowPeerAttribute.OK, sErrorMessage);
308     }
309 
showErrorBox(XMultiServiceFactory xMSF, String ResName, String ResPrefix, int ResID)310     public static int showErrorBox(XMultiServiceFactory xMSF, String ResName, String ResPrefix, int ResID)
311     {
312         Resource oResource;
313         String ProductName = Configuration.getProductName(xMSF);
314         oResource = new Resource(xMSF, ResName, ResPrefix);
315         String sErrorMessage = oResource.getResText(ResID);
316         sErrorMessage = JavaTools.replaceSubString(sErrorMessage, ProductName, "%PRODUCTNAME");
317         sErrorMessage = JavaTools.replaceSubString(sErrorMessage, String.valueOf((char) 13), "<BR>");
318         return showMessageBox(xMSF, "ErrorBox", com.sun.star.awt.VclWindowPeerAttribute.OK, sErrorMessage);
319     }
320 
321     /*
322      * example:
323      * (xMSF, "ErrorBox", com.sun.star.awt.VclWindowPeerAttribute.OK, "message")
324      */
325     /**
326      * @param windowServiceName one of the following strings:
327      * "ErrorBox", "WarningBox", "MessBox", "InfoBox", "QueryBox".
328      * There are other values possible, look
329      * under src/toolkit/source/awt/vcltoolkit.cxx
330      * @param windowAttribute see com.sun.star.awt.VclWindowPeerAttribute
331      * @return 0 = cancel, 1 = ok, 2 = yes,  3 = no(I'm not sure here)
332      * other values check for yourself ;-)
333      */
showMessageBox(XMultiServiceFactory xMSF, String windowServiceName, int windowAttribute, String MessageText)334     public static int showMessageBox(XMultiServiceFactory xMSF, String windowServiceName, int windowAttribute, String MessageText)
335     {
336 
337         short iMessage = 0;
338         try
339         {
340             if (MessageText == null)
341             {
342                 return 0;
343             }
344             XFrame xFrame = Desktop.getActiveFrame(xMSF);
345             XWindowPeer xWindowPeer = UnoRuntime.queryInterface(XWindowPeer.class, xFrame.getComponentWindow());
346             return showMessageBox(xMSF, xWindowPeer, windowServiceName, windowAttribute, MessageText);
347         }
348         catch (Exception exception)
349         {
350             exception.printStackTrace(System.out);
351         }
352         return iMessage;
353     }
354 
355     /**
356      * just like the other showMessageBox(...) method, but receives a
357      * peer argument to use to create the message box.
358      * @param xMSF
359      * @param peer
360      * @param windowServiceName
361      * @param windowAttribute
362      * @param MessageText
363      * @return
364      */
showMessageBox(XMultiServiceFactory xMSF, XWindowPeer peer, String windowServiceName, int windowAttribute, String MessageText)365     public static int showMessageBox(XMultiServiceFactory xMSF, XWindowPeer peer, String windowServiceName, int windowAttribute, String MessageText)
366     {
367         // If the peer is null we try to get one from the desktop...
368         if (peer == null)
369         {
370             return showMessageBox(xMSF, windowServiceName, windowAttribute, MessageText);
371         }
372         short iMessage = 0;
373         try
374         {
375             XInterface xAWTToolkit = (XInterface) xMSF.createInstance("com.sun.star.awt.Toolkit");
376             XToolkit xToolkit = UnoRuntime.queryInterface(XToolkit.class, xAWTToolkit);
377             com.sun.star.awt.WindowDescriptor oDescriptor = new com.sun.star.awt.WindowDescriptor();
378             oDescriptor.WindowServiceName = windowServiceName;
379             oDescriptor.Parent = peer;
380             oDescriptor.Type = com.sun.star.awt.WindowClass.MODALTOP;
381             oDescriptor.WindowAttributes = windowAttribute;
382             XWindowPeer xMsgPeer = xToolkit.createWindow(oDescriptor);
383             XMessageBox xMsgbox = UnoRuntime.queryInterface(XMessageBox.class, xMsgPeer);
384             XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, xMsgbox);
385             xMsgbox.setMessageText(MessageText);
386             iMessage = xMsgbox.execute();
387             xComponent.dispose();
388         }
389         catch (Exception e)
390         {
391             // TODO Auto-generated catch block
392             e.printStackTrace(System.out);
393         }
394         return iMessage;
395     }
396 
createStringSubstitution(XMultiServiceFactory xMSF)397     public static XStringSubstitution createStringSubstitution(XMultiServiceFactory xMSF)
398     {
399         Object xPathSubst = null;
400         try
401         {
402             xPathSubst = xMSF.createInstance(
403                     "com.sun.star.util.PathSubstitution");
404         }
405         catch (com.sun.star.uno.Exception e)
406         {
407             e.printStackTrace();
408         }
409         if (xPathSubst != null)
410         {
411             return UnoRuntime.queryInterface(
412                     XStringSubstitution.class, xPathSubst);
413         }
414         else
415         {
416             return null;
417         }
418     }
419 }
420