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.widget.placeholder;
35  
36  import static info.magnolia.ui.vaadin.gwt.client.editor.jsni.JavascriptUtils.getI18nMessage;
37  
38  import info.magnolia.rendering.template.AreaDefinition;
39  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlElement;
40  import info.magnolia.ui.vaadin.gwt.client.editor.event.NewComponentEvent;
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.Style.Cursor;
47  import com.google.gwt.event.dom.client.ClickEvent;
48  import com.google.gwt.event.dom.client.ClickHandler;
49  import com.google.gwt.event.dom.client.MouseDownEvent;
50  import com.google.gwt.event.dom.client.MouseDownHandler;
51  import com.google.gwt.user.client.ui.FlowPanel;
52  import com.google.gwt.user.client.ui.Label;
53  import com.google.gwt.user.client.ui.PushButton;
54  import com.google.web.bindery.event.shared.EventBus;
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      public ComponentPlaceHolder(EventBus eventBus, MgnlElement mgnlElement) throws IllegalArgumentException {
74  
75          super(eventBus, mgnlElement);
76  
77          setFields(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"));
102 
103         if (!onlyBar) {
104             createBoxPlaceHolder();
105         }
106 
107         setVisible(false);
108         createControls();
109     }
110 
111     private void createBoxPlaceHolder() {
112 
113         FlowPanel elementWrapper = new FlowPanel();
114         elementWrapper.setStyleName("mgnlEditorPlaceholderElements");
115 
116         if (this.showAddButton) {
117             elementWrapper.getElement().getStyle().setCursor(Cursor.POINTER);
118             elementWrapper.addDomHandler(new MouseDownHandler() {
119 
120                 @Override
121                 public void onMouseDown(MouseDownEvent event) {
122                     if (event.getNativeButton() == NativeEvent.BUTTON_RIGHT) {
123                         return;
124                     }
125                     getEventBus().fireEvent(new NewComponentEvent(areaWorkspace, areaPath, availableComponents));
126                 }
127             }, MouseDownEvent.getType());
128         }
129         add(elementWrapper);
130 
131     }
132 
133     private void createButtons() {
134 
135         if (this.showAddButton) {
136             PushButton button = new PushButton();
137             button.setTitle(getI18nMessage("buttons.add.js") + " " + labelString);
138             button.setStylePrimaryName("mgnlEditorPushButton");
139             button.addStyleName("add");
140 
141             button.addClickHandler(new ClickHandler() {
142                 @Override
143                 public void onClick(ClickEvent event) {
144                     getEventBus().fireEvent(new NewComponentEvent(areaWorkspace, areaPath, availableComponents));
145                 }
146             });
147             buttonWrapper.add(button);
148         }
149     }
150 
151     private void createControls() {
152 
153         if (this.showAddButton) {
154             final Label add = new Label();
155             add.setStyleName(ICON_CLASSNAME);
156             add.addStyleName(ADD_CLASSNAME);
157             add.addClickHandler(new ClickHandler() {
158                 @Override
159                 public void onClick(ClickEvent event) {
160                     getEventBus().fireEvent(new NewComponentEvent(areaWorkspace, areaPath, availableComponents));
161                 }
162             });
163             buttonWrapper.add(add);
164         }
165     }
166 
167     private void setFields(Map<String, String> attributes) throws IllegalArgumentException {
168 
169         this.showAddButton = Boolean.parseBoolean(attributes.get("showAddButton"));
170         this.type = attributes.get("type");
171 
172         this.areaWorkspace = attributes.get("workspace");
173         this.areaPath = attributes.get("path");
174 
175         this.label = getMgnlElement().getAttribute("label");
176 
177         if (AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
178             this.availableComponents = "";
179         } else {
180             this.availableComponents = attributes.get("availableComponents");
181         }
182     }
183 
184 }