34611

How to create an gwt widget for svg image properly?

Question:

I know how to load svg-images into gwt: <a href="https://stackoverflow.com/questions/12858907/how-to-load-a-svg-in-gwt-widget-from-an-url" rel="nofollow">last answer here</a>

with this i just trying to create a "SvgImage" class. It should work like gwt's image(url): I could add the image independent of loading state and as soon the loading is finished the image becomes visible. how to achieve this?(i tried to read image' sources but it is terrible - neither can't find the place where url is loaded nor where the LoadEvent is fired)

Or in general what is a good approach to create widgets that loads their contents via a given url?

Answer1:

i dug a liitle bit in gwt image source and found that there is "replaceElement(Node)" - a protected method especially made for image but this method is based on

Widget.getElement().replaceChild(newElement, oldElement);

Here is my complete SvgWidget class:

public class SvgWidget implements IsWidget { private String svgUrl; private ADBTexte strings; private IsWidget thisW; private Loading loadingWidget; @Inject private SvgWidget(@Assisted final String svgUrl, final ADBTexte strings, final Loading loadingWidget) { this.svgUrl = svgUrl; this.strings = strings; this.loadingWidget = loadingWidget; } @Override public Widget asWidget() { thisW = new SimplePanel(loadingWidget); RequestBuilder rB = new RequestBuilder(RequestBuilder.GET, svgUrl); rB.setCallback(new RequestCallback() { @Override public void onResponseReceived(final Request request, final Response response) { Widget result = new HTML(response.getText()); thisW.asWidget() .getElement() .replaceChild(result.getElement(), thisW.asWidget().getElement().getChild(0)); } @Override public void onError(final Request request, final Throwable exception) { Widget result = new Label(strings.chartError()); thisW.asWidget() .getElement() .replaceChild(result.getElement(), thisW.asWidget().getElement().getChild(0)); GWT.log("Error on loading chart: ", exception); } }); try { rB.send(); } catch (RequestException e) { Widget result = new Label(strings.chartError()); thisW.asWidget().getElement().replaceChild(result.getElement(), thisW.asWidget().getElement().getChild(0)); GWT.log("Error on sending request for chart: ", e); } return thisW.asWidget(); } /** * Use this to get an instance of {@link SvgWidget}. * */ public interface Factory { /** * * @param svgUrl * url for svg graphic * @return instance of {@link SvgWidget} displaying the svg accessible via the given url */ SvgWidget get(@Assisted String svgUrl); } }

The relevant stuff is in asWidget(). But i am unsure if this is a good/ the best solution:

<ul><li>is SimplePanel a good container (i only need the container to replace an element)</li> <li>probably i should implement HasLoadHandlers?!</li> <li>the widget displays a loading widget - probably it is better placed</li> <li>something else to take into account?</li> </ul>

(for those who don't know dependency injection (with gin): ignore all unknown annotations and the factory interfce)

Recommend

  • MIPS: Write AND read a file
  • CKEditor on appended textarea
  • Kotlin coroutine can't handle exception
  • write to Java console when my Javascript callback (made in Selenium) returns
  • CodeMirror : how to limit height in editor
  • Android performance: cost of SharedPreferences
  • How to apply implicit conversions between tuples?
  • How Django's url template tag works?
  • How to plot points around a circle in R
  • sinonjs - advance clock to 59 minutes and wait for 1 minute actually
  • IE6 textBox.focus(); causing “Unexpected call to method or property access”
  • Angularjs passing values
  • php email sending script not sending email
  • How to resolve “Error(s): Please enter a valid format for ”Select“.” when creating a Jawbone UP app?
  • How can I get a list of all open named pipes in Windows and avoiding possible exceptions?
  • How can I insert a DIV *between* an existing parent and child?
  • Appending same node in different windows
  • java - inserting text using JTextArea by number line
  • Add XHTML element referencing existing namespace
  • Unexpected behavior with exception handling in async, possible bug?
  • Ambiguous overload on template operators
  • Can't make a resizable / draggable
  • Replace Node Name
  • File Not Found Error in Python
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Bypass multiple inheritance in Java
  • CakePHP ACL tutorial initDB function warnings
  • Use of this Javascript
  • C++ Partial template specialization - design simplification
  • Recording logins for password protected directories
  • how to do an event when i swipe from fragment to the other
  • RectangularRangeIndicator format like triangular using dojo
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Trying to switch camera back to front but getting exception
  • How to get next/previous record number?
  • Python: how to group similar lists together in a list of lists?
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Programmatically clearing map cache
  • How can i traverse a binary tree from right to left in java?