Search in sources :

Example 6 with MouseEventWrapper

use of net.sf.mzmine.chartbasics.gui.wrapper.MouseEventWrapper in project mzmine2 by mzmine.

the class ChartGestureHandler method createHandler.

/**
 * Create preset handlers
 *
 * @param handler
 * @param g
 * @param param Parameters for specific handlers <br>
 * @return
 */
public static ChartGestureHandler createHandler(Handler handler, final ChartGesture g, Object[] param) {
    Consumer<ChartGestureEvent> newHandler = null;
    switch(handler) {
        case DEBUG:
            newHandler = e -> System.out.println(e.toString());
            break;
        case PREVIOUS_ZOOM_HISTORY:
            newHandler = e -> {
                ZoomHistory h = e.getChartWrapper().getZoomHistory();
                if (h != null) {
                    Range[] range = h.setPreviousPoint();
                    if (range != null && range.length > 0 && range[0] != null) {
                        ValueAxis dom = e.getChart().getXYPlot().getDomainAxis();
                        ValueAxis ran = e.getChart().getXYPlot().getRangeAxis();
                        ChartLogics.setZoomAxis(dom, range[0]);
                        ChartLogics.setZoomAxis(ran, range[1]);
                    }
                }
            };
            break;
        case NEXT_ZOOM_HISTORY:
            newHandler = e -> {
                ZoomHistory h = e.getChartWrapper().getZoomHistory();
                if (h != null) {
                    Range[] range = h.setNextPoint();
                    if (range != null && range.length > 0 && range[0] != null) {
                        ValueAxis dom = e.getChart().getXYPlot().getDomainAxis();
                        ValueAxis ran = e.getChart().getXYPlot().getRangeAxis();
                        ChartLogics.setZoomAxis(dom, range[0]);
                        ChartLogics.setZoomAxis(ran, range[1]);
                    }
                }
            };
            break;
        case TITLE_REMOVER:
            newHandler = e -> {
                if (e.getEntity() instanceof TitleEntity) {
                    TitleEntity te = (TitleEntity) e.getEntity();
                    te.getTitle().setVisible(false);
                }
            };
            break;
        case AUTO_ZOOM_AXIS:
            newHandler = e -> {
                ValueAxis a = e.getAxis();
                if (a != null)
                    ChartLogics.autoAxis(a);
            };
            break;
        case AUTO_ZOOM_OPPOSITE_AXIS:
            newHandler = e -> {
                if (e.getGesture().getEntity().equals(Entity.AXIS)) {
                    e.getChartWrapper().autoRangeAxis();
                    e.getChartWrapper().autoDomainAxis();
                } else if (e.getGesture().getEntity().equals(Entity.DOMAIN_AXIS))
                    e.getChartWrapper().autoRangeAxis();
                else
                    e.getChartWrapper().autoDomainAxis();
            };
            break;
        case SCROLL_AXIS:
            newHandler = e -> {
                ValueAxis axis = e.getAxis();
                if (axis != null) {
                    double diff = 0.03;
                    if (e.getMouseEvent().isMouseWheelEvent()) {
                        diff = -0.10 * e.getMouseEvent().getWheelRotation();
                    }
                    ChartLogics.offsetAxis(axis, diff);
                }
            };
            break;
        case SCROLL_AXIS_AND_AUTO_ZOOM:
            newHandler = e -> {
                ValueAxis axis = e.getAxis();
                if (axis != null) {
                    double diff = 0.03;
                    if (e.getMouseEvent().isMouseWheelEvent()) {
                        diff = -0.10 * e.getMouseEvent().getWheelRotation();
                    }
                    ChartLogics.offsetAxis(axis, diff);
                    if (e.getGesture().getEntity().equals(Entity.DOMAIN_AXIS))
                        e.getChartWrapper().autoRangeAxis();
                    else
                        e.getChartWrapper().autoDomainAxis();
                }
            };
            break;
        case ZOOM_AXIS_INCLUDE_ZERO:
            newHandler = e -> {
                ValueAxis axis = e.getAxis();
                if (axis != null) {
                    double diff = 0.05;
                    if (e.getMouseEvent().isMouseWheelEvent()) {
                        diff = -0.10 * e.getMouseEvent().getWheelRotation();
                    }
                    ChartLogics.zoomAxis(axis, diff, true);
                }
            };
            break;
        case ZOOM_AXIS_CENTER:
            newHandler = e -> {
                ValueAxis axis = e.getAxis();
                if (axis != null) {
                    MouseEventWrapper p = e.getMouseEvent();
                    double diff = 0.05;
                    if (e.getMouseEvent().isMouseWheelEvent()) {
                        diff = -0.10 * p.getWheelRotation();
                    }
                    // get data space coordinates
                    Point2D point = e.getCoordinates(p.getX(), p.getY());
                    if (point != null) {
                        // vertical ?
                        Boolean orient = e.isVerticalAxis(axis);
                        if (orient == null)
                            return;
                        else if (orient)
                            ChartLogics.zoomAxis(axis, diff, point.getY());
                        else
                            ChartLogics.zoomAxis(axis, diff, point.getX());
                    }
                }
            };
            break;
        default:
            break;
    }
    if (newHandler == null)
        return null;
    else
        return new ChartGestureHandler(g, newHandler);
}
Also used : ZoomHistory(net.sf.mzmine.chartbasics.listener.ZoomHistory) Point2D(java.awt.geom.Point2D) ValueAxis(org.jfree.chart.axis.ValueAxis) MouseEventWrapper(net.sf.mzmine.chartbasics.gui.wrapper.MouseEventWrapper) TitleEntity(org.jfree.chart.entity.TitleEntity) Range(org.jfree.data.Range)

Example 7 with MouseEventWrapper

use of net.sf.mzmine.chartbasics.gui.wrapper.MouseEventWrapper in project mzmine2 by mzmine.

the class ChartGestureDragDiffHandler method createConsumer.

/**
 * Handle PRESSED, DRAGGED, RELEASED events to generate drag diff events
 *
 * @return
 */
private Consumer<ChartGestureEvent> createConsumer() {
    return new Consumer<ChartGestureEvent>() {

        // variables
        boolean wasMouseZoomable = false;

        Point2D last = null, first = null;

        ChartGestureEvent startEvent = null, lastEvent = null;

        @Override
        public void accept(ChartGestureEvent event) {
            ChartViewWrapper chartPanel = event.getChartWrapper();
            JFreeChart chart = chartPanel.getChart();
            MouseEventWrapper e = event.getMouseEvent();
            // released?
            if (event.checkEvent(Event.RELEASED)) {
                chartPanel.setMouseZoomable(wasMouseZoomable);
                last = null;
            } else if (event.checkEvent(Event.PRESSED)) {
                // get data space coordinates
                last = chartPanel.mouseXYToPlotXY(e.getX(), e.getY());
                first = last;
                startEvent = event;
                lastEvent = event;
                if (last != null) {
                    wasMouseZoomable = chartPanel.isMouseZoomable();
                    chartPanel.setMouseZoomable(false);
                }
            } else if (event.checkEvent(Event.DRAGGED)) {
                if (last != null) {
                    // get data space coordinates
                    Point2D released = chartPanel.mouseXYToPlotXY(e.getX(), e.getY());
                    if (released != null) {
                        double offset = 0;
                        double start = 0;
                        // scroll x
                        if (getOrientation(event).equals(Orientation.HORIZONTAL)) {
                            offset = -(released.getX() - last.getX());
                            start = first.getX();
                        } else // scroll y
                        {
                            offset = -(released.getY() - last.getY());
                            start = first.getY();
                        }
                        // new dragdiff event
                        ChartGestureDragDiffEvent dragEvent = new ChartGestureDragDiffEvent(startEvent, lastEvent, event, start, offset, orient);
                        // choose handler by key filter
                        for (int i = 0; i < dragDiffHandler.length; i++) if (key[i].filter(event.getMouseEvent()))
                            dragDiffHandler[i].accept(dragEvent);
                        // set last event
                        lastEvent = event;
                        // save updated last
                        last = chartPanel.mouseXYToPlotXY(e.getX(), e.getY());
                    }
                }
            }
        }
    };
}
Also used : ChartViewWrapper(net.sf.mzmine.chartbasics.gui.wrapper.ChartViewWrapper) Consumer(java.util.function.Consumer) Point2D(java.awt.geom.Point2D) MouseEventWrapper(net.sf.mzmine.chartbasics.gui.wrapper.MouseEventWrapper) JFreeChart(org.jfree.chart.JFreeChart)

Example 8 with MouseEventWrapper

use of net.sf.mzmine.chartbasics.gui.wrapper.MouseEventWrapper in project mzmine2 by mzmine.

the class ChartGestureMouseAdapterFX method handleMousePressed.

/**
 * Handles a mouse pressed event. This implementation does nothing, override the method if
 * required.
 *
 * @param canvas the canvas ({@code null} not permitted).
 * @param e the event ({@code null} not permitted).
 */
@Override
public void handleMousePressed(ChartCanvas chartPanel, MouseEvent eOrig) {
    if (gestureHandlers == null || gestureHandlers.isEmpty() || !listensFor(Event.PRESSED))
        return;
    MouseEventWrapper e = new MouseEventWrapper(eOrig);
    ChartEntity entity = findChartEntity(e);
    ChartGesture.Entity gestureEntity = ChartGesture.getGestureEntity(entity);
    Button button = Button.getButton(e.getButton());
    // handle event
    lastDragEvent = new ChartGestureEvent(cw, e, entity, new ChartGesture(gestureEntity, Event.PRESSED, button));
    handleEvent(lastDragEvent);
}
Also used : ChartGesture(net.sf.mzmine.chartbasics.gestures.ChartGesture) Button(net.sf.mzmine.chartbasics.gestures.ChartGesture.Button) ChartGestureEvent(net.sf.mzmine.chartbasics.gestures.ChartGestureEvent) MouseEventWrapper(net.sf.mzmine.chartbasics.gui.wrapper.MouseEventWrapper) Entity(net.sf.mzmine.chartbasics.gestures.ChartGesture.Entity) ChartEntity(org.jfree.chart.entity.ChartEntity)

Aggregations

MouseEventWrapper (net.sf.mzmine.chartbasics.gui.wrapper.MouseEventWrapper)8 ChartGesture (net.sf.mzmine.chartbasics.gestures.ChartGesture)6 Button (net.sf.mzmine.chartbasics.gestures.ChartGesture.Button)6 Entity (net.sf.mzmine.chartbasics.gestures.ChartGesture.Entity)6 ChartGestureEvent (net.sf.mzmine.chartbasics.gestures.ChartGestureEvent)6 ChartEntity (org.jfree.chart.entity.ChartEntity)6 Point2D (java.awt.geom.Point2D)2 ChartViewWrapper (net.sf.mzmine.chartbasics.gui.wrapper.ChartViewWrapper)2 Consumer (java.util.function.Consumer)1 ZoomHistory (net.sf.mzmine.chartbasics.listener.ZoomHistory)1 JFreeChart (org.jfree.chart.JFreeChart)1 ValueAxis (org.jfree.chart.axis.ValueAxis)1 TitleEntity (org.jfree.chart.entity.TitleEntity)1 Range (org.jfree.data.Range)1