View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.listener.AreaListener;
46  import info.magnolia.ui.vaadin.gwt.client.widget.controlbar.ComponentPlaceHolder;
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   * <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   * 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         AreaElement area = new AreaElement(getAttribute("workspace"), getAttribute("path"), getAttribute("dialog"), getAttribute("availableComponents"));
107 
108         boolean addible = true;
109         if (getAttributes().containsKey(OperationPermissionDefinition.ADDIBLE)) {
110             addible = Boolean.parseBoolean(getAttribute(OperationPermissionDefinition.ADDIBLE));
111         }
112         area.setAddible(addible);
113 
114         return area;
115     }
116 
117     @Override
118     public void createOptionalArea() {
119         eventBus.fireEvent(new NewAreaEvent(getTypedElement()));
120     }
121 
122     @Override
123     public void editArea() {
124         eventBus.fireEvent(new EditAreaEvent(getTypedElement()));
125     }
126 
127     @Override
128     public void createNewComponent() {
129         eventBus.fireEvent(new NewComponentEvent(getTypedElement()));
130     }
131 
132     @Override
133     public boolean hasAddButton() {
134         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
135         boolean created = Boolean.parseBoolean(getAttribute("created"));
136 
137         return optional && !created;
138     }
139 
140     @Override
141     public boolean hasEditButton() {
142         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
143         boolean created = Boolean.parseBoolean(getAttribute("created"));
144         boolean dialog = null != getAttribute("dialog");
145 
146         if (dialog) {
147             // do not show edit-icon if the area has not been created
148             if (!optional || (optional && created)) {
149                 return true;
150             }
151         }
152         return false;
153     }
154 
155     @Override
156     public boolean hasAddComponentButton() {
157         return Boolean.parseBoolean(getAttribute("showAddButton"));
158     }
159 
160     @Override
161     public String getLabel() {
162         String label = getAttribute("label");
163         boolean optional = Boolean.parseBoolean(getAttribute("optional"));
164         return label + ((optional) ? " (optional)" : "");
165     }
166 
167     @Override
168     public boolean isBoxPlaceHolder() {
169         Element marker = getComponentMarkerElement();
170         boolean onlyBar = (marker != null && marker.getAttribute(AreaDefinition.CMS_ADD).equals("bar"));
171         return !onlyBar;
172     }
173 
174     @Override
175     public String getPlaceHolderLabel() {
176         String label = getAttribute("label");
177         boolean showAddButton = Boolean.parseBoolean(getAttribute("showAddButton"));
178         boolean showNewComponentArea = Boolean.parseBoolean(getAttribute("showNewComponentArea"));
179 
180         String labelString;
181         // if the add new component area should be visible
182         if (showNewComponentArea && !showAddButton) { // maximum of components is reached - show add new component area with the maximum reached message, but without the ADD button
183             labelString = getI18nMessage("buttons.component.maximum.js");
184         } else { // maximum of components is NOT reached - show add new component area with ADD button
185             labelString = getI18nMessage("buttons.component.new.js");
186             if (label != null && !label.isEmpty()) {
187                 labelString = getI18nMessage("buttons.new.js") + " " + label + " " + getI18nMessage("buttons.component.js");
188             }
189         }
190         return labelString;
191     }
192 
193     public void removeFocus() {
194         if (getControlBar() != null) {
195             getControlBar().removeFocus();
196         }
197 
198         if (getAreaEndBar() != null) {
199             getAreaEndBar().removeFocus();
200         }
201     }
202 
203     public void setFocus(boolean child) {
204         if (getControlBar() != null) {
205             getControlBar().setFocus(child);
206         }
207         if (getAreaEndBar() != null) {
208             getAreaEndBar().setFocus(child);
209         }
210     }
211 
212     public void setVisible(boolean visible) {
213         if (getControlBar() != null) {
214             getControlBar().setVisible(visible);
215         }
216         if (getAreaEndBar() != null) {
217             getAreaEndBar().setVisible(visible);
218         }
219     }
220 
221     public void setPlaceHolderVisible(boolean visible) {
222         if (getComponentPlaceHolder() != null) {
223             getComponentPlaceHolder().setVisible(visible);
224         }
225     }
226 
227     public void toggleInitFocus(boolean visible) {
228         if (visible) {
229             getControlBar().addStyleName(EDITOR_INIT_CLASS_NAME);
230             getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
231             getAreaEndBar().addStyleName(EDITOR_INIT_CLASS_NAME);
232         } else {
233             getControlBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
234             getAreaEndBar().removeStyleName(EDITOR_INIT_CLASS_NAME);
235         }
236     }
237 
238     public void onDragStart(boolean isDrag) {
239         if (getComponentPlaceHolder() != null) {
240             getComponentPlaceHolder().setStyleName("moveOngoing", isDrag);
241         }
242     }
243 }