KickJava   Java API By Example, From Geeks To Geeks.

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


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.PositionConstants;
33 import org.eclipse.draw2d.geometry.Rectangle;
34 import org.eclipse.gef.DragTracker;
35 import org.eclipse.gef.GraphicalEditPart;
36 import org.eclipse.gef.handles.RelativeHandleLocator;
37 import org.eclipse.swt.graphics.Cursor;
38
39 import org.nightlabs.editor2d.custom.EditorCursors;
40 import org.nightlabs.editor2d.tools.ShearTracker;
41
42
43 public class ShearHandle
44 extends EditorAbstractHandle
45 {
46   protected int cursorDirection = 0;
47   
48   public ShearHandle(GraphicalEditPart owner, int direction)
49   {
50     setOwner(owner);
51     setLocator(new RelativeHandleLocator(owner.getFigure(), direction));
52     cursorDirection = direction;
53     setCursor(getDefaultCursor(direction));
54   }
55   
56   protected Cursor getDefaultCursor(int direction)
57   {
58     switch(direction)
59     {
60         case(PositionConstants.NORTH):
61         case(PositionConstants.SOUTH):
62           return EditorCursors.SHEAR_HORIZONTAL;
63         case(PositionConstants.WEST):
64         case(PositionConstants.EAST):
65           return EditorCursors.SHEAR_VERTICAL;
66     }
67     return EditorCursors.NO;
68   }
69 // public void paintFigure(Graphics g)
70
// {
71
// Rectangle r = getBounds();
72
// r.shrink(1, 1);
73
// try {
74
// paintHandle(g, cursorDirection);
75
// } finally {
76
// //We don't really own rect 'r', so fix it.
77
// r.expand(1, 1);
78
// }
79
// }
80

81   public void paintFigure(Graphics g)
82   {
83     // TODO: draw Rotate Handles
84
Rectangle r = getBounds();
85     r.shrink(1, 1);
86     try {
87       g.setBackgroundColor(ColorConstants.black);
88       g.fillRectangle(r.x, r.y, r.width, r.height);
89       g.setForegroundColor(ColorConstants.white);
90       g.drawRectangle(r.x, r.y, r.width, r.height);
91     } finally {
92       //We don't really own rect 'r', so fix it.
93
r.expand(1, 1);
94     }
95   }
96   
97   protected void paintHandle(Graphics g, int direction)
98   {
99     g.setForegroundColor(ColorConstants.black);
100     switch(direction)
101     {
102         case(PositionConstants.EAST):
103         case(PositionConstants.WEST):
104         // draw down Arrow
105
g.drawLine(2, 2, 6, 6);
106         g.drawLine(6, 6, 5, 5);
107         g.drawLine(6, 6, 6, 5);
108         // draw up Arrow
109
g.drawLine(6, 6, 2, 2);
110         g.drawLine(2, 2, 3, 3);
111         g.drawLine(2, 2, 1, 3);
112         break;
113       case(PositionConstants.NORTH):
114       case(PositionConstants.SOUTH):
115         // draw down Arrow
116
g.drawLine(2, 2, 6, 6);
117         g.drawLine(6, 6, 5, 5);
118         g.drawLine(6, 6, 6, 5);
119         // draw up Arrow
120
g.drawLine(6, 6, 2, 2);
121         g.drawLine(2, 2, 3, 3);
122         g.drawLine(2, 2, 1, 3);
123         break;
124     }
125   }
126   
127   protected DragTracker createDragTracker() {
128     return new ShearTracker(getOwner(), cursorDirection);
129   }
130
131 }
132
Popular Tags