1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.example.demo.bestpractice;
21
22 import javax.enterprise.context.SessionScoped;
23 import javax.faces.component.UIParameter;
24 import javax.faces.event.ActionEvent;
25 import javax.inject.Named;
26 import java.io.Serializable;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Locale;
31
32 @SessionScoped
33 @Named("customizer")
34 public class ToolBarCustomizer implements Serializable {
35
36 private List<Item> list;
37
38 public ToolBarCustomizer() {
39 resetList();
40 }
41
42 public String resetList() {
43 list = new ArrayList<>(Arrays.asList(
44 new Item("new"),
45 new Item("edit"),
46 new Item("delete")
47 ));
48
49 return null;
50 }
51
52 public void itemUp(final ActionEvent event) {
53 final Item item = (Item) ((UIParameter) event.getComponent().getChildren().get(0)).getValue();
54 final int oldIndex = list.indexOf(item);
55 if (oldIndex > 0) {
56 list.remove(item);
57 list.add(oldIndex - 1, item);
58 }
59 }
60
61 public void itemDown(final ActionEvent event) {
62 final Item item = (Item) ((UIParameter) event.getComponent().getChildren().get(0)).getValue();
63 final int oldIndex = list.indexOf(item);
64 if (oldIndex < list.size() - 1) {
65 list.remove(item);
66 list.add(oldIndex + 1, item);
67 }
68 }
69
70 public List<Item> getList() {
71 return list;
72 }
73
74 public static class Item {
75
76 private String label;
77 private String name;
78
79 private boolean visible = true;
80
81 private Item(final String label) {
82 this.label = label;
83 this.name = "x-buttons-item-" + label.toLowerCase(Locale.ENGLISH) + ".xhtml";
84 }
85
86 public String getLabel() {
87 return label;
88 }
89
90 public String getName() {
91 return name;
92 }
93
94 public boolean isVisible() {
95 return visible;
96 }
97
98 public void setVisible(final boolean visible) {
99 this.visible = visible;
100 }
101 }
102 }