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.command.dnd;
19  
20  import java.util.List;
21  
22  import org.vectomatic.dom.svg.utils.SVGConstants;
23  import org.vectomatic.svg.edit.client.engine.SVGModel;
24  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
25  import org.vectomatic.svg.edit.client.model.svg.SVGViewBoxElementModel;
26  
27  import com.extjs.gxt.ui.client.event.DNDEvent;
28  
29  /**
30   * Base class for drag and drop handlers
31   * @author laaglu
32   */
33  public abstract class DndHandlerBase implements IDndHandler {
34  	@Override
35  	public boolean isValidSource(DNDEvent event, List<SVGElementModel> sourceElements) {
36  		// Forbid dragging the tree root or the viewbox of a model
37  		SVGModel model = sourceElements.get(0).getOwner();
38  		SVGElementModel root = model.getRoot();
39  		SVGViewBoxElementModel viewBox = model.getViewBox();
40  		for (SVGElementModel sourceElement : sourceElements) {
41  			if (sourceElement == root || sourceElement == viewBox) {
42  				return false;
43  			}
44  		}
45  		return true;
46  	}
47  	
48  	public static String getSourceElementNames(List<SVGElementModel> sourceElements) {
49  		int size = sourceElements.size();
50  		if (size == 1) {
51  			return sourceElements.get(0).<String>get(SVGConstants.SVG_TITLE_TAG);
52  		}
53  		StringBuilder builder = new StringBuilder();
54  		for (int i = 0; i < size; i++) {
55  			if (i > 0) {
56  				builder.append(";");
57  			}
58  			builder.append(sourceElements.get(i).<String>get(SVGConstants.SVG_TITLE_TAG));
59  		}
60  		return builder.toString();
61  	}
62  
63  	
64  	@Override
65  	public boolean isValidTarget(List<SVGElementModel> sourceElements, SVGElementModel target) {
66  		if (target == null) {
67  			return false;
68  		}
69  		// The viewBox is not a valid drop target
70  		if (target instanceof SVGViewBoxElementModel) {
71  			return false;
72  		}
73  		// An element cannot be dropped onto itself or one of its descendants
74  		for (SVGElementModel sourceElement : sourceElements) {
75  			if (sourceElement.isAncestorOf(target)) {
76  				return false;
77  			}
78  		}
79  		return true;
80  	}
81  
82  	@Override
83  	public int getKeyCode() {
84  		return -1;
85  	}
86  }