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 editing tab names on double click #118

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -95,14 +92,60 @@ 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);
this.setTabComponentAt(this.getTabCount() - 1, tabPanel);
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)) {
Expand Down
Loading