-
Notifications
You must be signed in to change notification settings - Fork 12
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
522 chat click links #537
Merged
Merged
522 chat click links #537
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
94b2688
generation and searching for uri via regex and replace them with the …
nnice 17bf281
css stuff to make it pretty
nnice ea8fc26
testing if an action event can be fired and the result contains an va…
nnice 7c02c35
some refactoring
nnice 2e193c7
fixed tests
nnice 498a4d6
fix text wrap
nnice 13bcba1
test fix to changed implementation
nnice a21431d
replace the old actionlogitems with a new one the "old" functionality…
ab2494b
Merge branch 'master' of github.com:Qabel/qabel-desktop into 522_chat…
nnice b82423f
Merge branch '522_chat_click_links' of github.com:nnice/qabel-desktop…
nnice 14a03d6
add own QabelChatLabel and Skin and made all childs into an Text obje…
nnice 065ecf9
some refactoring and cleanup
nnice 7779ade
Merge remote-tracking branch 'upstream/master' into pr_537
julianseeger e451fce
resolves #522 clickable links as Text in TextFlow
julianseeger ef207a0
#537 fixups
julianseeger 1d80c59
#537 fixed message loading
julianseeger ea12491
#522 fixups
julianseeger 48d01e1
#522 final url parsing
julianseeger 7318436
Merge remote-tracking branch 'upstream/master' into pr_537
julianseeger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,20 +2,73 @@ | |
|
||
import de.qabel.desktop.daemon.drop.TextMessage; | ||
import javafx.scene.Node; | ||
import javafx.scene.control.Hyperlink; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.text.TextFlow; | ||
import org.controlsfx.control.HyperlinkLabel; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.awt.*; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.ResourceBundle; | ||
import java.util.function.Consumer; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class PlaintextMessageRenderer implements FXMessageRenderer { | ||
private static final String STYLE_CLASS = "message-text"; | ||
public Consumer<String> browserOpener; | ||
|
||
public PlaintextMessageRenderer() { | ||
browserOpener = (uri) -> { | ||
try { | ||
Desktop.getDesktop().browse(new URI(uri)); | ||
} catch (IOException | URISyntaxException ignored) { | ||
} | ||
}; | ||
|
||
} | ||
|
||
@Override | ||
public Node render(String dropPayload, ResourceBundle resourceBundle) { | ||
Label label = new Label(renderString(dropPayload, resourceBundle)); | ||
label.getStyleClass().add("message-text"); | ||
String text = renderString(dropPayload, resourceBundle); | ||
return renderHyperlinks(text); | ||
} | ||
|
||
@NotNull | ||
TextFlow renderHyperlinks(String message) { | ||
String regex = "(https?:\\/\\/(?:www\\.|(?!www))[^\\s\\.]+\\.[^\\s]{2,}|www\\.[^\\s]+\\.[^\\s]{2,})"; | ||
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS | Pattern.MULTILINE); | ||
Matcher matcher = pattern.matcher(message); | ||
String result = matcher.replaceAll("[$1]"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract this replace stuff to a different place, it shouldn't be in a renderer. |
||
|
||
HyperlinkLabel hyperLinkLabel = new HyperlinkLabel(result); | ||
hyperLinkLabel.getStyleClass().add("text"); | ||
|
||
hyperLinkLabel.setOnAction(event -> { | ||
Hyperlink link = (Hyperlink) event.getSource(); | ||
final String uri = link == null ? "" : link.getText(); | ||
new Thread(() -> browserOpener.accept(uri)).start(); | ||
}); | ||
|
||
TextFlow node = new TextFlow(hyperLinkLabel); | ||
node.getStyleClass().add(STYLE_CLASS); | ||
return node; | ||
} | ||
|
||
@NotNull | ||
private Label renderLabel(String text) { | ||
Label label = new Label(text); | ||
label.getStyleClass().add(STYLE_CLASS); | ||
return label; | ||
} | ||
|
||
@Override | ||
public String renderString(String dropPayload, ResourceBundle resourceBundle) { | ||
return TextMessage.fromJson(dropPayload).getText(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also initialize it inline where you declare the member. Functionally it is identical.
A better way do to this is imho to create a dedicated browser opener interactor and replace the real one, which has Desktop.getDesktop() stuff, with a testable browser opener.