View Javadoc

1   /**
2    * This file Copyright (c) 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.controlbar;
35  
36  import com.google.gwt.core.client.GWT;
37  import com.google.gwt.event.dom.client.ClickEvent;
38  import com.google.gwt.event.dom.client.ClickHandler;
39  import com.google.gwt.event.shared.EventBus;
40  import com.google.gwt.user.client.ui.Label;
41  import com.google.gwt.user.client.ui.PushButton;
42  import info.magnolia.rendering.template.AreaDefinition;
43  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlElement;
44  import info.magnolia.ui.vaadin.gwt.client.editor.event.DeleteComponentEvent;
45  import info.magnolia.ui.vaadin.gwt.client.editor.event.EditComponentEvent;
46  import info.magnolia.ui.vaadin.gwt.client.editor.event.NewAreaEvent;
47  
48  import java.util.Map;
49  
50  import static info.magnolia.ui.vaadin.gwt.client.editor.jsni.JavascriptUtils.getI18nMessage;
51  
52  
53  /**
54   * Area bar.
55   */
56  public class AreaBar extends AbstractBar {
57  
58      private String name;
59  
60      private String type;
61  
62      private String dialog;
63  
64      private String availableComponents;
65  
66      private boolean optional;
67  
68      private boolean created;
69  
70      private boolean editable = true;
71  
72      private static final String NODE_TYPE = "mgnl:area";
73  
74      public AreaBar(EventBus eventBus, MgnlElement mgnlElement) {
75          super(eventBus, mgnlElement);
76  
77          setFields(mgnlElement.getAttributes());
78  
79          GWT.log("Area [" + this.name + "] is of type " + this.type);
80  
81          this.addStyleName("area");
82  
83          setVisible(false);
84          createControls();
85  
86      }
87  
88      private void createButtons() {
89  
90          if (this.dialog != null) {
91              // do not show edit-icon if the area has not been created
92              if (!this.optional || (this.optional && this.created)) {
93                  PushButton editButton = new PushButton();
94                  editButton.addClickHandler(new ClickHandler() {
95  
96                      @Override
97                      public void onClick(ClickEvent event) {
98                          getEventBus().fireEvent(new EditComponentEvent(dialog, getWorkspace(), getPath()));
99                      }
100                 });
101                 editButton.setTitle(getI18nMessage("buttons.area.edit.js"));
102                 editButton.setStylePrimaryName("mgnlEditorPushButton");
103                 editButton.addStyleName("edit");
104                 addPrimaryButton(editButton);
105             }
106         }
107         if (this.optional) {
108             if (this.created) {
109                 PushButton removeButton = new PushButton();
110                 removeButton.addClickHandler(new ClickHandler() {
111 
112                     @Override
113                     public void onClick(ClickEvent event) {
114                         getEventBus().fireEvent(new DeleteComponentEvent(getWorkspace(), getPath()));
115                     }
116                 });
117                 removeButton.setTitle(getI18nMessage("buttons.area.delete.js"));
118                 removeButton.setStylePrimaryName("mgnlEditorPushButton");
119                 removeButton.addStyleName("remove");
120                 addSecondaryButton(removeButton);
121             }
122             else {
123                 PushButton createbutton = new PushButton();
124                 createbutton.setStylePrimaryName("mgnlEditorButton");
125 
126                 createbutton.addClickHandler(new ClickHandler() {
127 
128                     @Override
129                     public void onClick(ClickEvent event) {
130                         getEventBus().fireEvent(new NewAreaEvent(getWorkspace(), NODE_TYPE, getPath()));
131                     }
132                 });
133                 createbutton.setTitle(getI18nMessage("buttons.area.add.js"));
134                 createbutton.setStylePrimaryName("mgnlEditorPushButton");
135                 createbutton.addStyleName("add");
136                 addSecondaryButton(createbutton);
137             }
138         }
139 
140     }
141 
142     private void createControls() {
143         if (this.optional) {
144             if (this.created) {
145                 final Label remove = new Label();
146                 remove.setStyleName(ICON_CLASSNAME);
147                 remove.addStyleName(REMOVE_CLASSNAME);
148                 remove.addClickHandler(new ClickHandler() {
149                     @Override
150                     public void onClick(ClickEvent event) {
151                         getEventBus().fireEvent(new DeleteComponentEvent(getWorkspace(), getPath()));
152                     }
153                 });
154                 addSecondaryButton(remove);
155 
156             }
157             else {
158                 final Label add = new Label();
159                 add.setStyleName(ICON_CLASSNAME);
160                 add.addStyleName(ADD_CLASSNAME);
161                 add.addClickHandler(new ClickHandler() {
162                     @Override
163                     public void onClick(ClickEvent event) {
164                         getEventBus().fireEvent(new NewAreaEvent(getWorkspace(), NODE_TYPE, getPath()));
165                     }
166                 });
167                 addSecondaryButton(add);
168             }
169         }
170 
171         if (this.dialog != null) {
172             // do not show edit-icon if the area has not been created
173             if (!this.optional || (this.optional && this.created)) {
174                 final Label edit = new Label();
175                 edit.setStyleName(ICON_CLASSNAME);
176                 edit.addStyleName(EDIT_CLASSNAME);
177                 edit.addClickHandler(new ClickHandler() {
178 
179                     @Override
180                     public void onClick(ClickEvent event) {
181                         getEventBus().fireEvent(new EditComponentEvent(getWorkspace(), getPath(), dialog));
182                     }
183                 });
184                 addPrimaryButton(edit);
185             }
186         }
187 
188     }
189 
190     private void setFields(Map<String, String> attributes) throws IllegalArgumentException {
191 
192         setWorkspace(attributes.get("workspace"));
193         setPath(attributes.get("path"));
194 
195         this.name = attributes.get("name");
196         this.type = attributes.get("type");
197 
198         this.dialog = attributes.get("dialog");
199 
200         if (attributes.containsKey("editable")) {
201             this.editable = Boolean.parseBoolean(attributes.get("editable"));
202         }
203 
204         availableComponents = "";
205         if (!AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
206             availableComponents = attributes.get("availableComponents");
207         }
208 
209         boolean showAddButton = Boolean.parseBoolean(attributes.get("showAddButton"));
210         this.optional = Boolean.parseBoolean(attributes.get("optional"));
211         this.created = Boolean.parseBoolean(attributes.get("created"));
212     }
213 
214     @Override
215     public String getDialog() {
216         return editable ? dialog : null;
217     }
218 
219     public String getAvailableComponents() {
220         return availableComponents;
221     }
222 
223 }