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.component; |
16 | |
17 | import java.awt.Cursor; |
18 | import java.awt.Dimension; |
19 | import java.awt.Point; |
20 | import java.awt.Toolkit; |
21 | import java.util.HashMap; |
22 | import java.util.List; |
23 | import java.util.Map; |
24 | |
25 | import javax.swing.ImageIcon; |
26 | |
27 | import org.apache.commons.logging.Log; |
28 | |
29 | /** |
30 | * @author Jean-Francois Poilpret |
31 | */ |
32 | public class CursorFactoryImpl implements CursorFactory |
33 | { |
34 | public CursorFactoryImpl(Log logger, List<CursorContribution> config) |
35 | { |
36 | _logger = logger; |
37 | |
38 | // Initialize pre-defined cursors first |
39 | _cursors.put(CROSSHAIR, buildCursor(Cursor.CROSSHAIR_CURSOR)); |
40 | _cursors.put(HAND, buildCursor(Cursor.HAND_CURSOR)); |
41 | _cursors.put(TEXT, buildCursor(Cursor.TEXT_CURSOR)); |
42 | _cursors.put(WAIT, buildCursor(Cursor.WAIT_CURSOR)); |
43 | _cursors.put(E_RESIZE, buildCursor(Cursor.E_RESIZE_CURSOR)); |
44 | _cursors.put(W_RESIZE, buildCursor(Cursor.W_RESIZE_CURSOR)); |
45 | _cursors.put(N_RESIZE, buildCursor(Cursor.N_RESIZE_CURSOR)); |
46 | _cursors.put(S_RESIZE, buildCursor(Cursor.S_RESIZE_CURSOR)); |
47 | _cursors.put(NE_RESIZE, buildCursor(Cursor.NE_RESIZE_CURSOR)); |
48 | _cursors.put(SE_RESIZE, buildCursor(Cursor.SE_RESIZE_CURSOR)); |
49 | _cursors.put(NW_RESIZE, buildCursor(Cursor.NW_RESIZE_CURSOR)); |
50 | _cursors.put(SW_RESIZE, buildCursor(Cursor.SW_RESIZE_CURSOR)); |
51 | _cursors.put(MOVE, buildCursor(Cursor.MOVE_CURSOR)); |
52 | _cursors.put(DEFAULT, buildCursor(Cursor.DEFAULT_CURSOR)); |
53 | |
54 | // Initialize custom cursors |
55 | for (CursorContribution contrib: config) |
56 | { |
57 | CursorWrapper cursor = buildCursor(contrib); |
58 | if (cursor != null) |
59 | { |
60 | _cursors.put(contrib.getId(), cursor); |
61 | } |
62 | } |
63 | } |
64 | |
65 | public Cursor getCursor(String id) |
66 | { |
67 | CursorWrapper cursor = _cursors.get(id); |
68 | return (cursor == null ? null : cursor.getCursor()); |
69 | } |
70 | |
71 | public CursorInfo getCursorInfo(String id) |
72 | { |
73 | CursorWrapper cursor = _cursors.get(id); |
74 | return (cursor == null ? null : cursor.getCursorInfo()); |
75 | } |
76 | |
77 | private CursorWrapper buildCursor(int id) |
78 | { |
79 | return new CursorWrapper(Cursor.getPredefinedCursor(id), null); |
80 | } |
81 | |
82 | private CursorWrapper buildCursor(CursorContribution contrib) |
83 | { |
84 | ImageIcon icon = IconTools.loadIcon(contrib.getIcon()); |
85 | if (icon == null) |
86 | { |
87 | _logger.warn("buildCursor() bad icon resource: " + contrib.getLocation()); |
88 | return null; |
89 | } |
90 | |
91 | // Calculate cursor hotspot |
92 | double x = contrib.getHotspotX(); |
93 | if (x == -1.0) |
94 | { |
95 | x = CENTER; |
96 | } |
97 | if (x == 1.0) |
98 | { |
99 | x = MAX; |
100 | } |
101 | double y = contrib.getHotspotY(); |
102 | if (y == -1.0) |
103 | { |
104 | y = CENTER; |
105 | } |
106 | if (y == 1.0) |
107 | { |
108 | y = MAX; |
109 | } |
110 | |
111 | Toolkit kit = Toolkit.getDefaultToolkit(); |
112 | Dimension dim = kit.getBestCursorSize(icon.getIconWidth(), icon.getIconHeight()); |
113 | Point hotSpot = new Point((int) (x * dim.width), (int) (y * dim.height)); |
114 | |
115 | Cursor cursor = kit.createCustomCursor(icon.getImage(), hotSpot, contrib.getId()); |
116 | CursorInfo info = new CursorInfo(icon, hotSpot, dim); |
117 | return new CursorWrapper(cursor, info); |
118 | } |
119 | |
120 | static private class CursorWrapper |
121 | { |
122 | public CursorWrapper(Cursor cursor, CursorInfo info) |
123 | { |
124 | _cursor = cursor; |
125 | _info = info; |
126 | } |
127 | |
128 | public Cursor getCursor() |
129 | { |
130 | return _cursor; |
131 | } |
132 | |
133 | public CursorInfo getCursorInfo() |
134 | { |
135 | return _info; |
136 | } |
137 | |
138 | final private Cursor _cursor; |
139 | final private CursorInfo _info; |
140 | } |
141 | |
142 | static private final double CENTER = 0.5; |
143 | static private final double MAX = 0.99; |
144 | |
145 | private final Log _logger; |
146 | private final Map<String, CursorWrapper> _cursors = |
147 | new HashMap<String, CursorWrapper>(); |
148 | } |