View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of lib-gwt-svg-samples.
4    * 
5    * libgwtsvg-samples 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   * libgwtsvg-samples 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 libgwtsvg-samples.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  
19  /**
20   * This sample contains artwork from project Open Clip Art
21   * See the project website at http://www.openclipart.org/ for license details
22   */
23  
24  package org.vectomatic.svg.samples.client.widgets;
25  
26  import org.vectomatic.dom.svg.OMSVGGElement;
27  import org.vectomatic.dom.svg.OMSVGPathElement;
28  import org.vectomatic.dom.svg.OMSVGTSpanElement;
29  import org.vectomatic.dom.svg.OMText;
30  import org.vectomatic.dom.svg.ui.SVGImage;
31  import org.vectomatic.dom.svg.ui.SVGPushButton;
32  import org.vectomatic.dom.svg.ui.SVGResource;
33  import org.vectomatic.svg.samples.client.Main;
34  import org.vectomatic.svg.samples.client.Main.MainBundle;
35  import org.vectomatic.svg.samples.client.SampleBase;
36  
37  import com.google.gwt.core.client.GWT;
38  import com.google.gwt.dom.client.StyleInjector;
39  import com.google.gwt.event.dom.client.ClickEvent;
40  import com.google.gwt.event.dom.client.MouseDownEvent;
41  import com.google.gwt.event.dom.client.MouseEvent;
42  import com.google.gwt.event.dom.client.MouseOutEvent;
43  import com.google.gwt.event.dom.client.MouseOverEvent;
44  import com.google.gwt.event.shared.EventHandler;
45  import com.google.gwt.resources.client.ClientBundle;
46  import com.google.gwt.resources.client.CssResource;
47  import com.google.gwt.uibinder.client.UiBinder;
48  import com.google.gwt.uibinder.client.UiField;
49  import com.google.gwt.uibinder.client.UiHandler;
50  import com.google.gwt.user.client.Timer;
51  import com.google.gwt.user.client.Window;
52  import com.google.gwt.user.client.ui.HTML;
53  import com.google.gwt.user.client.ui.PopupPanel;
54  import com.google.gwt.user.client.ui.TabLayoutPanel;
55  
56  public class WidgetsSample extends SampleBase {
57  	private static class Tooltip extends PopupPanel {
58  		private HTML contents;
59  		private Timer timer;
60  			  
61  		public Tooltip() {
62  			super(true);
63  			contents = new HTML();
64  			add(contents);
65  			setStyleName(WidgetsSampleBundle.INSTANCE.getCss().tooltip());
66  		}
67  
68  		public void show(int x, int y, final String text, final int delay) {
69  			contents.setHTML(text);
70  			setPopupPosition(x, y);
71  			super.show();
72  			if (timer != null) {
73  				timer.cancel();
74  			}
75  			timer = new Timer() {
76  				public void run() {
77  					Tooltip.this.hide();
78  					timer = null;
79  				}
80  			};
81  			timer.schedule(delay);
82  		}
83  	}
84  
85  	public interface WidgetsSampleBundle extends ClientBundle {
86  		public static WidgetsSampleBundle INSTANCE = GWT.create(WidgetsSampleBundle.class);
87  		
88  		@Source("as_coeur_jean_victor_bal_.svg")
89  		SVGResource hearts();
90  		@Source("as_trefle_jean_victor_ba_.svg")
91  		SVGResource clubs();
92  		@Source("as_carreau_jean_victor_b_.svg")
93  		SVGResource diamonds();
94  		@Source("as_pique_jean_victor_bal_.svg")
95  		SVGResource spades();
96  		@Source("ledButton.svg")
97  		SVGResource led();
98  		@Source("play-pause.svg")
99  		SVGResource playButton();
100 		@Source("tooltip.css")
101 		public TooltipCss getCss();
102 	}
103 	
104 	interface TooltipCss extends CssResource {
105 		public String tooltip();
106 	}
107 	
108 	interface WidgetsSampleBinder extends UiBinder<TabLayoutPanel, WidgetsSample> {
109 	}
110 	private static WidgetsSampleBinder binder = GWT.create(WidgetsSampleBinder.class);
111 	
112 	// SVG defined in an external resource
113 	@UiField(provided=true)
114 	public static MainBundle mainBundle = Main.mainBundle;
115 	@UiField
116 	SVGImage hearts;
117 	@UiField
118 	SVGImage clubs;
119 	@UiField
120 	SVGImage diamonds;
121 	@UiField
122 	SVGImage spades;
123 	@UiField
124 	SVGPushButton clickMeButton;
125 	@UiField
126 	SVGPushButton holdMeDownButton;
127 	@UiField
128 	OMSVGTSpanElement clickCount;
129 	
130 	// SVG defined inline with bindings to internal elements
131 	@UiField
132 	OMSVGGElement eyes;
133 	@UiField
134 	OMSVGPathElement mouth;
135 
136 	private Tooltip tooltip;
137 
138 	@Override
139 	public TabLayoutPanel getPanel() {
140 		if (tabPanel == null) {
141 			TooltipCss css = WidgetsSampleBundle.INSTANCE.getCss();
142 			
143 			// Inject CSS in the document headers
144 			StyleInjector.inject(css.getText());
145 
146 			tooltip = new Tooltip();
147 			tabPanel = binder.createAndBindUi(this);
148 			tabPanel.setTabText(0, "Widgets");
149 			createCodeTabs("WidgetsSample");
150 		}
151 		return tabPanel;
152 	}
153 	
154 	private void showTooltip(MouseEvent<? extends EventHandler> e, String text) {
155 		tooltip.show(e.getClientX() + 20, e.getClientY() + 30, text, 3000);
156 	}
157 
158 	@UiHandler("hearts")
159 	public void onMouseOutHearts(MouseOutEvent event) {
160 		tooltip.hide();
161 	}
162 
163 	@UiHandler("hearts")
164 	public void onMouseOverHearts(MouseOverEvent event) {
165 		showTooltip(event, "hearts");
166 	}
167 
168 	@UiHandler("clubs")
169 	public void onMouseOutClubs(MouseOutEvent event) {
170 		tooltip.hide();
171 	}
172 
173 	@UiHandler("clubs")
174 	public void onMouseOverClubs(MouseOverEvent event) {
175 		showTooltip(event, "clubs");
176 	}
177 
178 	@UiHandler("diamonds")
179 	public void onMouseOutDiamonds(MouseOutEvent event) {
180 		tooltip.hide();
181 	}
182 
183 	@UiHandler("diamonds")
184 	public void onMouseOverDiamonds(MouseOverEvent event) {
185 		showTooltip(event, "diamonds");
186 	}
187 
188 	@UiHandler("spades")
189 	public void onMouseOutSpades(MouseOutEvent event) {
190 		tooltip.hide();
191 	}
192 
193 	@UiHandler("spades")
194 	public void onMouseOverSpades(MouseOverEvent event) {
195 		showTooltip(event, "spades");
196 	}
197 
198 	@UiHandler("eyes")
199 	public void onMouseOutEyes(MouseOutEvent event) {
200 		tooltip.hide();
201 	}
202 
203 	@UiHandler("eyes")
204 	public void onMouseOverEyes(MouseOverEvent event) {
205 		showTooltip(event, "eyes");
206 	}
207 
208 	@UiHandler("mouth")
209 	public void onMouseOutMouth(MouseOutEvent event) {
210 		tooltip.hide();
211 	}
212 
213 	@UiHandler("mouth")
214 	public void onMouseOverMouth(MouseOverEvent event) {
215 		showTooltip(event, "mouth");
216 	}
217 	
218 	@UiHandler("clickMeButton")
219 	public void onClick(ClickEvent event) {
220 		Window.alert("Ouch !");
221 	}
222 
223 	@UiHandler("holdMeDownButton")
224 	public void onMouseDown(MouseDownEvent event) {
225 		OMText text = (OMText) clickCount.getFirstChild();
226 		int count = Integer.parseInt(text.getData());
227 		text.setData(Integer.toString(count + 1));	
228 	}
229 }