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