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.gxt.widget;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.vectomatic.svg.edit.client.SVGWindow;
24  import org.vectomatic.svg.edit.client.command.CommandFactories;
25  import org.vectomatic.svg.edit.client.command.DndCommandFactory;
26  import org.vectomatic.svg.edit.client.engine.SVGModel;
27  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
28  
29  import com.extjs.gxt.ui.client.dnd.TreePanelDragSource;
30  import com.extjs.gxt.ui.client.event.DNDEvent;
31  import com.extjs.gxt.ui.client.store.TreeStoreModel;
32  import com.google.gwt.core.client.GWT;
33  
34  /**
35   * Drag source for SVGWindow
36   * @author laaglu
37   */
38  public class SVGTreePanelDragSource extends TreePanelDragSource {
39  	protected DndCommandFactory dndCommandFactory;
40  	protected SVGModel svgModel;
41  
42  	public SVGTreePanelDragSource(SVGWindow window) {
43  		super(window.getTree());
44  		svgModel = window.getSvgModel();
45  		dndCommandFactory = CommandFactories.getDndCommandFactory();
46  	}
47  	public SVGModel getSvgModel() {
48  		return svgModel;
49  	}
50  
51  	protected void onDragStart(DNDEvent event) {
52  		super.onDragStart(event);
53  		if (!event.isCancelled()) {
54  			// Extract the SVG models from the drag event
55  			List<TreeStoreModel> storeModels = event.getData();
56  			if (storeModels != null) {
57  				List<SVGElementModel> svgModels = new ArrayList<SVGElementModel>();
58  				for (TreeStoreModel storeModel : storeModels) {
59  					svgModels.add((SVGElementModel) storeModel.getModel());
60  				}
61  				
62  				if (dndCommandFactory.isValidSource(event, svgModels)) {
63  					dndCommandFactory.start(this);
64  				} else {
65  					event.setCancelled(true);
66  					event.getStatus().setStatus(false);
67  				}
68  			}
69  		}
70  	}
71  
72  
73  	@Override
74  	protected void onDragDrop(DNDEvent event) {
75  		GWT.log("SVGTreePanelDragSource.onDragDrop");
76  		dndCommandFactory.stop();
77  	}
78  
79  	@Override
80  	protected void onDragFail(DNDEvent event) {
81  		GWT.log("SVGTreePanelDragSource.onDragFail");
82  		dndCommandFactory.stop();
83  	}
84  }