View Javadoc

1   /**
2    * This file Copyright (c) 2013-2014 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 
121         area.setOptional(optional);
122         area.setCreated(created);
123         area.setAddible(addible && hasAvailableComponents && !(optional && !created) && !(areaIsTypeSingle && areaHasChildComponents) && !isMaxComponentsReached());
124 
125         return area;
126     }
127 
128     private boolean isMaxComponentsReached() {
129         boolean showAddButton = Boolean.parseBoolean(getAttribute("showAddButton"));
130         boolean showNewComponentArea = Boolean.parseBoolean(getAttribute("showNewComponentArea"));
131 
132         return showNewComponentArea && !showAddButton;
133     }
134 
135     @Override
136     public void createOptionalArea() {
137         eventBus.fireEvent(new NewAreaEvent(getTypedElement()));
138     }
139 
140     @Override
141     public void editArea() {
142         eventBus.fireEvent(new EditAreaEvent(getTypedElement()));
143     }
144 
145     @Override
146     public void createNewComponent() {
147         eventBus.fireEvent(new NewComponentEvent(getTypedElement()));
148     }
149 
150     @Override
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     @Override
159     public boolean hasEditButton() {
160         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
161         boolean created = Boolean.parseBoolean(getAttribute("created"));
162         boolean dialog = null != getAttribute("dialog");
163 
164         if (dialog) {
165             // do not show edit-icon if the area has not been created
166             if (!optional || (optional && created)) {
167                 return true;
168             }
169         }
170         return false;
171     }
172 
173     @Override
174     public boolean hasAddComponentButton() {
175         return Boolean.parseBoolean(getAttribute("showAddButton"));
176     }
177 
178     @Override
179     public String getLabel() {
180         String label = getAttribute("label");
181         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
182         return label + ((optional) ? " (optional)" : "");
183     }
184 
185     @Override
186     public boolean isBoxPlaceHolder() {
187         Element marker = getComponentMarkerElement();
188         boolean onlyBar = (marker != null && marker.getAttribute(AreaDefinition.CMS_ADD).equals("bar"));
189         return !onlyBar;
190     }
191 
192     @Override
193     public String getPlaceHolderLabel() {
194         String label = getAttribute("label");
195         String labelString;
196 
197         // if the add new component area should be visible
198         if (isMaxComponentsReached()) { // maximum of components is reached - show add new component area with the maximum reached message, but without the ADD button
199             labelString = getI18nMessage("buttons.component.maximum.js");
200         } else { // maximum of components is NOT reached - show add new component area with ADD button
201             labelString = getI18nMessage("buttons.component.new.js");
202             if (label != null && !label.isEmpty()) {
203                 labelString = getI18nMessage("buttons.new.js") + " " + label + " " + getI18nMessage("buttons.component.js");
204             }
205         }
206         return labelString;
207     }
208 
209     public void removeFocus() {
210         if (getControlBar() != null) {
211             getControlBar().removeFocus();
212         }
213 
214         if (getAreaEndBar() != null) {
215             getAreaEndBar().removeFocus();
216         }
217     }
218 
219     public void setFocus(boolean child) {
220         if (getControlBar() != null) {
221             getControlBar().setFocus(child);
222         }
223         if (getAreaEndBar() != null) {
224             getAreaEndBar().setFocus(child);
225         }
226     }
227 
228     public void setVisible(boolean visible) {
229         if (getControlBar() != null) {
230             getControlBar().setVisible(visible);
231         }
232         if (getAreaEndBar() != null) {
233             getAreaEndBar().setVisible(visible);
234         }
235     }
236 
237     public void setPlaceHolderVisible(boolean visible) {
238         if (getComponentPlaceHolder() != null) {
239             getComponentPlaceHolder().setVisible(visible);
240         }
241     }
242 
243     public void toggleInitFocus(boolean visible) {
244         if (visible) {
245             getControlBar().addStyleName(EDITOR_INIT_CLASS_NAME);
246             getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
247             getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
248         } else {
249             getControlBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
250             getAreaEndBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
251         }
252     }
253 
254     public void onDragStart(boolean isDrag) {
255         if (getComponentPlaceHolder() != null) {
256             getComponentPlaceHolder().setStyleName("moveOngoing", isDrag);
257         }
258     }
259 }