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