View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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 }