KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > editpolicy > tree > DrawComponentTreeContainerEditPolicy


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.editpolicy.tree;
29
30 import java.util.List JavaDoc;
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 JavaDoc 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");//$NON-NLS-1$
77
List JavaDoc 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"));//$NON-NLS-1$
94
}
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 // return createCreateCommand(child, null, index, EditorPlugin.getResourceString("command_create_drawcomponent"));//$NON-NLS-1$
104
Rectangle bounds = J2DUtil.toDraw2D(child.getBounds());
105         return createCreateCommand(child, bounds, index, EditorPlugin.getResourceString("command.create.drawcomponent"));//$NON-NLS-1$
106
}
107
108     protected Command getMoveChildrenCommand(ChangeBoundsRequest request)
109     {
110         CompoundCommand command = new CompoundCommand();
111         List JavaDoc editparts = request.getEditParts();
112         List JavaDoc 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