View Javadoc
1   /**
2    * This file Copyright (c) 2011-2014 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 static info.magnolia.templating.editor.client.jsni.JavascriptUtils.getI18nMessage;
37  
38  import info.magnolia.cms.security.operations.OperationPermissionDefinition;
39  import info.magnolia.templating.editor.client.PageEditor;
40  import info.magnolia.templating.editor.client.dom.MgnlElement;
41  import info.magnolia.templating.editor.client.widget.dnd.DragAndDrop;
42  import info.magnolia.templating.editor.client.widget.dnd.LegacyDragAndDrop;
43  
44  import java.util.Map;
45  
46  import com.google.gwt.dom.client.Element;
47  import com.google.gwt.dom.client.Style.Cursor;
48  import com.google.gwt.event.dom.client.ClickEvent;
49  import com.google.gwt.event.dom.client.ClickHandler;
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      private boolean moveable = true;
75      private boolean writable = true;
76      private boolean deletable = true;
77  
78      public ComponentBar(MgnlElement mgnlElement) throws IllegalArgumentException {
79  
80          super(mgnlElement);
81  
82          checkMandatories(mgnlElement.getAttributes());
83          addStyleName("component");
84  
85          if(DragDropEventBase.isSupported()) {
86              createDragAndDropHandlers();
87  
88          }
89          if (!this.isInherited) {
90              createButtons();
91              createMouseEventsHandlers();
92          }
93  
94          setVisible(false);
95          attach();
96      }
97  
98      public void setDraggable(boolean draggable) {
99          if(DragDropEventBase.isSupported()) {
100             if (draggable) {
101                 this.getElement().setDraggable(Element.DRAGGABLE_TRUE);
102                 getStyle().setCursor(Cursor.MOVE);
103             }
104             else {
105                 this.getElement().setDraggable(Element.DRAGGABLE_FALSE);
106                 getStyle().setCursor(Cursor.DEFAULT);
107             }
108         }
109     }
110 
111     private void checkMandatories(Map<String, String> attributes) {
112         String content = attributes.get("content");
113         int i = content.indexOf(':');
114         this.workspace = content.substring(0, i);
115         this.path = content.substring(i + 1);
116 
117         this.nodeName = path.substring(path.lastIndexOf("/") + 1);
118 
119         setId("__" + nodeName);
120 
121         this.dialog = attributes.get("dialog");
122 
123         this.isInherited = getMgnlElement().isInherited();
124 
125         if (attributes.containsKey("editable")) {
126             this.editable = Boolean.parseBoolean(attributes.get("editable"));
127         }
128 
129         if (this.isInherited || !this.editable) {
130             throw new IllegalArgumentException();
131         }
132 
133         if (attributes.containsKey(OperationPermissionDefinition.MOVEABLE)) {
134             this.moveable = Boolean.parseBoolean(attributes.get(OperationPermissionDefinition.MOVEABLE));
135         }
136         if (attributes.containsKey(OperationPermissionDefinition.WRITABLE)) {
137             this.writable = Boolean.parseBoolean(attributes.get(OperationPermissionDefinition.WRITABLE));
138         }
139         if (attributes.containsKey(OperationPermissionDefinition.DELETABLE)) {
140             this.deletable = Boolean.parseBoolean(attributes.get(OperationPermissionDefinition.DELETABLE));
141         }
142     }
143 
144     public String getNodeName() {
145         return nodeName;
146     }
147 
148     public String getPath() {
149         return path;
150     }
151 
152     private void createDragAndDropHandlers() {
153         DragAndDrop.dragAndDrop(this);
154     }
155 
156     private void createMouseEventsHandlers() {
157 
158         addDomHandler(new MouseDownHandler() {
159 
160             @Override
161             public void onMouseDown(MouseDownEvent event) {
162                 LegacyDragAndDrop.moveComponentEnd(ComponentBar.this);
163             }
164         }, MouseDownEvent.getType());
165 
166         addDomHandler(new MouseOverHandler() {
167 
168             @Override
169             public void onMouseOver(MouseOverEvent event) {
170                 LegacyDragAndDrop.moveComponentOver(ComponentBar.this);
171 
172             }
173         }, MouseOverEvent.getType());
174 
175         addDomHandler(new MouseOutHandler() {
176 
177             @Override
178             public void onMouseOut(MouseOutEvent event) {
179                 LegacyDragAndDrop.moveComponentOut(ComponentBar.this);
180             }
181         }, MouseOutEvent.getType());
182     }
183 
184     private void createButtons() {
185 
186         final PushButton remove = new PushButton();
187         remove.addClickHandler(new ClickHandler() {
188             @Override
189             public void onClick(ClickEvent event) {
190                 PageEditor.deleteComponent(path);
191             }
192         });
193         remove.setTitle(getI18nMessage("buttons.component.delete.js"));
194         remove.setStylePrimaryName("mgnlEditorPushButton");
195         remove.addStyleName("remove");
196         if (!deletable) {
197             remove.setVisible(false);
198         }
199 
200         addSecondaryButton(remove);
201 
202         final PushButton move = new PushButton();
203         move.addClickHandler(new ClickHandler() {
204             @Override
205             public void onClick(ClickEvent event) {
206                 toggleButtons(false);
207                 LegacyDragAndDrop.moveComponentStart(ComponentBar.this);
208             }
209         });
210         move.setTitle(getI18nMessage("buttons.component.move.js"));
211         move.setStylePrimaryName("mgnlEditorPushButton");
212         move.addStyleName("move");
213         if (!moveable) {
214             move.setVisible(false);
215         }
216         addPrimaryButton(move);
217 
218         if (dialog != null) {
219             final PushButton edit = new PushButton();
220             edit.addClickHandler(new ClickHandler() {
221                 @Override
222                 public void onClick(ClickEvent event) {
223                     PageEditor.openDialog(dialog, workspace, path);
224                 }
225             });
226             edit.setTitle(getI18nMessage("buttons.component.edit.js"));
227             edit.setStylePrimaryName("mgnlEditorPushButton");
228             edit.addStyleName("edit");
229             addPrimaryButton(edit);
230             if (!writable) {
231                 edit.setVisible(false);
232             }
233         }
234     }
235 }