1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.docking; |
16 | |
17 | import java.awt.Component; |
18 | import java.awt.Cursor; |
19 | import java.awt.Dimension; |
20 | import java.awt.Point; |
21 | import java.awt.Toolkit; |
22 | import java.util.HashMap; |
23 | import java.util.Map; |
24 | |
25 | import javax.swing.ImageIcon; |
26 | |
27 | import org.flexdock.docking.DockingConstants; |
28 | import org.flexdock.docking.DockingPort; |
29 | import org.jdesktop.application.ApplicationContext; |
30 | import org.jdesktop.application.ResourceMap; |
31 | |
32 | import net.sourceforge.hivegui.component.IconTools; |
33 | |
34 | public class CursorTracker implements DockingConstants |
35 | { |
36 | public void setContext(ApplicationContext context) |
37 | { |
38 | ResourceMap map = context.getResourceMap(CursorTracker.class); |
39 | // Create cursors |
40 | _none = createCursor(map, "region.none"); |
41 | _regionCursor.put(NORTH_REGION, createCursor(map, "region.north")); |
42 | _regionCursor.put(SOUTH_REGION, createCursor(map, "region.south")); |
43 | _regionCursor.put(WEST_REGION, createCursor(map, "region.west")); |
44 | _regionCursor.put(EAST_REGION, createCursor(map, "region.east")); |
45 | _regionCursor.put(CENTER_REGION, createCursor(map, "region.center")); |
46 | } |
47 | |
48 | public boolean trackCursor( Component dockable, |
49 | DockingPort port, |
50 | String targetRegion, |
51 | Component paintingTarget) |
52 | { |
53 | if (UNKNOWN_REGION.equals(targetRegion)) |
54 | { |
55 | return false; |
56 | } |
57 | if (!port.isDockingAllowed(dockable, targetRegion)) |
58 | { |
59 | paintingTarget.setCursor(_none); |
60 | return false; |
61 | } |
62 | paintingTarget.setCursor(_regionCursor.get(targetRegion)); |
63 | return true; |
64 | } |
65 | |
66 | static private Cursor createCursor(ResourceMap map, String name) |
67 | { |
68 | ImageIcon icon = map.getImageIcon(name + ".icon"); |
69 | Toolkit toolkit = Toolkit.getDefaultToolkit(); |
70 | Dimension dim = toolkit.getBestCursorSize(icon.getIconWidth(), icon.getIconHeight()); |
71 | icon = IconTools.centerIcon(icon, dim.width, dim.height); |
72 | Point hotSpot = new Point(dim.width / 2, dim.height / 2); |
73 | return toolkit.createCustomCursor(icon.getImage(), hotSpot, name); |
74 | } |
75 | |
76 | private Cursor _none; |
77 | private Map<String, Cursor> _regionCursor = new HashMap<String, Cursor>(); |
78 | } |