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;
19  
20  import org.vectomatic.dom.svg.utils.DOMHelper;
21  import org.vectomatic.dom.svg.utils.SVGConstants;
22  import org.vectomatic.svg.samples.client.dnd.DndSample;
23  import org.vectomatic.svg.samples.client.events.EventSample;
24  import org.vectomatic.svg.samples.client.features.FeaturesSample;
25  import org.vectomatic.svg.samples.client.parser.ParserSample;
26  import org.vectomatic.svg.samples.client.shapes.ShapesSample;
27  import org.vectomatic.svg.samples.client.smil.SmilSample;
28  import org.vectomatic.svg.samples.client.widgets.WidgetsSample;
29  import org.vectomatic.svg.samples.client.xpath.XPathSample;
30  
31  import com.google.gwt.core.client.EntryPoint;
32  import com.google.gwt.core.client.GWT;
33  import com.google.gwt.event.logical.shared.SelectionEvent;
34  import com.google.gwt.event.logical.shared.SelectionHandler;
35  import com.google.gwt.resources.client.ClientBundle;
36  import com.google.gwt.resources.client.CssResource;
37  import com.google.gwt.resources.client.ImageResource;
38  import com.google.gwt.safehtml.shared.SafeHtmlUtils;
39  import com.google.gwt.uibinder.client.UiBinder;
40  import com.google.gwt.uibinder.client.UiField;
41  import com.google.gwt.user.client.DOM;
42  import com.google.gwt.user.client.ui.AbstractImagePrototype;
43  import com.google.gwt.user.client.ui.DialogBox;
44  import com.google.gwt.user.client.ui.LayoutPanel;
45  import com.google.gwt.user.client.ui.RootLayoutPanel;
46  import com.google.gwt.user.client.ui.SplitLayoutPanel;
47  import com.google.gwt.user.client.ui.Tree;
48  import com.google.gwt.user.client.ui.TreeItem;
49  import com.google.gwt.user.client.ui.Widget;
50  
51  /**
52   * Application main class. Creates the base UI structure and sets up
53   * individual samples
54   * @author laaglu
55   */
56  public class Main implements EntryPoint {
57  	/**
58  	 * UiBinder template interface
59  	 */
60  	interface MainBinder extends UiBinder<SplitLayoutPanel, Main> {
61  	}
62  	private static MainBinder binder = GWT.create(MainBinder.class);
63  
64  	/**
65  	 * The type passed into the
66  	 * {@link org.vectomatic.svg.samples.generator.SourceGenerator}.
67  	 */
68  	private static final class GeneratorInfo {
69  	}
70  	
71  	/**
72  	 * Client bundle interface
73  	 */
74  	public interface MainBundle extends ClientBundle {
75  		@Source("orgball.gif")
76  		public ImageResource treeItem();
77  		@Source("main.css")
78  		public MainCss css();
79  	}
80  	
81  	/**
82  	 * CSS interface
83  	 */
84  	public interface MainCss extends CssResource {
85  		/**
86  		 * style of tab items
87  		 */
88  		public String tab();
89  		/**
90  		 * style of sample container
91  		 */
92  		public String sample();
93  	}
94  	
95  	/**
96  	 * The mai resource bundle
97  	 */
98  	public static MainBundle mainBundle = GWT.create(MainBundle.class);
99  	
100 	/**
101 	 * The sample navigation tree
102 	 */
103 	@UiField
104 	Tree tree;
105 	/**
106 	 * The split panel
107 	 */
108 	@UiField
109 	SplitLayoutPanel splitPanel;
110 	/**
111 	 * A LayoutPanel which acts as a card layout (it has several
112 	 * child widgets, but only one is visible at a given time).
113 	 */
114 	@UiField
115 	LayoutPanel sampleContainer;
116 
117 
118 	@Override
119 	public void onModuleLoad() {
120 		GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
121 			public void onUncaughtException(Throwable throwable) {
122 				String text = "Uncaught exception: ";
123 				while (throwable != null) {
124 					StackTraceElement[] stackTraceElements = throwable
125 							.getStackTrace();
126 					text += throwable.toString() + "\n";
127 					for (int i = 0; i < stackTraceElements.length; i++) {
128 						text += "    at " + stackTraceElements[i] + "\n";
129 					}
130 					throwable = throwable.getCause();
131 					if (throwable != null) {
132 						text += "Caused by: ";
133 					}
134 				}
135 				DialogBox dialogBox = new DialogBox(true);
136 				DOM.setStyleAttribute(dialogBox.getElement(), "backgroundColor", "#ABCDEF");
137 				System.err.print(text);
138 				text = text.replaceAll(" ", "&nbsp;");
139 				dialogBox.setHTML("<pre>" + text + "</pre>");
140 				dialogBox.center();
141 			}
142 		});
143 		mainBundle.css().ensureInjected();
144 		
145 	    // Generate the source code for the examples
146 	    GWT.create(GeneratorInfo.class);
147 	    SplitLayoutPanel panel = binder.createAndBindUi(this);
148 	    
149 		// Populate the sample tree
150 	    TreeItem shapesSample = tree.addItem(SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(mainBundle.treeItem()).getHTML() +  " shapes"));
151 	    shapesSample.setUserObject(new ShapesSample());
152 	    TreeItem eventSample = tree.addItem(SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(mainBundle.treeItem()).getHTML() +  " events"));
153 	    eventSample.setUserObject(new EventSample());
154 	    TreeItem parserSample = tree.addItem(SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(mainBundle.treeItem()).getHTML() +  " parser"));
155 	    parserSample.setUserObject(new ParserSample());
156 	    TreeItem featuresSample = tree.addItem(SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(mainBundle.treeItem()).getHTML() +  " features"));
157 	    featuresSample.setUserObject(new FeaturesSample());
158 	    TreeItem widgetsSample = tree.addItem(SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(mainBundle.treeItem()).getHTML() +  " widgets"));
159 	    widgetsSample.setUserObject(new WidgetsSample());
160 	    TreeItem smilSample = tree.addItem(SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(mainBundle.treeItem()).getHTML() +  " SMIL animation"));
161 	    smilSample.setUserObject(new SmilSample());
162 	    TreeItem xpathSample = tree.addItem(SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(mainBundle.treeItem()).getHTML() +  " XPath"));
163 	    xpathSample.setUserObject(new XPathSample());
164 	    if (DOMHelper.hasFeature(SVGConstants.SVG_FEATURE_DND_EVENTS)) {
165 	    	TreeItem dndSample = tree.addItem(SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(mainBundle.treeItem()).getHTML() +  " Drag-and-Drop"));
166 		    dndSample.setUserObject(new DndSample());
167 	    }
168 	    TreeItem about = tree.addItem(SafeHtmlUtils.fromTrustedString(AbstractImagePrototype.create(mainBundle.treeItem()).getHTML() +  " about"));
169 	    about.setUserObject(new AboutSample());
170 	    tree.addSelectionHandler(new SelectionHandler<TreeItem>() {
171 			@Override
172 			public void onSelection(SelectionEvent<TreeItem> event) {
173 				SampleBase currentSample = (SampleBase) event.getSelectedItem().getUserObject();
174 				selectSample(currentSample.getPanel());
175 			}
176 	    	
177 	    });
178 	    tree.setSelectedItem(shapesSample);
179 
180 	    RootLayoutPanel.get().add(panel);
181 	}
182 	
183 	/**
184 	 * If it is not already a child of the layoutPanel,
185 	 * adds this sample panel to children of the layoutPanel. Make all other children
186 	 * hidden except this this sample panel.
187 	 */
188 	private void selectSample(Widget samplePanel) {
189 		int count = sampleContainer.getWidgetCount();
190 		int index = -1;
191 		for (int i = 0; i < count; i++) {
192 			Widget w = sampleContainer.getWidget(i);
193 			if (w != samplePanel) {
194 				sampleContainer.setWidgetVisible(w, false);
195 			} else {
196 				sampleContainer.setWidgetVisible(w, true);
197 				index = i;
198 			}
199 		}
200 		if (index == -1) {
201 			sampleContainer.add(samplePanel);
202 			sampleContainer.setWidgetVisible(samplePanel, true);
203 		}
204 	}
205 }