View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.editor.dom.processor;
35  
36  import info.magnolia.rendering.template.AreaDefinition;
37  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlArea;
38  import info.magnolia.ui.vaadin.gwt.client.editor.model.Model;
39  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.AreaBar;
40  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.AreaEndBar;
41  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.ComponentPlaceHolder;
42  
43  import java.util.Map;
44  
45  import com.google.gwt.core.client.GWT;
46  import com.google.gwt.dom.client.Element;
47  import com.google.gwt.dom.client.Node;
48  
49  /**
50   * Processor for {@link MgnlArea}s. Extends the {@link AbstractMgnlElementProcessor} for handling widgets associated with areas.
51   * Removes areas which do not contain any {@link AreaBar} from the {@link Model}.
52   *
53   * @see AreaBar
54   * @see AreaEndBar
55   * @see ComponentPlaceHolder
56   */
57  public class AreaProcessor extends AbstractMgnlElementProcessor {
58  
59      public static final String ATTRIBUTE_OPTIONAL = "optional";
60      public static final String ATTRIBUTE_CREATED = "created";
61      public static final String ATTRIBUTE_TYPE = "type";
62      public static final String ATTRIBUTE_EDITABLE = "editable";
63      public static final String ATTRIBUTE_AVAILABLE_COMPONENTS = "availableComponents";
64      public static final String ATTRIBUTE_DIALOG = "dialog";
65      public static final String ATTRIBUTE_SHOW_NEW_COMPONENT_AREA = "showNewComponentArea";
66  
67      public AreaProcessor(Model model, MgnlArea mgnlElement) {
68          super(model, mgnlElement);
69      }
70  
71      @Override
72      public void process() {
73  
74          MgnlArea area = getMgnlElement();
75  
76          if (hasControlBar(area.getAttributes())) {
77              AreaBar areaBar = new AreaBar.Builder()
78                      .withListener(area)
79                      .withAdd(area.hasAddButton())
80                      .withEdit(area.hasEditButton())
81                      .withLevel(area.getLevel())
82                      .withLabel(area.getLabel())
83                      .withActivationStatus(area.getActivationStatus())
84                      .build();
85  
86              setEditBar(areaBar);
87              attachWidget();
88  
89              if (hasComponentPlaceHolder(area.getAttributes())) {
90                  ComponentPlaceHolder placeHolder = new ComponentPlaceHolder.Builder()
91                          .withListener(area)
92                          .withType(area.isBoxPlaceHolder())
93                          .withAddButton(area.hasAddComponentButton())
94                          .withLabel(area.getPlaceHolderLabel())
95                          .build();
96  
97                  attachComponentPlaceHolder(placeHolder);
98                  addToModel(placeHolder);
99              }
100 
101             AreaEndBar endBar = new AreaEndBar();
102             attachAreaEndBar(endBar);
103             addToModel(endBar);
104 
105         } else {
106 
107             GWT.log("Not creating areabar and area endbar for this element. Missing parameters. Will be deleted.");
108 
109             // if the area has no controls we, don't want it in the structure.
110 
111             // delete the element from the tree
112             // set all child parents to parent
113             area.delete();
114 
115             // remove it from the Model
116             getModel().removeMgnlElement(area);
117         }
118     }
119 
120     private void addToModel(ComponentPlaceHolder placeHolder) {
121         getModel().addElements(getMgnlElement(), placeHolder.getElement());
122 
123     }
124 
125     private void addToModel(AreaEndBar endBar) {
126         getModel().addElements(getMgnlElement(), endBar.getElement());
127         getMgnlElement().setAreaEndBar(endBar);
128     }
129 
130     protected boolean hasComponentPlaceHolder(Map<String, String> attributes) {
131 
132         final boolean optional = Boolean.parseBoolean(attributes.get(ATTRIBUTE_OPTIONAL));
133         final boolean created = Boolean.parseBoolean(attributes.get(ATTRIBUTE_CREATED));
134 
135         if (optional && !created) {
136             return false;
137         }
138 
139         final String type = attributes.get(ATTRIBUTE_TYPE);
140         if (AreaDefinition.TYPE_NO_COMPONENT.equals(type) || "".equals(attributes.get(ATTRIBUTE_AVAILABLE_COMPONENTS))) {
141             return false;
142         }
143 
144         return !AreaDefinition.TYPE_SINGLE.equals(type) || getMgnlElement().getComponents().isEmpty();
145     }
146 
147     protected boolean hasControlBar(Map<String, String> attributes) {
148 
149         // break no matter what follows
150         if (getMgnlElement().isInherited() || (attributes.containsKey(ATTRIBUTE_EDITABLE) && !Boolean.parseBoolean(attributes.get(ATTRIBUTE_EDITABLE)))) {
151             return false;
152         }
153 
154         final String type = attributes.get(ATTRIBUTE_TYPE);
155         if (Boolean.parseBoolean(attributes.get(ATTRIBUTE_OPTIONAL)) || AreaDefinition.TYPE_SINGLE.equals(type) || Boolean.parseBoolean(attributes.get(ATTRIBUTE_SHOW_NEW_COMPONENT_AREA))) {
156             return true;
157         }
158 
159         // area can be edited
160         final String dialog = attributes.get(ATTRIBUTE_DIALOG);
161         return !(dialog == null || "".equals(dialog));
162     }
163 
164     private void attachAreaEndBar(AreaEndBar controlBar) {
165         if (getMgnlElement().getFirstElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
166             Element element = getMgnlElement().getFirstElement();
167             if (element != null) {
168                 element.appendChild(controlBar.getElement());
169             }
170         } else {
171             Element element = getMgnlElement().getEndComment();
172             Node parentNode = element.getParentNode();
173             parentNode.insertBefore(controlBar.getElement(), element);
174         }
175         controlBar.onAttach();
176     }
177 
178     private void attachComponentPlaceHolder(ComponentPlaceHolder placeHolder) {
179         Element parent = getMgnlElement().getComponentMarkerElement();
180 
181         if (parent == null) {
182             if (getMgnlElement().getLastElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
183                 Element element = getMgnlElement().getFirstElement();
184                 if (element != null) {
185                     element.appendChild(placeHolder.getElement());
186                 }
187             } else {
188                 Element element = getMgnlElement().getEndComment();
189                 Node parentNode = element.getParentNode();
190                 parentNode.insertBefore(placeHolder.getElement(), element);
191             }
192         } else {
193             parent.insertFirst(placeHolder.getElement());
194         }
195         placeHolder.onAttach();
196         getMgnlElement().setComponentPlaceHolder(placeHolder);
197     }
198 
199     @Override
200     public MgnlArea getMgnlElement() {
201         return (MgnlArea) super.getMgnlElement();
202     }
203 }