View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.poc;
35  
36  import info.magnolia.icons.MagnoliaIcons;
37  import info.magnolia.poc.customer.Customer;
38  import info.magnolia.poc.customer.CustomerStatusRenderer;
39  import info.magnolia.poc.customer.CustomerService;
40  
41  import java.util.List;
42  
43  import javax.servlet.annotation.WebServlet;
44  
45  import org.jsoup.Jsoup;
46  import org.jsoup.safety.Whitelist;
47  
48  import com.vaadin.annotations.Theme;
49  import com.vaadin.annotations.Title;
50  import com.vaadin.annotations.VaadinServletConfiguration;
51  import com.vaadin.annotations.Widgetset;
52  import com.vaadin.contextmenu.ContextMenu;
53  import com.vaadin.icons.VaadinIcons;
54  import com.vaadin.server.VaadinRequest;
55  import com.vaadin.server.VaadinServlet;
56  import com.vaadin.ui.Grid.SelectionMode;
57  import com.vaadin.ui.HorizontalLayout;
58  import com.vaadin.ui.TreeGrid;
59  import com.vaadin.ui.UI;
60  import com.vaadin.ui.VerticalLayout;
61  import com.vaadin.ui.components.grid.ItemClickListener;
62  import com.vaadin.ui.renderers.HtmlRenderer;
63  
64  @Theme("poctheme")
65  @Title("Magnolia 6 Resurface")
66  @Widgetset("info.magnolia.poc.Widgetset")
67  public class TreeGridUI extends UI {
68  
69      @WebServlet(value = "/grid/*", asyncSupported = true)
70      @VaadinServletConfiguration(productionMode = false, ui = TreeGridUI.class)
71      public static class Servlet extends VaadinServlet {
72      }
73  
74      private TreeGrid<Customer> treeGrid = new TreeGrid<>();
75      private CustomerService service = CustomerService.getInstance();
76  
77      @Override
78      protected void init(VaadinRequest request) {
79          final VerticalLayout layout = new VerticalLayout();
80          layout.setSpacing(false);
81          HorizontalLayout main = new HorizontalLayout(treeGrid);
82          main.setSizeFull();
83          treeGrid.setSizeFull();
84  
85          treeGrid.addColumn(customer -> {
86              String iconHtml;
87              if (customer.isPersisted()) {
88                  iconHtml = VaadinIcons.FOLDER_O.getHtml();
89              } else {
90                  iconHtml = VaadinIcons.FILE_O.getHtml();
91              }
92              return iconHtml + " " + Jsoup.clean(customer.getLastName(), Whitelist.simpleText());
93          }, new HtmlRenderer()).setCaption("Last Name").setExpandRatio(5);
94          treeGrid.addColumn(Customer::getFirstName).setCaption("First Name").setExpandRatio(2);
95          treeGrid.addColumn(customer -> customer.getStatus().name(), new CustomerStatusRenderer()).setCaption("Status").setExpandRatio(0);
96          treeGrid.addColumn(Customer::getBirthDate).setCaption("Date of Birth").setExpandRatio(2);
97          treeGrid.addColumn(Customer::getEmail).setCaption("Email").setExpandRatio(2);
98  
99          treeGrid.setSelectionMode(SelectionMode.MULTI);
100 
101         treeGrid.addItemClickListener((ItemClickListener<Customer>) event -> {
102             if (event.getSource().getSelectionModel().isSelected(event.getItem())) {
103                 event.getSource().getSelectionModel().deselect(event.getItem());
104             } else {
105                 event.getSource().getSelectionModel().select(event.getItem());
106             }
107         });
108 
109         main.setExpandRatio(treeGrid, 1);
110         layout.addComponents(main);
111 
112         // refresh the customer list
113         updateList();
114 
115         ContextMenu contextMenu = new ContextMenu(this, false);
116         contextMenu.setAsContextMenuOf(layout);
117         contextMenu.addItem("Upload asset", MagnoliaIcons.UPLOAD, null);
118         contextMenu.addItem("Upload Zip archive", MagnoliaIcons.UPLOAD, null);
119         contextMenu.addItem("Add folder", MagnoliaIcons.ADD_FOLDER, null);
120         contextMenu.addItem("Delete folder", MagnoliaIcons.DELETE, null);
121         contextMenu.addItem("Publish incl. subnodes", MagnoliaIcons.PUBLISH_INCL_SUB, null);
122         contextMenu.addItem("Move folder", MagnoliaIcons.MOVE, null);
123 
124         setContent(layout);
125     }
126 
127     public void updateList() {
128         // fetch list of items from service and assign it to Grid
129         // in this case it's a list of customers
130         List<Customer> customers = service.findAll();
131         treeGrid.setItems(customers, Customer::getChildren);
132     }
133 
134     @Override
135     public boolean equals(Object obj) {
136         return super.equals(obj);
137     }
138 
139     @Override
140     public int hashCode() {
141         return super.hashCode();
142     }
143 }