Skip to content

Package: MouseWheelZoomingController$1

MouseWheelZoomingController$1

nameinstructionbranchcomplexitylinemethod
mouseWheelMoved(MouseWheelEvent)
M: 21 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.render;
28:
29: import java.awt.event.MouseWheelEvent;
30: import java.awt.event.MouseWheelListener;
31:
32: /***********************************************************************************************************************
33: *
34: * This class zooms listening to the mouse wheel.
35: *
36: * @author Fabrizio Giudici
37: *
38: **********************************************************************************************************************/
39: public class MouseWheelZoomingController
40: {
41: /**
42: * The attached renderer.
43: */
44: private final EditableImageRenderer imageRenderer;
45:
46: /**
47: * The controller for the scale operation.
48: */
49: private final ScaleController scaleController;
50:
51: /**
52: * The enablement state.
53: */
54: private boolean enabled;
55:
56: /**
57: * How fast the zoom acts with mouse clicks.
58: */
59: private double zoomFactor = 0.25;
60:
61: /*******************************************************************************************************************
62: *
63: * Listens to mouse clicks.
64: *
65: ******************************************************************************************************************/
66: private final MouseWheelListener mouseWheelListener = new MouseWheelListener()
67: {
68: @Override
69: public void mouseWheelMoved (final MouseWheelEvent event)
70: {
71: final var changeScale = Math.pow(2, zoomFactor * event.getWheelRotation());
72: scaleController.setScale(scaleController.getScale() * changeScale);
73: }
74: };
75:
76: /*******************************************************************************************************************
77: *
78: * Creates a new instance of this class, attached to a
79: * <code>ScaleController</code>. This controller must be
80: * activated with <code>setEnabled(true)</code> in order to be used.
81: *
82: * @param scaleController the scale controller
83: *
84: ******************************************************************************************************************/
85: public MouseWheelZoomingController (final ScaleController scaleController)
86: {
87: if (scaleController == null)
88: {
89: throw new IllegalArgumentException("scaleController is mandatory");
90: }
91:
92: this.scaleController = scaleController;
93: this.imageRenderer = scaleController.getImageRenderer();
94: }
95:
96: /*******************************************************************************************************************
97: *
98: * Enables or disables this controller. As this class attaches some
99: * listeners to the image renderer component, it's advisable to disable it
100: * when it's not needed, in order to facilitate garbage collection.
101: *
102: * @param enabled true if must be enabled, false otherwise
103: *
104: ******************************************************************************************************************/
105: public void setEnabled (final boolean enabled)
106: {
107: if (this.enabled != enabled)
108: {
109: this.enabled = enabled;
110:
111: if (enabled)
112: {
113: imageRenderer.addMouseWheelListener(mouseWheelListener);
114: }
115:
116: else
117: {
118: imageRenderer.removeMouseWheelListener(mouseWheelListener);
119: }
120: }
121: }
122:
123: /*******************************************************************************************************************
124: *
125: * Returns true if the controller is enabled.
126: *
127: * @return true if enabled
128: *
129: ******************************************************************************************************************/
130: public boolean isEnabled()
131: {
132: return enabled;
133: }
134:
135: public void setZoomFactor (final double zoomFactor)
136: {
137: this.zoomFactor = zoomFactor;
138: }
139:
140: public double getZoomFactor()
141: {
142: return zoomFactor;
143: }
144: }