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 java.util.Map;
37  
38  import info.magnolia.templating.editor.client.PageEditor;
39  import info.magnolia.templating.editor.client.dom.MgnlElement;
40  import info.magnolia.templating.editor.client.widget.dnd.DragAndDrop;
41  import info.magnolia.templating.editor.client.widget.dnd.LegacyDragAndDrop;
42  
43  
44  
45  import com.google.gwt.dom.client.Element;
46  import com.google.gwt.dom.client.Style.Cursor;
47  import com.google.gwt.event.dom.client.ClickEvent;
48  import com.google.gwt.event.dom.client.ClickHandler;
49  
50  import com.google.gwt.event.dom.client.DragDropEventBase;
51  import com.google.gwt.event.dom.client.MouseDownEvent;
52  import com.google.gwt.event.dom.client.MouseDownHandler;
53  import com.google.gwt.event.dom.client.MouseOutEvent;
54  import com.google.gwt.event.dom.client.MouseOutHandler;
55  import com.google.gwt.event.dom.client.MouseOverEvent;
56  import com.google.gwt.event.dom.client.MouseOverHandler;
57  import com.google.gwt.user.client.ui.PushButton;
58  
59  
60  
61  
62  /**
63   * Edit bar.
64   */
65  public class ComponentBar extends AbstractBar  {
66  
67      private String workspace;
68      private String path;
69      private String dialog;
70      private String nodeName;
71      private boolean isInherited;
72      private boolean editable = true;
73  
74      public ComponentBar(MgnlElement mgnlElement) throws IllegalArgumentException {
75  
76          super(mgnlElement);
77  
78          checkMandatories(mgnlElement.getAttributes());
79          addStyleName("component");
80  
81          if(DragDropEventBase.isSupported()) {
82              createDragAndDropHandlers();
83  
84          }
85          if (!this.isInherited) {
86              createButtons();
87              createMouseEventsHandlers();
88          }
89  
90          setVisible(false);
91          attach();
92  
93      }
94  
95      public void setDraggable(boolean draggable) {
96          if(DragDropEventBase.isSupported()) {
97              if (draggable) {
98                  this.getElement().setDraggable(Element.DRAGGABLE_TRUE);
99                  getStyle().setCursor(Cursor.MOVE);
100             }
101             else {
102                 this.getElement().setDraggable(Element.DRAGGABLE_FALSE);
103                 getStyle().setCursor(Cursor.DEFAULT);
104             }
105         }
106     }
107 
108     private void checkMandatories(Map<String, String> attributes) {
109         String content = attributes.get("content");
110         int i = content.indexOf(':');
111         this.workspace = content.substring(0, i);
112         this.path = content.substring(i + 1);
113 
114         this.nodeName = path.substring(path.lastIndexOf("/") + 1);
115 
116         setId("__" + nodeName);
117 
118         this.dialog = attributes.get("dialog");
119 
120         this.isInherited = Boolean.parseBoolean(attributes.get("inherited"));
121 
122         if (attributes.containsKey("editable")) {
123             this.editable = Boolean.parseBoolean(attributes.get("editable"));
124         }
125 
126         if (this.isInherited || !this.editable) {
127             throw new IllegalArgumentException();
128         }
129 
130     }
131 
132     public String getNodeName() {
133         return nodeName;
134     }
135 
136     public String getPath() {
137         return path;
138     }
139 
140     private void createDragAndDropHandlers() {
141         DragAndDrop.dragAndDrop(this);
142     }
143 
144     private void createMouseEventsHandlers() {
145 
146         addDomHandler(new MouseDownHandler() {
147 
148             @Override
149             public void onMouseDown(MouseDownEvent event) {
150                 LegacyDragAndDrop.moveComponentEnd(ComponentBar.this);
151             }
152         }, MouseDownEvent.getType());
153 
154         addDomHandler(new MouseOverHandler() {
155 
156             @Override
157             public void onMouseOver(MouseOverEvent event) {
158                 LegacyDragAndDrop.moveComponentOver(ComponentBar.this);
159 
160             }
161         }, MouseOverEvent.getType());
162 
163         addDomHandler(new MouseOutHandler() {
164 
165             @Override
166             public void onMouseOut(MouseOutEvent event) {
167                 LegacyDragAndDrop.moveComponentOut(ComponentBar.this);
168             }
169         }, MouseOutEvent.getType());
170     }
171 
172     private void createButtons() {
173 
174         final PushButton remove = new PushButton();
175         remove.addClickHandler(new ClickHandler() {
176             @Override
177             public void onClick(ClickEvent event) {
178                 PageEditor.deleteComponent(path);
179             }
180         });
181         remove.setStylePrimaryName("mgnlEditorPushButton");
182         remove.addStyleName("remove");
183 
184         addSecondaryButton(remove);
185 
186         final PushButton move = new PushButton();
187         move.addClickHandler(new ClickHandler() {
188             @Override
189             public void onClick(ClickEvent event) {
190                 toggleButtons(false);
191                 LegacyDragAndDrop.moveComponentStart(ComponentBar.this);
192             }
193         });
194         move.setStylePrimaryName("mgnlEditorPushButton");
195         move.addStyleName("move");
196         addPrimaryButton(move);
197 
198         if (dialog != null) {
199             final PushButton edit = new PushButton();
200             edit.addClickHandler(new ClickHandler() {
201                 @Override
202                 public void onClick(ClickEvent event) {
203                     PageEditor.openDialog(dialog, workspace, path);
204                 }
205             });
206             edit.setStylePrimaryName("mgnlEditorPushButton");
207             edit.addStyleName("edit");
208             addPrimaryButton(edit);
209         }
210     }
211 }