ShowDialog.bsh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * This file is part of the LibreOffice project.
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. *
  8. * This file incorporates work covered by the following license notice:
  9. *
  10. * Licensed to the Apache Software Foundation (ASF) under one or more
  11. * contributor license agreements. See the NOTICE file distributed
  12. * with this work for additional information regarding copyright
  13. * ownership. The ASF licenses this file to you under the Apache
  14. * License, Version 2.0 (the "License"); you may not use this file
  15. * except in compliance with the License. You may obtain a copy of
  16. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  17. */
  18. // this script serves as an example of how to launch a Basic Dialog
  19. // from a script
  20. import com.sun.star.uno.UnoRuntime;
  21. import com.sun.star.script.provider.XScriptContext;
  22. import com.sun.star.lang.XMultiComponentFactory;
  23. import com.sun.star.lang.EventObject;
  24. import com.sun.star.uno.Type;
  25. import com.sun.star.uno.AnyConverter;
  26. import com.sun.star.text.XTextDocument;
  27. import com.sun.star.beans.PropertyValue;
  28. import com.sun.star.script.XLibraryContainer;
  29. import com.sun.star.awt.*;
  30. import com.sun.star.util.*;
  31. boolean tryLoadingLibrary( xmcf, context, name )
  32. {
  33. try
  34. {
  35. obj = xmcf.createInstanceWithContext(
  36. "com.sun.star.script.Application" + name + "LibraryContainer",
  37. context.getComponentContext());
  38. xLibraryContainer = (XLibraryContainer)
  39. UnoRuntime.queryInterface(XLibraryContainer.class, obj);
  40. System.err.println("Got XLibraryContainer");
  41. serviceObj = context.getComponentContext().getValueByName(
  42. "/singletons/com.sun.star.util.theMacroExpander");
  43. xme = (XMacroExpander) AnyConverter.toObject(
  44. new Type(XMacroExpander.class), serviceObj);
  45. bootstrapName = "bootstraprc";
  46. if (System.getProperty("os.name").startsWith("Windows"))
  47. {
  48. bootstrapName = "bootstrap.ini";
  49. }
  50. libURL = xme.expandMacros(
  51. "$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/basic/ScriptBindingLibrary/" +
  52. name.toLowerCase() + ".xlb/");
  53. System.err.println("libURL is: " + libURL);
  54. xLibraryContainer.createLibraryLink(
  55. "ScriptBindingLibrary", libURL, false);
  56. System.err.println("liblink created");
  57. }
  58. catch (com.sun.star.uno.Exception e)
  59. {
  60. System.err.println("Got an exception loading lib: " + e.getMessage());
  61. return false;
  62. }
  63. return true;
  64. }
  65. // get the XMultiComponentFactory from the XSCRIPTCONTEXT
  66. XMultiComponentFactory xmcf =
  67. XSCRIPTCONTEXT.getComponentContext().getServiceManager();
  68. Object[] args = new Object[1];
  69. args[0] = XSCRIPTCONTEXT.getDocument();
  70. Object obj;
  71. try {
  72. // try to create an instance of the DialogProvider
  73. obj = xmcf.createInstanceWithArgumentsAndContext(
  74. "com.sun.star.awt.DialogProvider", args,
  75. XSCRIPTCONTEXT.getComponentContext());
  76. /*
  77. obj = xmcf.createInstanceWithContext(
  78. "com.sun.star.awt.DialogProvider",
  79. XSCRIPTCONTEXT.getComponentContext());
  80. */
  81. }
  82. catch (com.sun.star.uno.Exception e) {
  83. System.err.println("Error getting DialogProvider object");
  84. return 0;
  85. }
  86. // get the XDialogProvider interface from the object created above
  87. XDialogProvider xDialogProvider = (XDialogProvider)
  88. UnoRuntime.queryInterface(XDialogProvider.class, obj);
  89. System.err.println("Got DialogProvider, now get dialog");
  90. try {
  91. // try to create the Highlight dialog (found in the ScriptBindingLibrary)
  92. findDialog = xDialogProvider.createDialog("vnd.sun.star.script:" +
  93. "ScriptBindingLibrary.Highlight?location=application");
  94. if( findDialog == null )
  95. {
  96. if (tryLoadingLibrary(xmcf, XSCRIPTCONTEXT, "Dialog") == false ||
  97. tryLoadingLibrary(xmcf, XSCRIPTCONTEXT, "Script") == false)
  98. {
  99. System.err.println("Error loading ScriptBindingLibrary");
  100. return 0;
  101. }
  102. else
  103. {
  104. // try to create the Highlight dialog (found in the ScriptBindingLibrary)
  105. findDialog = xDialogProvider.createDialog("vnd.sun.star.script:" +
  106. "ScriptBindingLibrary.Highlight?location=application");
  107. }
  108. }
  109. }
  110. catch (java.lang.Exception e) {
  111. System.err.println("Got exception on first creating dialog: " +
  112. e.getMessage());
  113. }
  114. // execute the dialog in a new thread (so that this script can finish)
  115. Thread t = new Thread() {
  116. public void run() {
  117. findDialog.execute();
  118. }
  119. };
  120. t.start();
  121. return 0;