Embedding in HTML

It is easy to embed commands within HTML using the Swing JEditorPane. This allows standard anchor tags to activate a command.

   CommandHyperlinkListener linkListener = new CommandHyperlinkListener();

   JEditorPane editorPane = new JEditorPane();
   editorPane.setEditorKit(new HTMLEditorKit());
   editorPane.addHyperlinkListener(linkListener);

CommandHyperlinkListeners find their commands using a Command Container. The above example will use the global container, but you can also provide a specific container in the constructor.

When the following anchor tag is displayed in the editor pane clicking on on 'My Command' will activate the command registered with the id of 'my.command.id'.

   <a href='command://my.command.id'>My Command</a>

It is also possible to specifiy parameter values using the following syntax:

   <a href='command://my.command.id?parameterName=parameterValue'>
      My Command
   </a>

   <a href='command://my.command.id?aName=aValue,bName=bValue'>
      My Command
   </a>

The CommandHyperlinkListener has several static methods that read the text and tooltip from the command and build an anchor tag to match. It uses the 'html' face if available, otherwise the default face is used.

   String anchor = CommandHyperlinkListener.buildAnchorString(myCommand);