View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.vaadin.gwt.client.widget.controlbar;
35  
36  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.eventmanager.ControlBarEventHandler;
37  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.eventmanager.ControlBarEventManager;
38  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.listener.AreaListener;
39  
40  import com.google.gwt.core.client.GWT;
41  import com.google.gwt.dom.client.Element;
42  import com.google.gwt.dom.client.NativeEvent;
43  import com.google.gwt.user.client.DOM;
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      protected AreaListener listener;
57      protected PlaceHolderBar placeHolderBar = new PlaceHolderBar();
58      private boolean boxed;
59      private boolean addable;
60  
61      public ComponentPlaceHolder() {
62          setStyleName(SimpleBar.EDITOR_CLASS_NAME);
63          addStyleName(PLACEHOLDER_CLASS_NAME);
64      }
65  
66      protected void initLayout() {
67          placeHolderBar.initLayout();
68          add(placeHolderBar);
69  
70          if (addable) {
71              placeHolderBar.createAddButton(listener);
72          }
73  
74          if (boxed) {
75              Element element = DOM.createDiv();
76              element.addClassName(SimpleBar.EDITOR_CLASS_NAME);
77              element.addClassName(PLACEHOLDER_ELEMENT_BOX_CLASS_NAME);
78              getElement().appendChild(element);
79          }
80      }
81  
82      protected void addLabel(String label, int level) {
83          placeHolderBar.addLabel(label, level);
84      }
85  
86      @Override
87      public void setVisible(boolean visible) {
88          super.setVisible(visible);
89          placeHolderBar.setVisible(visible);
90      }
91  
92      @Override
93      public void onAttach() {
94          super.onAttach();
95      }
96  
97      /**
98       * A placeholder bar which resembles the {@link ComponentBar}.
99       */
100     private static class PlaceHolderBar extends AbstractBar {
101 
102         private ControlBarEventManager impl = GWT.create(ControlBarEventManager.class);
103 
104         public PlaceHolderBar() {
105             addStyleName(COMPONENT_CLASS_NAME);
106         }
107 
108         private void createAddButton(final AreaListener listener) {
109             final Label add = new Label();
110             add.setStyleName(ICON_CLASS_NAME);
111             add.addStyleName(ADD_CLASS_NAME);
112             impl.addClickOrTouchHandler(this, new ControlBarEventHandler() {
113                 @Override
114                 public void handle(NativeEvent event) {
115                     listener.createNewComponent();
116                 }
117             });
118             addButton(add);
119         }
120 
121     }
122 
123     /**
124      * Builder for the component-placeholder widget and the belonging place-holder-bar widget.
125      */
126     public static class Builder {
127 
128         private AreaListener listener;
129         private boolean boxed;
130         private boolean addable;
131         private String label;
132 
133         public Builder withListener(AreaListener listener) {
134             this.listener = listener;
135             return this;
136         }
137 
138         public Builder withType(boolean isBox) {
139             this.boxed = isBox;
140             return this;
141         }
142 
143         public Builder withAddButton(boolean isAdd) {
144             this.addable = isAdd;
145             return this;
146         }
147 
148         public Builder withLabel(String label) {
149             this.label = label;
150             return this;
151         }
152 
153         public ComponentPlaceHolder build() {
154             ComponentPlaceHolder placeHolder = GWT.create(ComponentPlaceHolder.class);
155             placeHolder.listener = listener;
156             placeHolder.addable = addable;
157             placeHolder.boxed = boxed;
158 
159             placeHolder.initLayout();
160             placeHolder.addLabel(label, 0);
161             placeHolder.setVisible(false);
162             return placeHolder;
163         }
164     }
165 }