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.templating.editor.client;
35  
36  import static info.magnolia.templating.editor.client.PageEditor.getDictionary;
37  
38  import info.magnolia.rendering.template.AreaDefinition;
39  import info.magnolia.templating.editor.client.dom.CMSBoundary;
40  import info.magnolia.templating.editor.client.dom.CMSComment;
41  
42  import com.google.gwt.core.client.GWT;
43  import com.google.gwt.dom.client.Style;
44  import com.google.gwt.dom.client.Style.Float;
45  import com.google.gwt.event.dom.client.ClickEvent;
46  import com.google.gwt.event.dom.client.ClickHandler;
47  import com.google.gwt.event.dom.client.MouseDownEvent;
48  import com.google.gwt.event.dom.client.MouseDownHandler;
49  import com.google.gwt.event.dom.client.MouseUpEvent;
50  import com.google.gwt.event.dom.client.MouseUpHandler;
51  import com.google.gwt.user.client.ui.Button;
52  
53  
54  /**
55   * Area bar.
56   */
57  public class AreaBarWidget extends AbstractBarWidget {
58  
59      private String workspace;
60      private String path;
61  
62      private String areaWorkspace;
63      private String areaPath;
64  
65      private String name;
66      private String availableComponents;
67      private String type;
68      private String dialog;
69      private boolean showAddButton = true;
70      private boolean optional = false;
71      private boolean created = true;
72  
73      public AreaBarWidget(CMSBoundary boundary, final PageEditor pageEditor) {
74          super(boundary);
75  
76          String content = boundary.getComment().getAttribute("content");
77          if (content != null) {
78              int i = content.indexOf(':');
79  
80              this.workspace = content.substring(0, i);
81              this.path = content.substring(i + 1);
82             }
83  
84  
85          CMSBoundary area = boundary.getParentArea();
86          boolean showBar = true;
87  
88          for (CMSBoundary parentArea = boundary.getParentArea(); parentArea != null; parentArea = parentArea.getParentArea()) {
89              for (CMSBoundary child : parentArea.getChildBoundaries()) {
90                  if (child.getWidget() != null) {
91                      showBar = false;
92                      break;
93                  }
94              }
95              if (showBar == false) {
96                  break;
97              }
98          }
99  
100         if (!showBar) {
101 
102             getElement().getStyle().setVisibility(Style.Visibility.HIDDEN);
103         }
104         String areaContent = area.getComment().getAttribute("content");
105         int i = areaContent.indexOf(':');
106         this.areaWorkspace = areaContent.substring(0, i);
107         this.areaPath = areaContent.substring(i + 1);
108 
109         this.name = area.getComment().getAttribute("name");
110         this.type = area.getComment().getAttribute("type");
111 
112         GWT.log("Area ["+this.name+"] is of type " + this.type);
113 
114         if(AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
115             this.availableComponents = "";
116         } else {
117             this.availableComponents = area.getComment().getAttribute("availableComponents");
118         }
119 
120         this.dialog = boundary.getComment().getAttribute("dialog");
121         if (area.getComment().hasAttribute("showAddButton")) {
122             this.showAddButton = Boolean.parseBoolean(area.getComment().getAttribute("showAddButton"));
123         }
124         if (area.getComment().hasAttribute("optional")) {
125             this.optional = Boolean.parseBoolean(area.getComment().getAttribute("optional"));
126             this.created = Boolean.parseBoolean(area.getComment().getAttribute("created"));
127         }
128 
129 
130         createButtons(pageEditor, boundary.getComment());
131         setClassName("mgnlAreaEditBar");
132 
133 
134         addDomHandler(new MouseDownHandler() {
135             @Override
136             public void onMouseDown(MouseDownEvent event) {
137                 //select();
138             }
139         }, MouseDownEvent.getType());
140 
141         addDomHandler(new MouseUpHandler() {
142             @Override
143             public void onMouseUp(MouseUpEvent event) {
144                 VisibilityHelper.getInstance().toggleVisibility(getBoundary().getParentArea());
145             }
146         }, MouseUpEvent.getType());
147 
148     }
149 
150     public String getAvailableComponents() {
151         return availableComponents;
152     }
153 
154     public String getType() {
155         return type;
156     }
157 
158     private void createButtons(final PageEditor pageEditor, final CMSComment comment) {
159         if(this.optional) {
160             if(!this.created) {
161                 Button createButton = new Button(getDictionary().get("buttons.create.js"));
162                 createButton.addClickHandler(new ClickHandler() {
163                     @Override
164                     public void onClick(ClickEvent event) {
165                         pageEditor.createComponent(areaWorkspace, areaPath, name, "mgnl:area");
166                     }
167                 });
168                 addButton(createButton, Float.RIGHT);
169 
170             } else {
171                 createEditAndAddComponentButtons(pageEditor, comment);
172 
173                 Button removeButton = new Button(getDictionary().get("buttons.remove.js"));
174                 removeButton.addClickHandler(new ClickHandler() {
175                     @Override
176                     public void onClick(ClickEvent event) {
177                         pageEditor.deleteComponent(path);
178                     }
179                 });
180                 removeButton.addStyleName("mgnlRemoveButton");
181                 addButton(removeButton, Float.RIGHT);
182             }
183         } else {
184             createEditAndAddComponentButtons(pageEditor, comment);
185         }
186     }
187 
188     private void createEditAndAddComponentButtons(final PageEditor pageEditor, final CMSComment comment) {
189         if (comment.hasAttribute("dialog")) {
190             Button editButton = new Button(getDictionary().get("buttons.edit.js"));
191             editButton.addClickHandler(new ClickHandler() {
192                 @Override
193                 public void onClick(ClickEvent event) {
194                     pageEditor.openDialog(dialog, workspace, path, null, null);
195                 }
196             });
197             addButton(editButton, Float.RIGHT);
198         }
199 
200         if (this.showAddButton) {
201             Button addButton = new Button(getDictionary().get("buttons.add.js"));
202             addButton.addClickHandler(new ClickHandler() {
203                 @Override
204                 public void onClick(ClickEvent event) {
205                     if (!AreaDefinition.TYPE_NO_COMPONENT.equals(type)) {
206                         pageEditor.addComponent(areaWorkspace, areaPath, name, null, availableComponents);
207                     }
208                 }
209             });
210             addButton(addButton, Float.RIGHT);
211         }
212     }
213 }