1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
45
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
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
100 tabPanel = binder.createAndBindUi(this);
101 tabPanel.setTabText(0, "Parser");
102 createCodeTabs("ParserSample");
103
104
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
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 }