View Javadoc

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