View Javadoc
1   /**
2    * This file Copyright (c) 2013-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;
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     @Override
152     public boolean hasAddButton() {
153         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
154         boolean created = Boolean.parseBoolean(getAttribute("created"));
155 
156         return optional && !created;
157     }
158 
159     @Override
160     public boolean hasEditButton() {
161         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
162         boolean created = Boolean.parseBoolean(getAttribute("created"));
163         boolean dialog = null != getAttribute("dialog");
164 
165         if (dialog) {
166             // do not show edit-icon if the area has not been created
167             if (!optional || (optional && created)) {
168                 return true;
169             }
170         }
171         return false;
172     }
173 
174     @Override
175     public boolean hasAddComponentButton() {
176         return Boolean.parseBoolean(getAttribute("showAddButton"));
177     }
178 
179     @Override
180     public String getLabel() {
181         String label = getAttribute("label");
182         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
183         return label + ((optional) ? " (optional)" : "");
184     }
185 
186     @Override
187     public boolean isBoxPlaceHolder() {
188         Element marker = getComponentMarkerElement();
189         boolean onlyBar = (marker != null && marker.getAttribute(AreaDefinition.CMS_ADD).equals("bar"));
190         return !onlyBar;
191     }
192 
193     @Override
194     public String getPlaceHolderLabel() {
195         String label = getAttribute("label");
196         String labelString;
197 
198         // if the add new component area should be visible
199         if (isMaxComponentsReached()) { // maximum of components is reached - show add new component area with the maximum reached message, but without the ADD button
200             labelString = "Maximum of components reached.";
201         } else { // maximum of components is NOT reached - show add new component area with ADD button
202             labelString = "New Component";
203             if (label != null && !label.isEmpty()) {
204                 labelString = "New" + " " + label + " " + "Component";
205             }
206         }
207         return labelString;
208     }
209 
210     @Override
211     public int getActivationStatus() {
212         boolean isOptionalArea = Boolean.parseBoolean(getAttribute("optional"));
213         boolean isCreatedArea = Boolean.parseBoolean(getAttribute("created"));
214 
215         if (isOptionalArea && !isCreatedArea) {
216             return -1;
217         }else if (containsAttribute(ACTIVATION_STATUS_KEY)) {
218             return Integer.parseInt(getAttribute(ACTIVATION_STATUS_KEY));
219         }
220         return NodeTypes.Activatable.ACTIVATION_STATUS_NOT_ACTIVATED;
221     }
222 
223     public void removeFocus() {
224         if (getControlBar() != null) {
225             getControlBar().removeFocus();
226         }
227 
228         if (getAreaEndBar() != null) {
229             getAreaEndBar().removeFocus();
230         }
231     }
232 
233     public void setFocus(boolean child) {
234         if (getControlBar() != null) {
235             getControlBar().setFocus(child);
236         }
237         if (getAreaEndBar() != null) {
238             getAreaEndBar().setFocus(child);
239         }
240     }
241 
242     public void setVisible(boolean visible) {
243         if (getControlBar() != null) {
244             getControlBar().setVisible(visible);
245         }
246         if (getAreaEndBar() != null) {
247             getAreaEndBar().setVisible(visible);
248         }
249     }
250 
251     public void setPlaceHolderVisible(boolean visible) {
252         if (getComponentPlaceHolder() != null) {
253             getComponentPlaceHolder().setVisible(visible);
254         }
255     }
256 
257     public void toggleInitFocus(boolean visible) {
258         if (visible) {
259             getControlBar().addStyleName(EDITOR_INIT_CLASS_NAME);
260             getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
261             getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
262         } else {
263             getControlBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
264             getAreaEndBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
265         }
266     }
267 
268     public void onDragStart(boolean isDrag) {
269         if (getComponentPlaceHolder() != null) {
270             getComponentPlaceHolder().setStyleName("moveOngoing", isDrag);
271         }
272     }
273 }