Skip to content

Package: MouseClickZoomingController$1

MouseClickZoomingController$1

nameinstructionbranchcomplexitylinemethod
scaleChanged(EditableImageRendererEvent)
M: 21 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 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.Cursor;
30: import java.awt.event.MouseAdapter;
31: import java.awt.event.MouseEvent;
32: import java.awt.event.MouseListener;
33: import it.tidalwave.image.render.event.EditableImageRendererAdapter;
34: import it.tidalwave.image.render.event.EditableImageRendererEvent;
35:
36: /***********************************************************************************************************************
37: *
38: * This class activates zoom-by-click, that is the capability of zooming in and
39: * out on the image by clicking with the mouse over it. The click toggles the
40: * zoom between the 1:1 and the 'fit to view' values. The actual zooming
41: * behaviour is delegated to a <code>ScaleController</code>, which allows to
42: * plug different implementations (e.g. animations).
43: *
44: * @author Fabrizio Giudici
45: *
46: **********************************************************************************************************************/
47: public class MouseClickZoomingController
48: {
49: /**
50: * The attached renderer.
51: */
52: private final EditableImageRenderer imageRenderer;
53:
54: /**
55: * The controller for the scale operation.
56: */
57: private final ScaleController scaleController;
58:
59: /**
60: * The enablement state.
61: */
62: private boolean enabled;
63:
64: /**
65: * The number of clicks requested to trigger the zoom.
66: */
67: private int clickCountToZoom = 2;
68:
69: private double factor = 1.0;
70:
71: /*******************************************************************************************************************
72: *
73: * Tracks scale changes and select a different cursor.
74: *
75: ******************************************************************************************************************/
76: private final EditableImageRendererAdapter scaleListener = new EditableImageRendererAdapter()
77: {
78: @Override
79: public void scaleChanged (final EditableImageRendererEvent editableImageRendererEvent)
80: {
81: final var scale = imageRenderer.getScale();
82:• imageRenderer.setCursor(Cursor.getPredefinedCursor((scale != imageRenderer.getFitScale())
83: ? Cursor.HAND_CURSOR
84: : Cursor.CROSSHAIR_CURSOR));
85: }
86: };
87:
88: /*******************************************************************************************************************
89: *
90: * Listens to mouse clicks.
91: *
92: ******************************************************************************************************************/
93: private final MouseListener clickListener = new MouseAdapter()
94: {
95: @Override
96: public void mouseClicked (final MouseEvent event)
97: {
98: if ((event.getClickCount() == clickCountToZoom) &&
99: (imageRenderer.getPositionOverImage(event.getPoint()) != null))
100: {
101: final var scale = imageRenderer.getScale();
102: scaleController.setScale((scale == 1) ? imageRenderer.getFitScale() * factor : 1, event.getPoint());
103: }
104: }
105: };
106:
107: /*******************************************************************************************************************
108: *
109: * Creates a new instance of this class, attached to a
110: * <code>ScaleController</code>. This controller must be
111: * activated with <code>setEnabled(true)</code> in order to be used.
112: *
113: * @param scaleController the scale controller
114: *
115: ******************************************************************************************************************/
116: public MouseClickZoomingController (final ScaleController scaleController)
117: {
118: if (scaleController == null)
119: {
120: throw new IllegalArgumentException("scaleController is mandatory");
121: }
122:
123: this.scaleController = scaleController;
124: this.imageRenderer = scaleController.getImageRenderer();
125: }
126:
127: /*******************************************************************************************************************
128: *
129: * Sets the number of clicks requested to trigger the zooming.
130: *
131: * @param clickCountToZoom the number of clicks
132: *
133: ******************************************************************************************************************/
134: public void setClickCountToZoom (final int clickCountToZoom)
135: {
136: this.clickCountToZoom = clickCountToZoom;
137: }
138:
139: /*******************************************************************************************************************
140: *
141: * Returns the number of clicks requested to trigger the zooming.
142: *
143: ******************************************************************************************************************/
144: public int getClickCountToZoom()
145: {
146: return clickCountToZoom;
147: }
148:
149: /*******************************************************************************************************************
150: *
151: * Enables or disables this controller. As this class attaches some
152: * listeners to the image renderer component, it's advisable to disable it
153: * when it's not needed, in order to facilitate garbage collection.
154: *
155: * @param enabled true if must be enabled, false otherwise
156: *
157: ******************************************************************************************************************/
158: public void setEnabled (final boolean enabled)
159: {
160: if (this.enabled != enabled)
161: {
162: this.enabled = enabled;
163:
164: if (enabled)
165: {
166: imageRenderer.addMouseListener(clickListener);
167: imageRenderer.addImageRendererListener(scaleListener);
168: }
169:
170: else
171: {
172: imageRenderer.removeMouseListener(clickListener);
173: imageRenderer.removeImageRendererListener(scaleListener);
174: imageRenderer.setCursor(Cursor.getDefaultCursor());
175: }
176: }
177: }
178:
179: /*******************************************************************************************************************
180: *
181: * Returns true if the controller is enabled.
182: *
183: * @return true if enabled
184: *
185: ******************************************************************************************************************/
186: public boolean isEnabled()
187: {
188: return enabled;
189: }
190:
191: public void setFactor (final double factor)
192: {
193: this.factor = factor;
194: }
195:
196: public double getFactor()
197: {
198: return factor;
199: }
200: }