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.ui.contentapp.browser;
35  
36  import info.magnolia.icons.MagnoliaIcons;
37  import info.magnolia.ui.contentapp.browser.Workbench.WorkbenchContext;
38  import info.magnolia.ui.contentapp.configuration.ContentViewDefinition;
39  import info.magnolia.ui.contentapp.configuration.ContentViewsDefinition;
40  import info.magnolia.ui.framework.UiFrameworkView;
41  import info.magnolia.ui.theme.ResurfaceTheme;
42  
43  import java.util.HashMap;
44  import java.util.Map;
45  import java.util.Optional;
46  
47  import javax.inject.Inject;
48  
49  import com.vaadin.shared.ui.ContentMode;
50  import com.vaadin.ui.Alignment;
51  import com.vaadin.ui.Button;
52  import com.vaadin.ui.Component;
53  import com.vaadin.ui.CssLayout;
54  import com.vaadin.ui.HorizontalLayout;
55  import com.vaadin.ui.Label;
56  import com.vaadin.ui.VerticalLayout;
57  
58  import lombok.AccessLevel;
59  import lombok.Getter;
60  
61  /**
62   * Container view that manages display of one of the pre-configured {@link ContentViewsDefinition content views}.
63   *
64   * @param <T>
65   *     type of data displayed in a content view
66   */
67  public class ContentViews<T> extends VerticalLayout implements UiFrameworkView {
68  
69      private final Map<String, Component> contentViewToggleControls = new HashMap<>();
70      @Getter(AccessLevel.PROTECTED)
71      private final ContentViewsDefinition<T> definition;
72      @Getter(AccessLevel.PROTECTED)
73      private final HorizontalLayout toolBar;
74      private final Label title;
75  
76      private ContentView currentActiveContentView;
77      private WorkbenchContext workbenchContext;
78  
79      @Inject
80      public ContentViews(WorkbenchContext workbenchContext, ContentViewsDefinition<T> definition) {
81  
82          this.workbenchContext = workbenchContext;
83          this.definition = definition;
84  
85          setSizeFull();
86          setSpacing(false);
87          setMargin(false);
88          addStyleName("content-views");
89  
90          title = new Label();
91          title.addStyleName("heading-1 tab-header");
92  
93          toolBar = new HorizontalLayout();
94          toolBar.addStyleName("toolbar");
95          toolBar.setWidth(100, Unit.PERCENTAGE);
96  
97          toolBar.setSpacing(true);
98          toolBar.setDefaultComponentAlignment(Alignment.BOTTOM_LEFT);
99  
100         toolBar.addComponent(title);
101         toolBar.setExpandRatio(title, 0);
102 
103         final CssLayout viewModes = new CssLayout();
104         viewModes.addStyleName("view-modes");
105         toolBar.addComponent(viewModes);
106         toolBar.setComponentAlignment(viewModes, Alignment.BOTTOM_RIGHT);
107 
108         addComponents(toolBar);
109 
110         definition.getViews().forEach((id, presenterDefinition) -> {
111             final Button contentViewIcon = createContentViewIcon(id, presenterDefinition.getIcon());
112             contentViewToggleControls.put(id, contentViewIcon);
113             viewModes.addComponent(contentViewIcon);
114         });
115 
116         workbenchContext.displayedContentViewId().observe(viewId -> {
117             this.contentViewToggleControls.values().forEach(control -> control.removeStyleName("active"));
118             viewId.ifPresent(this::switchView);
119         });
120     }
121 
122     private void switchView(String viewId) {
123         Optional.ofNullable(currentActiveContentView).ifPresent(UiFrameworkView::destroy);
124 
125         ContentViewDefinition contentViewDefinition = definition.getViews().get(viewId);
126         this.currentActiveContentView = (ContentView) create(contentViewDefinition);
127         this.contentViewToggleControls.get(viewId).addStyleName("active");
128 
129         addComponent(currentActiveContentView.asVaadinComponent());
130         setExpandRatio(currentActiveContentView.asVaadinComponent(), 1f);
131     }
132 
133     protected Button createContentViewIcon(final String viewType, final String icon) {
134         Button button = new Button(MagnoliaIcons.forCssClass(icon).orElse(MagnoliaIcons.VIEW_LIST),
135                 clickEvent -> workbenchContext.displayedContentViewId().set(viewType));
136         button.addStyleName(ResurfaceTheme.BUTTON_ICON);
137         return button;
138     }
139 
140     public void setTitle(String title) {
141         this.title.setValue(title);
142     }
143 
144     public void setTitle(String title, boolean isHtml) {
145         if (isHtml) {
146             this.title.setContentMode(ContentMode.HTML);
147         }
148         setTitle(title);
149     }
150 
151 }