View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.vaadin.gwt.client.widget.controlbar;
35  
36  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlArea;
37  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.eventmanager.ControlBarEventHandler;
38  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.eventmanager.ControlBarEventManager;
39  
40  import com.google.gwt.core.client.GWT;
41  import com.google.gwt.dom.client.NativeEvent;
42  import com.google.gwt.user.client.DOM;
43  import com.google.gwt.user.client.Element;
44  import com.google.gwt.user.client.ui.FlowPanel;
45  import com.google.gwt.user.client.ui.Label;
46  
47  /**
48   * A Widget for adding components to area. Marks where a newly created component will be injected.
49   * Is assembled by a {@link PlaceHolderBar} and an empty place holder area.
50   */
51  public class ComponentPlaceHolder extends FlowPanel {
52  
53      private final static String PLACEHOLDER_CLASS_NAME = "mgnlPlaceholder";
54      private final static String PLACEHOLDER_ELEMENT_BOX_CLASS_NAME = "mgnlPlaceholderBox";
55  
56      private final MgnlArea listener;
57  
58      private PlaceHolderBar placeHolderBar;
59  
60      public ComponentPlaceHolder(MgnlArea area) throws IllegalArgumentException {
61  
62          this.listener = area;
63          this.placeHolderBar = new PlaceHolderBar(area);
64          setStyleName(AbstractBar.EDITOR_CLASS_NAME);
65          addStyleName(PLACEHOLDER_CLASS_NAME);
66  
67          setVisible(false);
68          initLayout();
69      }
70  
71      protected void initLayout() {
72          add(placeHolderBar);
73          if (listener.isBoxPlaceHolder()) {
74  
75              Element element = DOM.createDiv();
76              element.addClassName(AbstractBar.EDITOR_CLASS_NAME);
77              element.addClassName(PLACEHOLDER_ELEMENT_BOX_CLASS_NAME);
78              getElement().appendChild(element);
79          }
80      }
81  
82      @Override
83      public void setVisible(boolean visible) {
84          super.setVisible(visible);
85          placeHolderBar.setVisible(visible);
86      }
87  
88      @Override
89      public void onAttach() {
90          super.onAttach();
91      }
92  
93      /**
94       * A placeholder bar which resembles the {@link ComponentBar}.
95       */
96      private class PlaceHolderBar extends AbstractBar {
97  
98          private final MgnlArea listener;
99  
100         private ControlBarEventManager impl = GWT.create(ControlBarEventManager.class);
101 
102         public PlaceHolderBar(MgnlArea area) {
103             super(area);
104             this.listener = area;
105 
106             addStyleName(COMPONENT_CLASS_NAME);
107 
108             initLayout();
109         }
110 
111         @Override
112         protected String getLabel() {
113             return listener.getPlaceHolderLabel();
114         }
115 
116         @Override
117         protected void createControls() {
118 
119             if (listener.hasAddComponentButton()) {
120                 final Label add = new Label();
121                 add.setStyleName(ICON_CLASS_NAME);
122                 add.addStyleName(ADD_CLASS_NAME);
123                 impl.addClickOrTouchHandler(this, new ControlBarEventHandler() {
124                     @Override
125                     public void handle(NativeEvent event) {
126                         listener.createNewComponent();
127                     }
128                 });
129                 addButton(add);
130             }
131         }
132     }
133 }