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 info.magnolia.rendering.template.AreaDefinition;
37  import info.magnolia.templating.editor.client.dom.CMSComment;
38  import info.magnolia.templating.editor.client.dom.MgnlElement;
39  
40  import com.google.gwt.dom.client.Style.Float;
41  import com.google.gwt.event.dom.client.ClickEvent;
42  import com.google.gwt.event.dom.client.ClickHandler;
43  import com.google.gwt.event.dom.client.MouseDownEvent;
44  import com.google.gwt.event.dom.client.MouseDownHandler;
45  import com.google.gwt.event.dom.client.MouseOutEvent;
46  import com.google.gwt.event.dom.client.MouseOutHandler;
47  import com.google.gwt.event.dom.client.MouseOverEvent;
48  import com.google.gwt.event.dom.client.MouseOverHandler;
49  import com.google.gwt.user.client.ui.Button;
50  
51  import static info.magnolia.templating.editor.client.PageEditor.getDictionary;
52  
53  /**
54   * Edit bar.
55   */
56  public class EditBarWidget extends AbstractBarWidget {
57  
58      private String workspace;
59      private String path;
60      private String dialog;
61      private String id;
62      private String parentAreaType;
63      private boolean isInherited;
64  
65      public EditBarWidget(MgnlElement mgnlElement, CMSComment comment, final PageEditor pageEditor) {
66  
67          super(mgnlElement, comment);
68          String content = comment.getAttribute("content");
69          int i = content.indexOf(':');
70          this.workspace = content.substring(0, i);
71          this.path = content.substring(i + 1);
72  
73          this.id = path.substring(path.lastIndexOf("/") + 1);
74  
75          setId("__"+id);
76  
77          this.dialog = comment.getAttribute("dialog");
78  
79          if(mgnlElement.getParentArea() != null) {
80              this.parentAreaType = mgnlElement.getParentArea().getComment().getAttribute("type");
81          }
82  
83          this.isInherited = Boolean.parseBoolean(comment.getAttribute("inherited"));
84  
85          createButtons(pageEditor);
86  
87          createMouseEventsHandlers(pageEditor);
88  
89          setClassName("mgnlEditBar");
90          if (this.isInherited) {
91              addStyleName("mgnlInherited");
92          }
93  //        getElement().getStyle().setVisibility(Style.Visibility.HIDDEN);
94          setVisible(false);
95      }
96  
97      private void createMouseEventsHandlers(final PageEditor pageEditor) {
98  
99          addDomHandler(new MouseDownHandler() {
100 
101             @Override
102             public void onMouseDown(MouseDownEvent event) {
103                 String parentPath = path.substring(0, path.lastIndexOf("/"));
104                 pageEditor.moveComponentEnd((EditBarWidget)event.getSource(), parentPath);
105 
106             }
107         }, MouseDownEvent.getType());
108 
109         addDomHandler(new MouseOverHandler() {
110 
111             @Override
112             public void onMouseOver(MouseOverEvent event) {
113                 pageEditor.moveComponentOver((EditBarWidget)event.getSource());
114             }
115         }, MouseOverEvent.getType());
116 
117         addDomHandler(new MouseOutHandler() {
118 
119             @Override
120             public void onMouseOut(MouseOutEvent event) {
121                 pageEditor.moveComponentOut((EditBarWidget)event.getSource());
122             }
123         }, MouseOutEvent.getType());
124     }
125 
126     private void createButtons(final PageEditor pageEditor) {
127 
128         if (!this.isInherited) {
129             final Button edit = new Button(getDictionary().get("buttons.edit.js"));
130             edit.addClickHandler(new ClickHandler() {
131                 @Override
132                 public void onClick(ClickEvent event) {
133                     pageEditor.openDialog(dialog, workspace, path, null, null);
134 
135                 }
136             });
137             addButton(edit, Float.RIGHT);
138         }
139 
140         //single area component obviously cannot be moved
141         if(AreaDefinition.TYPE_LIST.equals(parentAreaType)) {
142             final Button move = new Button(getDictionary().get("buttons.move.js"));
143             move.addClickHandler(new ClickHandler() {
144                 @Override
145                 public void onClick(ClickEvent event) {
146                     pageEditor.moveComponentStart(id);
147                 }
148             });
149             addButton(move, Float.RIGHT);
150         }
151 
152         if (!this.isInherited) {
153             final Button removeButton = new Button(getDictionary().get("buttons.delete.js"));
154             removeButton.addClickHandler(new ClickHandler() {
155                 @Override
156                 public void onClick(ClickEvent event) {
157                     pageEditor.deleteComponent(path);
158                 }
159             });
160             removeButton.addStyleName("mgnlRemoveButton");
161             addButton(removeButton, Float.RIGHT);
162         }
163     }
164 
165 }