View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.ui.actionbar.ActionbarView;
37  import info.magnolia.ui.api.view.View;
38  import info.magnolia.ui.vaadin.actionbar.ActionPopup;
39  import info.magnolia.ui.workbench.WorkbenchView;
40  
41  import com.vaadin.ui.MenuBar.MenuItem;
42  import com.vaadin.server.Resource;
43  import com.vaadin.ui.Component;
44  import com.vaadin.ui.CssLayout;
45  import com.vaadin.ui.HorizontalLayout;
46  import com.vaadin.ui.VerticalLayout;
47  
48  /**
49   * Implementation of {@link BrowserView}.
50   */
51  public class BrowserViewImpl extends VerticalLayout implements BrowserView {
52  
53      private ActionbarView actionBar;
54  
55      private ActionPopup actionPopup;
56  
57      private final CssLayout actionBarWrapper = new CssLayout();
58  
59      private final HorizontalLayout workbenchWapper = new HorizontalLayout();
60  
61      private WorkbenchView workbench;
62  
63      private BrowserView.Listener listener;
64  
65      public BrowserViewImpl() {
66          setSizeFull();
67          setMargin(false);
68          setSpacing(false);
69          addStyleName("browser");
70  
71          workbenchWapper.setSizeFull();
72          workbenchWapper.setMargin(false);
73          workbenchWapper.setSpacing(true);
74  
75          actionBarWrapper.setHeight(100, Unit.PERCENTAGE);
76          actionBarWrapper.addStyleName("actionbar");
77          workbenchWapper.addComponent(actionBarWrapper);
78          workbenchWapper.setExpandRatio(actionBarWrapper, 0);
79  
80          addComponent(workbenchWapper);
81          setExpandRatio(workbenchWapper, 1);
82          createActionPopup();
83  
84      }
85  
86      @Override
87      public void setActionbarView(final ActionbarView actionBar) {
88          Component c = actionBar.asVaadinComponent();
89          Component old = actionBarWrapper.getComponentCount() != 0 ? actionBarWrapper.getComponent(0) : null;
90          if (old == null) {
91              actionBarWrapper.addComponent(c);
92          } else {
93              actionBarWrapper.replaceComponent(old, c);
94          }
95          this.actionBar = actionBar;
96      }
97  
98      @Override
99      public Component asVaadinComponent() {
100         return this;
101     }
102 
103     @Override
104     public void setListener(BrowserView.Listener listener) {
105         this.listener = listener;
106     }
107 
108     @Override
109     public void setWorkbenchView(WorkbenchView workbench) {
110         if (this.workbench == null) {
111             workbenchWapper.addComponent(workbench.asVaadinComponent(), 0); // add as first
112         } else {
113             workbenchWapper.replaceComponent(this.workbench.asVaadinComponent(), workbench.asVaadinComponent());
114         }
115         workbenchWapper.setExpandRatio(workbench.asVaadinComponent(), 1);
116 
117         View statusBarView = workbench.getStatusBarView();
118         if (statusBarView != null) {
119             addComponent(statusBarView.asVaadinComponent());
120             setExpandRatio(statusBarView.asVaadinComponent(), 0);
121         }
122         this.workbench = workbench;
123     }
124 
125     /**
126      * Create an ActionPopup which will be triggered by right-clicks on items in Content views.
127      */
128     protected void createActionPopup() {
129         actionPopup = new ActionPopup(this, false);
130         actionPopup.setAsContextMenuOf(this);
131     }
132 
133     @Override
134     public ActionPopup getActionPopup() {
135         return actionPopup;
136     }
137 
138     @Override
139     public MenuItem addPopupItem(String label, Resource iconFontResource) {
140         return actionPopup.addItem(label, iconFontResource, item -> {});
141     }
142 }