01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: Cursors.java 3711 2007-04-06 07:38:30Z gbevin $
07: */
08: package com.uwyn.rife.swing;
09:
10: import com.uwyn.rife.rep.Participant;
11: import com.uwyn.rife.rep.Rep;
12: import java.awt.Cursor;
13: import java.awt.Image;
14: import java.awt.Point;
15: import java.awt.Toolkit;
16: import java.util.ArrayList;
17: import java.util.HashMap;
18:
19: public class Cursors {
20: public final static String DEFAULT_PARTICIPANT_NAME = "ParticipantCursors";
21:
22: private String mPath = null;
23: private HashMap<String, Cursor> mCursors = null;
24:
25: public Cursors() {
26: mCursors = new HashMap<String, Cursor>();
27: }
28:
29: public Cursors(String path) {
30: if (null == path)
31: throw new IllegalArgumentException("path can't be null.");
32: if (0 == path.length())
33: throw new IllegalArgumentException("path can't be empty.");
34:
35: mPath = path;
36: initialize();
37:
38: assert mPath != null;
39: assert mPath.length() > 0;
40: assert mCursors != null;
41: }
42:
43: private void initialize() {
44: Images images = Images.getRepInstance();
45:
46: mCursors = new HashMap<String, Cursor>();
47:
48: Point hotspot = new Point(6, 6);
49: String cursor_filename_short = null;
50: for (String cursor_filename : images.getImageIconNames(mPath)) {
51: cursor_filename_short = cursor_filename.substring(mPath
52: .length(), cursor_filename.length() - 4);
53:
54: Image icon_image = images.getImageIcon(cursor_filename)
55: .getImage();
56: mCursors.put(cursor_filename_short, Toolkit
57: .getDefaultToolkit().createCustomCursor(icon_image,
58: hotspot, cursor_filename_short));
59: }
60: }
61:
62: public static boolean hasRepInstance() {
63: return Rep.hasParticipant(DEFAULT_PARTICIPANT_NAME);
64: }
65:
66: public static Cursors getRepInstance() {
67: Participant participant = Rep
68: .getParticipant(DEFAULT_PARTICIPANT_NAME);
69: if (null == participant) {
70: return null;
71: }
72:
73: return (Cursors) participant.getObject();
74: }
75:
76: public Cursor getCursor(String path) {
77: if (null == path)
78: throw new IllegalArgumentException("path can't be null.");
79:
80: return mCursors.get(path);
81: }
82:
83: public ArrayList<String> getCursorNames(String prefix) {
84: if (null == prefix)
85: throw new IllegalArgumentException("prefix can't be null.");
86:
87: ArrayList<String> matching_cursors = new ArrayList<String>();
88:
89: for (String cursor_filename : mCursors.keySet()) {
90: if (cursor_filename.startsWith(prefix)) {
91: matching_cursors.add(cursor_filename);
92: }
93: }
94:
95: return matching_cursors;
96: }
97: }
|