changeset 54:3d6f0e82beea

TabColumns: Ability to move cards between columns Via [CTRL]+[LEFT], [CTRL]+[RIGHT], [CTRL]+[HOME] and [CTRL]+[END].
author Fox
date Sat, 03 Dec 2022 19:20:29 +0100
parents eed982f6d415
children 154c07f4c23c
files src/junotu/TabColumns.java
diffstat 1 files changed, 101 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/TabColumns.java	Wed Nov 30 16:13:03 2022 +0100
+++ b/src/junotu/TabColumns.java	Sat Dec 03 19:20:29 2022 +0100
@@ -49,6 +49,15 @@
 
     final static int COLUMN_CONTENT_WIDTH = 256;
     final static int COLUMN_WIDTH = COLUMN_CONTENT_WIDTH+16;
+
+    public final String KEY_ACTION_CARD_UP         = "card_up";
+    public final String KEY_ACTION_CARD_DOWN       = "card_down";
+    public final String KEY_ACTION_CARD_FULL_UP    = "card_full_up";
+    public final String KEY_ACTION_CARD_FULL_DOWN  = "card_full_down";
+    public final String KEY_ACTION_CARD_LEFT       = "card_left";
+    public final String KEY_ACTION_CARD_RIGHT      = "card_right";
+    public final String KEY_ACTION_CARD_FULL_LEFT  = "card_full_left";
+    public final String KEY_ACTION_CARD_FULL_RIGHT = "card_full_right";
     
     private class ColumnWidget extends JPanel implements ActionListener, MouseListener {
 	JLabel title;
@@ -56,12 +65,7 @@
 	Box cards;
 	JButton addCard;
 
-	private final String KEY_ACTION_CARD_UP        = "card_up";
-	private final String KEY_ACTION_CARD_DOWN      = "card_down";
-	private final String KEY_ACTION_CARD_FULL_UP   = "card_full_up";
-	private final String KEY_ACTION_CARD_FULL_DOWN = "card_full_down";
-
-	public ColumnWidget()
+	public ColumnWidget( TabColumns parent )
 	{
 	    this.setLayout( new GridBagLayout() );
 	    
@@ -131,6 +135,30 @@
 		KeyStroke.getKeyStroke( KeyEvent.VK_PAGE_DOWN, InputEvent.ALT_DOWN_MASK ),
 		WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
 	    );
+	    registerKeyboardAction(
+	        parent,
+		KEY_ACTION_CARD_LEFT,
+		KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, InputEvent.ALT_DOWN_MASK ),
+		WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
+	    );
+	    registerKeyboardAction(
+	        parent,
+		KEY_ACTION_CARD_RIGHT,
+		KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, InputEvent.ALT_DOWN_MASK ),
+		WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
+	    );
+	    registerKeyboardAction(
+	        parent,
+		KEY_ACTION_CARD_FULL_LEFT,
+		KeyStroke.getKeyStroke( KeyEvent.VK_HOME, InputEvent.ALT_DOWN_MASK ),
+		WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
+	    );
+	    registerKeyboardAction(
+	        parent,
+		KEY_ACTION_CARD_FULL_RIGHT,
+		KeyStroke.getKeyStroke( KeyEvent.VK_END, InputEvent.ALT_DOWN_MASK ),
+		WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
+	    );
 
 	    addCard.addActionListener(this);
 
@@ -150,10 +178,15 @@
 	
 	public void insertCard( Card card, int at )
 	{
+	    ColumnCardWidget cardWidget = new ColumnCardWidget( card );
+	    insertCardRaw( cardWidget, at );
+	}
+
+	public void insertCardRaw( ColumnCardWidget cardWidget, int at )
+	{
 	    if( at == -1 ) {
 		at = cards.getComponentCount();
 	    }
-	    ColumnCardWidget cardWidget = new ColumnCardWidget( card );
 	    cardWidget.addMouseListener(this);
 	    /* TODO: Check if works properly. */
 	    cards.add( cardWidget, at );
@@ -175,6 +208,17 @@
 	    }
 	}
 
+	public ColumnCardWidget popCard( int at )
+	{
+	    if( at < 0 ) {
+		return null;
+	    }
+	    ColumnCardWidget cardWidget = (ColumnCardWidget)cards.getComponent(at);
+	    cards.remove(at);
+	    cardWidget.removeMouseListener(this);
+	    return cardWidget;
+	}
+
 	public int selectedCard()
 	{
 	    Component[] cardList = cards.getComponents();
@@ -187,6 +231,11 @@
 	    return -1;
 	}
 
+	public int cardCount()
+	{
+	    return cards.getComponentCount();
+	}
+
 	public void actionPerformed( ActionEvent e )
 	{
 	    Object source = e.getSource();
@@ -356,17 +405,61 @@
 
     public void insertColumn()
     {
-	ColumnWidget column = new ColumnWidget();
+	ColumnWidget column = new ColumnWidget(this);
 	column.titleSet("New column");
 	column.addMouseListener(this);
 	columns.add(column);
 	columns.revalidate();
     }
 
+    public int findColumn( ColumnWidget columnWidget )
+    {
+	Component[] columnsList = columns.getComponents();
+	for( int i = 0; i < columnsList.length; i++ ) {
+	    if( columnsList[i] == columnWidget ) {
+		return i;
+	    }
+	}
+	return -1;
+    }
+
+    public void moveCard( int from, int at, int to )
+    {
+	ColumnWidget fromColumn = (ColumnWidget)columns.getComponent(from);
+	ColumnWidget toColumn = (ColumnWidget)columns.getComponent(to);
+	ColumnCardWidget cardWidget = fromColumn.popCard(at);
+	if( cardWidget == null ) {
+	    return;
+	}
+	toColumn.insertCardRaw(cardWidget, min(at, toColumn.cardCount()) );
+	cardWidget.select();
+    }
+
     public void actionPerformed( ActionEvent e )
     {
 	if( e.getSource() == addColumn ) {
 	    insertColumn();
+	    return;
+	}
+	ColumnWidget sourceColumn = (ColumnWidget)e.getSource();
+	if( sourceColumn != null ) {
+	    int columnIndex = findColumn(sourceColumn);
+	    switch( e.getActionCommand() ){
+
+	    case KEY_ACTION_CARD_LEFT: {
+		moveCard( columnIndex, sourceColumn.selectedCard(), max( columnIndex-1, 0) );
+	    }
+	    case KEY_ACTION_CARD_RIGHT: {
+		moveCard( columnIndex, sourceColumn.selectedCard(), min( columnIndex+1, columns.getComponentCount()-1) );
+	    }
+	    case KEY_ACTION_CARD_FULL_LEFT: {
+		moveCard( columnIndex, sourceColumn.selectedCard(), 0 );
+	    }
+	    case KEY_ACTION_CARD_FULL_RIGHT: {
+		moveCard( columnIndex, sourceColumn.selectedCard(), columns.getComponentCount()-1 );
+	    }
+		
+	    }
 	}
     }