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.svg.edit.client.command.DndCommandFactory.DropGesture;
23  import org.vectomatic.svg.edit.client.command.ICommand;
24  import org.vectomatic.svg.edit.client.engine.SVGModel;
25  import org.vectomatic.svg.edit.client.model.ModelConstants;
26  import org.vectomatic.svg.edit.client.model.svg.SVGElementModel;
27  import org.vectomatic.svg.edit.client.model.svg.SVGNamedElementModel;
28  
29  import com.extjs.gxt.ui.client.event.DNDEvent;
30  import com.extjs.gxt.ui.client.util.Format;
31  import com.google.gwt.event.dom.client.KeyCodes;
32  
33  
34  /**
35   * Class to manage intra-model clone drag-and-drop interactions
36   * and inter-model copy drag-and-drop interactions 
37   * @author laaglu
38   */
39  public class DndCopyHandler extends DndHandlerBase {
40  	@Override
41  	public boolean isValidSource(DNDEvent event, List<SVGElementModel> sourceElements) {
42  		return event.isControlKey() && super.isValidSource(event, sourceElements);
43  	}
44  
45  	@Override
46  	public String getOperationCssAttr() {
47  		return "copy";
48  	}
49  
50  	@Override
51  	public String getMessage(List<SVGElementModel> sourceElements) {
52  		return Format.substitute(ModelConstants.INSTANCE.dndCopy(), getSourceElementNames(sourceElements));
53  	}
54  
55  	@Override
56  	public int getKeyCode() {
57  		return KeyCodes.KEY_CTRL;
58  	}
59  
60  	@Override
61  	public void createCommands(List<SVGElementModel> sourceElements, SVGElementModel target, DropGesture dropGesture) {
62  		ICommand command = null;
63  		SVGModel src = sourceElements.get(0).getOwner();
64  		SVGModel dest = target.getOwner();
65  		if (src == dest) {
66  			command = new CloneCommand(sourceElements, target, dropGesture);
67  		} else {
68  			String description = null;
69  			switch(dropGesture) {
70  				case OnNode:
71  					description = ModelConstants.INSTANCE.dndCopyCmdIn();
72  					break;
73  				case BeforeNode:
74  					description = ModelConstants.INSTANCE.dndCopyCmdBefore();
75  					break;
76  				case AfterNode:
77  					description = ModelConstants.INSTANCE.dndCopyCmdAfter();
78  					break;
79  			}
80  			description = Format.substitute(description, SVGNamedElementModel.getNames(sourceElements), target.toString(), ((SVGNamedElementModel)src.getRoot()).getName());
81  			command = new CopyCommand(sourceElements, target, dropGesture, description);
82  		}
83  		command.commit();
84  		dest.getCommandStore().addCommand(command);		
85  	}
86  	
87  }