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.widget.controlbar;
35  
36  import info.magnolia.templating.editor.client.PageEditor;
37  import info.magnolia.templating.editor.client.dom.MgnlElement;
38  import static info.magnolia.templating.editor.client.jsni.JavascriptUtils.*;
39  
40  import com.google.gwt.dom.client.Element;
41  import com.google.gwt.dom.client.Style.Cursor;
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.DragStartEvent;
46  
47  import com.google.gwt.event.dom.client.DragEndEvent;
48  import com.google.gwt.event.dom.client.DragEndHandler;
49  import com.google.gwt.event.dom.client.DragOverEvent;
50  import com.google.gwt.event.dom.client.DragOverHandler;
51  import com.google.gwt.event.dom.client.DragStartHandler;
52  import com.google.gwt.event.dom.client.DropEvent;
53  import com.google.gwt.event.dom.client.DropHandler;
54  import com.google.gwt.event.dom.client.MouseDownEvent;
55  import com.google.gwt.event.dom.client.MouseDownHandler;
56  import com.google.gwt.event.dom.client.MouseOutEvent;
57  import com.google.gwt.event.dom.client.MouseOutHandler;
58  import com.google.gwt.event.dom.client.MouseOverEvent;
59  import com.google.gwt.event.dom.client.MouseOverHandler;
60  import com.google.gwt.user.client.ui.Button;
61  
62  
63  /**
64   * Edit bar.
65   */
66  public class ComponentBar extends AbstractBar {
67  
68      private String workspace;
69      private String path;
70      private String dialog;
71      private String id;
72      private String parentAreaType;
73      private boolean isInherited;
74  
75      public ComponentBar(MgnlElement mgnlElement) {
76  
77          super(mgnlElement);
78  
79          if(mgnlElement.getParentArea() != null) {
80  
81              MgnlElement area = mgnlElement.getParentArea();
82              String content = mgnlElement.getComment().getAttribute("content");
83              int i = content.indexOf(':');
84              this.workspace = content.substring(0, i);
85              this.path = content.substring(i + 1);
86  
87              this.id = path.substring(path.lastIndexOf("/") + 1);
88  
89              setId("__"+id);
90  
91              this.dialog = mgnlElement.getComment().getAttribute("dialog");
92  
93              this.parentAreaType = area.getComment().getAttribute("type");
94          }
95          this.isInherited = Boolean.parseBoolean(mgnlElement.getComment().getAttribute("inherited"));
96  
97          addStyleName("component");
98          if (isInherited) {
99              addStyleName("mgnlInherited");
100         }
101         else {
102             createButtons();
103             createMouseEventsHandlers();
104         }
105 
106         setVisible(false);
107         attach();
108         PageEditor.model.addEditBar(getMgnlElement(), this);
109 
110         if (!this.isInherited) {
111             createDragAndDropHandlers();
112         }
113     }
114 
115     private void createDragAndDropHandlers() {
116         this.addDomHandler(new DragStartHandler() {
117             @Override
118             public void onDragStart(DragStartEvent event) {
119                 ComponentBar.this.getElement().getStyle().setCursor(Cursor.MOVE);
120                 toggleButtons(false);
121                 event.setData("id", id);
122                 event.getDataTransfer().setDragImage(getElement(), 10, 10);
123             }
124         }, DragStartEvent.getType());
125 
126         this.addDomHandler(new DragEndHandler() {
127             @Override
128             public void onDragEnd(DragEndEvent event) {
129                 toggleButtons(true);
130             }
131         }, DragEndEvent.getType());
132 
133         this.addDomHandler(new DragOverHandler() {
134             @Override
135             public void onDragOver(DragOverEvent event) {
136                 event.stopPropagation();
137             }
138         }, DragOverEvent.getType());
139 
140 
141         this.addDomHandler(new DropHandler() {
142             @Override
143             public void onDrop(DropEvent event) {
144                 String idSource = event.getData("id");
145                 String parentPath = path.substring(0, path.lastIndexOf("/"));
146                 moveComponent(id, idSource, parentPath);
147                 //PageEditor.moveComponentEnd((ComponentBar)event.getSource(), parentPath);
148             }
149         }, DropEvent.getType());
150 
151         this.getElement().setDraggable(Element.DRAGGABLE_TRUE);
152     }
153 
154     private void createMouseEventsHandlers() {
155 
156         addDomHandler(new MouseDownHandler() {
157 
158             @Override
159             public void onMouseDown(MouseDownEvent event) {
160                 String parentPath = path.substring(0, path.lastIndexOf("/"));
161                 PageEditor.moveComponentEnd((ComponentBar)event.getSource(), parentPath);
162             }
163         }, MouseDownEvent.getType());
164 
165         addDomHandler(new MouseOverHandler() {
166 
167             @Override
168             public void onMouseOver(MouseOverEvent event) {
169                 PageEditor.moveComponentOver((ComponentBar)event.getSource());
170 
171             }
172         }, MouseOverEvent.getType());
173 
174         addDomHandler(new MouseOutHandler() {
175 
176             @Override
177             public void onMouseOut(MouseOutEvent event) {
178                 PageEditor.moveComponentOut((ComponentBar)event.getSource());
179             }
180         }, MouseOutEvent.getType());
181     }
182 
183     private void createButtons() {
184 
185         if (dialog != null && !this.isInherited) {
186             final Button edit = new Button(getI18nMessage("buttons.edit.js"));
187             edit.addClickHandler(new ClickHandler() {
188                 @Override
189                 public void onClick(ClickEvent event) {
190                     PageEditor.openDialog(dialog, workspace, path, null, null);
191 
192                 }
193             });
194             addPrimaryButton(edit, Float.RIGHT);
195         }
196 
197         //single area component obviously cannot be moved
198 /*        if(AreaDefinition.TYPE_LIST.equals(parentAreaType)) {
199             final Button move = new Button(getI18nMessage("buttons.move.js"));
200             move.addClickHandler(new ClickHandler() {
201                 @Override
202                 public void onClick(ClickEvent event) {
203                     toggleButtons(false);
204                     PageEditor.moveComponentStart(id);
205                 }
206             });
207             addPrimaryButton(move, Float.RIGHT);
208         }*/
209 
210         if (!this.isInherited) {
211             final Button removeButton = new Button(getI18nMessage("buttons.delete.js"));
212             removeButton.addClickHandler(new ClickHandler() {
213                 @Override
214                 public void onClick(ClickEvent event) {
215                     PageEditor.deleteComponent(path);
216                 }
217             });
218             addSecondaryButton(removeButton, Float.RIGHT);
219         }
220     }
221 
222 
223 }