001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2014, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
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, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 25-Jun-2014 : Version 1 (DG);
038 *
039 */
040
041package org.jfree.chart.fx.interaction;
042
043import javafx.scene.input.MouseEvent;
044import javafx.scene.input.ScrollEvent;
045import org.jfree.chart.fx.ChartCanvas;
046import org.jfree.chart.util.ParamChecks;
047
048/**
049 * A base class that can be used to implement the {@link MouseHandlerFX}
050 * interface.
051 * 
052 * <p>THE API FOR THIS CLASS IS SUBJECT TO CHANGE IN FUTURE RELEASES.  This is
053 * so that we can incorporate feedback on the (new) JavaFX support in 
054 * JFreeChart.</p>
055 * 
056 * @since 1.0.18
057 */
058public class AbstractMouseHandlerFX implements MouseHandlerFX {
059
060    /** The handler id. */
061    private String id;
062    
063    /** 
064     * A flag used to enable/disable the handler (usually temporarily, removing
065     * a handler is the preferred way to disable it permanently).
066     */
067    private boolean enabled;
068    
069    /** Requires ALT key modifier? */
070    private boolean altKey;
071    
072    /** Requires CTRL key modifier? */
073    private boolean ctrlKey;
074
075    /** Requires META key modifier? */
076    private boolean metaKey;
077    
078    /** Requires SHIFT key modifier? */
079    private boolean shiftKey;
080    
081    /**
082     * Creates a new instance.  The modifier keys are used to select a 
083     * mouse handler to be the current "live" handler (when a handler is
084     * used as an auxiliary handler, the modifier keys are not relevant).
085     * 
086     * @param id  the handler id (<code>null</code> not permitted).
087     * @param altKey  require ALT key modifier?
088     * @param ctrlKey  require ALT key modifier?
089     * @param metaKey  require ALT key modifier?
090     * @param shiftKey   require ALT key modifier?
091     */
092    public AbstractMouseHandlerFX(String id, boolean altKey, boolean ctrlKey, 
093            boolean metaKey, boolean shiftKey) {
094        ParamChecks.nullNotPermitted(id, "id");
095        this.id = id;
096        this.enabled = true;
097        this.altKey = altKey;
098        this.ctrlKey = ctrlKey;
099        this.metaKey = metaKey;
100        this.shiftKey = shiftKey;
101    }
102    
103    /**
104     * Returns the ID for the handler.
105     * 
106     * @return The ID (never <code>null</code>).
107     */
108    @Override
109    public String getID() {
110        return this.id;
111    }
112    
113    /**
114     * Returns the flag that controls whether or not the handler is enabled.
115     * 
116     * @return A boolean. 
117     */
118    @Override
119    public boolean isEnabled() {
120        return this.enabled;
121    }
122    
123    /**
124     * Sets the flag that controls the enabled/disabled state of the handler.
125     * 
126     * @param enabled  the new flag value. 
127     */
128    public void setEnabled(boolean enabled) {
129        this.enabled = enabled;
130    }
131    
132    /**
133     * Returns <code>true</code> if the specified mouse event has modifier
134     * keys that match this handler.
135     * 
136     * @param e  the mouse event (<code>null</code> not permitted).
137     * 
138     * @return A boolean. 
139     */
140    @Override
141    public boolean hasMatchingModifiers(MouseEvent e) {
142        boolean b = true;
143        b = b && (this.altKey == e.isAltDown());
144        b = b && (this.ctrlKey == e.isControlDown());
145        b = b && (this.metaKey == e.isMetaDown());
146        b = b && (this.shiftKey == e.isShiftDown());
147        return b;
148    }
149    
150    /**
151     * Handles a mouse moved event.  This implementation does nothing,
152     * override the method if required.
153     * 
154     * @param canvas  the canvas (<code>null</code> not permitted).
155     * @param e  the event (<code>null</code> not permitted).
156     */
157    @Override
158    public void handleMouseMoved(ChartCanvas canvas, MouseEvent e) {
159        // does nothing unless overridden
160    }
161    
162    /**
163     * Handles a mouse clicked event.  This implementation does nothing,
164     * override the method if required.
165     * 
166     * @param canvas  the canvas (<code>null</code> not permitted).
167     * @param e  the event (<code>null</code> not permitted).
168     */
169    @Override
170    public void handleMouseClicked(ChartCanvas canvas, MouseEvent e) {
171        // does nothing unless overridden
172    }
173
174    /**
175     * Handles a mouse pressed event.  This implementation does nothing,
176     * override the method if required.
177     * 
178     * @param canvas  the canvas (<code>null</code> not permitted).
179     * @param e  the event (<code>null</code> not permitted).
180     */
181    @Override
182    public void handleMousePressed(ChartCanvas canvas, MouseEvent e) {
183        // does nothing unless overridden        
184    }
185    
186    /**
187     * Handles a mouse dragged event.  This implementation does nothing,
188     * override the method if required.
189     * 
190     * @param canvas  the canvas (<code>null</code> not permitted).
191     * @param e  the event (<code>null</code> not permitted).
192     */
193    @Override
194    public void handleMouseDragged(ChartCanvas canvas, MouseEvent e) {
195        // does nothing unless overridden
196    }
197    
198    /**
199     * Handles a mouse released event.  This implementation does nothing,
200     * override the method if required.
201     * 
202     * @param canvas  the canvas (<code>null</code> not permitted).
203     * @param e  the event (<code>null</code> not permitted).
204     */
205    @Override
206    public void handleMouseReleased(ChartCanvas canvas, MouseEvent e) {
207        // does nothing unless overridden
208    }
209
210    /**
211     * Handles a scroll event.  This implementation does nothing,
212     * override the method if required.
213     * 
214     * @param canvas  the canvas (<code>null</code> not permitted).
215     * @param e  the event (<code>null</code> not permitted).
216     */
217    @Override
218    public void handleScroll(ChartCanvas canvas, ScrollEvent e) {
219        // does nothing unless overridden
220    }
221    
222}