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