View Javadoc
1   /**
2    * This file Copyright (c) 2011-2017 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             /**
110              * if the area has no controls we, don't want it in the structure.
111              * We can't delete the child components, because in this way we would lose the information about the inheritance for child areas.
112              * We can't delete inner areas, inner areas can have inheritance disabled explicitly.
113              * We must delete the area here, because selection model is heavy relying on current structure (e.g. if we don't remove main area from model, underlying areas won't be set visible - see {@link info.magnolia.ui.vaadin.gwt.client.editor.model.focus.FocusModelImpl#init()}), even in this way we can create incorrect structure with components as component child.
114              */
115 
116             // delete the element from the tree
117             // set all child parents to parent
118             area.delete();
119 
120             // remove it from the Model
121             getModel().removeMgnlElement(area);
122         }
123     }
124 
125     private void addToModel(ComponentPlaceHolder placeHolder) {
126         getModel().addElements(getMgnlElement(), placeHolder.getElement());
127 
128     }
129 
130     private void addToModel(AreaEndBar endBar) {
131         getModel().addElements(getMgnlElement(), endBar.getElement());
132         getMgnlElement().setAreaEndBar(endBar);
133     }
134 
135     protected boolean hasComponentPlaceHolder(Map<String, String> attributes) {
136 
137         final boolean optional = Boolean.parseBoolean(attributes.get(ATTRIBUTE_OPTIONAL));
138         final boolean created = Boolean.parseBoolean(attributes.get(ATTRIBUTE_CREATED));
139 
140         if (optional && !created) {
141             return false;
142         }
143 
144         final String type = attributes.get(ATTRIBUTE_TYPE);
145         if (AreaDefinition.TYPE_NO_COMPONENT.equals(type) || "".equals(attributes.get(ATTRIBUTE_AVAILABLE_COMPONENTS))) {
146             return false;
147         }
148 
149         return !AreaDefinition.TYPE_SINGLE.equals(type) || getMgnlElement().getComponents().isEmpty();
150     }
151 
152     protected boolean hasControlBar(Map<String, String> attributes) {
153 
154         // break no matter what follows
155         if (getMgnlElement().isInherited() || (attributes.containsKey(ATTRIBUTE_EDITABLE) && !Boolean.parseBoolean(attributes.get(ATTRIBUTE_EDITABLE)))) {
156             return false;
157         }
158 
159         final String type = attributes.get(ATTRIBUTE_TYPE);
160         if (Boolean.parseBoolean(attributes.get(ATTRIBUTE_OPTIONAL)) || AreaDefinition.TYPE_SINGLE.equals(type) || Boolean.parseBoolean(attributes.get(ATTRIBUTE_SHOW_NEW_COMPONENT_AREA))) {
161             return true;
162         }
163 
164         // area can be edited
165         final String dialog = attributes.get(ATTRIBUTE_DIALOG);
166         return !(dialog == null || "".equals(dialog));
167     }
168 
169     private void attachAreaEndBar(AreaEndBar controlBar) {
170         if (getMgnlElement().getFirstElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
171             Element element = getMgnlElement().getFirstElement();
172             if (element != null) {
173                 element.appendChild(controlBar.getElement());
174             }
175         } else {
176             Element element = getMgnlElement().getEndComment();
177             Node parentNode = element.getParentNode();
178             parentNode.insertBefore(controlBar.getElement(), element);
179         }
180         controlBar.onAttach();
181     }
182 
183     private void attachComponentPlaceHolder(ComponentPlaceHolder placeHolder) {
184         Element parent = getMgnlElement().getComponentMarkerElement();
185 
186         if (parent == null) {
187             if (getMgnlElement().getLastElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
188                 Element element = getMgnlElement().getFirstElement();
189                 if (element != null) {
190                     element.appendChild(placeHolder.getElement());
191                 }
192             } else {
193                 Element element = getMgnlElement().getEndComment();
194                 Node parentNode = element.getParentNode();
195                 parentNode.insertBefore(placeHolder.getElement(), element);
196             }
197         } else {
198             parent.insertFirst(placeHolder.getElement());
199         }
200         placeHolder.onAttach();
201         getMgnlElement().setComponentPlaceHolder(placeHolder);
202     }
203 
204     @Override
205     public MgnlArea getMgnlElement() {
206         return (MgnlArea) super.getMgnlElement();
207     }
208 }