下面列出了怎么用javafx.scene.input.ContextMenuEvent的API类实例代码及写法,或者点击链接到github查看源代码。
public void addOrUpdateGroupCategory(GroupCategory groupCategory) {
Platform.runLater(new Runnable() {
@Override
public void run() {
ListNodePanel node = groupListNodeMap.get(groupCategory.getId());
if (null == node) {
node = new ListNodePanel();
groupListNodeMap.put(groupCategory.getId(), node);
}
node.setText(groupCategory.getName());
node.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent event) {
gcmv.setGroupCategory(groupCategory);
gcmv.show(mainFrame, event.getScreenX(), event.getScreenY());
event.consume();
}
});
// node.setNumberText("[0]");
groupRoot.addNode(node);
}
});
}
/**
* 设置用户头像点击事件
*
* @author: XiaHui
* @param head
* @param userData
* @createDate: 2017年6月9日 上午11:17:31
* @update: XiaHui
* @updateDate: 2017年6月9日 上午11:17:31
*/
protected void setUserDataHeadEvent(HeadItem head, UserData userData) {
head.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent e) {
uhmv.setUserData(userData);
uhmv.show(mainFrame, e.getScreenX(), e.getScreenY());
e.consume();
}
});
head.setOnMouseClicked((MouseEvent me) -> {
if (me.getClickCount() == 2) {
UserChatService cs = appContext.getService(UserChatService.class);
cs.showUserChat(userData);
}
me.consume();
});
}
/**
* 设置群头像的点击事件
*
* @author: XiaHui
* @param head
* @param group
* @createDate: 2017年6月9日 上午11:12:22
* @update: XiaHui
* @updateDate: 2017年6月9日 上午11:12:22
*/
protected void setGroupHeadEvent(HeadItem head, Group group) {
head.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent e) {
PersonalBox pb = appContext.getBox(PersonalBox.class);
boolean isOwner = pb.isOwner(group.getId());
gcm.showEdit(isOwner);
gcm.setGroup(group);
gcm.show(mainFrame, e.getScreenX(), e.getScreenY());
e.consume();
}
});
head.setOnMouseClicked((MouseEvent me) -> {
if (me.getClickCount() == 2) {
GroupChatService cs = appContext.getService(GroupChatService.class);
cs.showGroupChat(group);
} else if (me.getButton() == MouseButton.SECONDARY) {
// gcm.setGroup(group);
// gcm.show(mainFrame, me.getScreenX(), me.getScreenY());
}
me.consume();
});
}
public void setChatUserDataHeadEvent(ChatItem head) {
head.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent e) {
e.consume();
}
});
head.setOnMouseClicked((MouseEvent me) -> {
me.consume();
if (me.getClickCount() == 1) {
UserData userData = head.getAttribute(UserData.class);
UserChatService cs = this.appContext.getService(UserChatService.class);
cs.removeUserPrompt(userData.getId());
}
});
}
public void setChatGroupHeadEvent(ChatItem head) {
head.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent e) {
e.consume();
}
});
head.setOnMouseClicked((MouseEvent me) -> {
me.consume();
if (me.getClickCount() == 1) {
Group group = head.getAttribute(Group.class);
GroupChatService cs = appContext.getService(GroupChatService.class);
cs.removeGroupPrompt(group.getId());;
}
});
}
public void setUserHeadEvent(SimpleListItem head) {
head.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent e) {
e.consume();
}
});
head.setOnMouseClicked((MouseEvent me) -> {
me.consume();
if (me.getClickCount() == 2) {
UserData userData = head.getAttribute(UserData.class);
UserChatService cs = this.appContext.getService(UserChatService.class);
cs.showUserChat(userData);
}
});
}
public void setGroupHeadEvent(LastItem head) {
head.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent e) {
e.consume();
}
});
head.setOnMouseClicked((MouseEvent me) -> {
if (me.getClickCount() == 2) {
Group group = head.getAttribute(Group.class);
GroupChatService cs = appContext.getService(GroupChatService.class);
cs.showGroupChat(group);
}
me.consume();
});
}
public void setUserDataHeadEvent(LastItem head) {
head.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent e) {
e.consume();
}
});
head.setOnMouseClicked((MouseEvent me) -> {
me.consume();
if (me.getClickCount() == 2) {
UserData userData = head.getAttribute(UserData.class);
UserChatService cs = this.appContext.getService(UserChatService.class);
cs.showUserChat(userData);
}
});
}
/**
* Creates a new viewer instance.
*
* @param chart the chart ({@code null} permitted).
* @param contextMenuEnabled enable the context menu?
*/
public ChartViewer(JFreeChart chart, boolean contextMenuEnabled) {
this.canvas = new ChartCanvas(chart);
this.canvas.setTooltipEnabled(true);
this.canvas.addMouseHandler(new ZoomHandlerFX("zoom", this));
setFocusTraversable(true);
getChildren().add(this.canvas);
this.zoomRectangle = new Rectangle(0, 0, new Color(0, 0, 1, 0.25));
this.zoomRectangle.setManaged(false);
this.zoomRectangle.setVisible(false);
getChildren().add(this.zoomRectangle);
this.contextMenu = createContextMenu();
setOnContextMenuRequested((ContextMenuEvent event) -> {
contextMenu.show(ChartViewer.this.getScene().getWindow(),
event.getScreenX(), event.getScreenY());
});
this.contextMenu.setOnShowing(
e -> ChartViewer.this.getCanvas().setTooltipEnabled(false));
this.contextMenu.setOnHiding(
e -> ChartViewer.this.getCanvas().setTooltipEnabled(true));
}
private void updateContextMenu(final ContextMenuEvent event)
{
final ContextMenu menu = channel_table.getContextMenu();
final List<ChannelInfo> selection = channel_table.getSelectionModel().getSelectedItems();
if (selection.isEmpty())
menu.getItems().clear();
else
{
menu.getItems().setAll(new AddToPlotAction(channel_table, model, undo, selection),
new SeparatorMenuItem());
SelectionService.getInstance().setSelection(channel_table, selection);
ContextMenuHelper.addSupportedEntries(channel_table, menu);
menu.show(channel_table.getScene().getWindow(), event.getScreenX(), event.getScreenY());
}
}
public void addOrUpdateUserCategory(UserCategory userCategory) {
Platform.runLater(new Runnable() {
@Override
public void run() {
String userCategoryId = userCategory.getId();
ListNodePanel node = userListNodeMap.get(userCategoryId);
if (null == node) {
node = new ListNodePanel();
userListNodeMap.put(userCategoryId, node);
}
node.setText(userCategory.getName());
// node.setNumberText("[0/0]");
node.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent event) {
ucmv.setUserCategory(userCategory);
ucmv.show(mainFrame, event.getScreenX(), event.getScreenY());
event.consume();
}
});
userRoot.addNode(node);
}
});
}
public void setUserCategoryNodeEvent(ListNodePane node) {
node.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent event) {
UserCategory userCategory = (UserCategory) node.getUserData();
ucmv.setUserCategory(userCategory);
ucmv.show(node, event.getScreenX(), event.getScreenY());
event.consume();
}
});
}
public void setGroupCategoryNodeEvent(ListNodePane node) {
node.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent event) {
GroupCategory groupCategory = (GroupCategory) node.getUserData();
gcmv.setGroupCategory(groupCategory);
gcmv.show(node, event.getScreenX(), event.getScreenY());
event.consume();
}
});
}
@FXML
public void createTreeContextMenu(ContextMenuEvent e) {
final ObservableList<TreeItem<File>> selectedItems = treeView.selectionModelProperty().getValue().getSelectedItems();
List<File> selectedFiles = selectedItems.stream().map(item -> {
return item.getValue();
}).collect(Collectors.toList());
createContextMenu(e, selectedFiles, treeView);
}
public UICanvasEditor(@NotNull Resolution resolution, @NotNull UICanvasConfiguration configuration, @NotNull UINode rootNode) {
super(resolution, rootNode);
setConfig(configuration);
gc.setTextBaseline(VPos.CENTER);
this.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
@Override
public void handle(ContextMenuEvent event) {
if (contextMenu != null) {
Point2D p = getCanvas().localToScreen(contextMenuPosition.getX(), contextMenuPosition.getY());
contextMenu.show(getCanvas(), p.getX(), p.getY());
}
}
});
absRegionComponent = new ArmaAbsoluteBoxComponent(resolution);
selection.selected.addListener(new ListChangeListener<UINode>() {
@Override
public void onChanged(Change<? extends UINode> c) {
requestPaint();
}
});
getTimer().getRunnables().add(new Runnable() {
@Override
public void run() {
prepaint();
}
});
initializeSelectionEffect();
}
public void setPopupMenu(UIPopupMenu popupMenu) {
this.popupMenu = popupMenu;
this.getControl().setOnContextMenuRequested(this.popupMenu != null ? new EventHandler<ContextMenuEvent>() {
public void handle(ContextMenuEvent event) {
showPopupMenu(event.getScreenX(), event.getScreenY());
}
} : null);
}
private void showContextMenu(ContextMenuEvent e) {
view.requestFocus();
if ( view.isContextMenuPresent() ) {
ContextMenu menu = view.getContextMenu();
double x = e.getScreenX() + view.getContextMenuXOffset();
double y = e.getScreenY() + view.getContextMenuYOffset();
menu.show( view, x, y );
}
}
private void createContextMenu(ContextMenuEvent e,
final List<File> selectedItems,
Control control) {
contextMenu.getItems().clear();
if (!selectedItems.isEmpty()) {
open.setOnAction(event -> {
selectedItems.forEach(item -> {
openResource(item, null);
});
});
copy.setOnAction(event -> {
final ClipboardContent content = new ClipboardContent();
content.putString(selectedItems.stream()
.map(f -> {
return f.getPath();
})
.collect(Collectors.joining(System.getProperty("line.separator"))));
Clipboard.getSystemClipboard().setContent(content);
});
contextMenu.getItems().add(copy);
contextMenu.getItems().add(open);
}
// If just one entry selected, check if there are multiple apps from which to select
if (selectedItems.size() == 1) {
final File file = selectedItems.get(0);
final URI resource = ResourceParser.getURI(file);
final List<AppResourceDescriptor> applications = ApplicationService.getApplications(resource);
if (applications.size() > 0) {
openWith.getItems().clear();
for (AppResourceDescriptor app : applications) {
final MenuItem open_app = new MenuItem(app.getDisplayName());
final URL icon_url = app.getIconURL();
if (icon_url != null)
open_app.setGraphic(new ImageView(icon_url.toExternalForm()));
open_app.setOnAction(event -> app.create(resource));
openWith.getItems().add(open_app);
}
contextMenu.getItems().add(openWith);
}
}
contextMenu.show(control.getScene().getWindow(), e.getScreenX(), e.getScreenY());
}
private void showContextMenu(final ContextMenuEvent event)
{
final ContextMenu menu = new ContextMenu();
final ObservableList<MenuItem> items = menu.getItems();
// If this pane is empty, offer 'name', 'un-lock', 'close' in context menu
if (getTabs().isEmpty())
{
// Always possible to name a pane
items.add(new NamePaneMenuItem(this));
// If 'fixed', offer menu to un-lock.
// Happens if content of a locked pane failed to load,
// leaving an empty, locked pane to which nothing can be added until unlocked.
if (isFixed())
items.add(new UnlockMenuItem(this));
else
{
// Not fixed, but empty.
// Offer 'close', if possible.
if (dock_parent instanceof SplitDock &&
((SplitDock) dock_parent).canMerge())
{
final MenuItem close = new MenuItem(Messages.DockClose, new ImageView(close_icon));
close.setOnAction(evt ->
{
if (!getName().isBlank())
{
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.initOwner(dock_parent.getScene().getWindow());
alert.setTitle(Messages.DockCloseNamedPaneTitle);
alert.setContentText(MessageFormat.format(Messages.DockCloseNamedPaneText, getName()));
alert.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO);
DialogHelper.positionDialog(alert, this, 0, 0);
alert.showAndWait().ifPresent(type -> {
if (type == ButtonType.NO)
return;
setName("");
});
}
mergeEmptyAnonymousSplit();
});
items.addAll(new SeparatorMenuItem(), close);
}
}
}
if (! items.isEmpty())
menu.show(getScene().getWindow(), event.getScreenX(), event.getScreenY());
}
/**
* Creates a new viewer instance.
*
* @param chart the org.sjwimmer.tacharting.chart ({@code null} permitted).
* @param contextMenuEnabled enable the context menu?
*/
public TaChartViewer(JFreeChart chart, boolean contextMenuEnabled) {
this.canvas = new TaChartCanvas(chart);
this.canvas.setTooltipEnabled(true);
this.canvas.addMouseHandler(new TaZoomHandlerFX("zoom", this));
setFocusTraversable(true);
getChildren().add(this.canvas);
this.zoomRectangle = new Rectangle(0, 0, new Color(0, 0, 1, 0.5));
this.zoomRectangle.setManaged(false);
this.zoomRectangle.setVisible(false);
getChildren().add(this.zoomRectangle);
this.contextMenu = createContextMenu();
setOnContextMenuRequested((ContextMenuEvent event) -> {
contextMenu.show(TaChartViewer.this.getScene().getWindow(),
event.getScreenX(), event.getScreenY());
});
getContextMenu().setOnShowing(
e -> TaChartViewer.this.getCanvas().setTooltipEnabled(false));
getContextMenu().setOnHiding(
e -> TaChartViewer.this.getCanvas().setTooltipEnabled(true));
this.xCrosshair = new Line(0,0,this.getPrefWidth(),0);
this.yCrosshair = new Line(0,0,0,this.getPrefHeight());
this.xCrosshair.setMouseTransparent(true);
this.yCrosshair.setMouseTransparent(true);
this.getChildren().add(xCrosshair);
this.getChildren().add(yCrosshair);
this.xLabel = new Label("");
this.yLabel = new Label("");
this.yLabel.setMouseTransparent(true);
this.xLabel.setMouseTransparent(true);
this.getChildren().add(xLabel);
this.getChildren().add(yLabel);
/**Custom Mouse Listener for the CrosshairOverlay */
this.setOnMouseMoved( e ->{
final double x = e.getX();
final double y = e.getY();
Rectangle2D dataArea = getCanvas().getRenderingInfo().getPlotInfo().getDataArea();
if(x > dataArea.getMinX() && y > dataArea.getMinY() && x < dataArea.getMaxX() && y < dataArea.getMaxY()) {
setCrosshairVisible(true);
CombinedDomainXYPlot combinedDomainXYPlot = (CombinedDomainXYPlot) getCanvas().getChart().getPlot();
XYPlot plot = (XYPlot) combinedDomainXYPlot.getSubplots().get(0);
org.jfree.chart.axis.ValueAxis xAxis = plot.getDomainAxis();
RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
xCrosshair.setStartY(dataArea.getMinY());
xCrosshair.setStartX(x);
xCrosshair.setEndY(dataArea.getMaxY());
xCrosshair.setEndX(x);
xLabel.setLayoutX(x);
xLabel.setLayoutY(dataArea.getMinY());
double value = xAxis.java2DToValue(e.getX(), dataArea, xAxisEdge);
long itemLong = (long) (value);
Date itemDate = new Date(itemLong);
xLabel.setText(String.valueOf(new SimpleDateFormat().format(itemDate)));
org.jfree.chart.axis.ValueAxis yAxis = plot.getRangeAxis();
RectangleEdge yAxisEdge = plot.getRangeAxisEdge();
Rectangle2D subDataArea = getCanvas().getRenderingInfo().getPlotInfo().getSubplotInfo(0).getDataArea();
yCrosshair.setStartY(y);
yCrosshair.setStartX(dataArea.getMinX());
yCrosshair.setEndX(dataArea.getMaxX());
yCrosshair.setEndY(y);
yLabel.setLayoutY(y);
yLabel.setLayoutX(dataArea.getMinX());
String yValue = CalculationUtils.roundToString(yAxis.java2DToValue(y, subDataArea, yAxisEdge), 2);
yLabel.setText(yValue);
} else {
setCrosshairVisible(false);
}
});
}
private void initialize() {
this.getStyleClass().add(DEFAULT_STYLE_CLASS);
expanded.addListener((o, oldVal, newVal) -> {
if (newVal) {
expand();
} else {
collapse();
}
});
verticalGap.addListener((o, oldVal, newVal) -> {
if (isExpanded()) {
expand();
} else {
collapse();
}
});
// handle selection model on the list ( FOR NOW : we only support single selection on the list if it contains sublists)
sublistsProperty.get().addListener((ListChangeListener.Change<? extends JFXListView<?>> c) -> {
while (c.next()) {
if (c.wasAdded() || c.wasUpdated() || c.wasReplaced()) {
if (sublistsProperty.get().size() == 1) {
this.getSelectionModel()
.selectedItemProperty()
.addListener((o, oldVal, newVal) -> clearSelection(this));
// prevent selecting the sublist item by clicking the right mouse button
this.addEventFilter(ContextMenuEvent.CONTEXT_MENU_REQUESTED, Event::consume);
}
c.getAddedSubList()
.forEach(item -> item.getSelectionModel()
.selectedItemProperty()
.addListener((o, oldVal, newVal) -> clearSelection(item)));
}
}
});
// listen to index changes
this.getSelectionModel().selectedIndexProperty().addListener((o, oldVal, newVal) -> {
if (newVal.intValue() != -1) {
updateOverAllSelectedIndex();
}
});
}
public MarkdownEditorPane() {
textArea = new MarkdownTextArea();
textArea.setWrapText(true);
textArea.setUseInitialStyleForInsertion(true);
textArea.getStyleClass().add("markdown-editor");
textArea.getStylesheets().add("org/markdownwriterfx/editor/MarkdownEditor.css");
textArea.getStylesheets().add("org/markdownwriterfx/prism.css");
textArea.textProperty().addListener((observable, oldText, newText) -> {
textChanged(newText);
});
textArea.addEventHandler(ContextMenuEvent.CONTEXT_MENU_REQUESTED, this::showContextMenu);
textArea.addEventHandler(MouseEvent.MOUSE_PRESSED, this::hideContextMenu);
textArea.setOnDragEntered(this::onDragEntered);
textArea.setOnDragExited(this::onDragExited);
textArea.setOnDragOver(this::onDragOver);
textArea.setOnDragDropped(this::onDragDropped);
smartEdit = new SmartEdit(this, textArea);
Nodes.addInputMap(textArea, sequence(
consume(keyPressed(PLUS, SHORTCUT_DOWN), this::increaseFontSize),
consume(keyPressed(MINUS, SHORTCUT_DOWN), this::decreaseFontSize),
consume(keyPressed(DIGIT0, SHORTCUT_DOWN), this::resetFontSize),
consume(keyPressed(W, ALT_DOWN), this::showWhitespace),
consume(keyPressed(I, ALT_DOWN), this::showImagesEmbedded)
));
// create scroll pane
VirtualizedScrollPane<MarkdownTextArea> scrollPane = new VirtualizedScrollPane<>(textArea);
// create border pane
borderPane = new BottomSlidePane(scrollPane);
overlayGraphicFactory = new ParagraphOverlayGraphicFactory(textArea);
textArea.setParagraphGraphicFactory(overlayGraphicFactory);
updateFont();
updateShowLineNo();
updateShowWhitespace();
// initialize properties
markdownText.set("");
markdownAST.set(parseMarkdown(""));
// find/replace
findReplacePane = new FindReplacePane(textArea);
findHitsChangeListener = this::findHitsChanged;
findReplacePane.addListener(findHitsChangeListener);
findReplacePane.visibleProperty().addListener((ov, oldVisible, newVisible) -> {
if (!newVisible)
borderPane.setBottom(null);
});
// listen to option changes
optionsListener = e -> {
if (textArea.getScene() == null)
return; // editor closed but not yet GCed
if (e == Options.fontFamilyProperty() || e == Options.fontSizeProperty())
updateFont();
else if (e == Options.showLineNoProperty())
updateShowLineNo();
else if (e == Options.showWhitespaceProperty())
updateShowWhitespace();
else if (e == Options.showImagesEmbeddedProperty())
updateShowImagesEmbedded();
else if (e == Options.markdownRendererProperty() || e == Options.markdownExtensionsProperty()) {
// re-process markdown if markdown extensions option changes
parser = null;
textChanged(textArea.getText());
}
};
WeakInvalidationListener weakOptionsListener = new WeakInvalidationListener(optionsListener);
Options.fontFamilyProperty().addListener(weakOptionsListener);
Options.fontSizeProperty().addListener(weakOptionsListener);
Options.markdownRendererProperty().addListener(weakOptionsListener);
Options.markdownExtensionsProperty().addListener(weakOptionsListener);
Options.showLineNoProperty().addListener(weakOptionsListener);
Options.showWhitespaceProperty().addListener(weakOptionsListener);
Options.showImagesEmbeddedProperty().addListener(weakOptionsListener);
// workaround a problem with wrong selection after undo:
// after undo the selection is 0-0, anchor is 0, but caret position is correct
// --> set selection to caret position
textArea.selectionProperty().addListener((observable,oldSelection,newSelection) -> {
// use runLater because the wrong selection temporary occurs while edition
Platform.runLater(() -> {
IndexRange selection = textArea.getSelection();
int caretPosition = textArea.getCaretPosition();
if (selection.getStart() == 0 && selection.getEnd() == 0 && textArea.getAnchor() == 0 && caretPosition > 0)
textArea.selectRange(caretPosition, caretPosition);
});
});
}
private void showContextMenu(ContextMenuEvent event) {
MenuItem item = new MenuItem("全て除去");
item.setOnAction(this::removeAll);
ContextMenu contextMenu = new ContextMenu(item);
contextMenu.show(this.getScene().getWindow(), event.getScreenX(), event.getScreenY());
}
@FXML
public void createListContextMenu(ContextMenuEvent e) {
final ObservableList<File> selectedItems = listView.selectionModelProperty().getValue().getSelectedItems();
createContextMenu(e, selectedItems, listView);
}