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.dnd.DataTransferExt;
21  import org.vectomatic.dom.svg.utils.SVGConstants;
22  import org.vectomatic.file.File;
23  import org.vectomatic.file.FileList;
24  import org.vectomatic.svg.edit.client.gxt.widget.ViewportExt;
25  
26  import com.google.gwt.core.client.GWT;
27  import com.google.gwt.event.dom.client.DragEnterEvent;
28  import com.google.gwt.event.dom.client.DragEnterHandler;
29  import com.google.gwt.event.dom.client.DragLeaveEvent;
30  import com.google.gwt.event.dom.client.DragLeaveHandler;
31  import com.google.gwt.event.dom.client.DragOverEvent;
32  import com.google.gwt.event.dom.client.DragOverHandler;
33  import com.google.gwt.event.dom.client.DropEvent;
34  import com.google.gwt.event.dom.client.DropHandler;
35  
36  /**
37   * Class to handler drag and drop of external files on the viewport.
38   * @author laaglu
39   */
40  public class DndHandler implements DragEnterHandler, DragLeaveHandler, DragOverHandler, DropHandler {
41  	private ViewportExt viewport;
42  	public DndHandler(ViewportExt viewport) {
43  		this.viewport = viewport;
44  		viewport.addDragEnterHandler(this);
45  		viewport.addDragLeaveHandler(this);
46  		viewport.addDragOverHandler(this);
47  		viewport.addDropHandler(this);
48  	}
49  	
50  	@Override
51  	public void onDragEnter(DragEnterEvent event) {
52  		viewport.setStyleAttribute("background-color", SVGConstants.CSS_GAINSBORO_VALUE);
53  		event.stopPropagation();
54  		event.preventDefault();
55  	}
56  	@Override
57  	public void onDragLeave(DragLeaveEvent event) {
58  		viewport.setStyleAttribute("background-color", SVGConstants.CSS_BEIGE_VALUE);
59  		event.stopPropagation();
60  		event.preventDefault();
61  	}
62  	@Override
63  	public void onDragOver(DragOverEvent event) {
64  		// Mandatory handler, otherwise the default
65  		// behavior will kick in and onDrop will never
66  		// be called
67  		event.stopPropagation();
68  		event.preventDefault();
69  	}
70  	@Override
71  	public void onDrop(DropEvent event) {
72  		GWT.log("onDrop");
73  		viewport.setStyleAttribute("background-color", SVGConstants.CSS_BEIGE_VALUE);
74  		FileList files = event.getDataTransfer().<DataTransferExt>cast().getFiles();
75  		for (File file : files) {
76  			if ("image/svg+xml".equals(file.getType())) {
77  				new FileLoadRequest(file).load();
78  			}
79  		}
80  		event.stopPropagation();
81  		event.preventDefault();
82  	}
83  }