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 org.vectomatic.dom.svg.utils.OMSVGParser;
21  import org.vectomatic.dom.svg.utils.ParserException;
22  import org.vectomatic.file.File;
23  import org.vectomatic.file.FileReader;
24  import org.vectomatic.file.events.LoadEndEvent;
25  import org.vectomatic.file.events.LoadEndHandler;
26  import org.vectomatic.svg.edit.client.AppConstants;
27  import org.vectomatic.svg.edit.client.AppMessages;
28  import org.vectomatic.svg.edit.client.SvgrealApp;
29  
30  /**
31   * Class to load files into the application
32   * @author laaglu
33   */
34  public class FileLoadRequest extends LoadRequestBase {
35  	private File file;
36  
37  	public FileLoadRequest(File file) {
38  		this.file = file;
39  		this.title = file.getName();
40  	}
41  
42  	@Override
43  	public void load() {
44  		final FileReader reader = new FileReader();
45  		reader.addLoadEndHandler(new LoadEndHandler() {
46  			
47  			@Override
48  			public void onLoadEnd(LoadEndEvent event) {
49  				SvgrealApp app = SvgrealApp.getApp();
50  				try {
51  					app.addWindow(OMSVGParser.parse(reader.getStringResult()), FileLoadRequest.this);
52  				} catch(ParserException e) {
53  					app.info(AppConstants.INSTANCE.openLocalMenuItem(), AppMessages.INSTANCE.loadErrorMessage(file.getName(), e.getMessage()));
54  				}
55  				
56  			}
57  		});
58  		reader.readAsText(file);
59  	}
60  	
61  	@Override
62  	public boolean equals(Object o) {
63  		if (o instanceof FileLoadRequest) {
64  			FileLoadRequest r = (FileLoadRequest)o;
65  			return file.getName().equals(r.file.getName()) && file.getSize() == r.file.getSize();
66  		}
67  		return false;
68  	}
69  	@Override
70  	public int hashCode() {
71  		return file.getName().hashCode();
72  	}
73  
74  }