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 static com.vaadin.server.Sizeable.Unit.PERCENTAGE;
37  
38  import info.magnolia.ui.vaadin.actionbar.Actionbar;
39  import info.magnolia.ui.vaadin.gwt.client.actionbar.shared.ActionbarItem;
40  
41  import java.io.BufferedReader;
42  import java.io.IOException;
43  import java.io.InputStream;
44  import java.io.InputStreamReader;
45  import java.util.HashMap;
46  import java.util.Map;
47  
48  import javax.servlet.annotation.WebServlet;
49  
50  import com.vaadin.annotations.Theme;
51  import com.vaadin.annotations.Title;
52  import com.vaadin.annotations.VaadinServletConfiguration;
53  import com.vaadin.annotations.Widgetset;
54  import com.vaadin.server.VaadinRequest;
55  import com.vaadin.server.VaadinServlet;
56  import com.vaadin.ui.Component;
57  import com.vaadin.ui.CssLayout;
58  import com.vaadin.ui.GridLayout;
59  import com.vaadin.ui.HorizontalLayout;
60  import com.vaadin.ui.Label;
61  import com.vaadin.ui.UI;
62  
63  @Theme("poctheme")
64  @Title("Magnolia 6 Resurface - Icon Presenter")
65  @Widgetset("info.magnolia.poc.Widgetset")
66  public class IconPresenterUI extends UI {
67  
68      @Override
69      protected void init(VaadinRequest request) {
70          HorizontalLayout layout = new HorizontalLayout();
71  
72          GridLayout workbench = new GridLayout(4, 10);
73          workbench.setMargin(true);
74          workbench.setSpacing(true);
75          workbench.addStyleNames("workbench", "actionbar-scrolling");
76  
77          final Map<String, String> icons = getAllIcons();
78  
79          Component actionbar = buildActionBar(icons);
80          layout.addComponents(workbench, actionbar);
81  
82          icons.entrySet().forEach(entry -> {
83              final Label present = new Label();
84              present.setCaptionAsHtml(true);
85              final String formattedContent = String.format("<span class=\"v-icon sampler-style %s\"></span>" +
86                      "<span class=\"sampler-text\">%s</span>", entry.getKey(), entry.getKey());
87              present.setCaption(formattedContent);
88              workbench.addComponent(present);
89          });
90  
91          layout.setExpandRatio(workbench, 1f);
92          layout.setExpandRatio(actionbar, 0f);
93  
94          workbench.setSizeFull();
95          layout.setWidth(100, PERCENTAGE);
96  
97          setContent(layout);
98      }
99  
100     private Map<String, String> getAllIcons() {
101         Map<String, String> iconsToCode = new HashMap<>();
102         try (
103                 InputStream file = getClass().getResourceAsStream("/VAADIN/themes/magnolia-icons/variables.scss");
104                 InputStreamReader fr = new InputStreamReader(file);
105                 BufferedReader br = new BufferedReader(fr)
106         ) {
107             String line = br.readLine();
108             while (line != null) {
109                 final boolean matches = line.matches("(?:\\$)(?:.*)(?:\\\"\\\\e)\\w+(?:\\\"\\;)$");
110                 if (matches) {
111                     final String iconName = line.substring(1, line.indexOf(":"));
112                     final String iconCode = line.substring(line.indexOf("\"") + 2, line.lastIndexOf("\""));
113                     iconsToCode.put(iconName, iconCode);
114                 }
115                 line = br.readLine();
116             }
117         } catch (IOException ignore) {
118         }
119         return iconsToCode;
120     }
121 
122     private Component buildActionBar(Map<String, String> icons) {
123         Actionbar actionbar = new Actionbar();
124         actionbar.addSection("main", "Icons");
125 
126         icons.entrySet().stream().forEach(icon -> {
127             actionbar.addAction(new ActionbarItem(icon.getKey(), icon.getKey(), icon.getKey(), "Icons"), "main");
128         });
129 
130         actionbar.setOpen(false);
131         actionbar.setHeightUndefined();
132 
133         CssLayout wrapper = new CssLayout();
134         wrapper.addComponent(actionbar);
135         wrapper.setWidthUndefined();
136         wrapper.setHeight(100, PERCENTAGE);
137         wrapper.addStyleNames("actionbar", "actionbar-scrolling");
138         return wrapper;
139     }
140 
141     @WebServlet(value = "/icons/*", asyncSupported = true)
142     @VaadinServletConfiguration(productionMode = false, ui = IconPresenterUI.class)
143     public static class Servlet extends VaadinServlet {
144     }
145 }