JavaFX在WebEngine中禁用突出显示和复制模式

时间:2014-11-20 11:27:45

标签: java javafx javafx-webengine

我从JEditorPane切换到WebEngine(JavaFX) 我曾经在JEditorPane中锁定文本突出显示(选择),如下所示。

my_editor.setEditable(false);
my_editor.getInputMap().put(KeyStroke.getKeyStroke("control C"), "none");

现在我喜欢对WebEngine做同样的事情,我该怎么做?禁用复制,突出显示和编辑模式。感谢。

2 个答案:

答案 0 :(得分:5)

如果要在不使用Javascript的情况下禁用JavaFX的复制,突出显示和编辑,一种方法是捕获事件并相应地处理它们,保留其余选项。

让我们使用事件调度程序来过滤一些事件:

关键事件:

  • 使用Ctrl + C或Ctrl + Insert
  • 避免复制
  • 避免使用shift + Arrow选择

对于鼠标事件:

  • 单击鼠标
  • ,避免选择单词,行,段落
  • 避免使用鼠标拖动进行选择,但允许拖动滚动条

(如果需要,可以添加其他人)

public class WebEventDispatcher implements EventDispatcher {

    private final EventDispatcher oldDispatcher;
    private Point2D limit;

    public WebEventDispatcher(EventDispatcher oldDispatcher) {
        this.oldDispatcher = oldDispatcher;
    }

    public void setLimit(Point2D limit){
        this.limit = limit;
    }

    private boolean allowDrag=false;

    @Override
    public Event dispatchEvent(Event event, EventDispatchChain tail) {
        if (event instanceof MouseEvent){
            MouseEvent m = (MouseEvent)event;
            if (event.getEventType().equals(MouseEvent.MOUSE_CLICKED) || 
                event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
                Point2D origin=new Point2D(m.getX(),m.getY());
                allowDrag=!(origin.getX()<limit.getX() && origin.getY()<limit.getY());
            }
            // avoid selection with mouse dragging, allowing dragging the scrollbars
            if (event.getEventType().equals(MouseEvent.MOUSE_DRAGGED)) {
                if(!allowDrag){
                    event.consume();
                }
            }
            // Avoid selection of word, line, paragraph with mouse click
            if(m.getClickCount()>1){
                event.consume();
            }
        }
        if (event instanceof KeyEvent && event.getEventType().equals(KeyEvent.KEY_PRESSED)){
            KeyEvent k= (KeyEvent)event;
            // Avoid copy with Ctrl+C or Ctrl+Insert
            if((k.getCode().equals(KeyCode.C) || k.getCode().equals(KeyCode.INSERT)) && k.isControlDown()){
                event.consume();
            }
            // Avoid selection with shift+Arrow
            if(k.isShiftDown() && (k.getCode().equals(KeyCode.RIGHT) || k.getCode().equals(KeyCode.LEFT) ||
                k.getCode().equals(KeyCode.UP) || k.getCode().equals(KeyCode.DOWN))){
                event.consume();
            }
        }
        return oldDispatcher.dispatchEvent(event, tail);
    }
}

现在,在您的场景中,禁用上下文菜单以避免复制/粘贴选项,找到没有滚动条的webview的内容区域(如果有),并设置自定义事件调度程序。

private Point2D pLimit;
private double width, height;

@Override
public void start(Stage primaryStage) {
    WebView webView = new WebView();
    WebEngine webEngine = webView.getEngine();

    // disable context menu (copy option)
    webView.setContextMenuEnabled(false);

    WebEventDispatcher webEventDispatcher = new WebEventDispatcher(webView.getEventDispatcher());
    webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {

        @Override
        public void changed(ObservableValue<? extends State> observable, State oldValue, State newValue) {
            if(newValue.equals(State.SUCCEEDED)){
                // dispatch all events
                webView.setEventDispatcher(webEventDispatcher);
            }
        }

    });
    webEngine.load("<URL>");

    Scene scene = new Scene(webView);

    primaryStage.setTitle("WebView scrollbar test");
    primaryStage.setScene(scene);
    primaryStage.show();

    webView.getChildrenUnmodifiable().addListener(new ListChangeListener<Node>() {

        @Override
        public void onChanged(Change<? extends Node> c) {
            pLimit=webView.localToScene(webView.getWidth(),webView.getHeight());
            webView.lookupAll(".scroll-bar").stream()
                    .map(s->(ScrollBar)s).forEach(s->{
                        if(s.getOrientation().equals(VERTICAL)){
                            width=s.getBoundsInLocal().getWidth();
                        }
                        if(s.getOrientation().equals(HORIZONTAL)){
                            height=s.getBoundsInLocal().getHeight();
                        }
                    });
            // dispatch all events
            webEventDispatcher.setLimit(pLimit.subtract(width, height));
        }
    });

}

答案 1 :(得分:0)

您可以使用以下CSS禁用突出显示和复制:

SELECT sys.databases.name, CONVERT(VARCHAR,SUM(size)*8/1024)+' MB' AS [TotalDiskSpace] FROM sys.databases JOIN sys.master_files ON sys.databases.database_id=sys.master_files.database_id GROUP BY sys.databases.name ORDER BY sys.databases.name