简体   繁体   English

Javafx中的RSyntaxTextArea可以替代吗?

[英]Any alternate to RSyntaxTextArea in Javafx?

I am working on a code editor for java which will be used in Parallel Computing and Distributed Computing. 我正在为Java使用代码编辑器,它将在并行计算和分布式计算中使用。 I'm looking for an alternate to RSyntaxTextArea in Javafx, bcz i tried to implement it in Javafx and it is not working well, like sometimes half of the text area doesn't show and cursor lags in the text area. 我正在寻找Javafx中RSyntaxTextArea的替代品,我尝试在Javafx中实现它,但它运行不正常,就像有时一半的文本区域不显示并且光标滞后于文本区域。

Tab textTab = new Tab("Sample Tab");
RSyntaxTextArea ta= new RSyntaxTextArea();
SwingNode sn = new SwingNode();

String text="";
ta.setText(text);
ta.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
ta.setAntiAliasingEnabled(true);
ta.setCodeFoldingEnabled(true);

  RTextScrollPane sp = new RTextScrollPane(ta);
  sn.setContent(sp);
  textTab.setContent(sn);

I am newbie in Javafx, so i don't know much how to fix these issues. 我是Javafx的新手,所以我不知道如何解决这些问题。 It also doesn't match the beauty of Javafx. 它也与Javafx的优势不符。

尝试使用Tomas Mikula的RichTextFX框架中的CodeArea (或更StyleClassedTextAreaStyleClassedTextArea )组件。

i have created my own class from RichTextFX here is the code 我已经从RichTextFX创建了自己的类,这是代码

 import java.time.Duration;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import javafx.concurrent.Task;
 import org.fxmisc.richtext.CodeArea;
 import org.fxmisc.richtext.PlainTextChange;
 import org.fxmisc.richtext.StyleSpans;
 import org.fxmisc.richtext.StyleSpansBuilder;
 import org.reactfx.EventStream;

 /**
  *
  * @author Nika
  */
 public class SyntaxTextArea {

 private static final String[] KEYWORDS = new String[]{
    "abstract", "assert", "boolean", "break", "byte",
    "case", "catch", "char", "class", "const",
    "continue", "default", "do", "double", "else",
    "enum", "extends", "final", "finally", "float",
    "for", "goto", "if", "implements", "import",
    "instanceof", "int", "interface", "long", "native",
    "new", "package", "private", "protected", "public",
    "return", "short", "static", "strictfp", "super",
    "switch", "synchronized", "this", "throw", "throws",
    "transient", "try", "void", "volatile", "while"
 };

 private static final Pattern KEYWORD_PATTERN = Pattern.compile("\\b(" +    String.join("|", KEYWORDS) + ")\\b");

private CodeArea codeArea;
private ExecutorService executor;

public SyntaxTextArea() {

    executor = Executors.newSingleThreadExecutor();
    codeArea = new CodeArea();
    EventStream<PlainTextChange> textChanges = codeArea.plainTextChanges();
    textChanges
            .successionEnds(Duration.ofMillis(500))
            .supplyTask(this::computeHighlightingAsync)
            .awaitLatest(textChanges)
            .subscribe(this::applyHighlighting);

   codeArea.getStylesheets().add(org.fxmisc.richtext.demo.JavaKeywordsAsync.class.getResource("java-keywords.css").toExternalForm());
}

public void setText(String text) {
    codeArea.replaceText(0, 0, text);

}

public String getText() {
  return  codeArea.getText();

}
public void appendText(String text) {
    codeArea.appendText(text);

}

public  CodeArea getNode(){
return codeArea;
}

public void setStyling(){

}
private Task<StyleSpans<Collection<String>>> computeHighlightingAsync() {
    String text = codeArea.getText();
    Task<StyleSpans<Collection<String>>> task = new Task<StyleSpans<Collection<String>>>() {
        @Override
        protected StyleSpans<Collection<String>> call() throws Exception {
            return computeHighlighting(text);
        }
    };
    executor.execute(task);
    return task;
}

private void applyHighlighting(StyleSpans<Collection<String>> highlighting) {
    codeArea.setStyleSpans(0, highlighting);
}

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    Matcher matcher = KEYWORD_PATTERN.matcher(text);
    int lastKwEnd = 0;
    StyleSpansBuilder<Collection<String>> spansBuilder
            = new StyleSpansBuilder<>();
    while (matcher.find()) {
        spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
        spansBuilder.add(Collections.singleton("keyword"), matcher.end() - matcher.start());
        lastKwEnd = matcher.end();
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}

 }

But you have to add JAR file from the RichTextFX in your libraries. 但是您必须从库中的RichTextFX添加JAR文件。 However i don't posses knowledge about css. 但是我不具备关于CSS的知识。 So i couldn't improved it in styling or theming(i wanted to set Monokai Look and feel to syntax Text Area, But don't worry i will find a way someday. Or i somebody do find before me please share.) Thanks all for your suggestions and help especially @James_D. 因此,我无法在样式或主题方面进行改进(我想将Monokai外观设置为Text Area语法,但是请放心,我有一天会找到方法。或者我确实有人在我之前找到了,请分享。)为您的建议和帮助,尤其是@James_D。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM