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  
37  import info.magnolia.rendering.template.AreaDefinition;
38  import info.magnolia.templating.editor.client.dom.MgnlElement;
39  import static info.magnolia.templating.editor.client.jsni.LegacyJavascript.*;
40  
41  import com.google.gwt.core.client.GWT;
42  import com.google.gwt.dom.client.Style.Float;
43  import com.google.gwt.event.dom.client.ClickEvent;
44  import com.google.gwt.event.dom.client.ClickHandler;
45  
46  import com.google.gwt.user.client.ui.Button;
47  import com.google.gwt.dom.client.Element;
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 name;
59      private String availableComponents;
60      private String type;
61      private String dialog;
62      private boolean showAddButton = true;
63      private boolean optional = false;
64      private boolean created = true;
65  
66      public AreaBarWidget(MgnlElement mgnlElement, final PageEditor pageEditor) {
67          super(mgnlElement);
68  
69          String content = mgnlElement.getComment().getAttribute("content");
70          if (content != null) {
71              int i = content.indexOf(':');
72  
73              this.workspace = content.substring(0, i);
74              this.path = content.substring(i + 1);
75             }
76  
77          setVisible(false);
78  
79          this.name = mgnlElement.getComment().getAttribute("name");
80          this.type = mgnlElement.getComment().getAttribute("type");
81  
82          GWT.log("Area ["+this.name+"] is of type " + this.type);
83  
84          if(AreaDefinition.TYPE_NO_COMPONENT.equals(this.type)) {
85              this.availableComponents = "";
86          } else {
87              this.availableComponents = mgnlElement.getComment().getAttribute("availableComponents");
88          }
89  
90          this.dialog = mgnlElement.getComment().getAttribute("dialog");
91          if (mgnlElement.getComment().hasAttribute("showAddButton")) {
92              this.showAddButton = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("showAddButton"));
93          }
94          if (mgnlElement.getComment().hasAttribute("optional")) {
95              this.optional = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("optional"));
96              this.created = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("created"));
97          }
98  
99  
100         createButtons(pageEditor);
101 
102         this.addStyleName("area");
103     }
104 
105     public void attach(MgnlElement mgnlElement) {
106         Element element = mgnlElement.getFirstElement();
107         if (element != null) {
108             element.insertFirst(getElement());
109         }
110         onAttach();
111     }
112 
113     public String getAvailableComponents() {
114         return availableComponents;
115     }
116 
117     public String getType() {
118         return type;
119     }
120 
121     private void createButtons(final PageEditor pageEditor) {
122         if(this.optional) {
123             if(!this.created) {
124                 Button createButton = new Button(getI18nMessage("buttons.create.js"));
125                 createButton.addClickHandler(new ClickHandler() {
126                     @Override
127                     public void onClick(ClickEvent event) {
128                         pageEditor.createComponent(workspace, path, "mgnl:area");
129                     }
130                 });
131                 addButton(createButton, Float.RIGHT);
132 
133             } else {
134                 createEditAndAddComponentButtons(pageEditor);
135 
136                 Button removeButton = new Button(getI18nMessage("buttons.remove.js"));
137                 removeButton.addClickHandler(new ClickHandler() {
138                     @Override
139                     public void onClick(ClickEvent event) {
140                         pageEditor.deleteComponent(path);
141                     }
142                 });
143                 removeButton.addStyleName("mgnlRemoveButton");
144                 addButton(removeButton, Float.RIGHT);
145             }
146         } else {
147             createEditAndAddComponentButtons(pageEditor);
148         }
149     }
150 
151     private void createEditAndAddComponentButtons(final PageEditor pageEditor) {
152         if (this.dialog != null) {
153             Button editButton = new Button(getI18nMessage("buttons.edit.js"));
154             editButton.addClickHandler(new ClickHandler() {
155                 @Override
156                 public void onClick(ClickEvent event) {
157                     pageEditor.openDialog(dialog, workspace, path, null, null);
158                 }
159             });
160             addButton(editButton, Float.RIGHT);
161         }
162 
163         if (this.showAddButton) {
164             Button addButton = new Button(getI18nMessage("buttons.add.js"));
165             addButton.addClickHandler(new ClickHandler() {
166                 @Override
167                 public void onClick(ClickEvent event) {
168                     if (!AreaDefinition.TYPE_NO_COMPONENT.equals(type)) {
169                         pageEditor.addComponent(workspace, path, null, availableComponents);
170                     }
171                 }
172             });
173             addButton(addButton, Float.RIGHT);
174         }
175     }
176 
177 
178 }