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.ui.vaadin.gwt.client.editor.widget.placeholder;
35  
36  
37  import com.google.gwt.dom.client.Element;
38  import com.google.gwt.dom.client.NativeEvent;
39  import com.google.gwt.dom.client.Style.Cursor;
40  import com.google.gwt.event.dom.client.ClickEvent;
41  import com.google.gwt.event.dom.client.ClickHandler;
42  import com.google.gwt.event.dom.client.MouseDownEvent;
43  import com.google.gwt.event.dom.client.MouseDownHandler;
44  import com.google.gwt.event.shared.EventBus;
45  import com.google.gwt.user.client.ui.FlowPanel;
46  import com.google.gwt.user.client.ui.Label;
47  import com.google.gwt.user.client.ui.PushButton;
48  import info.magnolia.rendering.template.AreaDefinition;
49  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlElement;
50  import info.magnolia.ui.vaadin.gwt.client.editor.event.NewComponentEvent;
51  
52  import java.util.Map;
53  
54  import static info.magnolia.ui.vaadin.gwt.client.editor.jsni.JavascriptUtils.getI18nMessage;
55  
56  /**
57   * A Widget for adding components to area.
58   */
59  public class ComponentPlaceHolder extends AbstractPlaceHolder {
60  
61      protected final static String ICON_CLASSNAME = "editorIcon";
62      protected final static String ADD_CLASSNAME = "icon-add-item";
63  
64      private boolean showAddButton = false;
65      private String availableComponents = "";
66      private String type = "";
67      private String areaWorkspace = "";
68      private String areaPath = "";
69      private FlowPanel buttonWrapper;
70      private String label;
71      private String labelString;
72  
73  
74      public ComponentPlaceHolder(EventBus eventBus, MgnlElement mgnlElement) throws IllegalArgumentException {
75  
76          super(eventBus, mgnlElement);
77  
78          setFields(mgnlElement.getAttributes());
79  
80          this.addStyleName("component");
81  
82          FlowPanel controlBar = new FlowPanel();
83          controlBar.setStyleName("mgnlEditorBar");
84          controlBar.addStyleName("placeholder");
85  
86          buttonWrapper = new FlowPanel();
87          buttonWrapper.setStylePrimaryName("mgnlEditorBarButtons");
88  
89          controlBar.add(buttonWrapper);
90          labelString = getI18nMessage("buttons.component.new.js");
91          if (this.label != null && !this.label.isEmpty()) {
92              labelString = getI18nMessage("buttons.new.js") + " " + label + " " +getI18nMessage("buttons.component.js");
93          }
94  
95          Label labelName = new Label(labelString);
96          labelName.setStyleName("mgnlEditorBarLabel");
97          controlBar.add(labelName);
98  
99          add(controlBar);
100 
101         Element marker = getMgnlElement().getComponentElement();
102         boolean onlyBar = (marker != null && marker.getAttribute(AreaDefinition.CMS_ADD).equals("bar"));
103 
104         if (!onlyBar) {
105             createBoxPlaceHolder();
106         }
107 
108         setVisible(false);
109         createControls();
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                     getEventBus().fireEvent(new NewComponentEvent(areaWorkspace, areaPath, 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                     getEventBus().fireEvent(new NewComponentEvent(areaWorkspace, areaPath, availableComponents));
146                 }
147             });
148             buttonWrapper.add(button);
149         }
150     }
151 
152     private void createControls() {
153 
154         if (this.showAddButton){
155             final Label add = new Label();
156             add.setStyleName(ICON_CLASSNAME);
157             add.addStyleName(ADD_CLASSNAME);
158             add.addClickHandler(new ClickHandler() {
159                 @Override
160                 public void onClick(ClickEvent event) {
161                     getEventBus().fireEvent(new NewComponentEvent(areaWorkspace, areaPath, availableComponents));
162                 }
163             });
164             buttonWrapper.add(add);
165         }
166     }
167 
168     private void setFields(Map<String, String> attributes) throws IllegalArgumentException {
169 
170         this.showAddButton = Boolean.parseBoolean(attributes.get("showAddButton"));
171         this.type = attributes.get("type");
172 
173         this.areaWorkspace = attributes.get("workspace");
174         this.areaPath = attributes.get("path");
175 
176         this.label = getMgnlElement().getAttribute("label");
177 
178         if(AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
179             this.availableComponents = "";
180         }
181         else {
182             this.availableComponents = attributes.get("availableComponents");
183         }
184     }
185 
186 }