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