View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 Lukas Laag
3    * This file is part of lib-gwt-svg.
4    * 
5    * libgwtsvg is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * libgwtsvg is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with libgwtsvg.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.dom.svg.ui;
19  
20  import java.util.Map;
21  
22  import org.vectomatic.dom.svg.OMSVGSVGElement;
23  
24  import com.google.gwt.event.dom.client.MouseDownEvent;
25  import com.google.gwt.event.dom.client.MouseOutEvent;
26  import com.google.gwt.event.dom.client.MouseOverEvent;
27  import com.google.gwt.event.dom.client.MouseUpEvent;
28  import com.google.gwt.uibinder.client.ElementParserToUse;
29  
30  /**
31   * SVG toggle button class
32   * <p>An SVG push toggle typically defines at least the following two faces:</p>
33   * <ul>
34   * <li>UP</li>
35   * <li>DOWN</li>
36   * </ul>
37   * <p>You can define an SVG toggle button using UiBinder templates. Use
38   * the <em>svgui:upFace</em>, <em>svgui:upDisabledFace</em>, <em>svgui:upHoveringFace</em>
39   * <em>svgui:downFace</em>, <em>svgui:downDisabledFace</em>, <em>svgui:downHoveringFace</em>
40   * tags to defined faces.</p>
41   * <p>Depending on your needs, you can either define the SVG inline with
42   * the <em>svgui:element</em> tag. This can be convenient if you want to 
43   * localize the button label, or use styles defined in the template. 
44   * Or you can use an {@link org.vectomatic.dom.svg.ui.SVGResource SVGResource} with the <em>resource</em> attribute,
45   * if your SVG is large or if you want to keep your template more readable.</p>
46   * <p>Each face contains one or more <em>svgui:styleChange</em> tags.
47   * The <em>classNames</em> attribute specifies the name of the
48   * CSS class selectors to be applied to the SVG element when the face
49   * is activated.</p>
50   * <p>The following section shows a sample UiBinder template:</p>
51   * <pre>
52   * &lt;svgui:SVGToggleButton resource="{resources.led}"&gt;
53   *  &lt;svgui:upFace&gt;&lt;svgui:styleChange classNames="{style.led-up}"/&gt;&lt;/svgui:upFace&gt;
54   *  &lt;svgui:downFace&gt;&lt;svgui:styleChange classNames="{style.led-down}"/&gt;&lt;/svgui:downFace&gt;
55   * &lt;/svgui:SVGToggleButton&gt;
56   * </pre>
57   * Note that by default the inline SVG in SVGToggleButtons is validated against the SVG 1.1 XSD schema.
58   * You can opt out of validation by setting the <code>validated="false"</code>
59   * attribute on the <em>svgui:element</em> tag.
60   * @author laaglu
61   */
62  @ElementParserToUse(className = "org.vectomatic.dev.svg.impl.gen.SVGButtonBaseParser")
63  public class SVGToggleButton extends SVGButtonBase {
64  	public SVGToggleButton() {
65  	}
66  	public SVGToggleButton(OMSVGSVGElement svgElement, Map<SVGFaceName, SVGFace> faces) {
67  		super(svgElement, faces);
68  		showFace(SVGFaceName.UP);
69  	}
70  	public SVGToggleButton(SVGResource resource, Map<SVGFaceName, SVGFace> faces) {
71  		this(resource.getSvg(), faces);
72  	}
73  
74  	/**
75  	 * Returns whether this button is down
76  	 * @return
77  	 * true if it is down, false if it is up
78  	 */
79  	public boolean isDown() {
80  		return currentFaceName == SVGFaceName.DOWN || currentFaceName == SVGFaceName.DOWN_DISABLED || currentFaceName == SVGFaceName.DOWN_HOVERING;
81  	}
82  	/**
83  	 * Sets whether this button is down.
84  	 * @param isDown
85  	 * true to force the button down, false to force the button up
86  	 */
87  	public void setDown(boolean isDown) {
88  		if (isDown) {
89  			showFace(SVGFaceName.DOWN);
90  		} else {
91  			showFace(SVGFaceName.UP);
92  		}
93  	}
94  	@Override
95  	public void onMouseDown(MouseDownEvent event) {
96  //		GWT.log("onMouseDown");
97  		if (isEnabled()) {
98  			switch(currentFaceName) {
99  				case UP_HOVERING:
100 				case UP:
101 					showFace(SVGFaceName.DOWN_HOVERING);
102 					break;
103 				case DOWN_HOVERING:
104 				case DOWN:
105 					showFace(SVGFaceName.UP_HOVERING);
106 					break;
107 			}
108 		}
109 		event.stopPropagation();
110 		event.preventDefault();
111 	}
112 	@Override
113 	public void onMouseUp(MouseUpEvent event) {
114 //		GWT.log("onMouseUp");
115 		if (isEnabled()) {
116 			if (currentFaceName == SVGFaceName.DOWN_HOVERING) {
117 				showFace(SVGFaceName.DOWN);
118 			} else if (currentFaceName == SVGFaceName.UP_HOVERING) {
119 				showFace(SVGFaceName.UP);
120 			}
121 		}
122 		event.stopPropagation();
123 		event.preventDefault();
124 	}
125 	@Override
126 	public void onMouseOver(MouseOverEvent event) {
127 //		GWT.log("onMouseOver");
128 		if (isEnabled()) {
129 			switch(currentFaceName) {
130 				case UP:
131 					showFace(SVGFaceName.UP_HOVERING);
132 					break;
133 				case DOWN:
134 					showFace(SVGFaceName.DOWN_HOVERING);
135 					break;
136 			}
137 		}
138 	}
139 	@Override
140 	public void onMouseOut(MouseOutEvent event) {
141 //		GWT.log("onMouseOut");
142 		if (isEnabled()) {
143 			switch(currentFaceName) {
144 				case UP_HOVERING:
145 					showFace(SVGFaceName.UP);
146 					break;
147 				case DOWN_HOVERING:
148 					showFace(SVGFaceName.DOWN);
149 					break;
150 			}
151 		}
152 	}
153 	
154 	@Override
155 	public SVGFace getFace(SVGFaceName faceName) {
156 		if (!faces.containsKey(faceName)) {
157 			switch (faceName) {
158 				case UP_HOVERING:
159 				case UP_DISABLED:
160 					faceName = SVGFaceName.UP;
161 					break;
162 				case DOWN:
163 				case DOWN_HOVERING:
164 				case DOWN_DISABLED:
165 					faceName = SVGFaceName.DOWN;
166 					break;
167 			}
168 		}
169 		return super.getFace(faceName);
170 	}
171 
172 }