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;
35  
36  
37  import info.magnolia.templating.editor.client.dom.CMSBoundary;
38  
39  import com.google.gwt.dom.client.Element;
40  import com.google.gwt.dom.client.Node;
41  import com.google.gwt.dom.client.Style;
42  import com.google.gwt.dom.client.Style.Float;
43  
44  import com.google.gwt.event.dom.client.MouseDownEvent;
45  import com.google.gwt.event.dom.client.MouseDownHandler;
46  import com.google.gwt.event.dom.client.MouseOutEvent;
47  import com.google.gwt.event.dom.client.MouseOutHandler;
48  import com.google.gwt.event.dom.client.MouseOverEvent;
49  import com.google.gwt.event.dom.client.MouseOverHandler;
50  import com.google.gwt.event.dom.client.MouseUpEvent;
51  import com.google.gwt.event.dom.client.MouseUpHandler;
52  import com.google.gwt.user.client.ui.Button;
53  import com.google.gwt.user.client.ui.FlowPanel;
54  import com.google.gwt.user.client.ui.InlineLabel;
55  import com.google.gwt.user.client.ui.Label;
56  
57  /**
58   * Base class for horizontal bars with buttons.
59   */
60  public abstract class AbstractBarWidget extends FlowPanel {
61  
62      private CMSBoundary boundary;
63      protected boolean hasControls = false;
64      private String label = "";
65  
66      public AbstractBarWidget(CMSBoundary boundary) {
67  
68          this.setBoundary(boundary);
69          if (boundary != null) {
70              this.label = boundary.getComment().getAttribute("label");
71              if (label == null || label.isEmpty()) {
72                  if (boundary.getParentArea() != null) {
73                      this.label = boundary.getParentArea().getComment().getAttribute("label");
74                  }
75              }
76          }
77  
78          if (this.label != null && !this.label.isEmpty()) {
79              Label areaName = new InlineLabel(this.label);
80              //tooltip. Nice to have when area label is truncated because too long.
81              areaName.setTitle(this.label);
82  
83              //setStylePrimaryName(..) replaces gwt default css class, in this case gwt-Label
84              areaName.setStylePrimaryName("mgnlAreaLabel");
85              add(areaName);
86          }
87          addDomHandler(new MouseOverHandler() {
88              @Override
89              public void onMouseOver(MouseOverEvent event) {
90                  select();
91              }
92          }, MouseOverEvent.getType());
93  
94          addDomHandler(new MouseOutHandler() {
95              @Override
96              public void onMouseOut(MouseOutEvent event) {
97                  deSelect();
98              }
99          }, MouseOutEvent.getType());
100     }
101 
102     /**
103      * Called when this bar widget is selected/clicked. Default implementation does nothing.
104      */
105     protected void select() {
106 
107         this.addStyleName("selected");
108 /*        Document.get().getElementById("mgnlBoundary").getStyle().setTop(boundary.getMinCoordinate().getTop(), Style.Unit.PX);
109         Document.get().getElementById("mgnlBoundary").getStyle().setLeft(boundary.getMinCoordinate().getLeft(), Style.Unit.PX);
110         Document.get().getElementById("mgnlBoundary").getStyle().setHeight(boundary.getMaxCoordinate().getTop()-boundary.getMinCoordinate().getTop(), Style.Unit.PX);
111         Document.get().getElementById("mgnlBoundary").getStyle().setHeight(boundary.getMaxCoordinate().getLeft()-boundary.getMinCoordinate().getLeft(), Style.Unit.PX);*/
112     }
113 
114     protected void deSelect() {
115 
116         this.removeStyleName("selected");
117 
118     }
119 
120     protected void setId(String id){
121         getElement().setId(id);
122     }
123 
124     protected void addButton(final Button button, final Float cssFloat) {
125         button.setStylePrimaryName("mgnlControlButton");
126         button.getElement().getStyle().setFloat(cssFloat);
127 
128         button.addMouseDownHandler(new MouseDownHandler() {
129 
130             @Override
131             public void onMouseDown(MouseDownEvent event) {
132                 //add push button style
133                 button.setStyleName("mgnlControlButton_PUSHED", true);
134             }
135         });
136         button.addMouseUpHandler(new MouseUpHandler() {
137 
138             @Override
139             public void onMouseUp(MouseUpEvent event) {
140                 //remove push button style
141                 button.setStyleName("mgnlControlButton_PUSHED", false);
142             }
143         });
144         add(button);
145         hasControls = true;
146     }
147 
148     protected void setClassName(String className) {
149         getElement().setClassName(className);
150     }
151 
152     /**
153      * @return the element's underlying {@link Style}. You can use this object to manipulate the css style attribute of this bar widget.
154      */
155     protected Style getStyle() {
156         return getElement().getStyle();
157     }
158 
159     public void attach(Element element) {
160         final Node parentNode = element.getParentNode();
161         parentNode.insertAfter(getElement(), element);
162         onAttach();
163     }
164     public void attach(Node node) {
165         final Node parentNode = node.getParentNode();
166         parentNode.insertAfter(getElement(), node);
167         onAttach();
168     }
169     public void setBoundary(CMSBoundary boundary) {
170         this.boundary = boundary;
171     }
172 
173     public CMSBoundary getBoundary() {
174         return boundary;
175     }
176 }