changeset 55:154c07f4c23c

TabColumns: Ability to edit column titles (double click)
author Fox
date Wed, 14 Dec 2022 17:01:00 +0100
parents 3d6f0e82beea
children edfeca7d5b45
files src/junotu/TabColumns.java
diffstat 1 files changed, 61 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/TabColumns.java	Sat Dec 03 19:20:29 2022 +0100
+++ b/src/junotu/TabColumns.java	Wed Dec 14 17:01:00 2022 +0100
@@ -14,6 +14,8 @@
 import java.awt.event.ActionListener;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseAdapter;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
 
 import javax.swing.KeyStroke;
 import javax.swing.SwingUtilities;
@@ -60,8 +62,8 @@
     public final String KEY_ACTION_CARD_FULL_RIGHT = "card_full_right";
     
     private class ColumnWidget extends JPanel implements ActionListener, MouseListener {
-	JLabel title;
 	JTextField titleEdit;
+	TitledBorder titledBorder;
 	Box cards;
 	JButton addCard;
 
@@ -69,11 +71,11 @@
 	{
 	    this.setLayout( new GridBagLayout() );
 	    
-	    title = new JLabel("");
+	    titleEdit = new JTextField("");
 	    cards = Box.createVerticalBox();
 	    addCard = new JButton("+");
 
-	    title.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) );
+	    titleEdit.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) );
 	    addCard.setFont( new Font( "Monospaced", Font.BOLD, 32 ) );
 
 	    GridBagConstraints constraints = new GridBagConstraints();
@@ -84,6 +86,8 @@
 	    constraints.gridx = 0;
 	    constraints.gridy = 0;
 
+	    this.add( titleEdit, constraints );
+	    constraints.gridy++;	    
 	    this.add( cards, constraints );
 	    constraints.gridy++;
 	    this.add( addCard, constraints );
@@ -98,19 +102,24 @@
 	    //this.setPreferredSize( new Dimension( COLUMN_WIDTH, 384 ) );
 	    this.setMaximumSize( new Dimension( COLUMN_WIDTH, 1000000 ) );
 
+	    titleEdit.setVisible(false);
+
+	    titledBorder = BorderFactory.createTitledBorder(
+		BorderFactory.createEtchedBorder(),
+	        "",
+	        TitledBorder.LEADING,
+	        TitledBorder.TOP,
+	        new Font( "Monospaced", Font.BOLD, 16 )
+	    );
+
 	    this.setBorder(
 		BorderFactory.createCompoundBorder(
 		    BorderFactory.createEmptyBorder( 0, 8, 0, 8 ),
-	            BorderFactory.createTitledBorder(
-		        BorderFactory.createEtchedBorder(),
-		            "Border title, but very very very long",
-		            TitledBorder.LEADING,
-		            TitledBorder.TOP,
-		            new Font( "Monospaced", Font.BOLD, 16 )
-		    )
+	            titledBorder
 		)
 	    );
 
+	    addMouseListener(this);
 	    registerKeyboardAction(
 		this,
 		KEY_ACTION_CARD_UP,
@@ -161,19 +170,52 @@
 	    );
 
 	    addCard.addActionListener(this);
+	    titleEdit.addFocusListener(
+					new FocusListener()
+					{
+					    @Override
+					    public void focusGained(FocusEvent e) {}
+					    
+					    @Override
+					    public void focusLost(FocusEvent e)
+					    {
+					        titleCommit();
+					    }
+					}
+					);
 
 	    addCard.setToolTipText("Add card.");
 	    
 	}
 
-	public void titleSet( String title_ )
+	public void titleSet( String title )
 	{
-	    title.setText(title_);
+	    titledBorder.setTitle(title);
+	    repaint();
 	}
 
 	public String titleGet()
 	{
-	    return title.getText();
+	    return titledBorder.getTitle();
+	}
+
+	public void titleEdit()
+	{
+	    titleEdit.setText(titleGet());
+	    titleEdit.setVisible(true);
+	    titleSet("              ");
+	    revalidate();
+	    titleEdit.grabFocus();
+	}
+
+	public void titleCommit()
+	{
+	    if( !titleEdit.isVisible() ) {
+		return;
+	    }
+	    titleSet(titleEdit.getText());
+	    titleEdit.setVisible(false);
+	    revalidate();
 	}
 	
 	public void insertCard( Card card, int at )
@@ -279,7 +321,12 @@
 
 	public void mouseClicked( MouseEvent e )
 	{
-	    if( e.getSource() instanceof ColumnCardWidget ) {
+	    Object source = e.getSource();
+	    if( source == this ) {
+		if( e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() >= 2 ) {
+		    titleEdit();
+		}
+	    } else if( source instanceof ColumnCardWidget ) {
 		if( e.getButton() == MouseEvent.BUTTON2 ) {
 		    cards.remove( (ColumnCardWidget)e.getSource() );
 		    cards.revalidate();