/* * * PanelLayerUI.java version 0.1 * * Copyright (C) 2008 Piet Blok. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Created on 3 aug 2008 */ package org.pbjar.jxlayer.test; import java.awt.Cursor; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JComponent; import javax.swing.JOptionPane; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; import javax.swing.Timer; import org.jdesktop.jxlayer.JXLayer; /** * A LayerUI that hides the cursor. After a MouseEvent or MouseMoveEvent the * cursor will reappear for some specified time. * * @author Piet Blok */ public final class HideCursorUI extends GeneralLayerUI { /** * Holds state information. */ private class MyState extends StateObject { private final Timer timer; private int timeout; private final Cursor oldCursor; public MyState(final JXLayer layer, int timeout) { this.timeout = timeout; oldCursor = layer.getGlassPane().getCursor(); this.timer = new Timer(timeout, new ActionListener() { @Override public void actionPerformed(ActionEvent event) { layer.getGlassPane().setCursor(nullCursor); } }); timer.setRepeats(false); } public int getTimeout() { return timeout; } public void resetCursor(JXLayer layer) { if (timeout > 0) { layer.getGlassPane().setCursor(oldCursor); timer.restart(); } } public void setTimeout(int timeout) { this.timeout = timeout; timer.setInitialDelay(timeout); } @Override public void uninstall(JXLayer layer) { timer.stop(); } } private static final Cursor nullCursor = Toolkit.getDefaultToolkit() .createCustomCursor( new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), new Point(0, 0), "nullCursor"); private final int timeout; /** * Create an instance. Via the timeout argument the time can be specified * for how long the cursor must be shown after a MouseEvent or * MouseMoveEvent. A value of 0 or negative indicates that the cursor will * not be shown at all, even after a MouseEvent or MouseMoveEvent. * * @param timeout * the timeout time in milliseconds */ public HideCursorUI(int timeout) { this.timeout = timeout; } public List getActions(final JXLayer layer) { ArrayList actionList = new ArrayList(); actionList.addAll(super.getActions(layer)); /* * Change the cursor timeout */ actionList.add(new AbstractAction("Set cursor timeout") { private static final long serialVersionUID = 1L; @Override public void actionPerformed(ActionEvent event) { MyState state = (MyState) HideCursorUI.this .getStateObject(layer); Integer timeout = state.getTimeout(); JSpinner spinner = new JSpinner(); SpinnerNumberModel model = (SpinnerNumberModel) spinner .getModel(); model.setStepSize(100); model.setMinimum(0); spinner.setValue(timeout); if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog( layer, spinner, "Change cursor timeout", JOptionPane.OK_CANCEL_OPTION)) { timeout = (Integer) spinner.getValue(); state.setTimeout(timeout); } } }); return actionList; } private void resetCursor(JXLayer layer) { ((MyState) getStateObject(layer)).resetCursor(layer); } @Override protected GeneralLayerUI.StateObject createStateObject( JXLayer layer) { return new MyState(layer, timeout); } @Override protected void processMouseEvent(MouseEvent e, JXLayer layer) { super.processMouseEvent(e, layer); resetCursor(layer); } @Override protected void processMouseMotionEvent(MouseEvent e, JXLayer l) { super.processMouseMotionEvent(e, l); resetCursor(l); } }