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.ui.vaadin.gwt.client.editor.widget.controlbar;
35  
36  import com.google.gwt.dom.client.Element;
37  import com.google.gwt.dom.client.Style.Cursor;
38  import com.google.gwt.event.dom.client.ClickEvent;
39  import com.google.gwt.event.dom.client.ClickHandler;
40  import com.google.gwt.event.dom.client.DragDropEventBase;
41  import com.google.gwt.event.dom.client.MouseDownEvent;
42  import com.google.gwt.event.dom.client.MouseDownHandler;
43  import com.google.gwt.event.dom.client.MouseOutEvent;
44  import com.google.gwt.event.dom.client.MouseOutHandler;
45  import com.google.gwt.event.dom.client.MouseOverEvent;
46  import com.google.gwt.event.dom.client.MouseOverHandler;
47  import com.google.gwt.event.shared.EventBus;
48  import com.google.gwt.user.client.ui.Label;
49  import com.google.gwt.user.client.ui.PushButton;
50  
51  import info.magnolia.ui.vaadin.gwt.client.editor.dom.MgnlElement;
52  import info.magnolia.ui.vaadin.gwt.client.editor.event.DeleteComponentEvent;
53  import info.magnolia.ui.vaadin.gwt.client.editor.event.EditComponentEvent;
54  import info.magnolia.ui.vaadin.gwt.client.editor.widget.dnd.DragAndDrop;
55  import info.magnolia.ui.vaadin.gwt.client.editor.widget.dnd.LegacyDragAndDrop;
56  
57  import java.util.Map;
58  
59  import static info.magnolia.ui.vaadin.gwt.client.editor.jsni.JavascriptUtils.getI18nMessage;
60  
61  
62  /**
63   * Edit bar.
64   */
65  public class ComponentBar extends AbstractBar {
66  
67      private String dialog;
68  
69      private String nodeName;
70  
71      private boolean isInherited;
72  
73      private boolean editable = true;
74  
75      public ComponentBar(EventBus eventBus, MgnlElement mgnlElement) {
76  
77          super(eventBus, mgnlElement);
78  
79          setFields(mgnlElement.getAttributes());
80          addStyleName("component");
81  
82  /*        if (DragDropEventBase.isSupported()) {
83              createDragAndDropHandlers();
84  
85          }*/
86          if (!this.isInherited) {
87              createControls();
88              //createMouseEventsHandlers();
89          }
90  
91          setVisible(false);
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 setFields(Map<String, String> attributes) {
109 
110         setWorkspace(attributes.get("workspace"));
111         setPath(attributes.get("path"));
112 
113         this.nodeName = getPath().substring(getPath().lastIndexOf("/") + 1);
114 
115         setId("__" + nodeName);
116 
117         this.dialog = attributes.get("dialog");
118 
119         this.isInherited = Boolean.parseBoolean(attributes.get("inherited"));
120 
121         if (attributes.containsKey("editable")) {
122             this.editable = Boolean.parseBoolean(attributes.get("editable"));
123         }
124 
125     }
126 
127     public String getNodeName() {
128         return nodeName;
129     }
130 
131     private void createDragAndDropHandlers() {
132         DragAndDrop.dragAndDrop(getEventBus(), this);
133     }
134 
135     private void createMouseEventsHandlers() {
136 
137         addDomHandler(new MouseDownHandler() {
138 
139             @Override
140             public void onMouseDown(MouseDownEvent event) {
141                 LegacyDragAndDrop.moveComponentEnd(ComponentBar.this);
142             }
143         }, MouseDownEvent.getType());
144 
145         addDomHandler(new MouseOverHandler() {
146 
147             @Override
148             public void onMouseOver(MouseOverEvent event) {
149                 LegacyDragAndDrop.moveComponentOver(ComponentBar.this);
150 
151             }
152         }, MouseOverEvent.getType());
153 
154         addDomHandler(new MouseOutHandler() {
155 
156             @Override
157             public void onMouseOut(MouseOutEvent event) {
158                 LegacyDragAndDrop.moveComponentOut(ComponentBar.this);
159             }
160         }, MouseOutEvent.getType());
161     }
162 
163     private void createButtons() {
164 
165         final PushButton remove = new PushButton();
166         remove.addClickHandler(new ClickHandler() {
167 
168             @Override
169             public void onClick(ClickEvent event) {
170                 getEventBus().fireEvent(new DeleteComponentEvent(getWorkspace(), getPath()));
171             }
172         });
173         remove.setTitle(getI18nMessage("buttons.component.delete.js"));
174         remove.setStylePrimaryName("mgnlEditorPushButton");
175         remove.addStyleName("remove");
176 
177         addSecondaryButton(remove);
178 
179         final PushButton move = new PushButton();
180         move.addClickHandler(new ClickHandler() {
181 
182             @Override
183             public void onClick(ClickEvent event) {
184                 toggleButtons(false);
185                 LegacyDragAndDrop.moveComponentStart(ComponentBar.this);
186             }
187         });
188         move.setTitle(getI18nMessage("buttons.component.move.js"));
189         move.setStylePrimaryName("mgnlEditorPushButton");
190         move.addStyleName("move");
191         addPrimaryButton(move);
192 
193         if (dialog != null) {
194             final PushButton edit = new PushButton();
195             edit.addClickHandler(new ClickHandler() {
196 
197                 @Override
198                 public void onClick(ClickEvent event) {
199                     getEventBus().fireEvent(new EditComponentEvent(getWorkspace(), getPath(), dialog));
200                 }
201             });
202             edit.setTitle(getI18nMessage("buttons.component.edit.js"));
203             edit.setStylePrimaryName("mgnlEditorPushButton");
204             edit.addStyleName("edit");
205             addPrimaryButton(edit);
206         }
207     }
208 
209     private void createControls() {
210 
211                final Label remove = new Label();
212                 remove.setStyleName(ICON_CLASSNAME);
213                 remove.addStyleName(REMOVE_CLASSNAME);
214                 remove.addClickHandler(new ClickHandler() {
215                     @Override
216                     public void onClick(ClickEvent event) {
217                         getEventBus().fireEvent(new DeleteComponentEvent(getWorkspace(), getPath()));
218                     }
219                 });
220                 addSecondaryButton(remove);
221 
222 
223         /* final Label move = new Label();
224         move.setStyleName("icon icon-trash");
225         move.addClickHandler(new ClickHandler() {
226             @Override
227             public void onClick(ClickEvent event) {
228                 getEventBus().fireEvent(new DeleteComponentEvent(getWorkspace(), getImagePathByNodePath()));
229             }
230         });
231         addSecondaryButton(move);*/
232 
233         final Label edit = new Label();
234         edit.setStyleName(ICON_CLASSNAME);
235         edit.addStyleName(EDIT_CLASSNAME);
236         edit.addClickHandler(new ClickHandler() {
237 
238             @Override
239             public void onClick(ClickEvent event) {
240                 getEventBus().fireEvent(new EditComponentEvent(getWorkspace(), getPath(), dialog));
241             }
242         });
243         addPrimaryButton(edit);
244 
245     }
246 
247     @Override
248     public String getDialog() {
249         return dialog;
250     }
251 }