KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.geom.AffineTransform JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.apache.log4j.Logger;
35 import org.eclipse.draw2d.IFigure;
36 import org.eclipse.draw2d.Label;
37 import org.eclipse.draw2d.Polyline;
38 import org.eclipse.draw2d.geometry.Dimension;
39 import org.eclipse.draw2d.geometry.Point;
40 import org.eclipse.draw2d.geometry.PrecisionRectangle;
41 import org.eclipse.draw2d.geometry.Rectangle;
42 import org.eclipse.gef.GraphicalEditPart;
43 import org.eclipse.gef.LayerConstants;
44 import org.eclipse.gef.Request;
45 import org.eclipse.gef.editpolicies.ResizableEditPolicy;
46 import org.eclipse.gef.requests.ChangeBoundsRequest;
47 import org.nightlabs.editor2d.EditorStateManager;
48 import org.nightlabs.editor2d.edit.ShapeDrawComponentEditPart;
49 import org.nightlabs.editor2d.figures.ShapeFigure;
50 import org.nightlabs.editor2d.handle.RotateCenterHandle;
51 import org.nightlabs.editor2d.handle.RotateHandleKit;
52 import org.nightlabs.editor2d.handle.ShapeEditHandleKit;
53 import org.nightlabs.editor2d.j2d.GeneralShape;
54 import org.nightlabs.editor2d.request.EditorEditShapeRequest;
55 import org.nightlabs.editor2d.request.EditorRequestConstants;
56 import org.nightlabs.editor2d.request.EditorRotateCenterRequest;
57 import org.nightlabs.editor2d.request.EditorRotateRequest;
58 import org.nightlabs.editor2d.util.EditorUtil;
59 import org.nightlabs.editor2d.util.FeedbackUtil;
60 import org.nightlabs.editor2d.util.J2DUtil;
61
62
63 public class DrawComponentResizeEditPolicy
64 extends ResizableEditPolicy
65 implements EditorRequestConstants
66 {
67   public static final Logger LOGGER = Logger.getLogger(DrawComponentResizeEditPolicy.class);
68      
69   /**
70    * Creates the figure used for feedback.
71    * @return the new feedback figure
72    */

73   protected IFigure createDragSourceFeedbackFigure()
74   {
75     IFigure figure = getCustomFeedbackFigure(getHost().getModel());
76     figure.setBounds(getInitialFeedbackBounds());
77     addFeedback(figure);
78     return figure;
79   }
80        
81   protected Polyline createPolylineFigure(GraphicalEditPart part)
82   {
83     ShapeDrawComponentEditPart sdcEP = (ShapeDrawComponentEditPart) part;
84     Polyline polyline = J2DUtil.toPolyline(sdcEP.getGeneralShape());
85     polyline.setLineStyle(2);
86     polyline.setXOR(true);
87     polyline.setFill(true);
88     polyline.setBackgroundColor(FeedbackUtil.getBackgroundColor());
89     polyline.setForegroundColor(FeedbackUtil.getForegroundColor());
90         
91     // transform each point to absolute
92
for (int i=0; i<polyline.getPoints().size(); i++) {
93       Point p = polyline.getPoints().getPoint(i);
94       Point newPoint = getConstraintFor(p);
95       polyline.getPoints().setPoint(newPoint, i);
96     }
97     
98     return polyline;
99   }
100       
101   protected ShapeFigure getCustomFeedbackFigure(Object JavaDoc modelPart)
102   {
103     return FeedbackUtil.getCustomFeedbackFigure(modelPart);
104   }
105   
106   /**
107    * @see org.eclipse.gef.editpolicies.NonResizableEditPolicy#initialFeedbackRectangle()
108    */

109   protected Rectangle getInitialFeedbackBounds()
110   {
111 // LOGGER.debug("InitalFeedbackBounds = "+getHostFigure().getBounds());
112
return getHostFigure().getBounds();
113   }
114
115   /**
116    * @see org.eclipse.gef.EditPolicy#understandsRequest(org.eclipse.gef.Request)
117    */

118   public boolean understandsRequest(Request request)
119   {
120     if (REQ_EDIT_SHAPE.equals(request.getType()))
121         return true;
122     
123     return super.understandsRequest(request);
124   }
125     
126   protected List JavaDoc createSelectionHandles()
127   {
128     if (EditorStateManager.getCurrentState() == EditorStateManager.STATE_EDIT_SHAPE)
129     {
130         List JavaDoc list = new ArrayList JavaDoc();
131         if (getHost() instanceof ShapeDrawComponentEditPart) {
132           ShapeDrawComponentEditPart sdcEditPart = (ShapeDrawComponentEditPart) getHost();
133           ShapeEditHandleKit.addHandles(sdcEditPart, list);
134           return list;
135         }
136     }
137     else if (EditorStateManager.getCurrentState() == EditorStateManager.STATE_ROTATE)
138     {
139       clearHandleLayer();
140       List JavaDoc list = new ArrayList JavaDoc();
141       RotateHandleKit.addHandles(getHost().getViewer().getSelectedEditParts(), list);
142       return list;
143     }
144     
145     return super.createSelectionHandles();
146   }
147     
148   protected void clearHandleLayer()
149   {
150     IFigure layer = getLayer(LayerConstants.HANDLE_LAYER);
151     for (int i = 0; i < layer.getChildren().size(); i++) {
152       IFigure figure = (IFigure) layer.getChildren().get(i);
153         layer.remove(figure);
154     }
155   }
156   
157   protected void removeSelectionHandles()
158   {
159     if (handles == null)
160         return;
161     
162     IFigure layer = getLayer(LayerConstants.HANDLE_LAYER);
163     if (layer.getChildren().isEmpty())
164       return;
165     
166     for (int i = 0; i < handles.size(); i++) {
167       if (layer.getChildren().contains(handles.get(i)))
168         layer.remove((IFigure)handles.get(i));
169     }
170     
171     handles = null;
172   }
173   
174   public void eraseSourceFeedback(Request request)
175   {
176     if (request.getType().equals(REQ_EDIT_SHAPE))
177       eraseEditShapeFeedback((EditorEditShapeRequest)request);
178     else if (request.getType().equals(REQ_ROTATE))
179       eraseRotateFeedback((EditorRotateRequest)request);
180     else if (request.getType().equals(REQ_EDIT_ROTATE_CENTER))
181       eraseEditRotateCenterFeedback((EditorRotateCenterRequest)request);
182 // else if (request.getType().equals(REQ_SHEAR))
183
// eraseShearFeedback();
184
else
185       super.eraseSourceFeedback(request);
186   }
187
188   /**
189    * Erases drag feedback. This method called whenever an erase feedback request is
190    * received of the appropriate type.
191    * @param request the request
192    */

193   protected void eraseEditShapeFeedback(EditorEditShapeRequest request)
194   {
195     if (feedback != null) {
196         removeFeedback(feedback);
197     }
198     feedback = null;
199   }
200     
201   protected void eraseEditRotateCenterFeedback(EditorRotateCenterRequest request)
202   {
203     if (feedback != null) {
204         removeFeedback(feedback);
205     }
206     feedback = null;
207   }
208   
209   public void showSourceFeedback(Request request)
210   {
211     if (request.getType().equals(REQ_EDIT_SHAPE))
212       showEditShapeFeedback((EditorEditShapeRequest)request);
213     else if (request.getType().equals(REQ_ROTATE))
214       showRotateFeedback((EditorRotateRequest)request);
215     else if (request.getType().equals(REQ_EDIT_ROTATE_CENTER))
216       showEditRotateCenterFeedback((EditorRotateCenterRequest)request);
217 // else if (request.getType().equals(REQ_SHEAR))
218
// showShearFeedback((EditorShearRequest)request);
219
else
220       super.showSourceFeedback(request);
221   }
222       
223 // protected ShapeFigure getShearFeedbackFigure()
224
// {
225
// if (feedback == null) {
226
// feedback = createDragSourceFeedbackFigure();
227
// PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy());
228
// feedback.setBounds(getConstraintFor(rect));
229
// }
230
// return (ShapeFigure) feedback;
231
// }
232

233   protected Rectangle rotateCenterBounds;
234   protected void showEditRotateCenterFeedback(EditorRotateCenterRequest request)
235   {
236     IFigure feedback = getEditRotateCenterFeedback(request);
237     Point location = request.getRotationCenter();
238     location.translate(getScrollOffset());
239     Point feedbackLocation = location.getCopy();
240     feedbackLocation.translate(-rotateCenterBounds.width/2, -rotateCenterBounds.height/2);
241     feedback.setLocation(feedbackLocation);
242     feedback.repaint();
243     
244     LOGGER.debug("feedBack.location = "+feedback.getBounds());
245   }
246   
247   protected IFigure createEditRotateCenterFeedback(EditorRotateCenterRequest request)
248   {
249     RotateCenterHandle figure = new RotateCenterHandle(request.getEditParts());
250     request.setMultiple(figure.isMultiple());
251     figure.setBackgroundColor(FeedbackUtil.getBackgroundColor());
252     figure.setForegroundColor(FeedbackUtil.getForegroundColor());
253     return figure;
254   }
255   
256   protected IFigure getEditRotateCenterFeedback(EditorRotateCenterRequest request)
257   {
258     if (feedback == null) {
259       feedback = createEditRotateCenterFeedback(request);
260       rotateCenterBounds = feedback.getBounds();
261       addFeedback(feedback);
262     }
263     return feedback;
264   }
265   
266   protected Polyline getPolylineFeedback()
267   {
268     if (feedback == null) {
269       feedback = createPolylineFigure((GraphicalEditPart)getHost());
270         addFeedback(feedback);
271     }
272     return (Polyline) feedback;
273   }
274   
275   protected void eraseRotateFeedback(EditorRotateRequest request)
276   {
277     if (feedback != null)
278       removeFeedback(feedback);
279     
280     feedback = null;
281     rotatedShape = null;
282     unrotatedShape = null;
283     rotationCenter = null;
284     rotationOffset = Double.MAX_VALUE;
285   }
286   
287   protected final AffineTransform JavaDoc at = new AffineTransform JavaDoc();
288   protected Point rotationCenter;
289   protected GeneralShape unrotatedShape;
290   protected GeneralShape rotatedShape;
291   protected double rotationOffset = Double.MAX_VALUE;
292   protected void showRotateFeedback(EditorRotateRequest request)
293   {
294     ShapeFigure rotationFeedback = getRotateFeedbackFigure();
295         
296     if (unrotatedShape == null)
297       unrotatedShape = (GeneralShape) rotationFeedback.getGeneralShape().clone();
298       
299     if (rotationCenter == null && request.getRotationCenter() != null)
300       rotationCenter = getConstraintFor(request.getRotationCenter());
301     
302     Point location = request.getLocation();
303     location.translate(getScrollOffset());
304      
305     if (rotationOffset == Double.MAX_VALUE)
306       rotationOffset = EditorUtil.calcRotation(location, rotationCenter);
307     
308     double rotationTmp = EditorUtil.calcRotation(location, rotationCenter);
309     double rotation = - (rotationTmp - rotationOffset);
310     request.setRotation(rotation);
311     double rotationInRadinans = Math.toRadians(rotation);
312     at.setToIdentity();
313     at.rotate(rotationInRadinans, rotationCenter.x, rotationCenter.y);
314 // rotationFeedback.transform(at);
315
rotatedShape = (GeneralShape) unrotatedShape.clone();
316     rotatedShape.transform(at);
317     rotationFeedback.setGeneralShape(rotatedShape);
318     getFeedbackLayer().repaint();
319     
320     LOGGER.debug("rotation = "+rotation);
321   }
322       
323 // protected void eraseShearFeedback()
324
// {
325
// if (feedback != null)
326
// removeFeedback(feedback);
327
//
328
// feedback = null;
329
// rotatedShape = null;
330
// unrotatedShape = null;
331
// }
332

333 // protected void showShearFeedback(EditorShearRequest request)
334
// {
335
// ShapeFigure shearFeedback = getShearFeedbackFigure();
336
// if (unrotatedShape == null)
337
// unrotatedShape = (GeneralShape) shearFeedback.getGeneralShape().clone();
338
//
339
// Point location = request.getLocation();
340
// location.translate(getScrollOffset());
341
// LOGGER.debug("location = "+location);
342
//
343
// Rectangle bounds = request.getShearBounds();
344
// LOGGER.debug("shearBounds = "+bounds);
345
//
346
// AffineTransform at = getShearTransform(location, bounds, request.getDirection());
347
// rotatedShape = (GeneralShape) unrotatedShape.clone();
348
// rotatedShape.transform(at);
349
// shearFeedback.setGeneralShape(rotatedShape);
350
// getFeedbackLayer().repaint();
351
//
352
// request.setAffineTransform(at);
353
// }
354

355 // protected AffineTransform getShearTransform(Point location, Rectangle bounds, int direction)
356
// {
357
// double shear = 0.0d;
358
// double idleShear = 0.0d;
359
// at.setToIdentity();
360
// switch(direction)
361
// {
362
// case(PositionConstants.WEST):
363
// case(PositionConstants.EAST):
364
// double heightMiddle = bounds.y + bounds.height/2;
365
// double y = location.y;
366
// double distanceY = heightMiddle - y;
367
// double height = bounds.height;
368
// shear = distanceY / height;
369
// at.shear(idleShear, shear);
370
// break;
371
// case(PositionConstants.NORTH):
372
// case(PositionConstants.SOUTH):
373
// double widthMiddle = bounds.x + bounds.width/2;
374
// double x = location.x;
375
// double distanceX = widthMiddle - x;
376
// double width = bounds.width;
377
// shear = distanceX / width;
378
// at.shear(shear, idleShear);
379
// break;
380
// }
381
// LOGGER.debug("shear = "+shear);
382
// return at;
383
// }
384

385   protected ShapeFigure getRotateFeedbackFigure()
386   {
387     if (feedback == null) {
388 // feedback = createRotateFeedbackFigure();
389
feedback = createDragSourceFeedbackFigure();
390       
391         PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy());
392         feedback.setBounds(getConstraintFor(rect));
393     }
394     return (ShapeFigure) feedback;
395   }
396     
397   protected void showEditShapeFeedback(EditorEditShapeRequest request)
398   {
399     Polyline polyline = getPolylineFeedback();
400     Point newPoint = new Point(request.getLocation().x, request.getLocation().y);
401     newPoint.translate(getScrollOffset());
402     polyline.setPoint(newPoint, request.getPathSegmentIndex());
403   }
404   
405   protected ShapeFigure getEditShapeFeedbackFigure()
406   {
407     if (feedback == null)
408         feedback = createEditShapeFeedbackFigure();
409     return (ShapeFigure) feedback;
410   }
411   
412 // protected ShapeFigure createEditShapeFeedbackFigure()
413
protected IFigure createEditShapeFeedbackFigure()
414   {
415     IFigure figure = getCustomFeedbackFigure(getHost().getModel());
416     figure.setBounds(getInitialFeedbackBounds());
417     addFeedback(figure);
418     return figure;
419   }
420       
421   public Rectangle getConstraintFor(Rectangle rectangle) {
422     return EditorUtil.oldToAbsolute((GraphicalEditPart)getHost(), rectangle);
423   }
424 // public Rectangle getConstraintFor(Rectangle rectangle) {
425
// return EditorUtil.toAbsoluteWithScrollOffset(getHost(), rectangle);
426
// }
427

428   public Point getConstraintFor(Point point){
429     return EditorUtil.toAbsolute((GraphicalEditPart)getHost(), point);
430   }
431 // public Point getConstraintFor(Point point){
432
// return EditorUtil.toAbsoluteWithScrollOffset(getHost(), point.x, point.y);
433
// }
434

435   protected Point getScrollOffset() {
436     return EditorUtil.getScrollOffset(getHost());
437   }
438   
439 // ****************************** BEGIN Workaround private feedback *****************************
440

441   protected IFigure feedback;
442   
443   /**
444    * Lazily creates and returns the feedback figure used during drags.
445    * @return the feedback figure
446    */

447   protected IFigure getDragSourceFeedbackFigure()
448   {
449     if (feedback == null)
450         feedback = createDragSourceFeedbackFigure();
451     return feedback;
452   }
453       
454   /**
455    * @see org.eclipse.gef.EditPolicy#deactivate()
456    */

457   public void deactivate() {
458     if (feedback != null) {
459         removeFeedback(feedback);
460         feedback = null;
461     }
462     hideFocus();
463     super.deactivate();
464   }
465     
466 // ****************************** END Workaround private feedback *****************************
467

468   protected boolean showFeedBackText = true;
469   protected Label feedbackLabel;
470     
471   protected void showChangeBoundsFeedback(ChangeBoundsRequest request)
472   {
473     IFigure feedback = getDragSourceFeedbackFigure();
474     
475     PrecisionRectangle rect = new PrecisionRectangle(getInitialFeedbackBounds().getCopy());
476     getHostFigure().translateToAbsolute(rect);
477     rect.translate(request.getMoveDelta());
478     rect.resize(request.getSizeDelta());
479     
480     feedback.translateToRelative(rect);
481     feedback.setBounds(rect);
482
483     if (showFeedBackText) {
484         showFeedbackText(request);
485     }
486         
487     getFeedbackLayer().repaint();
488   }
489   
490   /**
491    * Erases drag feedback. This method called whenever an erase feedback request is
492    * received of the appropriate type.
493    * @param request the request
494    */

495   protected void eraseChangeBoundsFeedback(ChangeBoundsRequest request)
496   {
497     if (feedback != null) {
498         removeFeedback(feedback);
499         if (showFeedBackText) {
500             eraseFeedbackText();
501         }
502     }
503     feedback = null;
504   }
505   
506   protected Label getFeedbackTextFigure()
507   {
508     if (feedbackLabel == null)
509         feedbackLabel = createFeedbackTextFigure("");
510     return feedbackLabel;
511   }
512   
513   protected Label createFeedbackTextFigure(String JavaDoc text)
514   {
515     Label l = new Label(text);
516     l.setBounds(getInitialFeedbackBounds());
517     addFeedback(l);
518     return l;
519   }
520     
521   protected void showFeedbackText(ChangeBoundsRequest request)
522   {
523     Label feedbackText = getFeedbackTextFigure();
524     
525     feedbackText.setText(getText(request));
526     feedbackText.setLocation(getFeedbackTextLocation(request));
527     feedbackText.setSize(100, 20);
528         
529     getFeedbackLayer().repaint();
530   }
531   
532   protected static final Dimension EMPTY_DIMENSION = new Dimension(0,0);
533   protected static final Point EMPTY_POINT = new Point(0,0);
534   
535   protected Point getFeedbackTextLocation(ChangeBoundsRequest request)
536   {
537     Point loc = request.getLocation();
538     loc.translate(EditorUtil.getScrollOffset(getHost()));
539     return loc;
540   }
541   
542   protected String JavaDoc getText(ChangeBoundsRequest request)
543   {
544     Dimension sizeDelta = request.getSizeDelta();
545 // Point moveDelta = request.getMoveDelta();
546
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
547     Rectangle feedbackBounds = getDragSourceFeedbackFigure().getBounds();
548     
549     if (sizeDelta.equals(EMPTY_DIMENSION)) {
550         Point absoluteLocation = EditorUtil.toAbsolute(getHost(), feedbackBounds.x, feedbackBounds.y);
551         
552         String JavaDoc x = "X";
553         String JavaDoc y = "Y";
554         sb.append(x+" ");
555         sb.append(absoluteLocation.x);
556         sb.append(", ");
557         sb.append(y+" ");
558         sb.append(absoluteLocation.y);
559     }
560     else {
561         Point absoluteSize = EditorUtil.toAbsolute(getHost(), feedbackBounds.width, feedbackBounds.height);
562         
563         String JavaDoc width = "W";
564         String JavaDoc height = "H";
565         sb.append(width+" ");
566         sb.append(absoluteSize.x);
567         sb.append(", ");
568         sb.append(height+" ");
569         sb.append(absoluteSize.y);
570     }
571     return sb.toString();
572   }
573     
574   protected void eraseFeedbackText()
575   {
576     if (feedbackLabel != null)
577       removeFeedback(feedbackLabel);
578     
579     feedbackLabel = null;
580   }
581 }
582
Popular Tags