KickJava   Java API By Example, From Geeks To Geeks.

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


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.handle;
29
30 import org.eclipse.draw2d.ColorConstants;
31 import org.eclipse.draw2d.Graphics;
32 import org.eclipse.draw2d.geometry.Dimension;
33 import org.eclipse.draw2d.geometry.Rectangle;
34 import org.eclipse.gef.DragTracker;
35 import org.eclipse.gef.EditPart;
36 import org.eclipse.gef.handles.AbstractHandle;
37 import org.eclipse.gef.handles.RelativeHandleLocator;
38 import org.eclipse.swt.graphics.Color;
39 import org.eclipse.swt.graphics.Image;
40
41 import org.nightlabs.editor2d.custom.EditorCursors;
42 import org.nightlabs.editor2d.edit.AbstractDrawComponentEditPart;
43 import org.nightlabs.editor2d.tools.RotateTracker;
44
45 public class RotateHandle
46 extends AbstractHandle
47 {
48   protected int cursorDirection = 0;
49   protected Image image;
50   
51 // public RotateHandle(GraphicalEditPart owner, int direction)
52
public RotateHandle(AbstractDrawComponentEditPart owner, int direction)
53   {
54     setOwner(owner);
55     setLocator(new RelativeHandleLocator(owner.getFigure(), direction));
56     setCursor(EditorCursors.ROTATE);
57     cursorDirection = direction;
58   }
59
60   protected DragTracker createDragTracker() {
61     return new RotateTracker((AbstractDrawComponentEditPart)getOwner(), cursorDirection);
62   }
63
64   /**
65    * Returns <code>true</code> if the handle's owner is the primary selection.
66    * @return <code>true</code> if the handles owner has primary selection.
67    */

68   protected boolean isPrimary() {
69     return getOwner().getSelected() == EditPart.SELECTED_PRIMARY;
70   }
71   
72   /**
73    * Returns the color for the inside of the handle.
74    * @return the color of the handle
75    */

76   protected Color getFillColor() {
77     return (isPrimary())
78       ? ColorConstants.black
79       : ColorConstants.white;
80   }
81   
82   /**
83    * Returns the color for the outside of the handle.
84    * @return the color for the border
85    */

86   protected Color getBorderColor() {
87     return (isPrimary())
88       ? ColorConstants.white
89       : ColorConstants.black;
90   }
91   
92   /**
93    * Draws the handle with fill color and outline color dependent
94    * on the primary selection status of the owner editpart.
95    *
96    * @param g The graphics used to paint the figure.
97    */

98   public void paintFigure(Graphics g)
99   {
100     // TODO: draw Rotate Handles
101
Rectangle r = getBounds();
102     r.shrink(1, 1);
103     try {
104       g.setBackgroundColor(getFillColor());
105       g.fillRectangle(r.x, r.y, r.width, r.height);
106       g.setForegroundColor(getBorderColor());
107       g.drawRectangle(r.x, r.y, r.width, r.height);
108     } finally {
109       //We don't really own rect 'r', so fix it.
110
r.expand(1, 1);
111     }
112   }
113   
114   /**
115    * The default size for square handles.
116    */

117   protected static final int DEFAULT_HANDLE_SIZE = 7;
118
119   {
120     init();
121   }
122   
123   /**
124    * Initializes the handle.
125    */

126   protected void init() {
127     setPreferredSize(new Dimension(DEFAULT_HANDLE_SIZE, DEFAULT_HANDLE_SIZE));
128   }
129 }
130
131
Popular Tags