View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU 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   * svgreal 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 General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.gxt.widget;
19  
20  import org.vectomatic.dom.svg.ui.SVGImage;
21  
22  import com.extjs.gxt.ui.client.core.El;
23  import com.extjs.gxt.ui.client.util.Util;
24  import com.extjs.gxt.ui.client.widget.button.Button;
25  import com.google.gwt.user.client.Element;
26  import com.google.gwt.user.client.ui.Accessibility;
27  
28  /**
29   * Button subclass which supports having an SVG image instead
30   * of an icon
31   * @author laaglu
32   */
33  public class SVGButton extends Button {
34  	protected SVGImage svgImage;
35  	
36  	public SVGButton(SVGImage svgImage) {
37  		setSVGImage(svgImage);
38  	}
39  
40  	public SVGImage getSVGImage() {
41  		return svgImage;
42  	}
43  
44  	/**
45  	 * Sets the button's icon style. The style name should match a CSS style
46  	 * that specifies a background image using the following format:
47  	 * 
48  	 * <pre>
49  	 * 
50  	 * &lt;code&gt; .my-icon { background: url(images/icons/my-icon.png) no-repeat
51  	 * center left !important; } &lt;/code&gt;
52  	 * 
53  	 * </pre>
54  	 * 
55  	 * @param svgImage
56  	 *            the icon
57  	 */
58  	public void setSVGImage(SVGImage svgImage) {
59  		if (rendered) {
60  			El oldIcon = buttonEl.selectNode("." + baseStyle + "-image");
61  			if (oldIcon != null) {
62  				oldIcon.remove();
63  				el().removeStyleName(baseStyle + "-text-icon",
64  						baseStyle + "-icon", baseStyle + "-noicon");
65  			}
66  			el().addStyleName(
67  					(svgImage != null ? (!Util.isEmptyString(text) ? " "
68  							+ baseStyle + "-text-icon" : " " + baseStyle
69  							+ "-icon") : " " + baseStyle + "-noicon"));
70  			Element e = null;
71  
72  			if (svgImage != null) {
73  				e = (Element) svgImage.getElement().cast();
74  
75  				Accessibility.setRole(e, "presentation");
76  				fly(e).addStyleName(baseStyle + "-image");
77  
78  				buttonEl.insertFirst(e);
79  				El.fly(e).makePositionable(true);
80  
81  			}
82  			autoWidth();
83  			alignIcon(e);
84  		}
85  		this.svgImage = svgImage;
86  	}
87  
88  	@Override
89  	protected void afterRender() {
90  		super.afterRender();
91  		setSVGImage(svgImage);
92  	}
93  
94  }