At present, you often need to bind hosts to perform related tests. The path of hosts is usually fixed. How can I change hosts quickly? Here the use of JavaFX to do a hosts management tool, currently can achieve IP highlighting, IP automatic detection, listening Ctrl+S to save.
Download experience:
Technology stack
- JavaFX
- RichTextEditor
- Java 1.8 +
support
- Highlight IP, comments, and so on
- Support Ctrl+S for saving
To support
- Multiple hosts configurations are supported
- Update the remote hosts file
- Exe file installation in Windows
Part of the implementation
IP comments are highlighted
Here, the re is used to identify IP and comments, and the RichTextEditor is used to display them. JavaFX can load CSS files
codeArea = new CodeArea(); codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea)); codeArea.richChanges() .filter(ch -> ! ch.getInserted().equals(ch.getRemoved())) // XXX .successionEnds(Duration.ofMillis(500)) .supplyTask(this::computeHighlightingAsync) .awaitLatest(codeArea.richChanges()) .filterMap(t -> { if(t.isSuccess()) { return Optional.of(t.get()); } else { t.getFailure().printStackTrace(); return Optional.empty(); } }) .subscribe(this::applyHighlighting); codeArea.replaceText(0, 0, HostsUtil.getHostsContent());Copy the code
Use regex to figure out what to highlight and which styles to use
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"; private static final String COMMENT_PATTERN = "#[^\n]*"; private static final Pattern PATTERN = Pattern.compile( "(?<KEYWORD>" + KEYWORD_PATTERN + ")" + "|(?<COMMENT>" + COMMENT_PATTERN + ")" ); private static StyleSpans<Collection<String>> computeHighlighting(String text) { Matcher matcher = PATTERN.matcher(text); int lastKwEnd = 0; StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>(); while(matcher.find()) { String styleClass = matcher.group("KEYWORD") ! = null ? "keyword" : matcher.group("COMMENT") ! = null ? "comment" : null; /* never happens */ assert styleClass ! = null; spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd); spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start()); lastKwEnd = matcher.end(); } spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd); return spansBuilder.create(); }Copy the code
Dynamic real-time highlighting
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);
}
Copy the code
Loading the CSS File
Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(codeArea)), 600, 400);
scene.getStylesheets().add(HostsEditor.class.getResource("hosts-keywords.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("JHosts Manager");
primaryStage.show();
Copy the code
Added shortcut key listening
Shortcuts that you understand, you can use here to make quick changes
codeArea.setOnKeyPressed(new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent event) { if (keyCombinationCtrlS.match(event)) { Boolean ret = HostsUtil.saveHostsContent(codeArea.getText()); if(ret == true){ System.out.println("save success!" ); codeArea.setStyle("-fx-border-color: green;" ); } else { System.out.println("save error!" ); codeArea.setStyle("-fx-border-color: red;" ); }}}});Copy the code
Add ICONS for different computers
Add icon is JavaFX provided by the function, the current MAC needs special processing, code below
if(SystemUtils.IS_OS_MAC){
primaryStage.getIcons().addAll(new Image("file:icon.icns"));
} else {
primaryStage.getIcons().addAll(new Image("file:icon.png"));
}
Copy the code
conclusion
The above is the implementation of JHosts Manager. If you want to run it, you can download it from the above code repository and use Maven to build it
Related articles
- Reverse analysis of an APP and use Java and PHP languages to achieve RC4 encryption and decryption
- How to solve the problem of Windows disk overflowing but do not know how to clean up
- Use ping, NSLookup, route, traceroute, MTR and other tools to troubleshoot network faults
- JMeter was used for stress tests involving signature encryption and random generation of special request parameters
- CentOS_MINI configuration solution 3_ replacing the update source _ installing common software _ creating management users _ service startup configuration
- CentOS_MINI configuration solution 2_ configuring nics
- CentOS_MINI Configuration Scheme 1 disable SELinux
- Yaf framework introduction to PHP framework