View Javadoc
1   /**
2    * This file Copyright (c) 2013-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;
35  
36  import info.magnolia.cms.security.operations.OperationPermissionDefinition;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.rendering.template.AreaDefinition;
39  import info.magnolia.ui.vaadin.gwt.client.editor.event.EditAreaEvent;
40  import info.magnolia.ui.vaadin.gwt.client.editor.event.NewAreaEvent;
41  import info.magnolia.ui.vaadin.gwt.client.editor.event.NewComponentEvent;
42  import info.magnolia.ui.vaadin.gwt.client.shared.AreaElement;
43  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.AreaEndBar;
44  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.ComponentPlaceHolder;
45  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.listener.AreaListener;
46  
47  import com.google.gwt.dom.client.Element;
48  import com.google.gwt.event.shared.EventBus;
49  
50  /**
51   * Represents an area inside the {@link CmsNode}-tree.
52   * An area can have 3 widgets associated with it:
53   *
54   * <pre>
55   *   <ul>
56   *     <li>{@link info.magnolia.ui.vaadin.gwt.client.widget.controlbar.AreaBar}</li>
57   *     <li>{@link AreaEndBar}</li>
58   *     <li>{@link ComponentPlaceHolder}</li>
59   *   </ul>
60   * </pre>
61   *
62   * Implements a listener interface for the {@link info.magnolia.ui.vaadin.gwt.client.widget.controlbar.AreaBar} and {@link ComponentPlaceHolder}.
63   * Provides wrapper functions used by the {@link info.magnolia.ui.vaadin.gwt.client.editor.model.focus.FocusModel}.
64   */
65  public class MgnlArea extends MgnlElement implements AreaListener {
66      public static final String EDITOR_INIT_CLASS_NAME = "init";
67      private AreaEndBar areaEndBar;
68      private ComponentPlaceHolder componentPlaceHolder;
69      private Element componentMarkerElement;
70      private EventBus eventBus;
71  
72      /**
73       * MgnlElement. Represents a node in the tree built on cms-tags.
74       */
75      public MgnlArea(MgnlElement parent, EventBus eventBus) {
76          super(parent);
77          this.eventBus = eventBus;
78      }
79  
80      private AreaEndBar getAreaEndBar() {
81          return areaEndBar;
82      }
83  
84      public void setAreaEndBar(AreaEndBar areaEndBar) {
85          this.areaEndBar = areaEndBar;
86      }
87  
88      private ComponentPlaceHolder getComponentPlaceHolder() {
89          return componentPlaceHolder;
90      }
91  
92      public void setComponentPlaceHolder(ComponentPlaceHolder componentPlaceHolder) {
93          this.componentPlaceHolder = componentPlaceHolder;
94      }
95  
96      public void setComponentMarkerElement(Element componentElement) {
97          this.componentMarkerElement = componentElement;
98      }
99  
100     public Element getComponentMarkerElement() {
101         return componentMarkerElement;
102     }
103 
104     @Override
105     public AreaElement getTypedElement() {
106         String availableComponents = getAttribute("availableComponents");
107         AreaElement area = new AreaElement(getAttribute("workspace"), getAttribute("path"), getAttribute("dialog"), availableComponents);
108 
109         boolean areaHasChildComponents = getComponents().size() > 0;
110         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
111         boolean created = Boolean.parseBoolean(getAttribute("created"));
112         boolean hasAvailableComponents = availableComponents != null && !availableComponents.isEmpty();
113         boolean addible = true;
114         if (getAttributes().containsKey(OperationPermissionDefinition.ADDIBLE)) {
115             addible = Boolean.parseBoolean(getAttribute(OperationPermissionDefinition.ADDIBLE));
116         }
117 
118         area.setOptional(optional);
119         area.setCreated(created);
120         area.setAddible(addible && hasAvailableComponents && !(optional && !created) && !(isTypeSingle() && areaHasChildComponents) && !isMaxComponentsReached());
121 
122         return area;
123     }
124 
125     public boolean isTypeSingle() {
126         return "single".equals(getAttribute("type"));
127     }
128 
129     public boolean isMaxComponentsReached() {
130         boolean showAddButton = Boolean.parseBoolean(getAttribute("showAddButton"));
131         boolean showNewComponentArea = Boolean.parseBoolean(getAttribute("showNewComponentArea"));
132 
133         return showNewComponentArea && !showAddButton;
134     }
135 
136     @Override
137     public void createOptionalArea() {
138         eventBus.fireEvent(new NewAreaEvent(getTypedElement()));
139     }
140 
141     @Override
142     public void editArea() {
143         eventBus.fireEvent(new EditAreaEvent(getTypedElement()));
144     }
145 
146     @Override
147     public void createNewComponent() {
148         eventBus.fireEvent(new NewComponentEvent(getTypedElement()));
149     }
150 
151     public boolean hasAddButton() {
152         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
153         boolean created = Boolean.parseBoolean(getAttribute("created"));
154 
155         return optional && !created;
156     }
157 
158     public boolean hasEditButton() {
159         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
160         boolean created = Boolean.parseBoolean(getAttribute("created"));
161         boolean dialog = null != getAttribute("dialog");
162 
163         if (dialog) {
164             // do not show edit-icon if the area has not been created
165             if (!optional || (optional && created)) {
166                 return true;
167             }
168         }
169         return false;
170     }
171 
172     public boolean hasAddComponentButton() {
173         return Boolean.parseBoolean(getAttribute("showAddButton"));
174     }
175 
176     public String getLabel() {
177         String label = getAttribute("label");
178         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
179         return label + ((optional) ? " (optional)" : "");
180     }
181 
182     public boolean isBoxPlaceHolder() {
183         Element marker = getComponentMarkerElement();
184         boolean onlyBar = (marker != null && marker.getAttribute(AreaDefinition.CMS_ADD).equals("bar"));
185         return !onlyBar;
186     }
187 
188     public String getPlaceHolderLabel() {
189         String label = getAttribute("label");
190         String labelString;
191 
192         // if the add new component area should be visible
193         if (isMaxComponentsReached()) { // maximum of components is reached - show add new component area with the maximum reached message, but without the ADD button
194             labelString = "Maximum of components reached.";
195         } else { // maximum of components is NOT reached - show add new component area with ADD button
196             labelString = "New Component";
197             if (label != null && !label.isEmpty()) {
198                 labelString = "New" + " " + label + " " + "Component";
199             }
200         }
201         return labelString;
202     }
203 
204     public int getActivationStatus() {
205         boolean isOptionalArea = Boolean.parseBoolean(getAttribute("optional"));
206         boolean isCreatedArea = Boolean.parseBoolean(getAttribute("created"));
207 
208         if (isOptionalArea && !isCreatedArea) {
209             return -1;
210         }else if (containsAttribute(ACTIVATION_STATUS_KEY)) {
211             return Integer.parseInt(getAttribute(ACTIVATION_STATUS_KEY));
212         }
213         return NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
214     }
215 
216     public void removeFocus() {
217         if (getControlBar() != null) {
218             getControlBar().removeFocus();
219         }
220 
221         if (getAreaEndBar() != null) {
222             getAreaEndBar().removeFocus();
223         }
224     }
225 
226     public void setFocus(boolean child) {
227         if (getControlBar() != null) {
228             getControlBar().setFocus(child);
229         }
230         if (getAreaEndBar() != null) {
231             getAreaEndBar().setFocus(child);
232         }
233     }
234 
235     public void setVisible(boolean visible) {
236         if (getControlBar() != null) {
237             getControlBar().setVisible(visible);
238         }
239         if (getAreaEndBar() != null) {
240             getAreaEndBar().setVisible(visible);
241         }
242     }
243 
244     public void setPlaceHolderVisible(boolean visible) {
245         if (getComponentPlaceHolder() != null) {
246             getComponentPlaceHolder().setVisible(visible);
247         }
248     }
249 
250     public void toggleInitFocus(boolean visible) {
251         if (visible) {
252             getControlBar().addStyleName(EDITOR_INIT_CLASS_NAME);
253             getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
254             getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
255         } else {
256             getControlBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
257             getAreaEndBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
258         }
259     }
260 
261     public void onDragStart(boolean isDrag) {
262         if (getComponentPlaceHolder() != null) {
263             getComponentPlaceHolder().setStyleName("moveOngoing", isDrag);
264         }
265     }
266 }