KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > editpolicy > DrawComponentContainerXYLayoutPolicy


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;
29
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.eclipse.draw2d.Graphics;
34 import org.eclipse.draw2d.IFigure;
35 import org.eclipse.draw2d.Label;
36 import org.eclipse.draw2d.PositionConstants;
37 import org.eclipse.draw2d.RectangleFigure;
38 import org.eclipse.draw2d.Shape;
39 import org.eclipse.draw2d.XYLayout;
40 import org.eclipse.draw2d.geometry.Dimension;
41 import org.eclipse.draw2d.geometry.Point;
42 import org.eclipse.draw2d.geometry.Rectangle;
43 import org.eclipse.gef.EditPart;
44 import org.eclipse.gef.EditPolicy;
45 import org.eclipse.gef.GraphicalEditPart;
46 import org.eclipse.gef.Request;
47 import org.eclipse.gef.SnapToGuides;
48 import org.eclipse.gef.commands.Command;
49 import org.eclipse.gef.commands.CompoundCommand;
50 import org.eclipse.gef.editpolicies.LayoutEditPolicy;
51 import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
52 import org.eclipse.gef.requests.ChangeBoundsRequest;
53 import org.eclipse.gef.requests.CreateRequest;
54 import org.eclipse.gef.rulers.RulerProvider;
55 import org.nightlabs.editor2d.DrawComponent;
56 import org.nightlabs.editor2d.DrawComponentContainer;
57 import org.nightlabs.editor2d.EditorGuide;
58 import org.nightlabs.editor2d.EditorPlugin;
59 import org.nightlabs.editor2d.ShapeDrawComponent;
60 import org.nightlabs.editor2d.command.ChangeGuideCommand;
61 import org.nightlabs.editor2d.command.CreateDrawComponentCommand;
62 import org.nightlabs.editor2d.command.CreateImageCommand;
63 import org.nightlabs.editor2d.command.CreateShapeCommand;
64 import org.nightlabs.editor2d.command.CreateTextCommand;
65 import org.nightlabs.editor2d.command.EditShapeCommand;
66 import org.nightlabs.editor2d.command.RotateCenterCommand;
67 import org.nightlabs.editor2d.command.RotateCommand;
68 import org.nightlabs.editor2d.command.SetConstraintCommand;
69 import org.nightlabs.editor2d.command.ShearCommand;
70 import org.nightlabs.editor2d.edit.ShapeDrawComponentEditPart;
71 import org.nightlabs.editor2d.figures.AbstractShapeFigure;
72 import org.nightlabs.editor2d.figures.FeedbackShapeFigure;
73 import org.nightlabs.editor2d.figures.ShapeFigure;
74 import org.nightlabs.editor2d.j2d.GeneralShape;
75 import org.nightlabs.editor2d.request.EditorBoundsRequest;
76 import org.nightlabs.editor2d.request.EditorCreateRequest;
77 import org.nightlabs.editor2d.request.EditorEditShapeRequest;
78 import org.nightlabs.editor2d.request.EditorLocationRequest;
79 import org.nightlabs.editor2d.request.EditorRequestConstants;
80 import org.nightlabs.editor2d.request.EditorRotateCenterRequest;
81 import org.nightlabs.editor2d.request.EditorRotateRequest;
82 import org.nightlabs.editor2d.request.EditorShearRequest;
83 import org.nightlabs.editor2d.request.ImageCreateRequest;
84 import org.nightlabs.editor2d.request.LineCreateRequest;
85 import org.nightlabs.editor2d.request.TextCreateRequest;
86 import org.nightlabs.editor2d.util.EditorUtil;
87 import org.nightlabs.editor2d.util.FeedbackUtil;
88 import org.nightlabs.editor2d.util.J2DUtil;
89
90 public class DrawComponentContainerXYLayoutPolicy
91 extends XYLayoutEditPolicy
92 implements EditorRequestConstants
93 {
94   public static final Logger LOGGER = Logger.getLogger(DrawComponentContainerXYLayoutPolicy.class);
95   
96   public DrawComponentContainerXYLayoutPolicy(XYLayout layout) {
97     super();
98     setXyLayout(layout);
99   }
100     
101   /**
102    * @see org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#createAddCommand(org.eclipse.gef.EditPart, java.lang.Object)
103    */

104   protected Command createAddCommand(EditPart child, Object JavaDoc constraint)
105   {
106     return null;
107   }
108   
109   protected Command createChangeConstraintCommand(ChangeBoundsRequest request,
110       EditPart child, Object JavaDoc constraint)
111   {
112         SetConstraintCommand cmd = new SetConstraintCommand();
113         DrawComponent part = (DrawComponent)child.getModel();
114         cmd.setPart(part);
115         cmd.setBounds(J2DUtil.toAWTRectangle((Rectangle)constraint));
116         Command result = cmd;
117         
118         if ((request.getResizeDirection() & PositionConstants.NORTH_SOUTH) != 0)
119         {
120             Integer JavaDoc guidePos = (Integer JavaDoc)request.getExtendedData().get(SnapToGuides.KEY_HORIZONTAL_GUIDE);
121             if (guidePos != null) {
122               result = chainGuideAttachmentCommand(request, part, result, true);
123             }
124             else if (part.getHorizontalGuide() != null)
125             {
126                 // SnapToGuides didn't provide a horizontal guide, but this part is attached
127
// to a horizontal guide. Now we check to see if the part is attached to
128
// the guide along the edge being resized. If that is the case, we need to
129
// detach the part from the guide; otherwise, we leave it alone.
130
int alignment = part.getHorizontalGuide().getAlignment(part);
131                 int edgeBeingResized = 0;
132                 if ((request.getResizeDirection() & PositionConstants.NORTH) != 0)
133                   edgeBeingResized = -1;
134                 else
135                   edgeBeingResized = 1;
136                 if (alignment == edgeBeingResized)
137                   result = result.chain(new ChangeGuideCommand(part, true));
138             }
139         }
140         
141         if ((request.getResizeDirection() & PositionConstants.EAST_WEST) != 0)
142         {
143           Integer JavaDoc guidePos = (Integer JavaDoc)request.getExtendedData().get(SnapToGuides.KEY_VERTICAL_GUIDE);
144           if (guidePos != null) {
145                 result = chainGuideAttachmentCommand(request, part, result, false);
146           }
147           else if (part.getVerticalGuide() != null)
148           {
149                 int alignment = part.getVerticalGuide().getAlignment(part);
150                 int edgeBeingResized = 0;
151                 if ((request.getResizeDirection() & PositionConstants.WEST) != 0)
152                 edgeBeingResized = -1;
153                 else
154                 edgeBeingResized = 1;
155                 if (alignment == edgeBeingResized)
156                   result = result.chain(new ChangeGuideCommand(part, false));
157             }
158         }
159         
160         if (request.getType().equals(REQ_MOVE_CHILDREN)
161         || request.getType().equals(REQ_ALIGN_CHILDREN))
162         {
163             result = chainGuideAttachmentCommand(request, part, result, true);
164             result = chainGuideAttachmentCommand(request, part, result, false);
165             result = chainGuideDetachmentCommand(request, part, result, true);
166             result = chainGuideDetachmentCommand(request, part, result, false);
167         }
168         
169         return result;
170   }
171   
172     /* (non-Javadoc)
173      * @see org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#createChangeConstraintCommand(org.eclipse.gef.EditPart, java.lang.Object)
174      */

175     protected Command createChangeConstraintCommand(EditPart child, Object JavaDoc constraint) {
176         return null;
177     }
178             
179   protected EditPolicy createChildEditPolicy(EditPart child) {
180     return new DrawComponentResizeEditPolicy();
181   }
182   
183   /* (non-Javadoc)
184    * @see org.eclipse.gef.editpolicies.LayoutEditPolicy#getDeleteDependantCommand(org.eclipse.gef.Request)
185    */

186   protected Command getDeleteDependantCommand(Request request) {
187     return null;
188   }
189
190   protected Command getOrphanChildrenCommand(Request request) {
191     return null;
192   }
193       
194   protected Command getAddCommand(Request generic)
195   {
196 // LOGGER.debug("getAddCommand()");
197

198     ChangeBoundsRequest request = (ChangeBoundsRequest)generic;
199     List JavaDoc editParts = request.getEditParts();
200     CompoundCommand command = new CompoundCommand();
201     command.setDebugLabel("Add in ConstrainedLayoutEditPolicy");//$NON-NLS-1$
202
GraphicalEditPart childPart;
203     Rectangle r;
204     Object JavaDoc constraint;
205
206     for (int i = 0; i < editParts.size(); i++)
207     {
208         childPart = (GraphicalEditPart)editParts.get(i);
209         r = childPart.getFigure().getBounds().getCopy();
210         int oldWidth = r.width;
211         int oldHeight = r.height;
212         //convert r to absolute from childpart figure
213
childPart.getFigure().translateToAbsolute(r);
214         r = request.getTransformedRectangle(r);
215         //convert this figure to relative
216
getLayoutContainer().translateToRelative(r);
217         
218         // WORKAROUND: reason = size changes when moving,
219
// solution = check old size before transforming, if size changed set old size
220
if ((r.width != oldWidth || r.height != oldHeight) &&
221             request.getSizeDelta().equals(0,0))
222           r.setSize(oldWidth, oldHeight);
223           
224         getLayoutContainer().translateFromParent(r);
225         r.translate(getLayoutOrigin().getNegated());
226         constraint = getConstraintFor(r);
227         command.add(createAddCommand(generic, childPart,
228             translateToModelConstraint(constraint)));
229     }
230     return command.unwrap();
231   }
232   
233     protected Command chainGuideAttachmentCommand(Request request, DrawComponent part, Command cmd, boolean horizontal)
234     {
235         Command result = cmd;
236         
237         // Attach to guide, if one is given
238
Integer JavaDoc guidePos = (Integer JavaDoc)request.getExtendedData()
239                 .get(horizontal ? SnapToGuides.KEY_HORIZONTAL_GUIDE
240                                 : SnapToGuides.KEY_VERTICAL_GUIDE);
241         if (guidePos != null) {
242             int alignment = ((Integer JavaDoc)request.getExtendedData()
243                     .get(horizontal ? SnapToGuides.KEY_HORIZONTAL_ANCHOR
244                                     : SnapToGuides.KEY_VERTICAL_ANCHOR)).intValue();
245             ChangeGuideCommand cgm = new ChangeGuideCommand(part, horizontal);
246             cgm.setNewGuide(findGuideAt(guidePos.intValue(), horizontal), alignment);
247             result = result.chain(cgm);
248         }
249
250         return result;
251     }
252     
253     protected Command chainGuideDetachmentCommand(Request request, DrawComponent part,
254             Command cmd, boolean horizontal) {
255         Command result = cmd;
256         
257         // Detach from guide, if none is given
258
Integer JavaDoc guidePos = (Integer JavaDoc)request.getExtendedData()
259                 .get(horizontal ? SnapToGuides.KEY_HORIZONTAL_GUIDE
260                                 : SnapToGuides.KEY_VERTICAL_GUIDE);
261         if (guidePos == null)
262             result = result.chain(new ChangeGuideCommand(part, horizontal));
263
264         return result;
265     }
266   
267     protected EditorGuide findGuideAt(int pos, boolean horizontal)
268     {
269         RulerProvider provider = ((RulerProvider)getHost().getViewer().getProperty(
270                 horizontal ? RulerProvider.PROPERTY_VERTICAL_RULER
271                 : RulerProvider.PROPERTY_HORIZONTAL_RULER));
272         return (EditorGuide)provider.getGuideAt(pos);
273     }
274     
275     /**
276      * Generates a draw2d constraint for the given <code>EditorCreateRequest</code>. If the
277      * EditorCreateRequest has a size, {@link #getConstraintFor(Rectangle)} is called with a
278      * Rectangle of that size and the result is returned. This is used during size-on-drop
279      * creation. Otherwise, {@link #getConstraintFor(Point)} is returned.
280      * <P>
281      * The EditorCreateRequest location is relative the Viewer. The location is made
282      * layout-relative before calling one of the methods mentioned above.
283      * @param request the EditorCreateRequest
284      * @return a draw2d constraint
285      */

286     protected Object JavaDoc getConstraintFor(CreateRequest request)
287     {
288         IFigure figure = getLayoutContainer();
289         Point where = request.getLocation().getCopy();
290         Dimension size = request.getSize();
291                 
292         figure.translateToRelative(where);
293         figure.translateFromParent(where);
294         where.translate(getLayoutOrigin().getNegated());
295         
296         if (size == null || size.isEmpty())
297             return getConstraintFor(where);
298         else
299         {
300             //$TODO Probably should use PrecisionRectangle at some point instead of two
301
// geometrical objects
302
size = size.getCopy();
303             figure.translateToRelative(size);
304             figure.translateFromParent(size);
305             
306             return getConstraintFor(new Rectangle(where, size));
307         }
308     }
309     
310     private static final Dimension DEFAULT_SIZE = new Dimension(-1, -1);
311     
312   public Rectangle getConstraintRectangleFor(Point point)
313   {
314     Point p = point.getCopy();
315         IFigure figure = getLayoutContainer();
316         figure.translateToRelative(p);
317         figure.translateFromParent(p);
318         p.translate(getLayoutOrigin().getNegated());
319         return new Rectangle(p, DEFAULT_SIZE);
320   }
321   
322   public Point getConstraintPointFor(Point point)
323   {
324     Point p = point.getCopy();
325         IFigure figure = getLayoutContainer();
326         figure.translateToRelative(p);
327         figure.translateFromParent(p);
328         p.translate(getLayoutOrigin().getNegated());
329         return p;
330   }
331   
332   public Rectangle getConstraintRectangleFor(Rectangle rectangle)
333   {
334     Rectangle r = rectangle.getCopy();
335         IFigure figure = getLayoutContainer();
336         figure.translateToRelative(r);
337         figure.translateFromParent(r);
338     r.translate(getLayoutOrigin().getNegated());
339     return r;
340   }
341     
342   public Command getCommand(Request request)
343   {
344     if (REQ_EDIT_SHAPE.equals(request.getType()))
345         return getEditShapeCommand((EditorEditShapeRequest)request);
346
347     if (request instanceof TextCreateRequest)
348       return getCreateTextCommand((TextCreateRequest)request);
349     
350     if (request instanceof EditorRotateRequest)
351       return getRotateCommand((EditorRotateRequest)request);
352
353     if (request instanceof EditorRotateCenterRequest)
354       return getRotateCenterCommand((EditorRotateCenterRequest)request);
355     
356     if (request instanceof ImageCreateRequest)
357       return getCreateImageCommand((ImageCreateRequest)request);
358     
359     if (request instanceof EditorShearRequest)
360       return getShearCommand((EditorShearRequest) request);
361       
362     return super.getCommand(request);
363   }
364   
365   protected Command getShearCommand(EditorShearRequest request)
366   {
367     ShearCommand cmd = new ShearCommand();
368     cmd.setEditParts(request.getEditParts());
369     cmd.setAffineTransform(request.getAffineTransform());
370     return cmd;
371   }
372   
373   protected Command getRotateCenterCommand(EditorRotateCenterRequest request)
374   {
375     RotateCenterCommand cmd = new RotateCenterCommand(request);
376     Point rotationCenter = request.getRotationCenter().getCopy();
377     rotationCenter = EditorUtil.toAbsolute(getHost(), rotationCenter.x, rotationCenter.y);
378     cmd.setRotationCenter(rotationCenter);
379     LOGGER.debug("cmd.rotationCenter = "+rotationCenter);
380     return cmd;
381   }
382   
383   protected Command getRotateCommand(EditorRotateRequest request)
384   {
385     RotateCommand cmd = new RotateCommand(request);
386     double rotation = request.getRotation();
387     cmd.setRotation(rotation);
388     LOGGER.debug("getRotateCommand().rotation = "+rotation);
389     return cmd;
390   }
391   
392   public Command getCreateTextCommand(TextCreateRequest request)
393   {
394     // TODO: Optimize Command (dont create each time a new Command)
395
CreateTextCommand create = new CreateTextCommand(request);
396     create.setParent(getDrawComponentContainer());
397       
398     Rectangle constraint = new Rectangle();
399     constraint = (Rectangle)getConstraintFor((EditorBoundsRequest)request);
400     create.setLocation(constraint);
401     
402     return create;
403   }
404   
405   public Command getCreateImageCommand(ImageCreateRequest request)
406   {
407     CreateImageCommand create = new CreateImageCommand();
408     create.setFileName(request.getFileName());
409     DrawComponent newPart = (DrawComponent)request.getNewObject();
410     create.setChild(newPart);
411     create.setParent(getDrawComponentContainer());
412     Rectangle constraint = (Rectangle)getConstraintFor(request);
413     create.setLocation(constraint);
414     return create;
415   }
416   
417   /**
418    * Returns the command contribution for the given edit shape request.
419    * By default, the request is redispatched to the host's parent as a {@link
420    * org.nightlabs.editor2d.request.EditorRequestConstants#REQ_EDIT_SHAPE}.
421    * The parent's editpolicies determine how to perform the resize based on the layout manager in use.
422    * @param request the edit shape request
423    * @return the command contribution obtained from the parent
424    */

425   protected Command getEditShapeCommand(EditorEditShapeRequest request)
426   {
427     EditShapeCommand editShapeCommand = null;
428     if (editShapeCommand == null)
429     {
430         editShapeCommand = new EditShapeCommand();
431         ShapeDrawComponentEditPart sdcEP = (ShapeDrawComponentEditPart) request.getTargetEditPart();
432         ShapeDrawComponent sdc = sdcEP.getShapeDrawComponent();
433         editShapeCommand.setShapeDrawComponent(sdc);
434         editShapeCommand.setPathSegmentIndex(request.getPathSegmentIndex());
435         editShapeCommand.setLabel(EditorPlugin.getResourceString("command.edit.shape"));
436     }
437     Point modelPoint = getConstraintPointFor(request.getLocation());
438     editShapeCommand.setLocation(modelPoint);
439         return editShapeCommand;
440   }
441                 
442     protected IFigure createSizeOnDropFeedback(EditorCreateRequest editorRequest)
443     {
444     Rectangle constrainedBounds = (Rectangle)getConstraintFor((EditorBoundsRequest)editorRequest);
445     
446     if (editorRequest.isUseShape())
447     {
448       Shape shape = editorRequest.getShape();
449       if (shape != null)
450       {
451         shape.setBounds(constrainedBounds);
452         addFeedback(shape);
453         return shape;
454       }
455     }
456     else
457     {
458       GeneralShape gp = editorRequest.getGeneralShape();
459         if (gp != null)
460         {
461           if (editorRequest.getMode() == EditorCreateRequest.BOUNDS_FIX_MODE)
462           {
463                 ShapeFigure shapeFigure = new AbstractShapeFigure();
464                 shapeFigure.setGeneralShape(gp);
465                 shapeFigure.setFill(false);
466                 addFeedback(shapeFigure);
467                 return shapeFigure;
468           }
469           else
470           {
471                 FeedbackShapeFigure shapeFigure = new FeedbackShapeFigure();
472                 shapeFigure.setGeneralShape(gp);
473                 shapeFigure.setFill(false);
474                 addFeedback(shapeFigure);
475                 return shapeFigure;
476           }
477         }
478     }
479     return null;
480   }
481         
482     /**
483      * Generates a draw2d constraint for the given <code>EditorBoundsRequest</code>. If the
484      * EditorBoundsRequest has a size, {@link #getConstraintFor(Rectangle)} is called with a
485      * Rectangle of that size and the result is returned. This is used during size-on-drop
486      * creation. Otherwise, {@link #getConstraintFor(Point)} is returned.
487      * <P>
488      * The EditorBoundsRequest location is relative the Viewer. The location is made
489      * layout-relative before calling one of the methods mentioned above.
490      * @param request the EditorCreateRequest
491      * @return a draw2d constraint
492      */

493     protected Object JavaDoc getConstraintFor(EditorBoundsRequest request)
494     {
495         IFigure figure = getLayoutContainer();
496         Point where = request.getLocation().getCopy();
497         Dimension size = request.getSize();
498                 
499         figure.translateToRelative(where);
500         figure.translateFromParent(where);
501         where.translate(getLayoutOrigin().getNegated());
502
503         if (size == null || size.isEmpty())
504             return getConstraintFor(where);
505         else {
506             //TODO Probably should use PrecisionRectangle at some point instead of two
507
// geometrical objects
508
size = size.getCopy();
509             figure.translateToRelative(size);
510             figure.translateFromParent(size);
511             return getConstraintFor(new Rectangle(where, size));
512         }
513     }
514     
515   /**
516    * Generates a draw2d constraint (the Point relative to the zomm)
517    * for the given <code>EditorLocationRequest</code>.
518    * {@link #getConstraintPointFor(Point)} is returned.
519    *
520    * The EditorLocationRequest location is relative the Viewer. The location is made
521    * layout-relative before calling one of the methods mentioned above.
522    * @param request the EditorCreateRequest
523    * @return a draw2d constraint
524    */

525   protected Point getConstraintFor(EditorLocationRequest request)
526   {
527     return getConstraintPointFor(request.getLocation());
528   }
529     
530   public DrawComponentContainer getDrawComponentContainer()
531   {
532     DrawComponentContainer container = (DrawComponentContainer)getHost().getModel();
533     return container.getRoot().getCurrentLayer();
534   }
535   
536     protected Command createAddCommand(Request request, EditPart childEditPart,
537             Object JavaDoc constraint)
538     {
539       DrawComponent part = (DrawComponent)childEditPart.getModel();
540         Rectangle rect = (Rectangle)constraint;
541
542         // TODO: maybe uncomment for Container related things
543
// AddDrawComponentCommand add = new AddDrawComponentCommand();
544
// add.setParent((MultiLayerDrawComponent)getHost().getModel());
545
// add.setChild(part);
546
// add.setLocation(rect);
547
// add.setLabel(EditorPlugin.getResourceString("command_add_command"));
548
// add.setDebugLabel("MLDC_XYEP add drawComponent");//$NON-NLS-1$
549

550         SetConstraintCommand setConstraint = new SetConstraintCommand();
551         setConstraint.setBounds(J2DUtil.toAWTRectangle(rect));
552         setConstraint.setPart(part);
553         setConstraint.setLabel(EditorPlugin.getResourceString("command.add.drawComponent"));
554         setConstraint.setDebugLabel("MLDC_XYEP setConstraint");//$NON-NLS-1$
555

556 // Command cmd = add.chain(setConstraint);
557
Command cmd = setConstraint;
558         cmd = chainGuideAttachmentCommand(request, part, cmd, true);
559         cmd = chainGuideAttachmentCommand(request, part, cmd, false);
560         cmd = chainGuideDetachmentCommand(request, part, cmd, true);
561         return chainGuideDetachmentCommand(request, part, cmd, false);
562     }
563     
564     protected Command getCreateCommand(CreateRequest request)
565     {
566       if (request instanceof EditorCreateRequest)
567       {
568         EditorCreateRequest req = (EditorCreateRequest) request;
569         return getEditorCreateCommand(req);
570       }
571       else {
572         CreateDrawComponentCommand create = new CreateDrawComponentCommand();
573             DrawComponent newPart = (DrawComponent)request.getNewObject();
574             create.setChild(newPart);
575             create.setParent(getDrawComponentContainer());
576             
577             Rectangle constraint = (Rectangle)getConstraintFor(request);
578             create.setLocation(constraint);
579             
580             Command cmd = chainGuideAttachmentCommand(request, newPart, create, true);
581             return chainGuideAttachmentCommand(request, newPart, cmd, false);
582       }
583     }
584     
585     protected Command getEditorCreateCommand(EditorCreateRequest request)
586     {
587       // TODO: Optimize Command (dont create each time a new Command)
588
CreateShapeCommand create = new CreateShapeCommand();
589     create.setGeneralShape(request.getGeneralShape());
590     ShapeDrawComponent newPart = (ShapeDrawComponent)request.getNewObject();
591     create.setChild(newPart);
592         create.setParent(getDrawComponentContainer());
593             
594         Rectangle constraint = new Rectangle();
595         if (request instanceof LineCreateRequest) {
596           LineCreateRequest lineRequest = (LineCreateRequest) request;
597           if (lineRequest.getCreationBounds() != null)
598             constraint = getConstraintRectangleFor(lineRequest.getCreationBounds());
599         }
600         else
601           constraint = (Rectangle)getConstraintFor((EditorBoundsRequest)request);
602         
603         create.setLocation(constraint);
604     
605         Command cmd = chainGuideAttachmentCommand(request, newPart, create, true);
606         return chainGuideAttachmentCommand(request, newPart, cmd, false);
607     }
608         
609     /**
610      * Places the feedback Polyline where the User indicated.
611      * @see LayoutEditPolicy#showSizeOnDropFeedback(CreateRequest)
612      */

613     protected void showSizeOnDropFeedback(EditorCreateRequest request)
614     {
615         Point p = request.getLocation().getCopy();
616         Dimension size = request.getSize().getCopy();
617         IFigure feedback = getSizeOnDropFeedback(request);
618         p.translate(getScrollOffset());
619                                         
620         Rectangle newBounds = new Rectangle(p, size).expand(getCreationFeedbackOffset(request));
621     feedback.setBounds(newBounds);
622     }
623     
624   protected Point getScrollOffset() {
625     return EditorUtil.getScrollOffset(getHost());
626   }
627         
628     /**
629      * Lazily creates and returns the Figure to use for size-on-drop feedback.
630      * @return the size-on-drop feedback figure
631      */

632     protected IFigure createDefaultSizeOnDropFeedback()
633     {
634         IFigure sizeOnDropFeedbackDefault = new RectangleFigure();
635         sizeOnDropFeedbackDefault.setBackgroundColor(FeedbackUtil.getBackgroundColor());
636         sizeOnDropFeedbackDefault.setForegroundColor(FeedbackUtil.getForegroundColor());
637         if (sizeOnDropFeedbackDefault instanceof Shape)
638         {
639             Shape s = (Shape) sizeOnDropFeedbackDefault;
640             s.setLineStyle(Graphics.LINE_SOLID);
641             s.setFillXOR(true);
642             s.setOutlineXOR(true);
643         }
644         addFeedback(sizeOnDropFeedbackDefault);
645         return sizeOnDropFeedbackDefault;
646     }
647   
648     /**
649      * Override to provide custom feedback figure for the given create request.
650      * @param createRequest the Editor create request
651      * @return custom feedback figure
652      */

653     protected IFigure createSizeOnDropFeedback(CreateRequest createRequest)
654     {
655       LOGGER.debug("createSizeOnDropFeedback!");
656       
657       if (createRequest instanceof EditorCreateRequest)
658       {
659         EditorCreateRequest editorRequest = (EditorCreateRequest) createRequest;
660         return createSizeOnDropFeedback(editorRequest);
661       }
662       else
663         return createDefaultSizeOnDropFeedback();
664     }
665     
666     //protected void showSizeOnDropFeedback(CreateRequest request)
667
//{
668
// if (request instanceof EditorCreateRequest)
669
// showSizeOnDropFeedback((EditorCreateRequest)request);
670
// else
671
// super.showSizeOnDropFeedback(request);
672
//}
673

674 //************************* BEGIN Text Feedback ******************************
675
protected void eraseSizeOnDropFeedback(Request request)
676     {
677         LOGGER.debug("eraseSizeOnDropFeedback!");
678         
679 // if (sizeOnDropFeedbackDefault != null) {
680
// removeFeedback(sizeOnDropFeedbackDefault);
681
// sizeOnDropFeedbackDefault = null;
682
// }
683
// else {
684
super.eraseSizeOnDropFeedback(request);
685 // }
686
if (showFeedbackText) {
687             eraseFeedbackText();
688         }
689     }
690     
691     protected void showSizeOnDropFeedback(CreateRequest request)
692     {
693       if (request instanceof EditorCreateRequest)
694         showSizeOnDropFeedback((EditorCreateRequest)request);
695       else
696         super.showSizeOnDropFeedback(request);
697       
698       if (showFeedbackText) {
699         showFeedbackText(request);
700       }
701     }
702         
703     protected boolean showFeedbackText = true;
704   protected Label feedbackText;
705   
706   protected Label getFeedbackTextFigure()
707   {
708     if (feedbackText == null)
709         feedbackText = createFeedbackTextFigure("");
710     return feedbackText;
711   }
712   
713   protected Rectangle getInitialFeedbackBounds()
714   {
715     return Rectangle.SINGLETON;
716   }
717   
718   protected Label createFeedbackTextFigure(String JavaDoc text)
719   {
720     Label l = new Label(text);
721     l.setBounds(getInitialFeedbackBounds());
722     addFeedback(l);
723     return l;
724   }
725   
726   protected void showFeedbackText(CreateRequest request)
727   {
728     Label feedbackText = getFeedbackTextFigure();
729     feedbackText.setText(getText(request));
730     feedbackText.setLocation(getFeedbackTextLocation(request));
731     feedbackText.setSize(100, 20);
732         
733     getFeedbackLayer().repaint();
734   }
735   
736   protected Point getFeedbackTextLocation(CreateRequest request)
737   {
738     // TODO: set location always on mouse location
739
Point loc = request.getLocation();
740     Dimension size = request.getSize();
741 // Rectangle feedbackBounds = getSizeOnDropFeedback().getBounds();
742
Point location = new Point(loc.x + size.width, loc.y + size.height);
743     location.translate(EditorUtil.getScrollOffset(getHost()));
744     return location;
745   }
746     
747   protected String JavaDoc getText(CreateRequest request)
748   {
749     Point relativeSize = new Point(request.getSize().width, request.getSize().height);
750     Point absoluteSize = EditorUtil.toAbsolute(getHost(), relativeSize.x, relativeSize.y);
751         
752     String JavaDoc width = "W";
753     String JavaDoc height = "H";
754     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
755     sb.append(width+" ");
756     sb.append(absoluteSize.x);
757     sb.append(", ");
758     sb.append(height+" ");
759     sb.append(absoluteSize.y);
760     return sb.toString();
761   }
762   
763   protected void eraseFeedbackText()
764   {
765     if (feedbackText != null)
766       removeFeedback(feedbackText);
767     
768     feedbackText = null;
769   }
770     
771 //************************* END Text Feedback ******************************
772
}
773
Popular Tags