From d05ffa136a7183803a9dd869da6a9938c0d8dbd0 Mon Sep 17 00:00:00 2001 From: Ramana Reddy Date: Thu, 5 Sep 2024 14:32:08 +0530 Subject: [PATCH] allow editing tab names on double click --- .../gui/TemplateGeneratorTabbedPane.java | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/projectdiscovery/nuclei/gui/TemplateGeneratorTabbedPane.java b/src/main/java/io/projectdiscovery/nuclei/gui/TemplateGeneratorTabbedPane.java index 152eaab..336a3dd 100644 --- a/src/main/java/io/projectdiscovery/nuclei/gui/TemplateGeneratorTabbedPane.java +++ b/src/main/java/io/projectdiscovery/nuclei/gui/TemplateGeneratorTabbedPane.java @@ -27,9 +27,7 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.MouseWheelEvent; +import java.awt.event.*; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -76,12 +74,11 @@ public void cleanup() { public void addTab(TemplateGeneratorTab templateGeneratorTab) { this.templateGeneratorTabs.add(templateGeneratorTab); final String tabName = Optional.ofNullable(templateGeneratorTab.getName()) - .orElseGet(() -> "Tab " + this.openedTabCounter++); + .orElseGet(() -> "Tab " + this.openedTabCounter++); templateGeneratorTab.setName(tabName); JPanel tabPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); tabPanel.setOpaque(false); - JLabel tabLabel = new JLabel(tabName); JButton closeButton = new JButton("x"); closeButton.setMargin(new Insets(0, 5, 0, 5)); closeButton.setBorderPainted(false); @@ -95,7 +92,40 @@ public void addTab(TemplateGeneratorTab templateGeneratorTab) { } }); + JLabel tabLabel = new JLabel(tabName); + // Create a JTextField for editing the tab name, initially hidden + JTextField tabNameEditor = new JTextField(tabName); + tabNameEditor.setVisible(false); + // Add action listener to handle renaming when the user presses Enter or loses focus + tabNameEditor.addActionListener(e -> finishEditingTabName(templateGeneratorTab, tabLabel, tabNameEditor, tabPanel)); + tabNameEditor.addFocusListener(new FocusAdapter() { + @Override + public void focusLost(FocusEvent e) { + finishEditingTabName(templateGeneratorTab, tabLabel, tabNameEditor, tabPanel); + } + }); + + tabLabel.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 1) { + // Handle single click: set focus to this tab + int index = indexOfTabComponent(tabPanel); + if (index != -1) { + setSelectedIndex(index); + } + } else if (e.getClickCount() == 2) { + // Handle double click: enable editing the tab name + tabLabel.setVisible(false); + tabNameEditor.setVisible(true); + tabNameEditor.requestFocus(); + tabNameEditor.selectAll(); + } + } + }); + tabPanel.add(tabLabel); + tabPanel.add(tabNameEditor); tabPanel.add(closeButton); this.addTab(tabName, templateGeneratorTab); @@ -103,6 +133,19 @@ public void addTab(TemplateGeneratorTab templateGeneratorTab) { this.setSelectedIndex(this.getTabCount() - 1); } + // Method to finish editing the tab name + private void finishEditingTabName(TemplateGeneratorTab templateGeneratorTab, JLabel tabLabel, JTextField tabNameEditor, JPanel tabPanel) { + String newTabName = tabNameEditor.getText().trim(); + if (!newTabName.isEmpty()) { + templateGeneratorTab.setName(newTabName); // Update the tab's model name + tabLabel.setText(newTabName); // Update the tab label + this.setTitleAt(this.indexOfTabComponent(tabPanel), newTabName); // Update the tab title + } + tabNameEditor.setVisible(false); // Hide the editor + tabLabel.setVisible(true); // Show the label + } + + @Override public void insertTab(String title, Icon icon, Component component, String tip, int index) { if (component.getClass().isAssignableFrom(TemplateGeneratorTab.class)) {