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          if (hasControlBar(getMgnlElement().getAttributes())) {
75              AreaBar areaBar = new AreaBar(getMgnlElement());
76              setEditBar(areaBar);
77              attachWidget();
78  
79              if (hasComponentPlaceHolder(getMgnlElement().getAttributes())) {
80                  ComponentPlaceHolder placeHolder = new ComponentPlaceHolder(getMgnlElement());
81                  attachComponentPlaceHolder(placeHolder);
82                  addToModel(placeHolder);
83              }
84  
85              AreaEndBar endBar = new AreaEndBar(getMgnlElement());
86              attachAreaEndBar(endBar);
87              addToModel(endBar);
88  
89          } else {
90  
91              GWT.log("Not creating areabar and area endbar for this element. Missing parameters. Will be deleted.");
92  
93              // if the area has no controls we, don't want it in the structure.
94  
95              // delete the element from the tree
96              // set all child parents to parent
97              getMgnlElement().delete();
98  
99              // remove it from the Model
100             getModel().removeMgnlElement(getMgnlElement());
101         }
102     }
103 
104     private void addToModel(ComponentPlaceHolder placeHolder) {
105         getModel().addElements(getMgnlElement(), placeHolder.getElement());
106 
107     }
108 
109     private void addToModel(AreaEndBar endBar) {
110         getModel().addElements(getMgnlElement(), endBar.getElement());
111         getMgnlElement().setAreaEndBar(endBar);
112     }
113 
114     protected boolean hasComponentPlaceHolder(Map<String, String> attributes) {
115 
116         final boolean optional = Boolean.parseBoolean(attributes.get(ATTRIBUTE_OPTIONAL));
117         final boolean created = Boolean.parseBoolean(attributes.get(ATTRIBUTE_CREATED));
118 
119         if (optional && !created) {
120             return false;
121         }
122 
123         final String type = attributes.get(ATTRIBUTE_TYPE);
124         if (AreaDefinition.TYPE_NO_COMPONENT.equals(type) || "".equals(attributes.get(ATTRIBUTE_AVAILABLE_COMPONENTS))) {
125             return false;
126         }
127 
128         return !AreaDefinition.TYPE_SINGLE.equals(type) || getMgnlElement().getComponents().isEmpty();
129     }
130 
131     protected boolean hasControlBar(Map<String, String> attributes) {
132 
133         // break no matter what follows
134         if (getMgnlElement().isInherited() || (attributes.containsKey(ATTRIBUTE_EDITABLE) && !Boolean.parseBoolean(attributes.get(ATTRIBUTE_EDITABLE)))) {
135             return false;
136         }
137 
138         final String type = attributes.get(ATTRIBUTE_TYPE);
139         if (Boolean.parseBoolean(attributes.get(ATTRIBUTE_OPTIONAL)) || AreaDefinition.TYPE_SINGLE.equals(type) || Boolean.parseBoolean(attributes.get(ATTRIBUTE_SHOW_NEW_COMPONENT_AREA))) {
140             return true;
141         }
142 
143         // area can be edited
144         final String dialog = attributes.get(ATTRIBUTE_DIALOG);
145         return !(dialog == null || "".equals(dialog));
146     }
147 
148     private void attachAreaEndBar(AreaEndBar controlBar) {
149         if (getMgnlElement().getFirstElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
150             Element element = getMgnlElement().getFirstElement();
151             if (element != null) {
152                 element.appendChild(controlBar.getElement());
153             }
154         } else {
155             Element element = getMgnlElement().getEndComment();
156             Node parentNode = element.getParentNode();
157             parentNode.insertBefore(controlBar.getElement(), element);
158         }
159         controlBar.onAttach();
160     }
161 
162     private void attachComponentPlaceHolder(ComponentPlaceHolder placeHolder) {
163         Element parent = getMgnlElement().getComponentMarkerElement();
164 
165         if (parent == null) {
166             if (getMgnlElement().getLastElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
167                 Element element = getMgnlElement().getFirstElement();
168                 if (element != null) {
169                     element.appendChild(placeHolder.getElement());
170                 }
171             } else {
172                 Element element = getMgnlElement().getEndComment();
173                 Node parentNode = element.getParentNode();
174                 parentNode.insertBefore(placeHolder.getElement(), element);
175             }
176         } else {
177             parent.insertFirst(placeHolder.getElement());
178         }
179         placeHolder.onAttach();
180         getMgnlElement().setComponentPlaceHolder(placeHolder);
181     }
182 
183     @Override
184     public MgnlArea getMgnlElement() {
185         return (MgnlArea) super.getMgnlElement();
186     }
187 }