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