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