KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > handle > RotateHandle


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 16.03.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.handle;
9
10 import org.eclipse.draw2d.ColorConstants;
11 import org.eclipse.draw2d.Graphics;
12 import org.eclipse.draw2d.geometry.Dimension;
13 import org.eclipse.draw2d.geometry.Rectangle;
14 import org.eclipse.gef.DragTracker;
15 import org.eclipse.gef.EditPart;
16 import org.eclipse.gef.handles.AbstractHandle;
17 import org.eclipse.gef.handles.RelativeHandleLocator;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.Image;
20
21 import com.nightlabs.editor2d.custom.EditorCursors;
22 import com.nightlabs.editor2d.edit.AbstractDrawComponentEditPart;
23 import com.nightlabs.editor2d.tools.RotateTracker;
24
25 public class RotateHandle
26 extends AbstractHandle
27 {
28   protected int cursorDirection = 0;
29   protected Image image;
30   
31 // public RotateHandle(GraphicalEditPart owner, int direction)
32
public RotateHandle(AbstractDrawComponentEditPart owner, int direction)
33   {
34     setOwner(owner);
35     setLocator(new RelativeHandleLocator(owner.getFigure(), direction));
36     setCursor(EditorCursors.ROTATE);
37     cursorDirection = direction;
38   }
39
40   protected DragTracker createDragTracker() {
41     return new RotateTracker((AbstractDrawComponentEditPart)getOwner(), cursorDirection);
42   }
43
44   /**
45    * Returns <code>true</code> if the handle's owner is the primary selection.
46    * @return <code>true</code> if the handles owner has primary selection.
47    */

48   protected boolean isPrimary() {
49     return getOwner().getSelected() == EditPart.SELECTED_PRIMARY;
50   }
51   
52   /**
53    * Returns the color for the inside of the handle.
54    * @return the color of the handle
55    */

56   protected Color getFillColor() {
57     return (isPrimary())
58       ? ColorConstants.black
59       : ColorConstants.white;
60   }
61   
62   /**
63    * Returns the color for the outside of the handle.
64    * @return the color for the border
65    */

66   protected Color getBorderColor() {
67     return (isPrimary())
68       ? ColorConstants.white
69       : ColorConstants.black;
70   }
71   
72   /**
73    * Draws the handle with fill color and outline color dependent
74    * on the primary selection status of the owner editpart.
75    *
76    * @param g The graphics used to paint the figure.
77    */

78   public void paintFigure(Graphics g)
79   {
80     // TODO: draw Rotate Handles
81
Rectangle r = getBounds();
82     r.shrink(1, 1);
83     try {
84       g.setBackgroundColor(getFillColor());
85       g.fillRectangle(r.x, r.y, r.width, r.height);
86       g.setForegroundColor(getBorderColor());
87       g.drawRectangle(r.x, r.y, r.width, r.height);
88     } finally {
89       //We don't really own rect 'r', so fix it.
90
r.expand(1, 1);
91     }
92   }
93   
94   /**
95    * The default size for square handles.
96    */

97   protected static final int DEFAULT_HANDLE_SIZE = 7;
98
99   {
100     init();
101   }
102   
103   /**
104    * Initializes the handle.
105    */

106   protected void init() {
107     setPreferredSize(new Dimension(DEFAULT_HANDLE_SIZE, DEFAULT_HANDLE_SIZE));
108   }
109 }
110
111
Popular Tags