1 8 package com.nightlabs.editor2d.editpolicy.tree; 9 10 import java.util.List ; 11 12 import org.eclipse.draw2d.geometry.Dimension; 13 import org.eclipse.draw2d.geometry.Rectangle; 14 import org.eclipse.gef.EditPart; 15 import org.eclipse.gef.commands.Command; 16 import org.eclipse.gef.commands.CompoundCommand; 17 import org.eclipse.gef.commands.UnexecutableCommand; 18 import org.eclipse.gef.editpolicies.TreeContainerEditPolicy; 19 import org.eclipse.gef.requests.ChangeBoundsRequest; 20 import org.eclipse.gef.requests.CreateRequest; 21 22 import com.nightlabs.editor2d.DrawComponent; 23 import com.nightlabs.editor2d.DrawComponentContainer; 24 import com.nightlabs.editor2d.EditorPlugin; 25 import com.nightlabs.editor2d.command.CreateDrawComponentCommand; 26 import com.nightlabs.editor2d.command.DrawComponentReorderCommand; 27 import com.nightlabs.editor2d.util.J2DUtil; 28 29 30 public class DrawComponentTreeContainerEditPolicy 31 extends TreeContainerEditPolicy 32 { 33 protected Command createCreateCommand(DrawComponent child, Rectangle r, int index, String label) 34 { 35 CreateDrawComponentCommand cmd = new CreateDrawComponentCommand(); 36 Rectangle rect; 37 if(r == null) { 38 rect = new Rectangle(); 39 rect.setSize(new Dimension(-1,-1)); 40 } 41 else { 42 rect = r; 43 } 44 cmd.setLocation(rect); 45 cmd.setParent((DrawComponentContainer)getHost().getModel()); 46 cmd.setChild(child); 47 cmd.setLabel(label); 48 if(index >= 0) 49 cmd.setIndex(index); 50 return cmd; 51 } 52 53 protected Command getAddCommand(ChangeBoundsRequest request) 54 { 55 CompoundCommand command = new CompoundCommand(); 56 command.setDebugLabel("Add in DrawComponentTreeContainerEditPolicy"); List editparts = request.getEditParts(); 58 int index = findIndexOfTreeItemAt(request.getLocation()); 59 60 for(int i = 0; i < editparts.size(); i++) 61 { 62 EditPart child = (EditPart)editparts.get(i); 63 if(isAncestor(child,getHost())) 64 command.add(UnexecutableCommand.INSTANCE); 65 else { 66 DrawComponent childModel = (DrawComponent)child.getModel(); 67 command.add(createCreateCommand( 68 childModel, 69 new Rectangle( 70 new org.eclipse.draw2d.geometry.Point(childModel.getX(), childModel.getY()), 71 new Dimension(childModel.getWidth(), childModel.getHeight()) 72 ), 73 index, "Reparent DrawComponent")); } 75 } 76 return command; 77 } 78 79 protected Command getCreateCommand(CreateRequest request) 80 { 81 DrawComponent child = (DrawComponent)request.getNewObject(); 82 int index = findIndexOfTreeItemAt(request.getLocation()); 83 Rectangle bounds = J2DUtil.toDraw2D(child.getBounds()); 85 return createCreateCommand(child, bounds, index, EditorPlugin.getResourceString("command_create_drawcomponent")); } 87 88 protected Command getMoveChildrenCommand(ChangeBoundsRequest request) 89 { 90 CompoundCommand command = new CompoundCommand(); 91 List editparts = request.getEditParts(); 92 List children = getHost().getChildren(); 93 int newIndex = findIndexOfTreeItemAt(request.getLocation()); 94 95 for(int i = 0; i < editparts.size(); i++) 96 { 97 EditPart child = (EditPart)editparts.get(i); 98 int tempIndex = newIndex; 99 int oldIndex = children.indexOf(child); 100 if(oldIndex == tempIndex || oldIndex + 1 == tempIndex) { 101 command.add(UnexecutableCommand.INSTANCE); 102 return command; 103 } else if(oldIndex <= tempIndex) { 104 tempIndex--; 105 } 106 command.add(new DrawComponentReorderCommand( 107 (DrawComponent)child.getModel(), 108 (DrawComponentContainer)getHost().getModel(), 109 tempIndex)); 110 } 111 return command; 112 } 113 114 protected boolean isAncestor(EditPart source, EditPart target) 115 { 116 if(source == target) 117 return true; 118 if(target.getParent() != null) 119 return isAncestor(source, target.getParent()); 120 return false; 121 } 122 123 } 124 | Popular Tags |