001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: CursorManager.java,v 1.17 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.gui;
024:
025: import javax.swing.*;
026: import java.awt.*;
027: import java.awt.event.ComponentAdapter;
028: import java.awt.event.ComponentEvent;
029: import java.util.WeakHashMap;
030:
031: /**
032: * @author $Author: jesper $
033: * @version $Revision: 1.17 $
034: */
035: public class CursorManager {
036: private static class RootCursorInfo {
037: private Cursor savedCursor;
038: private Cursor cursor;
039: private JComponent panel;
040:
041: private boolean cursorSet = false;
042:
043: RootCursorInfo(JComponent panel) {
044: this .panel = panel;
045: }
046:
047: public JComponent getComponent() {
048: return panel;
049: }
050:
051: public void pushCursor(Cursor cursor) {
052: if (savedCursor == null)
053: savedCursor = cursor;
054:
055: cursorSet = true;
056: }
057:
058: public Cursor popCursor() {
059: Cursor c = savedCursor;
060: savedCursor = null;
061:
062: cursorSet = false;
063: return c;
064: }
065:
066: public boolean isCursorSet() {
067: return cursorSet;
068: }
069:
070: public Cursor getCursor() {
071: return cursor;
072: }
073:
074: public void setCursor(Cursor cursor) {
075: this .cursor = cursor;
076: }
077:
078: }
079:
080: private static boolean enabled = true;
081: private static WeakHashMap windowPanels = new WeakHashMap();
082:
083: private CursorManager() {
084: }
085:
086: public static void setGlobalCursor(final JRootPane root,
087: Cursor cursor) {
088: if (root == null)
089: return;
090:
091: RootCursorInfo rci = (RootCursorInfo) windowPanels.get(root);
092:
093: if (rci == null) {
094: rci = new RootCursorInfo(new JComponent() {
095: });
096: windowPanels.put(root, rci);
097: root.getLayeredPane().add(rci.getComponent());
098: root.getLayeredPane().setLayer(rci.getComponent(),
099: JLayeredPane.DRAG_LAYER.intValue() + 10);
100: rci.getComponent().setBounds(0, 0, root.getWidth(),
101: root.getHeight());
102: root.getLayeredPane().addComponentListener(
103: new ComponentAdapter() {
104: public void componentResized(ComponentEvent e) {
105: ((RootCursorInfo) windowPanels.get(root))
106: .getComponent().setSize(
107: root.getSize());
108: }
109: });
110: }
111:
112: if (!rci.isCursorSet()) {
113: rci.setCursor(cursor);
114: rci
115: .pushCursor(root.isCursorSet() ? root.getCursor()
116: : null);
117: }
118:
119: if (enabled) {
120: root.setCursor(cursor);
121: rci.getComponent().setVisible(true);
122: }
123: }
124:
125: public static Cursor getCurrentGlobalCursor(JRootPane root) {
126: if (root == null)
127: return Cursor.getDefaultCursor();
128:
129: RootCursorInfo rci = (RootCursorInfo) windowPanels.get(root);
130: return rci == null || !rci.isCursorSet() ? Cursor
131: .getDefaultCursor() : rci.getCursor();
132: }
133:
134: public static void resetGlobalCursor(JRootPane root) {
135: if (root == null)
136: return;
137:
138: RootCursorInfo rci = (RootCursorInfo) windowPanels.get(root);
139:
140: if (rci != null && rci.isCursorSet()) {
141: root.setCursor(rci.popCursor());
142: rci.getComponent().setVisible(false);
143: }
144: }
145:
146: public static void setEnabled(boolean enabled) {
147: CursorManager.enabled = enabled;
148: }
149:
150: public static boolean isEnabled() {
151: return enabled;
152: }
153:
154: public static JComponent getCursorLayerComponent(JRootPane root) {
155: if (root == null)
156: return null;
157:
158: RootCursorInfo rci = (RootCursorInfo) windowPanels.get(root);
159: return rci == null ? null : rci.getComponent();
160: }
161:
162: public static boolean isGlobalCursorSet(JRootPane root) {
163: if (root == null)
164: return false;
165:
166: RootCursorInfo rci = (RootCursorInfo) windowPanels.get(root);
167: return rci != null && rci.isCursorSet();
168: }
169: }
|