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.ui.Alignment;
50  import com.vaadin.ui.Button;
51  import com.vaadin.ui.Component;
52  import com.vaadin.ui.CssLayout;
53  import com.vaadin.ui.HorizontalLayout;
54  import com.vaadin.ui.VerticalLayout;
55  
56  import lombok.AccessLevel;
57  import lombok.Getter;
58  
59  /**
60   * Container view that manages display of one of the pre-configured {@link ContentViewsDefinition content views}.
61   *
62   * @param <T>
63   *     type of data displayed in a content view
64   */
65  public class ContentViews<T> extends VerticalLayout implements UiFrameworkView {
66  
67      private final Map<String, Component> contentViewToggleControls = new HashMap<>();
68      @Getter(AccessLevel.PROTECTED)
69      private final ContentViewsDefinition<T> definition;
70      @Getter(AccessLevel.PROTECTED)
71      private final HorizontalLayout toolBar;
72  
73      private ContentView currentActiveContentView;
74      private WorkbenchContext workbenchContext;
75  
76      @Inject
77      public ContentViews(WorkbenchContext workbenchContext, ContentViewsDefinition<T> definition) {
78  
79          this.workbenchContext = workbenchContext;
80          this.definition = definition;
81  
82          setSizeFull();
83          setSpacing(false);
84          setMargin(false);
85          addStyleName("content-views");
86  
87          toolBar = new HorizontalLayout();
88          toolBar.addStyleName("toolbar");
89          toolBar.setWidth(100, Unit.PERCENTAGE);
90  
91          toolBar.setSpacing(true);
92          toolBar.setDefaultComponentAlignment(Alignment.BOTTOM_LEFT);
93  
94  
95          final CssLayout viewModes = new CssLayout();
96          viewModes.addStyleName("view-modes");
97          toolBar.addComponent(viewModes);
98          toolBar.setComponentAlignment(viewModes, Alignment.BOTTOM_RIGHT);
99  
100         addComponents(toolBar);
101 
102         definition.getViews().forEach((id, presenterDefinition) -> {
103             final Button contentViewIcon = createContentViewIcon(id, presenterDefinition.getIcon());
104             contentViewToggleControls.put(id, contentViewIcon);
105             viewModes.addComponent(contentViewIcon);
106         });
107 
108         workbenchContext.displayedContentViewId().observe(viewId -> {
109             this.contentViewToggleControls.values().forEach(control -> control.removeStyleName("active"));
110             viewId.ifPresent(this::switchView);
111         });
112     }
113 
114     private void switchView(String viewId) {
115         Optional.ofNullable(currentActiveContentView).ifPresent(UiFrameworkView::destroy);
116 
117         final ContentViewDefinition<T> contentViewDefinition = definition.getViews().get(viewId);
118         //TODO: we don't always want tree here, there should be a more clever check!
119         this.currentActiveContentView = getViewProvider().create(contentViewDefinition);
120         this.contentViewToggleControls.get(viewId).addStyleName("active");
121 
122         addComponent(currentActiveContentView.asVaadinComponent());
123         setExpandRatio(currentActiveContentView.asVaadinComponent(), 1f);
124     }
125 
126     protected Button createContentViewIcon(final String viewType, final String icon) {
127         Button button = new Button(MagnoliaIcons.forCssClass(icon).orElse(MagnoliaIcons.VIEW_LIST),
128                 clickEvent -> workbenchContext.displayedContentViewId().set(viewType));
129         button.addStyleName(ResurfaceTheme.BUTTON_ICON);
130         return button;
131     }
132 
133 }