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