Wiki source code of Using custom enum select picker for Wiki Macro parameters
Version 2.1 by Clément Desableau on 2023/06/01
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | There might be cases where you want to display a Wiki Macro parameter as a select picker. However, the enum custom picker can only be called from a java macro, from where you can pass the enum class //object// (which is not possible from a Wiki Macro point of view as you can only specify parameter types with Strings). | ||
2 | |||
3 | Hopefully, it is actually possible to use enums as Wiki Macro parameters, but this require a little bit more work. | ||
4 | |||
5 | Here is, for example, how to implement a custom {{{ Ascending }}} / {{{ Descending }}} select picker for a sort order parameter. | ||
6 | |||
7 | == Step 1: Java enum == | ||
8 | |||
9 | Write your custom java enum: | ||
10 | |||
11 | {{code language="java"}} | ||
12 | package your.enum.package; | ||
13 | |||
14 | public enum SortOrderEnum | ||
15 | { | ||
16 | ASCENDING, | ||
17 | DESCENDING; | ||
18 | } | ||
19 | {{/code}} | ||
20 | |||
21 | 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). | ||
22 | |||
23 | == Step 2: Custom displayer vm == | ||
24 | |||
25 | We need to forward the display of the parameter to the enum property. | ||
26 | |||
27 | In the skin of your instance, add a {{{ html_displayer/SortOrderEnum/edit.vm }}} vm customization with the following code: | ||
28 | |||
29 | {{code language="velocity"}} | ||
30 | #template('html_displayer/default.vm') | ||
31 | {{/code}} | ||
32 | |||
33 | == Step 3: Wiki Macro parameter type == | ||
34 | |||
35 | Simply set your parameter type to {{{ your.enum.package.SortOrderEnum }}} | ||
36 | |||
37 | You should now have your macro parameter picker as a select! | ||
38 | |||
39 | == Step 4 (optional): Translations == | ||
40 | |||
41 | By defaut, enum keys are displayed capitalized in the select, so "ASCENDING" would become "Ascending" (underscores {{{ "_" }}} also become whitespace {{{ " " }}}). | ||
42 | |||
43 | 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: | ||
44 | |||
45 | {{code language="plain"}} | ||
46 | your.enum.package.SortOrderEnum.ASCENDING=Ascending | ||
47 | your.enum.package.SortOrderEnum.DESCENDING=Descending | ||
48 | {{/code}} |