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