View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.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      private static Map<String, String> i18nKeys;
67  
68      public AreaProcessor(Model model, MgnlArea mgnlElement, Map<String, String> i18nKeys) {
69          super(model, mgnlElement);
70          this.i18nKeys = i18nKeys;
71      }
72  
73      @Override
74      public void process() {
75  
76          MgnlArea area = getMgnlElement();
77  
78          if (hasControlBar(area.getAttributes())) {
79              AreaBar areaBar = new AreaBar.Builder()
80                      .withListener(area)
81                      .withAdd(area.hasAddButton())
82                      .withEdit(area.hasEditButton())
83                      .withLevel(area.getLevel())
84                      .withLabel(area.getLabel())
85                      .withActivationStatus(area.getActivationStatus())
86                      .build();
87  
88              setEditBar(areaBar);
89              attachWidget();
90  
91              if (hasComponentPlaceHolder(area.getAttributes())) {
92                  ComponentPlaceHolder placeHolder = new ComponentPlaceHolder.Builder()
93                          .withListener(area)
94                          .withType(area.isBoxPlaceHolder())
95                          .withAddButton(area.hasAddComponentButton())
96                          .withLabel(area.getPlaceHolderLabel(i18nKeys))
97                          .build();
98  
99                  attachComponentPlaceHolder(placeHolder);
100                 addToModel(placeHolder);
101             }
102 
103             AreaEndBarvaadin/gwt/client/widget/controlbar/AreaEndBar.html#AreaEndBar">AreaEndBar endBar = new AreaEndBar();
104             attachAreaEndBar(endBar);
105             addToModel(endBar);
106 
107         } else {
108 
109             GWT.log("Not creating areabar and area endbar for this element. Missing parameters. Will be deleted.");
110 
111             /**
112              * if the area has no controls we, don't want it in the structure.
113              * We can't delete the child components, because in this way we would lose the information about the inheritance for child areas.
114              * We can't delete inner areas, inner areas can have inheritance disabled explicitly.
115              * 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.
116              */
117 
118             // delete the element from the tree
119             // set all child parents to parent
120             area.delete();
121 
122             // remove it from the Model
123             getModel().removeMgnlElement(area);
124         }
125     }
126 
127     private void addToModel(ComponentPlaceHolder placeHolder) {
128         getModel().addElements(getMgnlElement(), placeHolder.getElement());
129 
130     }
131 
132     private void addToModel(AreaEndBar endBar) {
133         getModel().addElements(getMgnlElement(), endBar.getElement());
134         getMgnlElement().setAreaEndBar(endBar);
135     }
136 
137     protected boolean hasComponentPlaceHolder(Map<String, String> attributes) {
138 
139         final boolean optional = Boolean.parseBoolean(attributes.get(ATTRIBUTE_OPTIONAL));
140         final boolean created = Boolean.parseBoolean(attributes.get(ATTRIBUTE_CREATED));
141 
142         if (optional && !created) {
143             return false;
144         }
145 
146         final String type = attributes.get(ATTRIBUTE_TYPE);
147         if (AreaDefinition.TYPE_NO_COMPONENT.equals(type) || "".equals(attributes.get(ATTRIBUTE_AVAILABLE_COMPONENTS))) {
148             return false;
149         }
150 
151         return !AreaDefinition.TYPE_SINGLE.equals(type) || getMgnlElement().getComponents().isEmpty();
152     }
153 
154     protected boolean hasControlBar(Map<String, String> attributes) {
155 
156         // break no matter what follows
157         if (getMgnlElement().isInherited() || (attributes.containsKey(ATTRIBUTE_EDITABLE) && !Boolean.parseBoolean(attributes.get(ATTRIBUTE_EDITABLE)))) {
158             return false;
159         }
160 
161         final String type = attributes.get(ATTRIBUTE_TYPE);
162         if (Boolean.parseBoolean(attributes.get(ATTRIBUTE_OPTIONAL)) || AreaDefinition.TYPE_SINGLE.equals(type) || Boolean.parseBoolean(attributes.get(ATTRIBUTE_SHOW_NEW_COMPONENT_AREA))) {
163             return true;
164         }
165 
166         // area can be edited
167         final String dialog = attributes.get(ATTRIBUTE_DIALOG);
168         return !(dialog == null || "".equals(dialog));
169     }
170 
171     private void attachAreaEndBar(AreaEndBar controlBar) {
172         if (getMgnlElement().getFirstElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
173             Element element = getMgnlElement().getFirstElement();
174             if (element != null) {
175                 element.appendChild(controlBar.getElement());
176             }
177         } else {
178             Element element = getMgnlElement().getEndComment();
179             Node parentNode = element.getParentNode();
180             parentNode.insertBefore(controlBar.getElement(), element);
181         }
182         controlBar.onAttach();
183     }
184 
185     private void attachComponentPlaceHolder(ComponentPlaceHolder placeHolder) {
186         Element parent = getMgnlElement().getComponentMarkerElement();
187 
188         if (parent == null) {
189             if (getMgnlElement().getLastElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
190                 Element element = getMgnlElement().getFirstElement();
191                 if (element != null) {
192                     element.appendChild(placeHolder.getElement());
193                 }
194             } else {
195                 Element element = getMgnlElement().getEndComment();
196                 Node parentNode = element.getParentNode();
197                 parentNode.insertBefore(placeHolder.getElement(), element);
198             }
199         } else {
200             parent.insertFirst(placeHolder.getElement());
201         }
202         placeHolder.onAttach();
203         getMgnlElement().setComponentPlaceHolder(placeHolder);
204     }
205 
206     @Override
207     public MgnlArea getMgnlElement() {
208         return (MgnlArea) super.getMgnlElement();
209     }
210 }