Skip to content

Commit

Permalink
allow editing tab names on double click (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
RamanaReddy0M authored Sep 5, 2024
1 parent 4ff4507 commit 9797534
Showing 1 changed file with 48 additions and 5 deletions.
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

0 comments on commit 9797534

Please sign in to comment.