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