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.CMSComment;
40  import info.magnolia.templating.editor.client.dom.MgnlElement;
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  
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(MgnlElement mgnlElement, CMSComment comment, final PageEditor pageEditor) {
70          super(mgnlElement, comment);
71  
72          String content = mgnlElement.getComment().getAttribute("content");
73          if (content != null) {
74              int i = content.indexOf(':');
75  
76              this.workspace = content.substring(0, i);
77              this.path = content.substring(i + 1);
78             }
79  
80          setVisible(false);
81  
82          String areaContent = mgnlElement.getComment().getAttribute("content");
83          int i = areaContent.indexOf(':');
84          this.areaWorkspace = areaContent.substring(0, i);
85          this.areaPath = areaContent.substring(i + 1);
86  
87          this.name = mgnlElement.getComment().getAttribute("name");
88          this.type = mgnlElement.getComment().getAttribute("type");
89  
90          GWT.log("Area ["+this.name+"] is of type " + this.type);
91  
92          if(AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
93              this.availableComponents = "";
94          } else {
95              this.availableComponents = mgnlElement.getComment().getAttribute("availableComponents");
96          }
97  
98          this.dialog = comment.getAttribute("dialog");
99          if (mgnlElement.getComment().hasAttribute("showAddButton")) {
100             this.showAddButton = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("showAddButton"));
101         }
102         if (comment.hasAttribute("optional")) {
103             this.optional = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("optional"));
104             this.created = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("created"));
105         }
106 
107 
108         createButtons(pageEditor, mgnlElement.getComment());
109         setClassName("mgnlAreaEditBar");
110     }
111 
112     public String getAvailableComponents() {
113         return availableComponents;
114     }
115 
116     public String getType() {
117         return type;
118     }
119 
120     private void createButtons(final PageEditor pageEditor, final CMSComment comment) {
121         if(this.optional) {
122             if(!this.created) {
123                 Button createButton = new Button(getDictionary().get("buttons.create.js"));
124                 createButton.addClickHandler(new ClickHandler() {
125                     @Override
126                     public void onClick(ClickEvent event) {
127                         pageEditor.createComponent(areaWorkspace, areaPath, name, "mgnl:area");
128                     }
129                 });
130                 addButton(createButton, Float.RIGHT);
131 
132             } else {
133                 createEditAndAddComponentButtons(pageEditor, comment);
134 
135                 Button removeButton = new Button(getDictionary().get("buttons.remove.js"));
136                 removeButton.addClickHandler(new ClickHandler() {
137                     @Override
138                     public void onClick(ClickEvent event) {
139                         pageEditor.deleteComponent(path);
140                     }
141                 });
142                 removeButton.addStyleName("mgnlRemoveButton");
143                 addButton(removeButton, Float.RIGHT);
144             }
145         } else {
146             createEditAndAddComponentButtons(pageEditor, comment);
147         }
148     }
149 
150     private void createEditAndAddComponentButtons(final PageEditor pageEditor, final CMSComment comment) {
151         if (comment.hasAttribute("dialog")) {
152             Button editButton = new Button(getDictionary().get("buttons.edit.js"));
153             editButton.addClickHandler(new ClickHandler() {
154                 @Override
155                 public void onClick(ClickEvent event) {
156                     pageEditor.openDialog(dialog, workspace, path, null, null);
157                 }
158             });
159             addButton(editButton, Float.RIGHT);
160         }
161 
162         if (this.showAddButton) {
163             Button addButton = new Button(getDictionary().get("buttons.add.js"));
164             addButton.addClickHandler(new ClickHandler() {
165                 @Override
166                 public void onClick(ClickEvent event) {
167                     if (!AreaDefinition.TYPE_NO_COMPONENT.equals(type)) {
168                         pageEditor.addComponent(areaWorkspace, areaPath, name, null, availableComponents);
169                     }
170                 }
171             });
172             addButton(addButton, Float.RIGHT);
173         }
174     }
175 }