KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > tool > SelectTool


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.viewer.tool;
28
29 import java.awt.Color JavaDoc;
30 import java.awt.Rectangle JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.nightlabs.editor2d.DrawComponent;
35 import org.nightlabs.editor2d.RectangleDrawComponent;
36 import org.nightlabs.editor2d.ShapeDrawComponent;
37 import org.nightlabs.editor2d.impl.RectangleDrawComponentImpl;
38 import org.nightlabs.editor2d.impl.ShapeDrawComponentImpl;
39 import org.nightlabs.editor2d.j2d.GeneralShape;
40 import org.nightlabs.editor2d.viewer.IDrawComponentConditional;
41 import org.nightlabs.editor2d.viewer.event.MouseEvent;
42
43
44 public class SelectTool
45 extends AbstractTool
46 {
47     public static final Logger LOGGER = Logger.getLogger(SelectTool.class);
48     
49     protected RectangleDrawComponent rect;
50     protected boolean rectAdded = false;
51     protected DrawComponent rollOverDC = null;
52 // protected JToolTip toolTip = null;
53

54     public void activate()
55     {
56         super.activate();
57         initRectangle();
58     }
59
60     public void deactivate()
61     {
62         super.deactivate();
63         rect = null;
64         rollOverDC = null;
65 // toolTip = null;
66
}
67
68     protected RectangleDrawComponent initRectangle()
69     {
70         rect = new RectangleDrawComponentImpl();
71         Rectangle JavaDoc r = new Rectangle JavaDoc(0, 0, 10, 10);
72         rect.setGeneralShape(new GeneralShape(r));
73         rect.setLineColor(Color.BLACK);
74         rect.setFill(false);
75         rect.setLineWidth(2);
76         rect.setLineStyle(2);
77         return rect;
78     }
79         
80     public void mouseMoved(MouseEvent me)
81     {
82         super.mouseMoved(me);
83
84         int currentX = getRelativeX(currentPoint.x);
85         int currentY = getRelativeY(currentPoint.y);
86         
87         // Draw Selection Rectangle
88
if (leftPressed || rightPressed)
89         {
90             int startX = getRelativeX(startPoint.x);
91             int startY = getRelativeY(startPoint.y);
92             int width = (int) Math.abs(currentX - startX);
93             int height = (int) Math.abs(currentY - startY);
94             
95             if (!rectAdded) {
96                 initRectangle();
97                 rect.setLocation(startX, startY);
98                 addToTempContent(rect);
99                 rectAdded = true;
100             }
101                             
102             if (startX > currentX)
103                 rect.setX(currentX);
104             if (width != 0)
105                 rect.setWidth(width);
106             
107             if (startY > currentY)
108                 rect.setY(currentY);
109             if (height != 0)
110                 rect.setHeight(height);
111                 
112             repaint();
113         }
114         
115         // Draw Rollover
116
if (rollOverDC != null) {
117             removeTempContent(rollOverDC);
118             rollOverDC = null;
119             
120 // removeTempContent(toolTip);
121
// toolTip = null;
122

123             repaint();
124         }
125         
126         DrawComponent dc = getViewer().getHitTestManager().findObjectAt(
127                 getViewer().getDrawComponent(), currentX, currentY, conditional, null);
128         
129         if (dc != null)
130         {
131             rollOverDC = createRollOverDrawComponent(dc);
132             addToTempContent(rollOverDC);
133             
134 // toolTip = createToolTip(dc, null, me.getX(), me.getY());
135
// addToTempContent(toolTip);
136

137             repaint();
138         }
139     }
140     
141     public void mousePressed(MouseEvent me)
142     {
143         super.mousePressed(me);
144         int startX = getRelativeX(startPoint.x);
145         int startY = getRelativeY(startPoint.y);
146         checkDrawComponents(startX, startY);
147     }
148         
149     public void mouseReleased(MouseEvent me)
150     {
151         super.mouseReleased(me);
152         rectAdded = false;
153         removeTempContent(rect);
154         if (rect != null)
155         {
156             if (leftPressed)
157                 checkDrawComponents(rect.getBounds());
158             if (rightPressed)
159                 getViewer().getZoomSupport().zoomTo(rect.getBounds());
160         }
161         leftPressed = false;
162         rightPressed = false;
163         rect = null;
164     }
165         
166     protected void checkDrawComponents(int x, int y)
167     {
168         getViewer().getSelectionManager().clearSelection(true);
169         DrawComponent dc = getViewer().getHitTestManager().findObjectAt(getViewer().getDrawComponent(),
170                 x, y, conditional, null);
171         if (dc != null) {
172             getViewer().getSelectionManager().addSelectedDrawComponent(dc);
173         }
174     }
175     
176     protected void checkDrawComponents(Rectangle JavaDoc r)
177     {
178         getViewer().getSelectionManager().clearSelection(true);
179         List JavaDoc drawComponents = getViewer().getHitTestManager().findObjectsAt(getViewer().getDrawComponent(),
180                 r, conditional, null);
181         if (!drawComponents.isEmpty()) {
182             getViewer().getSelectionManager().addSelectedDrawComponents(drawComponents);
183         }
184     }
185         
186     // TODO: condition should be set for HitTestManager
187
protected IDrawComponentConditional conditional = null;
188     public void setConditional(IDrawComponentConditional conditional) {
189         this.conditional = conditional;
190     }
191     public IDrawComponentConditional getConditional() {
192         return conditional;
193     }
194     
195     protected DrawComponent createRollOverDrawComponent(DrawComponent dc)
196     {
197         if (dc instanceof ShapeDrawComponent) {
198             ShapeDrawComponent original = (ShapeDrawComponent) dc;
199             ShapeDrawComponent sdc = new ShapeDrawComponentImpl();
200             sdc.setGeneralShape((GeneralShape)original.getGeneralShape().clone());
201             sdc.setLineWidth(3);
202             sdc.setFill(false);
203             sdc.setLineColor(Color.WHITE);
204             return sdc;
205         }
206         else {
207             RectangleDrawComponent rect = new RectangleDrawComponentImpl();
208             rect.setGeneralShape(new GeneralShape(dc.getBounds()));
209             rect.setLineWidth(3);
210             rect.setFill(false);
211             rect.setLineColor(Color.WHITE);
212             return rect;
213         }
214     }
215         
216 // protected JToolTip createToolTip(DrawComponent dc, String text, int x, int y)
217
// {
218
// JToolTip toolTip = new JToolTip();
219
// toolTip.setLocation(x, y);
220
// toolTip.setSize(200, 200);
221
//
222
// if (text == null)
223
// {
224
// if (dc != null) {
225
// toolTip = new JToolTip();
226
// toolTip.setTipText(
227
// ViewerPlugin.getResourceString("tooltip.name")+": "+dc.getName()+"\n"
228
// +ViewerPlugin.getResourceString("tooltip.id")+": "+dc.getId());
229
// }
230
// }
231
// else {
232
// toolTip.setTipText(text);
233
// }
234
// return toolTip;
235
// }
236
}
237
Popular Tags