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