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  
42  
43  
44  import com.google.gwt.dom.client.Style.Cursor;
45  import com.google.gwt.event.dom.client.ClickEvent;
46  import com.google.gwt.event.dom.client.ClickHandler;
47  
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              getStyle().setCursor(Cursor.MOVE);
81              createButtons(false);
82              createDragAndDropHandlers();
83          } else {
84              createButtons(true);
85              createMouseEventsHandlers();
86          }
87  
88          setVisible(false);
89          attach();
90  
91      }
92  
93      private void checkMandatories(Map<String, String> attributes) {
94          String content = attributes.get("content");
95          int i = content.indexOf(':');
96          this.workspace = content.substring(0, i);
97          this.path = content.substring(i + 1);
98  
99          this.nodeName = path.substring(path.lastIndexOf("/") + 1);
100 
101         setId("__" + nodeName);
102 
103         this.dialog = attributes.get("dialog");
104 
105         this.isInherited = Boolean.parseBoolean(attributes.get("inherited"));
106 
107         if (attributes.containsKey("editable")) {
108             this.editable = Boolean.parseBoolean(attributes.get("editable"));
109         }
110 
111         if (this.isInherited || !this.editable) {
112             throw new IllegalArgumentException();
113         }
114 
115     }
116 
117     public String getNodeName() {
118         return nodeName;
119     }
120 
121     public String getPath() {
122         return path;
123     }
124 
125     private void createDragAndDropHandlers() {
126         DragAndDrop.dragAndDrop(this);
127     }
128 
129     private void createMouseEventsHandlers() {
130 
131         addDomHandler(new MouseDownHandler() {
132 
133             @Override
134             public void onMouseDown(MouseDownEvent event) {
135                 String parentPath = path.substring(0, path.lastIndexOf("/"));
136                 PageEditor.moveComponentEnd((ComponentBar)event.getSource(), parentPath);
137             }
138         }, MouseDownEvent.getType());
139 
140         addDomHandler(new MouseOverHandler() {
141 
142             @Override
143             public void onMouseOver(MouseOverEvent event) {
144                 PageEditor.moveComponentOver((ComponentBar)event.getSource());
145 
146             }
147         }, MouseOverEvent.getType());
148 
149         addDomHandler(new MouseOutHandler() {
150 
151             @Override
152             public void onMouseOut(MouseOutEvent event) {
153                 PageEditor.moveComponentOut((ComponentBar)event.getSource());
154             }
155         }, MouseOutEvent.getType());
156     }
157 
158     private void createButtons(boolean createMoveButton) {
159 
160         if (dialog != null && !this.isInherited) {
161             final PushButton edit = new PushButton();
162             edit.addClickHandler(new ClickHandler() {
163                 @Override
164                 public void onClick(ClickEvent event) {
165                     PageEditor.openDialog(dialog, workspace, path, null, null);
166                 }
167             });
168             edit.setStylePrimaryName("mgnlEditorPushButton");
169             edit.addStyleName("edit");
170             addPrimaryButton(edit);
171         }
172 
173         if(createMoveButton && !this.isInherited) {
174             final PushButton move = new PushButton();
175             move.addClickHandler(new ClickHandler() {
176                 @Override
177                 public void onClick(ClickEvent event) {
178                     toggleButtons(false);
179                     PageEditor.moveComponentStart(nodeName);
180                 }
181             });
182             move.setStylePrimaryName("mgnlEditorPushButton");
183             move.addStyleName("move");
184             addPrimaryButton(move);
185         }
186 
187         if (!this.isInherited) {
188             final PushButton remove = new PushButton();
189             remove.addClickHandler(new ClickHandler() {
190                 @Override
191                 public void onClick(ClickEvent event) {
192                     PageEditor.deleteComponent(path);
193                 }
194             });
195             remove.setStylePrimaryName("mgnlEditorPushButton");
196             remove.addStyleName("remove");
197 
198             addSecondaryButton(remove);
199         }
200     }
201 }