View Javadoc

1   /**
2    * This file Copyright (c) 2010-2011 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.templating.editor.client.widget.placeholder;
35  
36  
37  import java.util.Map;
38  
39  import info.magnolia.rendering.template.AreaDefinition;
40  import info.magnolia.templating.editor.client.PageEditor;
41  import info.magnolia.templating.editor.client.dom.MgnlElement;
42  
43  import com.google.gwt.dom.client.Element;
44  import com.google.gwt.dom.client.Node;
45  import com.google.gwt.event.dom.client.ClickEvent;
46  import com.google.gwt.event.dom.client.ClickHandler;
47  import com.google.gwt.event.dom.client.MouseDownEvent;
48  import com.google.gwt.event.dom.client.MouseDownHandler;
49  import com.google.gwt.user.client.ui.FlowPanel;
50  import com.google.gwt.user.client.ui.Label;
51  import com.google.gwt.user.client.ui.PushButton;
52  
53  /**
54   * A Widget for adding components to area.
55   *
56   * @version $Id$
57   */
58  public class ComponentPlaceHolder extends AbstractPlaceHolder {
59  
60      private boolean optional = false;
61      private boolean created = false;
62      private boolean showAddButton = false;
63      private String availableComponents = "";
64      private String type = "";
65      private String areaWorkspace = "";
66      private String areaPath = "";
67      private String name = "";
68      private FlowPanel buttonWrapper;
69  
70      public ComponentPlaceHolder(MgnlElement mgnlElement) throws IllegalArgumentException {
71  
72          super(mgnlElement);
73  
74          checkMandatories(mgnlElement.getAttributes());
75  
76          this.addStyleName("component");
77  
78          FlowPanel controlBar = new FlowPanel();
79          controlBar.setStyleName("mgnlEditorBar");
80          controlBar.addStyleName("placeholder");
81  
82          buttonWrapper = new FlowPanel();
83          buttonWrapper.setStylePrimaryName("mgnlEditorBarButtons");
84  
85          controlBar.add(buttonWrapper);
86  
87          Label label = new Label("New Component");
88          label.setStyleName("mgnlEditorBarLabel");
89          controlBar.add(label);
90  
91          add(controlBar);
92  
93          Element marker = getMgnlElement().getComponentElement();
94          boolean onlyBar = (marker != null && marker.getAttribute(AreaDefinition.CMS_ADD).equals("bar")) ? true: false;
95  
96          if (!onlyBar) {
97              FlowPanel elementWrapper = new FlowPanel();
98              elementWrapper.setStyleName("mgnlEditorPlaceholderElements");
99              add(elementWrapper);
100         }
101 
102         setVisible(false);
103         createMouseEventsHandlers();
104         createButtons();
105         attach();
106     }
107 
108     private void createMouseEventsHandlers() {
109 
110         if (this.showAddButton){
111             addDomHandler(new MouseDownHandler() {
112 
113                 @Override
114                 public void onMouseDown(MouseDownEvent event) {
115                     PageEditor.addComponent(areaWorkspace, areaPath, null, availableComponents);
116 
117                 }
118             }, MouseDownEvent.getType());
119         }
120     }
121 
122     private void createButtons() {
123 
124         if (this.showAddButton){
125             PushButton button = new PushButton();
126             button.setStylePrimaryName("mgnlEditorPushButton");
127             button.addStyleName("add");
128 
129             button.addClickHandler(new ClickHandler() {
130                 @Override
131                 public void onClick(ClickEvent event) {
132                     PageEditor.addComponent(areaWorkspace, areaPath, null, availableComponents);
133                 }
134             });
135             buttonWrapper.add(button);
136         }
137     }
138 
139     public void attach() {
140         Element parent = getMgnlElement().getComponentElement();
141 
142         if (parent == null) {
143             if (getMgnlElement().getLastElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
144                 attach(getMgnlElement());
145             }
146             else {
147                 attach(getMgnlElement().getEndComment().getElement());
148             }
149         }
150         else {
151             parent.insertFirst(getElement());
152         }
153         onAttach();
154         PageEditor.model.addComponentPlaceHolder(getMgnlElement(), this);
155     }
156 
157     public void attach(MgnlElement mgnlElement) {
158         Element element = mgnlElement.getFirstElement();
159         if (element != null) {
160             element.appendChild(getElement());
161         }
162     }
163 
164     public void attach(Element element) {
165         final Node parentNode = element.getParentNode();
166         parentNode.insertBefore(getElement(), element);
167     }
168 
169     private void checkMandatories(Map<String, String> attributes) throws IllegalArgumentException {
170 
171         this.showAddButton = Boolean.parseBoolean(attributes.get("showAddButton"));
172         this.optional = Boolean.parseBoolean(attributes.get("optional"));
173         this.created = Boolean.parseBoolean(attributes.get("created"));
174         this.type = attributes.get("type");
175         this.name = attributes.get("name");
176 
177 
178         String areaContent = attributes.get("content");
179         int i = areaContent.indexOf(':');
180         this.areaWorkspace = areaContent.substring(0, i);
181         this.areaPath = areaContent.substring(i + 1);
182 
183         if(AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
184             this.availableComponents = "";
185         }
186         else {
187             this.availableComponents = attributes.get("availableComponents");
188         }
189 
190         if (availableComponents.equals("")) {
191             throw new IllegalArgumentException();
192         }
193 
194         if (this.type.equals(AreaDefinition.TYPE_SINGLE) && !getMgnlElement().getComponents().isEmpty()) {
195             throw new IllegalArgumentException();
196         }
197 
198 
199     }
200 
201 }