Wiki source code of Using custom enum select picker for Wiki Macro parameters
Version 1.3 by Clément Desableau on 2023/06/01
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | There might be cases you want to display a Wiki Macro parameter as a select picker. | ||
2 | However, the enum custom picker can only be called from a java macro, from where you can pass the enum class _object_ (this is not possible from a Wiki Macro point of view as you can only specify parameter types with Strings). | ||
3 | |||
4 | Here is, for example, how to implement a custom {{{ Ascending }}} / {{{ Descending }}} select picker for a sort order parameter. | ||
5 | |||
6 | * Step 1: The java enum | ||
7 | |||
8 | Write your custom java enum: | ||
9 | |||
10 | {{code language="java"}} | ||
11 | package your.enum.package; | ||
12 | |||
13 | public enum SortOrderEnum | ||
14 | { | ||
15 | ASCENDING, | ||
16 | DESCENDING; | ||
17 | } | ||
18 | {{/code}} | ||
19 | |||
20 | Then bundle it as a jar, and import it in your instance (by manually placing it in the {{{ WEB-INF/lib }}} directory, or using the extension manager). | ||
21 | |||
22 | * Step 2: The displayer vm | ||
23 | |||
24 | We need to forward the display of the parameter to the enum property. | ||
25 | |||
26 | In the skin of your instance, add a {{{ html_displayer/SortOrderEnum/edit.vm }}} vm customization with the following code: | ||
27 | |||
28 | {{code language="velocity"}} | ||
29 | #template('html_displayer/default.vm') | ||
30 | {{/code}} | ||
31 | |||
32 | * Step 3: The parameter type | ||
33 | |||
34 | Simply set your parameter type to {{{ your.enum.package.SortOrderEnum }}}. | ||
35 | You should now have your macro parameter display as a select in edit mode. | ||
36 | |||
37 | * Step 4 (optional): Translations | ||
38 | |||
39 | By defaut, enum keys are displayed capitalized in the select, so "ASCENDING" would become "Ascending" (underscores {{{ "_" }}} also become whitespace {{{ " " }}}). | ||
40 | |||
41 | In case you need the enum keys to be translated, you can create a page with a {{{ XWiki.TranslationDocumentClass }}} object, where the translation keys are your enum full name concatenated with the enum value: | ||
42 | |||
43 | {{code language="plain"}} | ||
44 | your.enum.package.SortOrderEnum.ASCENDING=Ascending | ||
45 | your.enum.package.SortOrderEnum.DESCENDING=Descending | ||
46 | {{/code}} |