Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.7] Contextmenu not supported by : org.fxmisc.richtext.CodeArea #363

Closed
pierrecochet opened this issue Sep 19, 2016 · 8 comments
Closed

Comments

@pierrecochet
Copy link

pierrecochet commented Sep 19, 2016

Hi,
Since i use the 0.7 the property ContextMenu isn't recognized, whereas it works on 0.6. Do i have any way to fix it, or any altrenative ?

@JordanMartinez
Copy link
Contributor

Just a guess, but I think this is because StyledTextArea does not extend Control but Region in the new release....

@rivagti
Copy link

rivagti commented Dec 15, 2016

@JordanMartinez yeah, you're right. but any idea how to implement it?

@nazmuddin
Copy link

nazmuddin commented Dec 15, 2016

@JordanMartinez , I did it like this in GenericStyledArea

private Popup popup;
    private Button cut;
    private Button copy;
    private Button paste;
    private Button select;

    private EventHandler contextMenu = new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if (event.getButton() == event.getButton().SECONDARY) {
                popup.setAutoFix(false);
                popup.show(area, event.getScreenX(), event.getScreenY());
            } else if (popup.isShowing() && event.getClickCount() == 1) {
                popup.hide();
            }
        }
    };
    
    public RichTextFx() {
        popup = new Popup();
        cut = new Button("Cut");
        cut.setOnAction(e ->{
            area.cut();
            popup.hide();
        });
        copy = new Button("Copy");
        copy.setOnAction(e -> {
            area.copy();
            popup.hide();
        });
        paste = new Button("Paste");
        paste.setOnAction(e ->{
            area.paste();
            popup.hide();
        });
        select = new Button("Select All");
        select.setOnAction(e->{
            area.selectAll();
            popup.hide();
        });
        VBox box = new VBox();
        box.setId("popup"); 
        box.getStylesheets().add(getClass().getResource("context-menu.css").toExternalForm());
        box.setPrefSize(80, 50);
        box.getChildren().addAll(cut, copy, paste, select);

        popup.getContent().add(box);
        area.setOnMouseClicked(contextMenu);
   }

Here is context-menu.css

.button{
    -fx-focus-color: transparent;
    -fx-faint-focus-color: transparent;
    -fx-background-color : transparent;
}

#popup {
    -fx-background-color : ghostwhite;
}

area is instance of GenericStyledArea. Hope it helps.

@JordanMartinez
Copy link
Contributor

@rivagti See @nazmuddin's example in combination with mine below. He uses a Popup whereas I use a ContextMenu. He uses setOn[EventType] whereas I use InputMap. The two should give you a good enough idea for how to resolve this issue:

package org.fxmisc.richtext.demo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import org.fxmisc.richtext.InlineCssTextArea;
import org.fxmisc.wellbehaved.event.EventPattern;
import org.fxmisc.wellbehaved.event.InputMap;
import org.fxmisc.wellbehaved.event.Nodes;

public class ContextMenuExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        // create your context menu somewhere in your code...
        ContextMenu menu = new ContextMenu();
        menu.getItems().addAll(
                new MenuItem("Menu 1"),
                new MenuItem("Menu 2"),
                new MenuItem("Menu 3")
        );

        InlineCssTextArea area = new InlineCssTextArea("Some text");

        // add an input mapping that shows that context menu when you 
        //  right click somewhere in the area
        Nodes.addInputMap(area, 
                InputMap.consume(
                    EventPattern.mouseClicked(MouseButton.SECONDARY),
                    e -> {
                        // show the area using the mouse event's screen x & y values
                        menu.show(area, e.getScreenX(), e.getScreenY());
                    }
                )
        );

        primaryStage.setScene(new Scene(area, 500, 500));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

@JordanMartinez
Copy link
Contributor

Should this feature be supported out-of-box?
We could add these methods in StyledTextArea:

private ContextMenu contextMenu = null
public final Optional<ContextMenu> getContextMenu() {
    return Optional.ofNullable(contextMenu);
}
public final void setContextMenu(ContextMenu menu) {
    contextMenu = menu;
}

private double contextMenuOffsetX;
public final double getContextMenuOffsetX() { return contextMenuOffsetX; }
public final void setContextMenuOffsetX(double xOffset) { contextMenuOffsetX = xOffset; }

// same for yOffset

and add this handler to StyledTextAreaBehavior

InputMapTemplate.consumeUnless(
    mouseClicked(SECONDARY),
    () -> !area.getContextMenu().isPresent(),
    e -> {
            double xOffset = area.getContextMenuOffsetX();
            double yOffset = area.getContextMenuOffsetY();
            area.getContextMenu().show(area, e.getScreenX() + xOffset, e.getScreenY() + yOffset);
        }
}

@rivagti
Copy link

rivagti commented Dec 16, 2016

@JordanMartinez yes, I think so. anyway thank you both... @nazmuddin

@berry120
Copy link

I ended up here after searching for how to get this working in 0.7, so +1 for supporting this out the box (especially since it's a feature regression from 0.6 otherwise.)

@JordanMartinez
Copy link
Contributor

Closed by #439.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants