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  package org.vectomatic.svg.samples.client.parser;
19  
20  import org.vectomatic.dom.svg.OMSVGSVGElement;
21  import org.vectomatic.dom.svg.ui.ExternalSVGResource;
22  import org.vectomatic.dom.svg.ui.ExternalSVGResource.Validated;
23  import org.vectomatic.dom.svg.ui.SVGResource;
24  import org.vectomatic.dom.svg.utils.ParserException;
25  import org.vectomatic.svg.samples.client.Main;
26  import org.vectomatic.svg.samples.client.Main.MainBundle;
27  import org.vectomatic.svg.samples.client.SampleBase;
28  
29  import com.google.gwt.core.client.GWT;
30  import com.google.gwt.event.dom.client.ChangeEvent;
31  import com.google.gwt.resources.client.ClientBundle;
32  import com.google.gwt.resources.client.ResourceCallback;
33  import com.google.gwt.resources.client.ResourceException;
34  import com.google.gwt.uibinder.client.UiBinder;
35  import com.google.gwt.uibinder.client.UiField;
36  import com.google.gwt.uibinder.client.UiHandler;
37  import com.google.gwt.user.client.Element;
38  import com.google.gwt.user.client.Window;
39  import com.google.gwt.user.client.ui.HTML;
40  import com.google.gwt.user.client.ui.ListBox;
41  import com.google.gwt.user.client.ui.TabLayoutPanel;
42  
43  /**
44   * Class to demonstrate the parsing of SVG files
45   * @author laaglu
46   */
47  public class ParserSample extends SampleBase {
48  	interface ParserSampleBinder extends UiBinder<TabLayoutPanel, ParserSample> {
49  	}
50  	private static ParserSampleBinder binder = GWT.create(ParserSampleBinder.class);
51  
52  	public interface ParserSampleBundle extends ClientBundle {
53  		public static ParserSampleBundle INSTANCE = GWT.create(ParserSampleBundle.class);
54  		
55  		@Source("tiger.svg")
56  		ExternalSVGResource tiger();
57  		@Source("lion.svg")
58  		ExternalSVGResource lion();
59  		@Source("butterfly.svg")
60  		ExternalSVGResource butterfly();
61  		@Source("notwellformed.svg")
62  		@Validated(validated = false)
63  		ExternalSVGResource notwellformed();
64  	}
65  
66  	@UiField(provided=true)
67  	public static MainBundle mainBundle = Main.mainBundle;
68  	@UiField
69  	HTML svgContainer;
70  	@UiField
71  	ListBox documentListBox;
72  	OMSVGSVGElement svg;
73  	private int lastSelectedIndex;
74  
75  	ResourceCallback<SVGResource> callback = new ResourceCallback<SVGResource>() {
76  
77  		@Override
78  		public void onError(ResourceException e) {
79  			sourceHtml.setHTML("Cannot find resource");
80  		}
81  
82  		@Override
83  		public void onSuccess(SVGResource resource) {
84  			// Insert the SVG root element into the HTML UI
85  			Element div = svgContainer.getElement();
86  			svg = resource.getSvg();
87  			if (div.hasChildNodes()) {
88  				div.replaceChild(svg.getElement(), div.getFirstChild());
89  			} else {
90  				div.appendChild(svg.getElement());					
91  			}
92  		}
93  		
94  	};
95  	
96  	@Override
97  	public TabLayoutPanel getPanel() {
98  		if (tabPanel == null) {
99  			// Initialize the UI with UiBinder
100 			tabPanel = binder.createAndBindUi(this);
101 			tabPanel.setTabText(0, "Parser");
102 			createCodeTabs("ParserSample");
103 
104 			// Fill the list box with svg file names
105 			documentListBox.addItem("tiger");
106 			documentListBox.addItem("lion");
107 			documentListBox.addItem("butterfly");
108 			documentListBox.addItem("not well formed");
109 			documentListBox.setSelectedIndex(0);
110 			documentListBoxChange(null);
111 		}
112 		return tabPanel;
113 	}
114 	
115 	@UiHandler("documentListBox")
116 	public void documentListBoxChange(ChangeEvent event) {
117 		// Request the contents of the file
118 		try {
119 			switch(documentListBox.getSelectedIndex()) {
120 				case 0:
121 					ParserSampleBundle.INSTANCE.tiger().getSvg(callback);
122 					break;
123 				case 1:
124 					ParserSampleBundle.INSTANCE.lion().getSvg(callback);
125 					break;
126 				case 2:
127 					ParserSampleBundle.INSTANCE.butterfly().getSvg(callback);
128 					break;
129 				case 3:
130 					try {
131 						ParserSampleBundle.INSTANCE.notwellformed().getSvg(callback);
132 					} catch(ParserException e) {
133 						Window.alert("Parsing error: " + e.getMessage());
134 						documentListBox.setSelectedIndex(lastSelectedIndex);
135 					}
136 					break;
137 			}
138 			lastSelectedIndex = documentListBox.getSelectedIndex();
139 		} catch(ResourceException e) {
140 			sourceHtml.setHTML("Cannot find resource");
141 		}
142 	}
143 }