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  import static info.magnolia.templating.editor.client.jsni.JavascriptUtils.getI18nMessage;
37  
38  import java.util.Map;
39  
40  import info.magnolia.rendering.template.AreaDefinition;
41  import info.magnolia.templating.editor.client.PageEditor;
42  import info.magnolia.templating.editor.client.dom.MgnlElement;
43  
44  import com.google.gwt.dom.client.Element;
45  import com.google.gwt.dom.client.Node;
46  import com.google.gwt.event.dom.client.ClickEvent;
47  import com.google.gwt.event.dom.client.ClickHandler;
48  import com.google.gwt.event.dom.client.MouseDownEvent;
49  import com.google.gwt.event.dom.client.MouseDownHandler;
50  import com.google.gwt.user.client.ui.Button;
51  import com.google.gwt.user.client.ui.FlowPanel;
52  import com.google.gwt.user.client.ui.Label;
53  
54  /**
55   * A Widget for adding components to area.
56   *
57   * @version $Id$
58   */
59  public class ComponentPlaceHolder extends AbstractPlaceHolder {
60  
61      private boolean optional = false;
62      private boolean created = false;
63      private boolean showAddButton = false;
64      private String availableComponents = "";
65      private String type = "";
66      private String areaWorkspace = "";
67      private String areaPath = "";
68      private String name = "";
69      private FlowPanel buttonWrapper;
70  
71      public ComponentPlaceHolder(MgnlElement mgnlElement) throws IllegalArgumentException {
72  
73          super(mgnlElement);
74  
75          checkMandatories(mgnlElement.getComment().getAttributes());
76  
77          this.addStyleName("component");
78  
79          FlowPanel controlBar = new FlowPanel();
80          controlBar.setStyleName("mgnlEditorBar");
81          controlBar.addStyleName("placeholder");
82  
83          buttonWrapper = new FlowPanel();
84          buttonWrapper.setStylePrimaryName("mgnlEditorBarButtons");
85  
86          controlBar.add(buttonWrapper);
87  
88          Label label = new Label("New Component");
89          label.setStyleName("mgnlEditorBarLabel");
90          controlBar.add(label);
91  
92          add(controlBar);
93  
94          FlowPanel elementWrapper = new FlowPanel();
95          elementWrapper.setStyleName("mgnlEditorPlaceholderElements");
96  
97          setVisible(false);
98  
99  
100         add(elementWrapper);
101 
102         createButtons();
103         PageEditor.model.addComponentPlaceHolder(mgnlElement, this);
104     }
105 
106     @SuppressWarnings("unused")
107     private void createMouseEventsHandlers() {
108 
109         if (this.optional && !this.created) {
110             if(!this.created) {
111                 addDomHandler(new MouseDownHandler() {
112 
113                     @Override
114                     public void onMouseDown(MouseDownEvent event) {
115                         PageEditor.createComponent(areaWorkspace, areaPath, "mgnl:area");
116 
117                     }
118                 }, MouseDownEvent.getType());
119             }
120 
121         }
122         else if (this.showAddButton){
123             addDomHandler(new MouseDownHandler() {
124 
125                 @Override
126                 public void onMouseDown(MouseDownEvent event) {
127                     PageEditor.addComponent(areaWorkspace, areaPath, null, availableComponents);
128 
129                 }
130             }, MouseDownEvent.getType());
131         }
132     }
133 
134     private void createButtons() {
135 
136         if (this.showAddButton){
137             Button button = new Button(getI18nMessage("buttons.add.js"));
138             button.setStylePrimaryName("mgnlEditorButton");
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 }