Bug 12496 fixed: zoom_rect could lead to a crash in log scale
[scilab.git] / scilab / modules / renderer / src / java / org / scilab / modules / renderer / JoGLView / interaction / ZoomRubberBox.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Scilab Enterprises - Bruno JOFRET
4  * Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
5  *
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11  */
12
13 package org.scilab.modules.renderer.JoGLView.interaction;
14
15 import java.awt.event.MouseEvent;
16
17 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
18 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
19 import org.scilab.modules.renderer.JoGLView.DrawerVisitor;
20 import org.scilab.modules.renderer.JoGLView.util.ScaleUtils;
21
22 /**
23  * @author Bruno JOFRET
24  */
25 public class ZoomRubberBox extends RubberBox {
26
27     /**
28      * Default constructor.
29      *
30      * @param drawerVisitor parent drawer visitor.
31      */
32     public ZoomRubberBox(DrawerVisitor drawerVisitor) {
33         super(drawerVisitor);
34     }
35
36     @Override
37     public final void mouseClicked(MouseEvent e) {
38         mouseButton = e.getButton();
39         if (e.getButton() == MouseEvent.BUTTON1) {
40             switch (status) {
41                 case WAIT_POINT_A:
42                     if (setPointA(e.getPoint())) {
43                         status = Status.WAIT_POINT_B;
44                     } else {
45                         setEnable(false);
46                         fireRubberBoxEnd();
47                     }
48                     break;
49                 case WAIT_POINT_B:
50                     setPointB(e.getPoint());
51                     if (pointBComputer.is2D()) {
52                         process();
53                         setEnable(false);
54                         fireRubberBoxEnd();
55                     } else {
56                         status = Status.WAIT_POINT_C;
57                     }
58                     break;
59                 case WAIT_POINT_C:
60                     setPointC(e.getPoint());
61                     status = Status.WAIT_POINT_D;
62                     break;
63                 case WAIT_POINT_D:
64                     setPointD(e.getPoint());
65                     process();
66                     setEnable(false);
67                     fireRubberBoxEnd();
68                     break;
69                 default:
70             }
71             updateInfoMessage();
72         }
73
74         if (e.getButton() == MouseEvent.BUTTON3) {
75             setEnable(false);
76             fireRubberBoxEnd();
77         }
78     }
79
80     /**
81      * Actually set the zoom box depending on current value of firstPoint and secondPoint.
82      */
83     @Override
84     protected void process() {
85         Double[] bounds = {
86             Math.min(firstPoint.getX(), secondPoint.getX()), Math.max(firstPoint.getX(), secondPoint.getX()),
87             Math.min(firstPoint.getY(), secondPoint.getY()), Math.max(firstPoint.getY(), secondPoint.getY()),
88             Math.min(firstPoint.getZ(), secondPoint.getZ()), Math.max(firstPoint.getZ(), secondPoint.getZ()),
89         };
90
91         if (bounds[0].compareTo(bounds[1]) != 0 && bounds[2].compareTo(bounds[3]) != 0 && bounds[4].compareTo(bounds[5]) != 0) {
92             Boolean zoomed = tightZoomBounds(axes, bounds);
93             double[][] factors = axes.getScaleTranslateFactors();
94             bounds[0] = (bounds[0] - factors[1][0]) / factors[0][0];
95             bounds[1] = (bounds[1] - factors[1][0]) / factors[0][0];
96             bounds[2] = (bounds[2] - factors[1][1]) / factors[0][1];
97             bounds[3] = (bounds[3] - factors[1][1]) / factors[0][1];
98             bounds[4] = (bounds[4] - factors[1][2]) / factors[0][2];
99             bounds[5] = (bounds[5] - factors[1][2]) / factors[0][2];
100
101             boolean[] logFlags = { axes.getXAxisLogFlag(), axes.getYAxisLogFlag(), axes.getZAxisLogFlag()};
102             ScaleUtils.applyInverseLogScaleToBounds(bounds, logFlags);
103
104             GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_BOX__, bounds);
105             GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_ENABLED__, zoomed);
106             getDrawerVisitor().getCanvas().redraw();
107         }
108     }
109 }