|
14 | 14 | import java.awt.event.MouseEvent; |
15 | 15 | import java.awt.event.MouseWheelEvent; |
16 | 16 | import java.awt.geom.Point2D; |
17 | | -import java.util.Objects; |
18 | 17 | import java.util.Optional; |
19 | 18 | import org.opencv.core.Point3; |
20 | 19 | import org.weasis.core.Messages; |
21 | | -import org.weasis.core.api.gui.util.ActionState; |
22 | 20 | import org.weasis.core.api.gui.util.Feature; |
23 | 21 | import org.weasis.core.api.gui.util.MouseActionAdapter; |
24 | 22 | import org.weasis.core.api.gui.util.WinUtil; |
25 | 23 | import org.weasis.core.api.media.data.ImageElement; |
26 | 24 | import org.weasis.core.ui.model.layer.LayerAnnotation; |
| 25 | +import org.weasis.core.ui.model.layer.LayerItem; |
27 | 26 | import org.weasis.core.util.StringUtil; |
28 | 27 |
|
29 | | -public class FocusHandler<E extends ImageElement> extends MouseActionAdapter { |
| 28 | +/** |
| 29 | + * Handles focus and mouse interactions for image viewer canvas. Manages view selection, button |
| 30 | + * interactions, and pixel information display. |
| 31 | + * |
| 32 | + * @param <E> the type of image element |
| 33 | + */ |
| 34 | +public final class FocusHandler<E extends ImageElement> extends MouseActionAdapter { |
| 35 | + private static final long UPDATE_INTERVAL_MS = 50; |
| 36 | + |
30 | 37 | private final ViewCanvas<E> viewCanvas; |
| 38 | + private long lastUpdateTime; |
31 | 39 |
|
32 | 40 | public FocusHandler(ViewCanvas<E> viewCanvas) { |
33 | 41 | this.viewCanvas = viewCanvas; |
34 | 42 | } |
35 | 43 |
|
36 | 44 | @Override |
37 | 45 | public void mousePressed(MouseEvent evt) { |
38 | | - ImageViewerPlugin<E> pane = viewCanvas.getEventManager().getSelectedView2dContainer(); |
39 | | - if (Objects.isNull(pane)) { |
| 46 | + var pane = viewCanvas.getEventManager().getSelectedView2dContainer(); |
| 47 | + if (pane == null) { |
40 | 48 | return; |
41 | 49 | } |
42 | 50 |
|
43 | | - ViewButton selectedButton = null; |
| 51 | + var selectedButton = findViewButtonAt(evt.getPoint()); |
44 | 52 | // Do select the view when pressing on a view button |
45 | | - for (ViewButton b : viewCanvas.getViewButtons()) { |
46 | | - if (b.isVisible() && b.contains(evt.getPoint())) { |
47 | | - selectedButton = b; |
48 | | - break; |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - if (evt.getClickCount() == 2 && selectedButton == null) { |
| 53 | + if (evt.getClickCount() == 2 && selectedButton.isEmpty()) { |
53 | 54 | pane.maximizedSelectedImagePane(viewCanvas, evt); |
54 | 55 | return; |
55 | 56 | } |
56 | 57 |
|
57 | | - if (pane.isContainingView(viewCanvas) && pane.getSelectedImagePane() != viewCanvas) { |
58 | | - // register all actions of the EventManager with this view waiting the focus gained in some |
59 | | - // cases is not |
60 | | - // enough, because others mouseListeners are triggered before the focus event (that means |
61 | | - // before |
62 | | - // registering the view in the EventManager) |
63 | | - pane.setSelectedImagePane(viewCanvas); |
64 | | - } |
65 | | - // request the focus even it is the same pane selected |
| 58 | + selectViewIfNeeded(pane); |
66 | 59 | viewCanvas.getJComponent().requestFocusInWindow(); |
67 | 60 |
|
68 | | - // Do select the view when pressing on a view button |
69 | | - if (selectedButton != null) { |
70 | | - viewCanvas.getJComponent().setCursor(DefaultView2d.DEFAULT_CURSOR); |
71 | | - evt.consume(); |
72 | | - selectedButton.showPopup(evt.getComponent(), evt.getX(), evt.getY()); |
| 61 | + if (selectedButton.isPresent()) { |
| 62 | + handleViewButtonClick(evt, selectedButton.get()); |
73 | 63 | return; |
74 | 64 | } |
75 | 65 |
|
76 | | - Optional<Feature<? extends ActionState>> action = |
77 | | - viewCanvas.getEventManager().getMouseAction(evt.getModifiersEx()); |
78 | | - viewCanvas |
79 | | - .getJComponent() |
80 | | - .setCursor(action.isPresent() ? action.get().getCursor() : DefaultView2d.DEFAULT_CURSOR); |
| 66 | + updateCursorForMouseAction(evt); |
81 | 67 | } |
82 | 68 |
|
83 | 69 | @Override |
84 | 70 | public void mouseWheelMoved(MouseWheelEvent e) { |
85 | 71 | ImageViewerEventManager<E> eventManager = viewCanvas.getEventManager(); |
86 | | - if (eventManager != null) { |
87 | | - ImageViewerPlugin<E> container = |
88 | | - WinUtil.getParentOfClass(viewCanvas.getJComponent(), ImageViewerPlugin.class); |
89 | | - if (container != null) { |
90 | | - if (!container.equals(eventManager.getSelectedView2dContainer())) { |
91 | | - eventManager.setSelectedView2dContainer(container); |
92 | | - } |
93 | | - if (!container.getSelectedImagePane().equals(viewCanvas)) { |
94 | | - container.setSelectedImagePane(viewCanvas); |
95 | | - } |
96 | | - } |
| 72 | + if (eventManager == null) { |
| 73 | + return; |
| 74 | + } |
| 75 | + ImageViewerPlugin<E> container = |
| 76 | + WinUtil.getParentOfClass(viewCanvas.getJComponent(), ImageViewerPlugin.class); |
| 77 | + if (container == null) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (!container.equals(eventManager.getSelectedView2dContainer())) { |
| 82 | + eventManager.setSelectedView2dContainer(container); |
| 83 | + } |
| 84 | + if (!container.getSelectedImagePane().equals(viewCanvas)) { |
| 85 | + container.setSelectedImagePane(viewCanvas); |
97 | 86 | } |
98 | 87 | } |
99 | 88 |
|
100 | 89 | @Override |
101 | 90 | public void mouseMoved(MouseEvent evt) { |
102 | 91 | showPixelInfos(evt); |
103 | | - for (ViewButton b : viewCanvas.getViewButtons()) { |
104 | | - if (b.isVisible()) { |
105 | | - boolean hover = b.contains(evt.getPoint()); |
106 | | - if (hover != b.isHover()) { |
107 | | - b.setHover(hover); |
108 | | - viewCanvas.getJComponent().repaint(); |
109 | | - } |
110 | | - } |
111 | | - } |
| 92 | + updateViewButtonsHoverState(evt.getPoint()); |
112 | 93 | } |
113 | 94 |
|
114 | 95 | @Override |
115 | 96 | public void mouseReleased(MouseEvent evt) { |
116 | 97 | viewCanvas.getJComponent().setCursor(DefaultView2d.DEFAULT_CURSOR); |
117 | | - for (ViewButton b : viewCanvas.getViewButtons()) { |
118 | | - if (b.isVisible() && b.contains(evt.getPoint())) { |
119 | | - evt.consume(); |
120 | | - break; |
121 | | - } |
| 98 | + findViewButtonAt(evt.getPoint()).ifPresent(_ -> evt.consume()); |
| 99 | + } |
| 100 | + |
| 101 | + private Optional<ViewButton> findViewButtonAt(Point point) { |
| 102 | + return viewCanvas.getViewButtons().stream() |
| 103 | + .filter(ViewButton::isVisible) |
| 104 | + .filter(button -> button.contains(point)) |
| 105 | + .findFirst(); |
| 106 | + } |
| 107 | + |
| 108 | + private void selectViewIfNeeded(ImageViewerPlugin<E> pane) { |
| 109 | + if (pane.isContainingView(viewCanvas) && pane.getSelectedImagePane() != viewCanvas) { |
| 110 | + // Register all EventManager actions immediately with this view. Waiting for focus gain |
| 111 | + // is not enough since other MouseListeners may trigger before the focus event occurs, |
| 112 | + // resulting in the view not yet being registered in the EventManager. |
| 113 | + pane.setSelectedImagePane(viewCanvas); |
122 | 114 | } |
123 | 115 | } |
124 | 116 |
|
125 | | - protected void showPixelInfos(MouseEvent mouseevent) { |
126 | | - LayerAnnotation infoLayer = viewCanvas.getInfoLayer(); |
127 | | - if (infoLayer != null) { |
128 | | - Point2D pModel = |
129 | | - viewCanvas.getImageCoordinatesFromMouse(mouseevent.getX(), mouseevent.getY()); |
130 | | - PixelInfo pixelInfo = |
131 | | - viewCanvas.getPixelInfo( |
132 | | - new Point((int) Math.floor(pModel.getX()), (int) Math.floor(pModel.getY()))); |
133 | | - if (pixelInfo == null) { |
134 | | - return; |
135 | | - } |
136 | | - Rectangle oldBound = infoLayer.getPixelInfoBound(); |
| 117 | + private void handleViewButtonClick(MouseEvent evt, ViewButton button) { |
| 118 | + viewCanvas.getJComponent().setCursor(DefaultView2d.DEFAULT_CURSOR); |
| 119 | + evt.consume(); // Consume event to not select the view |
| 120 | + button.showPopup(evt.getComponent(), evt.getX(), evt.getY()); |
| 121 | + } |
| 122 | + |
| 123 | + private void updateCursorForMouseAction(MouseEvent evt) { |
| 124 | + var action = viewCanvas.getEventManager().getMouseAction(evt.getModifiersEx()); |
| 125 | + var cursor = action.map(Feature::getCursor).orElse(DefaultView2d.DEFAULT_CURSOR); |
| 126 | + viewCanvas.getJComponent().setCursor(cursor); |
| 127 | + } |
| 128 | + |
| 129 | + private void updateViewButtonsHoverState(Point mousePoint) { |
| 130 | + viewCanvas.getViewButtons().stream() |
| 131 | + .filter(ViewButton::isVisible) |
| 132 | + .forEach( |
| 133 | + button -> { |
| 134 | + boolean hover = button.contains(mousePoint); |
| 135 | + if (hover != button.isHover()) { |
| 136 | + button.setHover(hover); |
| 137 | + viewCanvas.getJComponent().repaint(); |
| 138 | + } |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + private void showPixelInfos(MouseEvent mouseevent) { |
| 143 | + var infoLayer = viewCanvas.getInfoLayer(); |
| 144 | + if (!isPixelInfoVisible(infoLayer)) { |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + if (!shouldUpdatePixelInfo()) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + var pixelInfo = computePixelInfo(mouseevent); |
| 153 | + if (pixelInfo == null) { |
| 154 | + return; |
| 155 | + } |
| 156 | + updatePixelInfoDisplay(pixelInfo); |
| 157 | + } |
| 158 | + |
| 159 | + private boolean isPixelInfoVisible(LayerAnnotation infoLayer) { |
| 160 | + return infoLayer != null |
| 161 | + && infoLayer.getVisible() |
| 162 | + && infoLayer.getDisplayPreferences(LayerItem.PIXEL) |
| 163 | + && !infoLayer.getDisplayPreferences(LayerItem.MIN_ANNOTATIONS); |
| 164 | + } |
| 165 | + |
| 166 | + private boolean shouldUpdatePixelInfo() { |
| 167 | + long currentTime = System.currentTimeMillis(); |
| 168 | + if (currentTime - lastUpdateTime < UPDATE_INTERVAL_MS) { |
| 169 | + return false; |
| 170 | + } |
| 171 | + lastUpdateTime = currentTime; |
| 172 | + return true; |
| 173 | + } |
| 174 | + |
| 175 | + private PixelInfo computePixelInfo(MouseEvent mouseevent) { |
| 176 | + Point2D pModel = viewCanvas.getImageCoordinatesFromMouse(mouseevent.getX(), mouseevent.getY()); |
| 177 | + var pixelInfo = |
| 178 | + viewCanvas.getPixelInfo( |
| 179 | + new Point((int) Math.floor(pModel.getX()), (int) Math.floor(pModel.getY()))); |
| 180 | + if (pixelInfo != null) { |
137 | 181 | Point3 point3d = |
138 | 182 | viewCanvas.getVolumeCoordinatesFromMouse(mouseevent.getX(), mouseevent.getY()); |
139 | 183 | pixelInfo.setPosition3d(point3d); |
140 | | - oldBound.width = |
141 | | - Math.max( |
142 | | - oldBound.width, |
143 | | - viewCanvas |
144 | | - .getJComponent() |
145 | | - .getGraphics() |
146 | | - .getFontMetrics() |
147 | | - .stringWidth( |
148 | | - Messages.getString("DefaultView2d.pix") |
149 | | - + StringUtil.COLON_AND_SPACE |
150 | | - + pixelInfo) |
151 | | - + 4); |
152 | | - infoLayer.setPixelInfo(pixelInfo); |
153 | | - viewCanvas.getJComponent().repaint(oldBound); |
154 | 184 | } |
| 185 | + |
| 186 | + return pixelInfo; |
| 187 | + } |
| 188 | + |
| 189 | + private void updatePixelInfoDisplay(PixelInfo pixelInfo) { |
| 190 | + var infoLayer = viewCanvas.getInfoLayer(); |
| 191 | + Rectangle oldBound = infoLayer.getPixelInfoBound(); |
| 192 | + int textWidth = calculatePixelInfoTextWidth(pixelInfo); |
| 193 | + oldBound.width = Math.max(oldBound.width, textWidth + 4); |
| 194 | + |
| 195 | + infoLayer.setPixelInfo(pixelInfo); |
| 196 | + viewCanvas.getJComponent().repaint(oldBound); |
| 197 | + } |
| 198 | + |
| 199 | + private int calculatePixelInfoTextWidth(PixelInfo pixelInfo) { |
| 200 | + String text = |
| 201 | + Messages.getString("DefaultView2d.pix") |
| 202 | + + StringUtil.COLON_AND_SPACE |
| 203 | + + pixelInfo.getPixelValueText() |
| 204 | + + " - " |
| 205 | + + pixelInfo.getPixelPositionText(); |
| 206 | + |
| 207 | + return viewCanvas |
| 208 | + .getJComponent() |
| 209 | + .getFontMetrics(viewCanvas.getJComponent().getFont()) |
| 210 | + .stringWidth(text); |
155 | 211 | } |
156 | 212 | } |
0 commit comments