Skip to content

Commit c8446c6

Browse files
committed
Using an enum for select style
1 parent 42cb67f commit c8446c6

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

src/main/java/io/jenkins/plugins/datatables/TableConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.core.JsonProcessingException;
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9+
import io.jenkins.plugins.datatables.options.SelectStyle;
910

1011
/**
1112
* Provides a configuration for the whole DataTable. This is merged with a default configuration in table.js.
@@ -105,15 +106,15 @@ public boolean isUseButtons() {
105106
/**
106107
* Enable selection.
107108
*
108-
* @param selectionStyle
109-
* The <a href="https://datatables.net/reference/option/select.style">select.style</a> option
109+
* @param selectStyle
110+
* The {@link SelectStyle selection style}
110111
*
111112
* @return this {@link TableConfiguration} for chaining methods
112113
*
113114
* @see <a href="https://datatables.net/reference/option/select">https://datatables.net/reference/option/select</a>
114115
*/
115-
public TableConfiguration select(final String selectionStyle) {
116-
configuration.put("select", selectionStyle);
116+
public TableConfiguration select(final SelectStyle selectStyle) {
117+
configuration.put("select", selectStyle.getStyle());
117118
useSelect = true;
118119
return this;
119120
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.jenkins.plugins.datatables.options;
2+
3+
/**
4+
* The possible values of the <a href="https://datatables.net/reference/option/select.style">select.style</a> option,
5+
* which is used for enabling the selection for datatables and setting its style (the default is {@link #OS}.
6+
*
7+
* @author Florian Orendi
8+
*/
9+
public enum SelectStyle {
10+
11+
/**
12+
* Selection can only be performed via the API.
13+
*/
14+
API("api"),
15+
16+
/**
17+
* Only a single item can be selected, any other selected items will be automatically
18+
* deselected when a new item is selected.
19+
*/
20+
SINGLE("single"),
21+
22+
/**
23+
* Multiple items can be selected. Selection is performed by simply clicking on the items to be selected.
24+
*/
25+
MULTI("multi"),
26+
27+
/**
28+
* Operating System (OS) style selection.
29+
* This is the most comprehensive option and provides complex behaviours such as ctrl/cmd clicking
30+
* to select / deselect individual items, shift clicking to select ranges
31+
* and an unmodified click to select a single item.
32+
*/
33+
OS("os"),
34+
35+
/**
36+
* A hybrid between the {@link #OS} style and {@link #MULTI},
37+
* allowing easy multi-row selection without immediate de-selection when clicking on a row.
38+
*/
39+
MULTI_SHIFT("multi+shift");
40+
41+
private final String style;
42+
43+
SelectStyle(final String style) {
44+
this.style = style;
45+
}
46+
47+
public String getStyle() {
48+
return style;
49+
}
50+
}

src/test/java/io/jenkins/plugins/datatables/TableConfigurationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.jenkins.plugins.datatables;
22

3+
import io.jenkins.plugins.datatables.options.SelectStyle;
34
import org.junit.jupiter.api.Test;
45

56
import static io.jenkins.plugins.datatables.TableConfigurationAssert.*;
@@ -34,7 +35,7 @@ void shouldCreateResponsiveConfiguration() {
3435

3536
@Test
3637
void shouldCreateSelectConfiguration() {
37-
TableConfiguration configuration = new TableConfiguration().select("single");
38+
TableConfiguration configuration = new TableConfiguration().select(SelectStyle.SINGLE);
3839

3940
assertThat(configuration).hasConfiguration("{\"select\":\"single\"}");
4041
assertThat(configuration).isNotUseButtons();

0 commit comments

Comments
 (0)