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