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          FlowPanel elementWrapper = new FlowPanel();
94          elementWrapper.setStyleName("mgnlEditorPlaceholderElements");
95  
96          setVisible(false);
97  
98  
99          add(elementWrapper);
100 
101         createButtons();
102         PageEditor.model.addComponentPlaceHolder(mgnlElement, this);
103     }
104 
105     @SuppressWarnings("unused")
106     private void createMouseEventsHandlers() {
107 
108         if (this.optional && !this.created) {
109             if(!this.created) {
110                 addDomHandler(new MouseDownHandler() {
111 
112                     @Override
113                     public void onMouseDown(MouseDownEvent event) {
114                         PageEditor.createComponent(areaWorkspace, areaPath, "mgnl:area");
115 
116                     }
117                 }, MouseDownEvent.getType());
118             }
119 
120         }
121         else if (this.showAddButton){
122             addDomHandler(new MouseDownHandler() {
123 
124                 @Override
125                 public void onMouseDown(MouseDownEvent event) {
126                     PageEditor.addComponent(areaWorkspace, areaPath, null, availableComponents);
127 
128                 }
129             }, MouseDownEvent.getType());
130         }
131     }
132 
133     private void createButtons() {
134 
135         if (this.showAddButton){
136             PushButton button = new PushButton();
137             button.setStylePrimaryName("mgnlEditorPushButton");
138             button.addStyleName("add");
139 
140             button.addClickHandler(new ClickHandler() {
141                 @Override
142                 public void onClick(ClickEvent event) {
143                     PageEditor.addComponent(areaWorkspace, areaPath, null, availableComponents);
144                 }
145             });
146             buttonWrapper.add(button);
147         }
148     }
149 
150     public void attach() {
151         Element parent = getMgnlElement().getComponentElement();
152 
153         if (parent == null) {
154             if (getMgnlElement().getLastElement() != null && getMgnlElement().getFirstElement() == getMgnlElement().getLastElement()) {
155                 attach(getMgnlElement());
156             }
157             else {
158                 attach(getMgnlElement().getEndComment().getElement());
159             }
160         }
161         else {
162             parent.insertFirst(getElement());
163         }
164         onAttach();
165     }
166 
167     public void attach(MgnlElement mgnlElement) {
168         Element element = mgnlElement.getFirstElement();
169         if (element != null) {
170             element.appendChild(getElement());
171         }
172     }
173 
174     public void attach(Element element) {
175         final Node parentNode = element.getParentNode();
176         parentNode.insertBefore(getElement(), element);
177     }
178 
179     private void checkMandatories(Map<String, String> attributes) throws IllegalArgumentException {
180 
181         this.showAddButton = Boolean.parseBoolean(attributes.get("showAddButton"));
182         this.optional = Boolean.parseBoolean(attributes.get("optional"));
183         this.created = Boolean.parseBoolean(attributes.get("created"));
184         this.type = attributes.get("type");
185         this.name = attributes.get("name");
186 
187 
188         String areaContent = attributes.get("content");
189         int i = areaContent.indexOf(':');
190         this.areaWorkspace = areaContent.substring(0, i);
191         this.areaPath = areaContent.substring(i + 1);
192 
193         if(AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
194             this.availableComponents = "";
195         } else {
196             this.availableComponents = attributes.get("availableComponents");
197         }
198 
199         if (availableComponents.equals("")) {
200             throw new IllegalArgumentException();
201         }
202 
203         if (this.type.equals(AreaDefinition.TYPE_SINGLE) && (this.created || !getMgnlElement().getComponents().isEmpty())) {
204             throw new IllegalArgumentException();
205         }
206 
207     }
208 
209 }