Browse Source

Working example xontext menus\!

master
Drew Short 10 years ago
parent
commit
c57c985ff6
  1. 20
      gui/src/main/scala/com/sothr/imagetools/ui/component/ImageTile.scala
  2. 75
      gui/src/main/scala/com/sothr/imagetools/ui/component/ImageTilePane.scala

20
gui/src/main/scala/com/sothr/imagetools/ui/component/ImageTile.scala

@ -1,11 +1,11 @@
package com.sothr.imagetools.ui.component package com.sothr.imagetools.ui.component
import java.io.FileInputStream import java.io.FileInputStream
import javafx.event.EventHandler
import javafx.event.{EventType, EventHandler}
import javafx.geometry.{Orientation, Insets, Pos} import javafx.geometry.{Orientation, Insets, Pos}
import javafx.scene.control.{Separator, Tooltip, Label} import javafx.scene.control.{Separator, Tooltip, Label}
import javafx.scene.image.{ImageView} import javafx.scene.image.{ImageView}
import javafx.scene.input.MouseEvent
import javafx.scene.input.{PickResult, ContextMenuEvent, MouseEvent}
import javafx.scene.layout.VBox import javafx.scene.layout.VBox
import grizzled.slf4j.Logging import grizzled.slf4j.Logging
@ -51,6 +51,16 @@ class ImageTile(thumbnailWidth: Integer,
} }
} else if (event.isSecondaryButtonDown) { } else if (event.isSecondaryButtonDown) {
//right click context menu //right click context menu
debug("Requesting Context Menu")
val contextMenuEvent = new ContextMenuEvent(
thisTile,
thisTile,
ContextMenuEvent.CONTEXT_MENU_REQUESTED,
event.getX, event.getY,
event.getScreenX, event.getScreenY,
false,
new PickResult(thisTile, event.getSceneX, event.getSceneY))
imageTilePane.handleContextMenu(contextMenuEvent)
} }
} }
} }
@ -94,6 +104,12 @@ class ImageTile(thumbnailWidth: Integer,
imageLabel.setTooltip(tooltip) imageLabel.setTooltip(tooltip)
this.getChildren.add(imageLabel) this.getChildren.add(imageLabel)
//this.setOnContextMenuRequested(new EventHandler[ContextMenuEvent] {
// override def handle(event: ContextMenuEvent): Unit = {
// imageTilePane.handleContextMenu(event)
// }
//})
def getImageData: com.sothr.imagetools.engine.image.Image = { def getImageData: com.sothr.imagetools.engine.image.Image = {
imageData imageData
} }

75
gui/src/main/scala/com/sothr/imagetools/ui/component/ImageTilePane.scala

@ -2,18 +2,85 @@ package com.sothr.imagetools.ui.component
import java.util import java.util
import javafx.collections.{ModifiableObservableListBase, ObservableList} import javafx.collections.{ModifiableObservableListBase, ObservableList}
import javafx.scene.control.MultipleSelectionModel
import javafx.event.{ActionEvent, EventHandler}
import javafx.geometry.Side
import javafx.scene.control.{MenuItem, ContextMenu, MultipleSelectionModel}
import javafx.scene.input.ContextMenuEvent
import javafx.scene.layout._ import javafx.scene.layout._
import javafx.scene.paint.{Color, Paint}
import javafx.scene.paint.Color
import javafx.scene.Node
import grizzled.slf4j.Logging
/** /**
* Custom Tile Pane with a multi selection model * Custom Tile Pane with a multi selection model
* *
* Created by drew on 8/29/14. * Created by drew on 8/29/14.
*/ */
class ImageTilePane extends TilePane {
class ImageTilePane extends TilePane with Logging {
val selectionModel = new ImageTilePaneSelectionModel(this) val selectionModel = new ImageTilePaneSelectionModel(this)
//this.setOnContextMenuRequested(new EventHandler[ContextMenuEvent] {
// override def handle(event: ContextMenuEvent): Unit = {
// handleContextMenu(event)
// }
//})
def handleContextMenu(event: ContextMenuEvent) = {
//Build and show a context menu
debug("Context Menu Request Received")
val numSelected = this.selectionModel.getSelectedIndices.size()
if (numSelected > 0) {
if (numSelected == 1) {
val contextMenu = getSingleSelectionContextMenu
debug("Showing context menu")
contextMenu.show(event.getTarget.asInstanceOf[Node],Side.RIGHT,0d,0d)
} else {
val contextMenu = getMulipleSelectionContextMenu
debug("Showing context menu")
contextMenu.show(event.getTarget.asInstanceOf[Node],Side.RIGHT,0d,0d)
}
}
}
def getSingleSelectionContextMenu : ContextMenu = {
debug("Building single-selection context menu")
val contextMenu = new ContextMenu()
val item1 = new MenuItem("Single Selection")
item1.setOnAction(new EventHandler[ActionEvent]() {
def handle(e: ActionEvent) = {
debug("Single Selection")
}
})
val item2 = new MenuItem("BlahBlah")
item2.setOnAction(new EventHandler[ActionEvent]() {
def handle(e: ActionEvent) = {
debug("BlahBlah")
}
})
contextMenu.getItems.addAll(item1, item2)
contextMenu
}
def getMulipleSelectionContextMenu : ContextMenu = {
debug("Building multi-selection context menu")
val contextMenu = new ContextMenu()
val item1 = new MenuItem("Multi Selection")
item1.setOnAction(new EventHandler[ActionEvent]() {
def handle(e: ActionEvent) = {
debug("Multi Selection")
}
})
val item2 = new MenuItem("BlahBlah")
item2.setOnAction(new EventHandler[ActionEvent]() {
def handle(e: ActionEvent) = {
debug("BlahBlah")
}
})
contextMenu.getItems.addAll(item1, item2)
contextMenu
}
def imageSelected(imageTile: ImageTile) = { def imageSelected(imageTile: ImageTile) = {
this.selectionModel.clearAndSelect(this.getChildren.indexOf(imageTile)) this.selectionModel.clearAndSelect(this.getChildren.indexOf(imageTile))
} }
@ -132,7 +199,7 @@ class ImageTilePaneSelectionModel[ImageTile](parentTilePane: ImageTilePane) exte
this.selectedIndexes.contains(index) this.selectedIndexes.contains(index)
} }
private def clearSelectionFormatting = {
private def clearSelectionFormatting() = {
val iterator = this.parentTilePane.getChildren.iterator() val iterator = this.parentTilePane.getChildren.iterator()
while (iterator.hasNext) { while (iterator.hasNext) {
//remove the selection styling //remove the selection styling

Loading…
Cancel
Save