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