Skip to content
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

Allow links clickable in template editor #119

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
allow links clickable in template editor
RamanaReddy0M committed Sep 5, 2024
commit e5757d1e11f8f421f60c83ca809203e559d978d9
Original file line number Diff line number Diff line change
@@ -20,15 +20,20 @@
import org.yaml.snakeyaml.error.YAMLException;

import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -121,14 +126,14 @@ private String createCommand(Path nucleiPath, URL targetUrl) {
}

return String.format("%s -v -t %s -u %s",
wrapWithQuotesIfNecessary(nucleiPath.toString()),
wrapWithQuotesIfNecessary(this.templatePath.toString()),
wrapWithQuotesIfNecessary(targetUrl.toString()));
wrapWithQuotesIfNecessary(nucleiPath.toString()),
wrapWithQuotesIfNecessary(this.templatePath.toString()),
wrapWithQuotesIfNecessary(targetUrl.toString()));
}

private static String wrapWithQuotesIfNecessary(String input) {
return input.contains(" ") ? String.format("\"%s\"", input)
: input;
: input;
RamanaReddy0M marked this conversation as resolved.
Show resolved Hide resolved
}

private void setKeyboardShortcuts() {
@@ -221,6 +226,7 @@ private void setupAutoCompletion(RSyntaxTextArea textEditor) {
}
}


private RSyntaxTextArea createSmartTextEditor(String templateYaml) {
// TODO https://github.com/bobbylight/RSyntaxTextArea/issues/269
JTextComponent.removeKeymap("RTextAreaKeymap");
@@ -249,9 +255,49 @@ private RSyntaxTextArea createSmartTextEditor(String templateYaml) {

setupAutoCompletion(textEditor);

textEditor.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) { // Trigger on double-click
int offset = textEditor.viewToModel2D(e.getPoint());
String url = getUrlAtOffset(textEditor, offset);
if (url != null) {
try {
Desktop.getDesktop().browse(new URI(url));
RamanaReddy0M marked this conversation as resolved.
Show resolved Hide resolved
} catch (IOException | URISyntaxException ex) {
ex.printStackTrace();
}
}
}
}
});

return textEditor;
}


private String getUrlAtOffset(RSyntaxTextArea textEditor, int offset) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method could use a unit test

try {
int start = offset;
int end = offset;
while (start > 0 && !Character.isWhitespace(textEditor.getText(start - 1, 1).charAt(0))) {
start--;
}
while (end < textEditor.getDocument().getLength() && !Character.isWhitespace(textEditor.getText(end, 1).charAt(0))) {
end++;
}

String potentialUrl = textEditor.getText(start, end - start);
if (potentialUrl.startsWith("http://") || potentialUrl.startsWith("https://")) {
return potentialUrl;
}
} catch (BadLocationException e) {
e.printStackTrace();
}
return null;
}


private JMenu createTemplateEditorMenuItems() {
final JMenu templateMenu = new JMenu("Add");
templateMenu.add(createTemplateEditorClassificationMenu());
@@ -476,8 +522,8 @@ public void approveSelection() {
final File selectedFile = getSelectedFile();
if (selectedFile.exists() && getDialogType() == SAVE_DIALOG) {
final int result = JOptionPane.showConfirmDialog(this, "The selected file already exists. Do you want to overwrite it?",
"Overwrite existing file?",
JOptionPane.YES_NO_CANCEL_OPTION);
"Overwrite existing file?",
JOptionPane.YES_NO_CANCEL_OPTION);
switch (result) {
case JOptionPane.YES_OPTION:
super.approveSelection();
@@ -511,14 +557,14 @@ private void executeButtonClick() {
}

CommandLineUtils.asyncExecuteCommand(command,
bufferedReader -> bufferedReader.lines()
.map(line -> line + "\n")
.forEach(line -> SwingUtilities.invokeLater(() -> {
this.outputPane.appendText(line, noColor);
this.outputPane.repaint();
})),
exitCode -> SwingUtilities.invokeLater(() -> this.outputPane.appendText("\nThe process exited with code " + exitCode)),
this.nucleiGeneratorSettings::logError);
bufferedReader -> bufferedReader.lines()
RamanaReddy0M marked this conversation as resolved.
Show resolved Hide resolved
.map(line -> line + "\n")
.forEach(line -> SwingUtilities.invokeLater(() -> {
this.outputPane.appendText(line, noColor);
this.outputPane.repaint();
})),
exitCode -> SwingUtilities.invokeLater(() -> this.outputPane.appendText("\nThe process exited with code " + exitCode)),
this.nucleiGeneratorSettings::logError);
}
}
}