|
2 | 2 |
|
3 | 3 | import io.github.makbn.jlmap.JLMap; |
4 | 4 | import io.github.makbn.jlmap.element.menu.JLContextMenuMediator; |
| 5 | +import io.github.makbn.jlmap.element.menu.JLHasContextMenu; |
| 6 | +import io.github.makbn.jlmap.fx.JLMapView; |
5 | 7 | import io.github.makbn.jlmap.model.JLObject; |
| 8 | +import javafx.scene.control.ContextMenu; |
| 9 | +import javafx.scene.control.MenuItem; |
| 10 | +import javafx.scene.image.Image; |
| 11 | +import javafx.scene.image.ImageView; |
6 | 12 | import lombok.NonNull; |
| 13 | +import lombok.experimental.FieldDefaults; |
7 | 14 | import lombok.extern.slf4j.Slf4j; |
8 | 15 |
|
| 16 | +import java.util.Objects; |
| 17 | + |
9 | 18 | /** |
10 | 19 | * JavaFX-specific implementation of the context menu mediator. |
11 | 20 | * <p> |
|
35 | 44 | * @since 2.0.0 |
36 | 45 | */ |
37 | 46 | @Slf4j |
| 47 | +@FieldDefaults(level = lombok.AccessLevel.PRIVATE) |
38 | 48 | public class JavaFXContextMenuMediator implements JLContextMenuMediator { |
| 49 | + public static final double ICON_SIZE = 14.0; |
| 50 | + ContextMenu universalContextMenu; |
| 51 | + boolean mouseHandlerRegistered = false; |
| 52 | + long lastMenuShowTime = 0; |
39 | 53 |
|
40 | 54 | /** |
41 | 55 | * {@inheritDoc} |
42 | 56 | */ |
43 | 57 | @Override |
44 | | - public <T extends JLObject<T>> void showContextMenu(@NonNull JLMap<?> map, @NonNull JLObject<T> object, double x, double y) { |
| 58 | + public synchronized <T extends JLObject<T>> void showContextMenu(@NonNull JLMap<?> map, @NonNull JLObject<T> object, double x, double y) { |
| 59 | + log.debug("Showing context menu for object: {} at ({}, {})", object.getJLId(), x, y); |
| 60 | + ensureContextMenuInitialized(); |
| 61 | + |
| 62 | + if (!(object instanceof JLHasContextMenu<?> objectWithContextMenu)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + logContextMenuDebugInfo(objectWithContextMenu); |
| 67 | + |
| 68 | + if (shouldShowContextMenu(objectWithContextMenu)) { |
| 69 | + populateMenuItems(objectWithContextMenu); |
| 70 | + displayContextMenu(map, x, y); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void ensureContextMenuInitialized() { |
| 75 | + if (universalContextMenu == null) { |
| 76 | + universalContextMenu = new ContextMenu(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private void logContextMenuDebugInfo(JLHasContextMenu<?> objectWithContextMenu) { |
| 81 | + log.debug("Object is JLHasContextMenu: true"); |
| 82 | + log.debug("Has context menu: {}, Enabled: {}", objectWithContextMenu.hasContextMenu(), objectWithContextMenu.isContextMenuEnabled()); |
| 83 | + log.debug("Context menu object: {}", objectWithContextMenu.getContextMenu()); |
| 84 | + if (objectWithContextMenu.getContextMenu() != null) { |
| 85 | + log.debug("Context menu items count: {}", objectWithContextMenu.getContextMenu().getItemCount()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private boolean shouldShowContextMenu(JLHasContextMenu<?> objectWithContextMenu) { |
| 90 | + return objectWithContextMenu.hasContextMenu() && objectWithContextMenu.isContextMenuEnabled(); |
| 91 | + } |
| 92 | + |
| 93 | + private void populateMenuItems(JLHasContextMenu<?> objectWithContextMenu) { |
| 94 | + universalContextMenu.getItems().clear(); |
| 95 | + Objects.requireNonNull(objectWithContextMenu.getContextMenu()).getItems().forEach(item -> { |
| 96 | + log.debug("Adding menu item: {}", item.getText()); |
| 97 | + MenuItem menuItem = createMenuItem(item, objectWithContextMenu); |
| 98 | + universalContextMenu.getItems().add(menuItem); |
| 99 | + }); |
| 100 | + } |
45 | 101 |
|
| 102 | + private MenuItem createMenuItem(io.github.makbn.jlmap.element.menu.JLMenuItem item, JLHasContextMenu<?> objectWithContextMenu) { |
| 103 | + MenuItem menuItem = new MenuItem(item.getText()); |
| 104 | + menuItem.setOnAction(e -> |
| 105 | + Objects.requireNonNull(objectWithContextMenu.getContextMenu()).getOnMenuItemListener().onMenuItemSelected(item)); |
| 106 | + if (item.getIcon() != null && !item.getIcon().isBlank()) { |
| 107 | + menuItem.setGraphic(createIcon(item.getIcon())); |
| 108 | + } |
| 109 | + return menuItem; |
| 110 | + } |
| 111 | + |
| 112 | + private void displayContextMenu(@NonNull JLMap<?> map, double x, double y) { |
| 113 | + if (!(map instanceof JLMapView jlMapView)) { |
| 114 | + log.warn("Map is not a JLMapView: {}", map.getClass()); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + log.debug("Showing context menu with {} items at WebView-relative position ({}, {})", |
| 119 | + universalContextMenu.getItems().size(), x, y); |
| 120 | + |
| 121 | + registerClickHandlerIfNeeded(jlMapView); |
| 122 | + showMenuAtPosition(jlMapView, x, y); |
| 123 | + } |
| 124 | + |
| 125 | + private void registerClickHandlerIfNeeded(JLMapView jlMapView) { |
| 126 | + if (mouseHandlerRegistered) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + jlMapView.getWebView().setOnMouseClicked(event -> { |
| 131 | + long timeSinceShow = System.currentTimeMillis() - lastMenuShowTime; |
| 132 | + if (universalContextMenu != null && universalContextMenu.isShowing() && timeSinceShow > 100) { |
| 133 | + log.debug("Hiding context menu due to WebView click (time since show: {}ms)", timeSinceShow); |
| 134 | + universalContextMenu.hide(); |
| 135 | + } |
| 136 | + }); |
| 137 | + mouseHandlerRegistered = true; |
| 138 | + } |
| 139 | + |
| 140 | + private void showMenuAtPosition(JLMapView jlMapView, double x, double y) { |
| 141 | + javafx.geometry.Point2D screenCoords = jlMapView.getWebView().localToScreen(x, y); |
| 142 | + if (screenCoords != null) { |
| 143 | + log.debug("Screen coordinates: ({}, {})", screenCoords.getX(), screenCoords.getY()); |
| 144 | + lastMenuShowTime = System.currentTimeMillis(); |
| 145 | + universalContextMenu.show(jlMapView.getWebView(), screenCoords.getX(), screenCoords.getY()); |
| 146 | + } else { |
| 147 | + log.warn("Could not convert coordinates to screen space"); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private ImageView createIcon(String url) { |
| 152 | + Image image = new Image(url); |
| 153 | + ImageView imageView = new ImageView(image); |
| 154 | + imageView.setFitWidth(ICON_SIZE); |
| 155 | + imageView.setFitHeight(ICON_SIZE); |
| 156 | + imageView.setPreserveRatio(true); |
| 157 | + return imageView; |
46 | 158 | } |
47 | 159 |
|
48 | 160 | /** |
49 | 161 | * {@inheritDoc} |
50 | 162 | */ |
51 | 163 | @Override |
52 | | - public <T extends JLObject<T>> void hideContextMenu(@NonNull JLMap<?> map, @NonNull JLObject<T> object) { |
53 | | - |
| 164 | + public synchronized <T extends JLObject<T>> void hideContextMenu(@NonNull JLMap<?> map, @NonNull JLObject<T> object) { |
| 165 | + log.debug("Hiding context menu for object: {}", object.getJLId()); |
| 166 | + if (universalContextMenu != null) { |
| 167 | + universalContextMenu.hide(); |
| 168 | + universalContextMenu.getItems().clear(); |
| 169 | + } |
54 | 170 | } |
55 | 171 |
|
56 | 172 | /** |
|
0 commit comments