View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal 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   * svgreal 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 svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.load;
19  
20  import java.io.IOException;
21  
22  import org.vectomatic.dom.svg.utils.OMSVGParser;
23  import org.vectomatic.dom.svg.utils.ParserException;
24  import org.vectomatic.svg.edit.client.AppConstants;
25  import org.vectomatic.svg.edit.client.AppMessages;
26  import org.vectomatic.svg.edit.client.SvgrealApp;
27  
28  import com.google.gwt.core.client.GWT;
29  import com.google.gwt.http.client.Request;
30  import com.google.gwt.http.client.RequestBuilder;
31  import com.google.gwt.http.client.RequestCallback;
32  import com.google.gwt.http.client.RequestException;
33  import com.google.gwt.http.client.Response;
34  
35  /**
36   * Class to load URLs into the application
37   * @author laaglu
38   */
39  public class UrlLoadRequest extends LoadRequestBase {
40  	private String url;
41  	public UrlLoadRequest(String url) {
42  		this.url = url;
43  		int index = url.lastIndexOf('/');
44  		this.title = (index != -1 && index != url.length() - 1) ? url.substring(index + 1) : url;
45  	}
46  
47  	@Override
48  	public void load() {
49  		final SvgrealApp app = SvgrealApp.getApp();
50  		final String resourceUrl = FetchUtils.getFetchUrl(url, "text/xml");
51  		RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, resourceUrl);
52  		requestBuilder.setCallback(new RequestCallback() {
53  			public void onError(Request request, Throwable e) {
54  				GWT.log("Cannot fetch " + url, e);
55  				app.info(AppConstants.INSTANCE.openUrlMenuItem(), AppMessages.INSTANCE.loadErrorMessage(url, e.getMessage()));
56  			}
57  
58  			private void onSuccess(Request request, Response response) {
59  				try {
60  					app.addWindow(OMSVGParser.parse(response.getText()), UrlLoadRequest.this);
61  				} catch(ParserException e) {
62  					app.info(AppConstants.INSTANCE.openLocalMenuItem(), AppMessages.INSTANCE.loadErrorMessage(resourceUrl, e.getMessage()));
63  				}
64  			}
65  			
66  			public void onResponseReceived(Request request, Response response) {
67  				if (response.getStatusCode() == Response.SC_OK) {
68  					onSuccess(request, response);
69  				} else {
70  					onError(request, new IOException(AppMessages.INSTANCE.httpErrorMessage(Integer.toString(response.getStatusCode()))));
71  				}
72  			}
73  		});
74  		try {
75  			requestBuilder.send();
76  		} catch (RequestException e) {
77  			GWT.log("Cannot fetch " + url, e);
78  			app.info(AppConstants.INSTANCE.openUrlMenuItem(), AppMessages.INSTANCE.loadErrorMessage(url, e.getMessage()));
79  		}
80  	}
81  
82  	@Override
83  	public boolean equals(Object o) {
84  		if (o instanceof UrlLoadRequest) {
85  			UrlLoadRequest r = (UrlLoadRequest)o;
86  			return url.equals(r.url);
87  		}
88  		return false;
89  	}
90  	@Override
91  	public int hashCode() {
92  		return url.hashCode();
93  	}
94  
95  }