Plug rubberbox function :
[scilab.git] / scilab / modules / renderer / src / java / org / scilab / modules / renderer / JoGLView / interaction / InteractionManager.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009-2010 - DIGITEO - Pierre Lando
4  * Copyright (C) 2012 - Scilab Enterprises - Bruno JOFRET
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-en.txt
11  */
12
13 package org.scilab.modules.renderer.JoGLView.interaction;
14
15 import org.scilab.modules.commons.utils.BlockingResult;
16 import org.scilab.modules.renderer.JoGLView.DrawerVisitor;
17
18 /**
19  * @author Pierre Lando
20  */
21 public class InteractionManager implements RubberBoxListener {
22
23     /** The rubber box */
24     private RubberBox rubberBox;
25
26     /** The drag, zoom, rotate interaction manager */
27     private final DragZoomRotateInteraction dragZoomRotateInteraction;
28
29     /** Parent drawer visitor */
30     private DrawerVisitor drawerVisitor;
31
32     /**
33      * Default constructor.
34      * @param drawerVisitor parent drawer visitor.
35      */
36     public InteractionManager(DrawerVisitor drawerVisitor) {
37         this.drawerVisitor = drawerVisitor;
38         dragZoomRotateInteraction = new DragZoomRotateInteraction(drawerVisitor);
39         dragZoomRotateInteraction.setEnable(true);
40     }
41
42     /**
43      * Finalize method.
44      * @exception Throwable the <code>Exception</code> raised by this method
45      */
46     public void finalize() throws Throwable {
47         if (rubberBox != null) {
48             rubberBox.removeListener(this);
49         }
50         super.finalize();
51     }
52
53     /**
54      * Called to start zooming.
55      */
56     public void startInteractiveZoom() {
57         final ZoomRubberBox rubberBox = new ZoomRubberBox(drawerVisitor);
58         dragZoomRotateInteraction.setEnable(false);
59
60         rubberBox.addListener(new RubberBoxListener() {
61                         public void rubberBoxEnd() {
62                                 dragZoomRotateInteraction.setEnable(true);
63                                 drawerVisitor.removePostRendering(rubberBox);
64                         }
65                 });
66         drawerVisitor.addPostRendering(rubberBox);
67         rubberBox.setEnable(true);
68     }
69
70     public double[] startClickRubberBox(double initialRect[]) {
71         final BlockingResult<double []> result = new BlockingResult<double[]>(); 
72         final PointRubberBox rubberBox;
73         if (initialRect.length == 0) {
74             rubberBox = new TwoPointsRubberBox(drawerVisitor);
75         } else {
76             rubberBox = new OnePointRubberBox(drawerVisitor, initialRect);
77         }
78                 
79         dragZoomRotateInteraction.setEnable(false);
80         rubberBox.addListener(new RubberBoxListener() {
81             @Override
82             public void rubberBoxEnd() {
83                 result.setResult(rubberBox.getResults());
84                 dragZoomRotateInteraction.setEnable(true);
85                 drawerVisitor.removePostRendering(rubberBox);
86             }
87         });
88         drawerVisitor.addPostRendering(rubberBox);
89         rubberBox.setEnable(true);
90         return result.getResult();
91     }
92     
93     public double[] startDragRubberBox() {
94         final BlockingResult<double []> result = new BlockingResult<double []>(); 
95         final DragPointRubberBox rubberBox = new DragPointRubberBox(drawerVisitor);
96
97         dragZoomRotateInteraction.setEnable(false);
98         rubberBox.addListener(new RubberBoxListener() {
99             @Override
100             public void rubberBoxEnd() {
101                 result.setResult(rubberBox.getResults());
102                 dragZoomRotateInteraction.setEnable(true);
103                 drawerVisitor.removePostRendering(rubberBox);
104             }
105         });
106         drawerVisitor.addPostRendering(rubberBox);
107         rubberBox.setEnable(true);
108         return result.getResult();
109     }
110     
111     @Override
112     public void rubberBoxEnd() {
113         dragZoomRotateInteraction.setEnable(true);
114         rubberBox = null;
115     }
116 }