Skip to content

Commit 1e4b934

Browse files
committed
修改说明文件。
1 parent fff33dd commit 1e4b934

6 files changed

Lines changed: 106 additions & 22 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
**GitHub地址:**[xJavaFxTool](https://github.com/864381832/xJavaFxTool)
55

6+
**腾讯云开发平台地址:**[xJavaFxTool](https://dev.tencent.com/u/xwintop/p/xJavaFxTool)
7+
68
**xJavaFxTool交流QQ群:== [387473650](https://jq.qq.com/?_wv=1027&k=59UDEAD) ==**
79

810
xJavaFxTool是使用javaFx开发的实用小工具集,目前项目刚刚建立,利用业余时间把工作中遇到的一些问题总结起来,打包进小工具集中,供大家参考和使用,里面包含了javaFx的一些功能的示例,如布局、国际化、第三方UI库([controlsfx](http://fxexperience.com/controlsfx/)[JFoenix](http://www.jfoenix.com/)等)、外部jar包加载(插件机制)等一些常用功能,想学习javaFx的同学可以参考参考,学习javaFx的资料参考[www.javafxchina.net](http://www.javafxchina.net/main/)

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@
218218
<version>1.0.3</version>
219219
</dependency>
220220

221+
<!--语法高亮工具-->
222+
<dependency>
223+
<groupId>org.fxmisc.richtext</groupId>
224+
<artifactId>richtextfx</artifactId>
225+
<version>0.10.1</version>
226+
</dependency>
227+
221228
<dependency>
222229
<groupId>org.apache.rocketmq</groupId>
223230
<artifactId>rocketmq-client</artifactId>

src/main/java/com/xwintop/xJavaFxTool/controller/debugTools/SwitchHostsToolController.java

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22

33
import com.xwintop.xJavaFxTool.services.debugTools.SwitchHostsToolService;
44
import com.xwintop.xJavaFxTool.view.debugTools.SwitchHostsToolView;
5-
6-
import java.net.URL;
7-
import java.util.ResourceBundle;
8-
5+
import javafx.concurrent.Task;
96
import javafx.event.ActionEvent;
107
import javafx.fxml.FXML;
118
import javafx.scene.control.TreeItem;
129
import lombok.Getter;
1310
import lombok.Setter;
1411
import lombok.extern.slf4j.Slf4j;
12+
import org.fxmisc.richtext.CodeArea;
13+
import org.fxmisc.richtext.LineNumberFactory;
14+
import org.fxmisc.richtext.model.StyleSpans;
15+
import org.fxmisc.richtext.model.StyleSpansBuilder;
16+
17+
import java.net.URL;
18+
import java.time.Duration;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.Optional;
22+
import java.util.ResourceBundle;
23+
import java.util.concurrent.ExecutorService;
24+
import java.util.concurrent.Executors;
25+
import java.util.regex.Matcher;
26+
import java.util.regex.Pattern;
27+
1528
/**
1629
* @ClassName: SwitchHostsToolController
1730
* @Description: 切换Hosts工具
@@ -23,22 +36,43 @@
2336
@Setter
2437
@Slf4j
2538
public class SwitchHostsToolController extends SwitchHostsToolView {
26-
2739
private SwitchHostsToolService switchHostsToolService = new SwitchHostsToolService(this);
2840

41+
private static final String KEYWORD_PATTERN = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}\\b";
42+
private static final String COMMENT_PATTERN = "#[^\n]*";
43+
private static final Pattern PATTERN = Pattern.compile("(?<KEYWORD>" + KEYWORD_PATTERN + ")" + "|(?<COMMENT>" + COMMENT_PATTERN + ")");
44+
private ExecutorService executor;
45+
2946
@Override
3047
public void initialize(URL location, ResourceBundle resources) {
3148
try {
3249
initView();
3350
initEvent();
3451
initService();
3552
} catch (Exception e) {
36-
e.printStackTrace();
37-
log.error(e.getMessage());
53+
log.error("加载报错", e);
3854
}
3955
}
4056

4157
private void initView() {
58+
executor = Executors.newSingleThreadExecutor();
59+
hostTextArea = new CodeArea();
60+
hostTextArea.setParagraphGraphicFactory(LineNumberFactory.get(hostTextArea));
61+
hostTextArea.richChanges()
62+
.filter(ch -> !ch.getInserted().equals(ch.getRemoved())) // XXX
63+
.successionEnds(Duration.ofMillis(500))
64+
.supplyTask(this::computeHighlightingAsync)
65+
.awaitLatest(hostTextArea.richChanges())
66+
.filterMap(t -> {
67+
if(t.isSuccess()) {
68+
return Optional.of(t.get());
69+
} else {
70+
t.getFailure().printStackTrace();
71+
return Optional.empty();
72+
}
73+
})
74+
.subscribe(this::applyHighlighting);
75+
4276
TreeItem<String> treeItem = new TreeItem<String>("Hosts");
4377
treeItem.setExpanded(true);
4478
hostFileTreeView.setRoot(treeItem);
@@ -82,4 +116,38 @@ private void editAction(ActionEvent event) throws Exception {
82116
@FXML
83117
private void deleteAction(ActionEvent event) {
84118
}
119+
120+
private Task<StyleSpans<Collection<String>>> computeHighlightingAsync() {
121+
String text = hostTextArea.getText();
122+
Task<StyleSpans<Collection<String>>> task = new Task<StyleSpans<Collection<String>>>() {
123+
@Override
124+
protected StyleSpans<Collection<String>> call() throws Exception {
125+
return computeHighlighting(text);
126+
}
127+
};
128+
executor.execute(task);
129+
return task;
130+
}
131+
132+
private void applyHighlighting(StyleSpans<Collection<String>> highlighting) {
133+
hostTextArea.setStyleSpans(0, highlighting);
134+
}
135+
136+
private static StyleSpans<Collection<String>> computeHighlighting(String text) {
137+
Matcher matcher = PATTERN.matcher(text);
138+
int lastKwEnd = 0;
139+
StyleSpansBuilder<Collection<String>> spansBuilder
140+
= new StyleSpansBuilder<>();
141+
while(matcher.find()) {
142+
String styleClass =
143+
matcher.group("KEYWORD") != null ? "keyword" :
144+
matcher.group("COMMENT") != null ? "comment" :
145+
null; /* never happens */ assert styleClass != null;
146+
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
147+
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
148+
lastKwEnd = matcher.end();
149+
}
150+
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
151+
return spansBuilder.create();
152+
}
85153
}

src/main/java/com/xwintop/xJavaFxTool/services/debugTools/SwitchHostsToolService.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.sun.jna.Platform;
44
import com.xwintop.xJavaFxTool.controller.debugTools.SwitchHostsToolController;
5-
import com.xwintop.xcore.util.SystemInfoUtil;
65
import com.xwintop.xcore.util.javafx.TooltipUtil;
76
import lombok.Getter;
87
import lombok.Setter;
@@ -26,28 +25,22 @@ public class SwitchHostsToolService {
2625

2726
private String commonHostString = "# common\n" +
2827
"# 这儿是公用 hosts,其内容会插入到各个方案最前面";
29-
private String systemHostString = "";
28+
// private String systemHostString = "";
3029
private String localHost1String = "# 方案一\n" +
3130
"# 什么也没有绑定\n" +
3231
"#\n" +
3332
"# 在这儿输入你需要绑定的 hosts";
3433
private String localHost2String = "# 方案二\n";
3534

3635
public void reloadSystemHosts() throws Exception {
37-
String fileName = null;
38-
if (Platform.isWindows()) {
39-
fileName = "C://WINDOWS//system32//drivers//etc//hosts";
40-
} else {
41-
fileName = "/etc/hosts";
42-
}
43-
// String fileName = SystemInfoUtil.getHostsFilePath();
44-
String systemHostString = FileUtils.readFileToString(new File(fileName), "utf-8");
45-
switchHostsToolController.getHostTextArea().setText(systemHostString);
46-
36+
String fileName = this.getHostsFilePath();
37+
String systemHostString = FileUtils.readFileToString(new File(fileName));
38+
// switchHostsToolController.getHostTextArea().setText(systemHostString);
39+
switchHostsToolController.getHostTextArea().replaceText(0, 0, systemHostString);
4740
}
4841

4942
public void editAction() throws Exception {
50-
String fileName = SystemInfoUtil.getHostsFilePath();
43+
String fileName = this.getHostsFilePath();
5144
String systemHostString = switchHostsToolController.getHostTextArea().getText();
5245
FileUtils.writeByteArrayToFile(new File(fileName), systemHostString.getBytes());
5346
TooltipUtil.showToast("保存配置成功");
@@ -56,4 +49,14 @@ public void editAction() throws Exception {
5649
public SwitchHostsToolService(SwitchHostsToolController switchHostsToolController) {
5750
this.switchHostsToolController = switchHostsToolController;
5851
}
52+
53+
public String getHostsFilePath() {
54+
String fileName = null;
55+
if (Platform.isWindows()) {
56+
fileName = "C://WINDOWS//system32//drivers//etc//hosts";
57+
} else {
58+
fileName = "/etc/hosts";
59+
}
60+
return fileName;
61+
}
5962
}

src/main/java/com/xwintop/xJavaFxTool/view/debugTools/SwitchHostsToolView.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javafx.scene.control.TreeView;
88
import lombok.Getter;
99
import lombok.Setter;
10+
import org.fxmisc.richtext.CodeArea;
1011

1112
/**
1213
* @ClassName: SwitchHostsToolView
@@ -29,6 +30,7 @@ public abstract class SwitchHostsToolView implements Initializable {
2930
@FXML
3031
protected TreeView<String> hostFileTreeView;
3132
@FXML
32-
protected TextArea hostTextArea;
33+
// protected TextArea hostTextArea;
34+
protected CodeArea hostTextArea;
3335

3436
}

src/main/resources/com/xwintop/xJavaFxTool/fxmlView/debugTools/SwitchHostsTool.fxml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
<?import javafx.geometry.Insets?>
44
<?import javafx.scene.control.Button?>
55
<?import javafx.scene.control.TextArea?>
6+
<?import org.fxmisc.richtext.CodeArea?>
67
<?import javafx.scene.control.TreeView?>
78
<?import javafx.scene.layout.AnchorPane?>
89
<?import javafx.scene.layout.BorderPane?>
910
<?import javafx.scene.layout.HBox?>
1011
<?import javafx.scene.layout.VBox?>
1112

1213

14+
<?import org.fxmisc.richtext.CodeArea?>
1315
<AnchorPane prefHeight="493.0" prefWidth="817.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.xwintop.xJavaFxTool.controller.debugTools.SwitchHostsToolController">
1416
<children>
1517
<BorderPane layoutX="14.0" layoutY="41.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
@@ -32,7 +34,7 @@
3234
</VBox>
3335
</left>
3436
<center>
35-
<TextArea fx:id="hostTextArea" BorderPane.alignment="CENTER" />
37+
<CodeArea fx:id="hostTextArea" BorderPane.alignment="CENTER" />
3638
</center>
3739
</BorderPane>
3840
</children>

0 commit comments

Comments
 (0)