SQF User Input Dialog

Sometimes, when executing a quick fix action, you need to interact with the user and get input from them, or make them choose between different options. The quick fix specification defines the sqf:user-entry for such interactions. The sqf:user-entry element defines a value that must be set manually by the user.

If you need multiple values to be specified by the user, you can define multiple user-entry elements. For each user-entry, a dialog box will be displayed where the user can specify values. The user-entry element can be used as an XPath variable where the XPath variable is the name of the user-entry.

In the following example, the quick fix action defines two user entries, one that will allow the user to specify the value of the hypertext link, and one the will allow them to specify the link title. When the quick fix will be executed, two dialog boxes will be displayed, one for the link value and a second one for the link title. The order of the dialog boxes is define by the order of the defined user entries in the quick fix.

Example 11. Quick fix action that presents two user entry dialogs

<sqf:fix id="addAnchorAndTitle">
  <sqf:description>
    <sqf:title>Surround image with a hypertext link</sqf:title>
  </sqf:description>
  <sqf:user-entry name="href">
    <sqf:description>
      <sqf:title>Enter anchor href value:</sqf:title>
    </sqf:description>
  </sqf:user-entry>
  <sqf:user-entry name="linkTitle">
    <sqf:description>
      <sqf:title>Enter link title:</sqf:title>
    </sqf:description>
  </sqf:user-entry>
  <sqf:replace>
    <a href="{$href}" title="{$linkTitle}">
      <xsl:copy-of select="."/>
    </a>
  </sqf:replace>
</sqf:fix>

In a quick fix, you can also use you own extensions using programming languages such as Java. This will allow you to present your own custom dialog boxes to get input from the user or make complex processing that is not possible with the current Schematron or XSLT support.

For example, you can invoke the JFileChooser dialog box to allow the user to choose a file from the file system, and then use the file path in the document. To do this, you need to create a quick fix action that will call an XSL template that will present the dialog box and obtain the selected value from the user.

Example 12. Quick fix that displays a browse dialog

<sqf:fix id="addSrc">
  <sqf:description>
    <sqf:title>Browse and add @src attribute</sqf:title>
  </sqf:description>
  <sqf:add node-type="attribute" target="src">
    <xsl:call-template name="browse"/>
  </sqf:add>
</sqf:fix>

To display the dialog box from XSLT, you need to create a new instance using a new() call, and then use the showOpenDialog() to show a dialog box that will allow the user to choose a file. Depending on whether the user decides to cancel the dialog box or choose a file, you can obtain the current selected file using the getSelectedFile() call. The result of the template will be sent to the quick fix action that will inset it in the document.

Example 13. Quick fix that displays a browse dialog

<xsl:template name="browse">
  <xsl:variable name="dialog" select="jBrowse:new()"/>
  <xsl:variable 
    name="result" 
    select="jBrowse:showOpenDialog($dialog, $dialog)"/>
  <xsl:if test="$result = 0">
    <xsl:value-of
     select="jBrowse:getSelectedFile($dialog)"/>
  </xsl:if>
</xsl:template>