001/* ================================================
002 * JFreeChart-FX : JavaFX extensions for JFreeChart
003 * ================================================
004 *
005 * (C) Copyright 2017-2021, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  https://github.com/jfree/jfreechart-fx
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ---------------------------
028 * AbstractMouseHandlerFX.java
029 * ---------------------------
030 * (C) Copyright 2014-2021, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.fx.interaction;
038
039import javafx.scene.input.MouseEvent;
040import javafx.scene.input.ScrollEvent;
041import org.jfree.chart.fx.ChartCanvas;
042import org.jfree.chart.util.Args;
043
044/**
045 * A base class that can be used to implement the {@link MouseHandlerFX}
046 * interface.
047 */
048public class AbstractMouseHandlerFX implements MouseHandlerFX {
049
050    /** The handler id. */
051    private final String id;
052    
053    /** 
054     * A flag used to enable/disable the handler (usually temporarily, removing
055     * a handler is the preferred way to disable it permanently).
056     */
057    private boolean enabled;
058    
059    /** Requires ALT key modifier? */
060    private final boolean altKey;
061    
062    /** Requires CTRL key modifier? */
063    private final boolean ctrlKey;
064
065    /** Requires META key modifier? */
066    private final boolean metaKey;
067    
068    /** Requires SHIFT key modifier? */
069    private final boolean shiftKey;
070    
071    /**
072     * Creates a new instance.  The modifier keys are used to select a 
073     * mouse handler to be the current "live" handler (when a handler is
074     * used as an auxiliary handler, the modifier keys are not relevant).
075     * 
076     * @param id  the handler id ({@code null} not permitted).
077     * @param altKey  require ALT key modifier?
078     * @param ctrlKey  require ALT key modifier?
079     * @param metaKey  require ALT key modifier?
080     * @param shiftKey   require ALT key modifier?
081     */
082    public AbstractMouseHandlerFX(String id, boolean altKey, boolean ctrlKey, 
083            boolean metaKey, boolean shiftKey) {
084        Args.nullNotPermitted(id, "id");
085        this.id = id;
086        this.enabled = true;
087        this.altKey = altKey;
088        this.ctrlKey = ctrlKey;
089        this.metaKey = metaKey;
090        this.shiftKey = shiftKey;
091    }
092    
093    /**
094     * Returns the ID for the handler.
095     * 
096     * @return The ID (never {@code null}).
097     */
098    @Override
099    public String getID() {
100        return this.id;
101    }
102    
103    /**
104     * Returns the flag that controls whether or not the handler is enabled.
105     * 
106     * @return A boolean. 
107     */
108    @Override
109    public boolean isEnabled() {
110        return this.enabled;
111    }
112    
113    /**
114     * Sets the flag that controls the enabled/disabled state of the handler.
115     * 
116     * @param enabled  the new flag value. 
117     */
118    public void setEnabled(boolean enabled) {
119        this.enabled = enabled;
120    }
121    
122    /**
123     * Returns {@code true} if the specified mouse event has modifier
124     * keys that match this handler.
125     * 
126     * @param e  the mouse event ({@code null} not permitted).
127     * 
128     * @return A boolean. 
129     */
130    @Override
131    public boolean hasMatchingModifiers(MouseEvent e) {
132        boolean b = true;
133        b = b && (this.altKey == e.isAltDown());
134        b = b && (this.ctrlKey == e.isControlDown());
135        b = b && (this.metaKey == e.isMetaDown());
136        b = b && (this.shiftKey == e.isShiftDown());
137        return b;
138    }
139    
140    /**
141     * Handles a mouse moved event.  This implementation does nothing,
142     * override the method if required.
143     * 
144     * @param canvas  the canvas ({@code null} not permitted).
145     * @param e  the event ({@code null} not permitted).
146     */
147    @Override
148    public void handleMouseMoved(ChartCanvas canvas, MouseEvent e) {
149        // does nothing unless overridden
150    }
151    
152    /**
153     * Handles a mouse clicked event.  This implementation does nothing,
154     * override the method if required.
155     * 
156     * @param canvas  the canvas ({@code null} not permitted).
157     * @param e  the event ({@code null} not permitted).
158     */
159    @Override
160    public void handleMouseClicked(ChartCanvas canvas, MouseEvent e) {
161        // does nothing unless overridden
162    }
163
164    /**
165     * Handles a mouse pressed event.  This implementation does nothing,
166     * override the method if required.
167     * 
168     * @param canvas  the canvas ({@code null} not permitted).
169     * @param e  the event ({@code null} not permitted).
170     */
171    @Override
172    public void handleMousePressed(ChartCanvas canvas, MouseEvent e) {
173        // does nothing unless overridden        
174    }
175    
176    /**
177     * Handles a mouse dragged event.  This implementation does nothing,
178     * override the method if required.
179     * 
180     * @param canvas  the canvas ({@code null} not permitted).
181     * @param e  the event ({@code null} not permitted).
182     */
183    @Override
184    public void handleMouseDragged(ChartCanvas canvas, MouseEvent e) {
185        // does nothing unless overridden
186    }
187    
188    /**
189     * Handles a mouse released event.  This implementation does nothing,
190     * override the method if required.
191     * 
192     * @param canvas  the canvas ({@code null} not permitted).
193     * @param e  the event ({@code null} not permitted).
194     */
195    @Override
196    public void handleMouseReleased(ChartCanvas canvas, MouseEvent e) {
197        // does nothing unless overridden
198    }
199
200    /**
201     * Handles a scroll event.  This implementation does nothing,
202     * override the method if required.
203     * 
204     * @param canvas  the canvas ({@code null} not permitted).
205     * @param e  the event ({@code null} not permitted).
206     */
207    @Override
208    public void handleScroll(ChartCanvas canvas, ScrollEvent e) {
209        // does nothing unless overridden
210    }
211    
212}