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.application; |
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.Map; |
23 | import java.util.StringTokenizer; |
24 | |
25 | import javax.swing.ImageIcon; |
26 | |
27 | import org.jdesktop.application.ResourceConverter; |
28 | import org.jdesktop.application.ResourceMap; |
29 | |
30 | import net.sourceforge.hivegui.component.CursorInfo; |
31 | import net.sourceforge.hivegui.component.Cursors; |
32 | |
33 | public class CursorInfoConverter extends ResourceConverter |
34 | { |
35 | public CursorInfoConverter() |
36 | { |
37 | super(CursorInfo.class); |
38 | _iconConverter = forType(ImageIcon.class); |
39 | |
40 | // Initialize pre-defined cursors first |
41 | // Initialize pre-defined cursors first |
42 | _cursors.put(Cursors.CROSSHAIR, buildCursor(Cursor.CROSSHAIR_CURSOR)); |
43 | _cursors.put(Cursors.HAND, buildCursor(Cursor.HAND_CURSOR)); |
44 | _cursors.put(Cursors.TEXT, buildCursor(Cursor.TEXT_CURSOR)); |
45 | _cursors.put(Cursors.WAIT, buildCursor(Cursor.WAIT_CURSOR)); |
46 | _cursors.put(Cursors.E_RESIZE, buildCursor(Cursor.E_RESIZE_CURSOR)); |
47 | _cursors.put(Cursors.W_RESIZE, buildCursor(Cursor.W_RESIZE_CURSOR)); |
48 | _cursors.put(Cursors.N_RESIZE, buildCursor(Cursor.N_RESIZE_CURSOR)); |
49 | _cursors.put(Cursors.S_RESIZE, buildCursor(Cursor.S_RESIZE_CURSOR)); |
50 | _cursors.put(Cursors.NE_RESIZE, buildCursor(Cursor.NE_RESIZE_CURSOR)); |
51 | _cursors.put(Cursors.SE_RESIZE, buildCursor(Cursor.SE_RESIZE_CURSOR)); |
52 | _cursors.put(Cursors.NW_RESIZE, buildCursor(Cursor.NW_RESIZE_CURSOR)); |
53 | _cursors.put(Cursors.SW_RESIZE, buildCursor(Cursor.SW_RESIZE_CURSOR)); |
54 | _cursors.put(Cursors.MOVE, buildCursor(Cursor.MOVE_CURSOR)); |
55 | _cursors.put(Cursors.DEFAULT, buildCursor(Cursor.DEFAULT_CURSOR)); |
56 | } |
57 | |
58 | private CursorInfo buildCursor(int id) |
59 | { |
60 | return new CursorInfo(Cursor.getPredefinedCursor(id)); |
61 | } |
62 | |
63 | @Override public Object parseString(String s, ResourceMap r) |
64 | throws ResourceConverterException |
65 | { |
66 | CursorInfo info = _cursors.get(s); |
67 | if (info == null) |
68 | { |
69 | // Split s into: iconfile, hotspotx, hotspoty |
70 | StringTokenizer tokenize = new StringTokenizer(s, ","); |
71 | if (tokenize.countTokens() != 0 && tokenize.countTokens() != MAX_TOKENS) |
72 | { |
73 | throw new ResourceConverterException( |
74 | "Bad format, expected: \"icon[,hotspotx[,hotspoty]]", s); |
75 | } |
76 | ImageIcon icon = (ImageIcon) _iconConverter.parseString(tokenize.nextToken(), r); |
77 | double x = getHotspotRate(tokenize); |
78 | double y = getHotspotRate(tokenize); |
79 | |
80 | Toolkit kit = Toolkit.getDefaultToolkit(); |
81 | Dimension dim = kit.getBestCursorSize(icon.getIconWidth(), icon.getIconHeight()); |
82 | Point hotspot = new Point((int) (x * dim.width), (int) (y * dim.height)); |
83 | info = new CursorInfo(icon, hotspot, dim); |
84 | _cursors.put(s, info); |
85 | } |
86 | return info; |
87 | } |
88 | |
89 | static private double getHotspotRate(StringTokenizer token) |
90 | { |
91 | if (!token.hasMoreTokens()) |
92 | { |
93 | return MEAN_COORDINATE; |
94 | } |
95 | try |
96 | { |
97 | double coord = Double.parseDouble(token.nextToken()); |
98 | if (coord >= 1.0) |
99 | { |
100 | coord = MAX_COORDINATE; |
101 | } |
102 | else if (coord < 0.0) |
103 | { |
104 | coord = MEAN_COORDINATE; |
105 | } |
106 | return coord; |
107 | } |
108 | catch (NumberFormatException e) |
109 | { |
110 | return MEAN_COORDINATE; |
111 | } |
112 | } |
113 | |
114 | static private final int MAX_TOKENS = 3; |
115 | static private final double MAX_COORDINATE = 0.99; |
116 | static private final double MEAN_COORDINATE = 0.5; |
117 | |
118 | private final ResourceConverter _iconConverter; |
119 | private final Map<String, CursorInfo> _cursors = new HashMap<String, CursorInfo>(); |
120 | } |