~josejuan-sanchez/+junk/original-jhv-experimental-version

« back to all changes in this revision

Viewing changes to src/jhv/src/org/helioviewer/jhv/gui/controller/MainImagePanelMouseZoomBoxController.java

  • Committer: José Juan Sánchez Hernández
  • Date: 2013-02-05 13:32:08 UTC
  • Revision ID: josejuan.sanchez@gmail.com-20130205133208-dfz1sh1uge5pjkny
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.helioviewer.jhv.gui.controller;
 
2
 
 
3
import java.awt.Color;
 
4
import java.awt.Cursor;
 
5
import java.awt.Rectangle;
 
6
import java.awt.event.MouseEvent;
 
7
 
 
8
import org.helioviewer.base.math.Vector2dDouble;
 
9
import org.helioviewer.base.math.Vector2dInt;
 
10
import org.helioviewer.jhv.gui.ImageViewerGui;
 
11
import org.helioviewer.jhv.gui.components.TopToolBar.SelectionMode;
 
12
import org.helioviewer.viewmodel.changeevent.ChangeEvent;
 
13
import org.helioviewer.viewmodel.metadata.MetaData;
 
14
import org.helioviewer.viewmodel.region.Region;
 
15
import org.helioviewer.viewmodel.region.StaticRegion;
 
16
import org.helioviewer.viewmodel.renderer.screen.ScreenRenderGraphics;
 
17
import org.helioviewer.viewmodel.renderer.screen.ScreenRenderer;
 
18
import org.helioviewer.viewmodel.view.ComponentView;
 
19
import org.helioviewer.viewmodel.view.View;
 
20
import org.helioviewer.viewmodel.view.ViewHelper;
 
21
import org.helioviewer.viewmodel.viewport.Viewport;
 
22
import org.helioviewer.viewmodel.viewportimagesize.ViewportImageSize;
 
23
 
 
24
/**
 
25
 * Implementation of ImagePanelInputController for the main image panel using
 
26
 * zoom box selection mode.
 
27
 * 
 
28
 * <p>
 
29
 * By using this controller, the user can zoom in the region of interest of the
 
30
 * main image panel by selecting the desired area with a rubber band. Zooming is
 
31
 * also possible by using the mouse wheel or double-clicking into the image.
 
32
 * After selecting an area, the selection mode switches back to pan selection
 
33
 * mode.
 
34
 * 
 
35
 * <p>
 
36
 * Also, see {@link org.helioviewer.jhv.gui.components.MainImagePanel} and
 
37
 * {@link MainImagePanelMousePanController}.
 
38
 * 
 
39
 */
 
40
public class MainImagePanelMouseZoomBoxController extends MainImagePanelMouseController {
 
41
 
 
42
    private volatile Vector2dInt lastMouseCoordinates;
 
43
    private volatile Vector2dInt draggedToCoordinates;
 
44
    private volatile Rectangle rectangle = new Rectangle();
 
45
 
 
46
    private volatile Rubberband rubberband = new Rubberband();
 
47
 
 
48
    /**
 
49
     * {@inheritDoc}
 
50
     */
 
51
    public void setView(View newView) {
 
52
        super.setView(newView);
 
53
 
 
54
        if (newView != null)
 
55
            newView.getAdapter(ComponentView.class).addPostRenderer(rubberband);
 
56
    }
 
57
 
 
58
    /**
 
59
     * {@inheritDoc}
 
60
     */
 
61
    public void mouseEntered(MouseEvent e) {
 
62
        if (imagePanel != null) {
 
63
            imagePanel.setCursor(Cursor.getDefaultCursor());
 
64
        }
 
65
    }
 
66
 
 
67
    /**
 
68
     * {@inheritDoc}
 
69
     */
 
70
    public void mouseExited(MouseEvent e) {
 
71
        if (imagePanel != null) {
 
72
            imagePanel.setCursor(Cursor.getDefaultCursor());
 
73
        }
 
74
        super.mouseExited(e);
 
75
    }
 
76
 
 
77
    /**
 
78
     * {@inheritDoc}
 
79
     */
 
80
    public void mousePressed(MouseEvent e) {
 
81
        if (e.getButton() == MouseEvent.BUTTON1) {
 
82
            lastMouseCoordinates = new Vector2dInt(e.getX(), e.getY());
 
83
            rectangle.x = e.getX();
 
84
            rectangle.y = e.getY();
 
85
            rectangle.width = 0;
 
86
            rectangle.height = 0;
 
87
        }
 
88
    }
 
89
 
 
90
    /**
 
91
     * {@inheritDoc}
 
92
     */
 
93
    public void mouseReleased(MouseEvent e) {
 
94
        if (e.getButton() == MouseEvent.BUTTON1) {
 
95
            draggedToCoordinates = new Vector2dInt(e.getX(), e.getY());
 
96
 
 
97
            Region r = regionView.getRegion();
 
98
            Viewport v = viewportView.getViewport();
 
99
            MetaData m = metaDataView.getMetaData();
 
100
            if (r == null || v == null || m == null) {
 
101
                return;
 
102
            }
 
103
 
 
104
            recalculateRectangle();
 
105
 
 
106
            if (rectangle.height <= 2 && rectangle.width <= 2)
 
107
                return;
 
108
 
 
109
            ViewportImageSize vis = ViewHelper.calculateViewportImageSize(v, r);
 
110
 
 
111
            Vector2dDouble imageOffset = ViewHelper.convertScreenToImageDisplacement(new Vector2dInt(rectangle.x, rectangle.y + rectangle.height).subtract(new Vector2dInt(0, v.getHeight())), r, vis);
 
112
            Vector2dDouble newCorner = Vector2dDouble.add(r.getLowerLeftCorner(), imageOffset);
 
113
 
 
114
            Vector2dDouble size = ViewHelper.convertScreenToImageDisplacement(new Vector2dInt(rectangle.width, rectangle.height * -1), r, vis);
 
115
 
 
116
            Region region = StaticRegion.createAdaptedRegion(newCorner, size);
 
117
            Region regionShifted = ViewHelper.cropRegionToImage(region, m);
 
118
            regionView.setRegion(regionShifted, new ChangeEvent());
 
119
 
 
120
            lastMouseCoordinates = null;
 
121
 
 
122
            ImageViewerGui.getSingletonInstance().getTopToolBar().setActiveSelectionMode(SelectionMode.PAN);
 
123
            ImageViewerGui.getSingletonInstance().getMainImagePanel().getInputController().mouseEntered(e);
 
124
        }
 
125
    }
 
126
 
 
127
    /**
 
128
     * {@inheritDoc}
 
129
     */
 
130
    public void mouseDragged(MouseEvent e) {
 
131
        if (lastMouseCoordinates != null) {
 
132
            draggedToCoordinates = new Vector2dInt(e.getX(), e.getY());
 
133
 
 
134
            recalculateRectangle();
 
135
            imagePanel.repaint();
 
136
        }
 
137
    }
 
138
 
 
139
    /**
 
140
     * Get method that calculates and returns the rectangle of the rubber band.
 
141
     * 
 
142
     */
 
143
    private void recalculateRectangle() {
 
144
 
 
145
        if (lastMouseCoordinates.getX() < draggedToCoordinates.getX()) {
 
146
            rectangle.x = lastMouseCoordinates.getX();
 
147
            rectangle.width = draggedToCoordinates.getX() - lastMouseCoordinates.getX();
 
148
        } else {
 
149
            rectangle.x = draggedToCoordinates.getX();
 
150
            rectangle.width = lastMouseCoordinates.getX() - draggedToCoordinates.getX();
 
151
        }
 
152
 
 
153
        if (lastMouseCoordinates.getY() < draggedToCoordinates.getY()) {
 
154
            rectangle.y = lastMouseCoordinates.getY();
 
155
            rectangle.height = draggedToCoordinates.getY() - lastMouseCoordinates.getY();
 
156
        } else {
 
157
            rectangle.y = draggedToCoordinates.getY();
 
158
            rectangle.height = lastMouseCoordinates.getY() - draggedToCoordinates.getY();
 
159
        }
 
160
    }
 
161
 
 
162
    /**
 
163
     * Private class implementing ScreenRenderer to draw the rubber band onto
 
164
     * the image.
 
165
     * 
 
166
     * <p>
 
167
     * For further informations about drawing into the image, see
 
168
     * {@link org.helioviewer.viewmodel.renderer.screen.ScreenRenderer}.
 
169
     */
 
170
    private class Rubberband implements ScreenRenderer {
 
171
 
 
172
        /**
 
173
         * {@inheritDoc}
 
174
         */
 
175
        public void render(ScreenRenderGraphics g) {
 
176
            if (lastMouseCoordinates != null) {
 
177
                g.setColor(Color.YELLOW);
 
178
                g.drawRectangle(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
 
179
            }
 
180
        }
 
181
    }
 
182
}