Mercurial Hosting > junotu
changeset 53:eed982f6d415
TabColumns: Ability to move cards within a column
Using [ALT]+[UP], [ALT]+[DOWN], [ALT]+[PAGE UP], and [ALT]+[PAGE DOWN].
author | Fox |
---|---|
date | Wed, 30 Nov 2022 16:13:03 +0100 |
parents | 7cf2788649a7 |
children | 3d6f0e82beea |
files | src/junotu/TabColumns.java |
diffstat | 1 files changed, 105 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/junotu/TabColumns.java Fri Nov 25 21:16:38 2022 +0100 +++ b/src/junotu/TabColumns.java Wed Nov 30 16:13:03 2022 +0100 @@ -38,6 +38,9 @@ import javax.swing.BorderFactory; import javax.swing.border.TitledBorder; +import static java.lang.Math.min; +import static java.lang.Math.max; + import junotu.Main; import junotu.Window.Tab; import junotu.Card; @@ -53,6 +56,11 @@ 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() { this.setLayout( new GridBagLayout() ); @@ -99,6 +107,31 @@ ) ); + registerKeyboardAction( + this, + KEY_ACTION_CARD_UP, + KeyStroke.getKeyStroke( KeyEvent.VK_UP, InputEvent.ALT_DOWN_MASK ), + WHEN_ANCESTOR_OF_FOCUSED_COMPONENT + ); + registerKeyboardAction( + this, + KEY_ACTION_CARD_DOWN, + KeyStroke.getKeyStroke( KeyEvent.VK_DOWN, InputEvent.ALT_DOWN_MASK ), + WHEN_ANCESTOR_OF_FOCUSED_COMPONENT + ); + registerKeyboardAction( + this, + KEY_ACTION_CARD_FULL_UP, + KeyStroke.getKeyStroke( KeyEvent.VK_PAGE_UP, InputEvent.ALT_DOWN_MASK ), + WHEN_ANCESTOR_OF_FOCUSED_COMPONENT + ); + registerKeyboardAction( + this, + KEY_ACTION_CARD_FULL_DOWN, + KeyStroke.getKeyStroke( KeyEvent.VK_PAGE_DOWN, InputEvent.ALT_DOWN_MASK ), + WHEN_ANCESTOR_OF_FOCUSED_COMPONENT + ); + addCard.addActionListener(this); addCard.setToolTipText("Add card."); @@ -127,9 +160,68 @@ cards.revalidate(); } + public void moveCard( int at, int to ) + { + if( at < 0 ) { + return; + } + ColumnCardWidget cardWidget = (ColumnCardWidget)cards.getComponent(at); + boolean focused = cardWidget.isSelected(); + cards.remove(at); + cards.add( cardWidget, to ); + cards.revalidate(); + if( focused ) { + cardWidget.select(); + } + } + + public int selectedCard() + { + Component[] cardList = cards.getComponents(); + for( int i = 0; i < cardList.length; i++ ) { + if( ((ColumnCardWidget)cardList[i]).isSelected() ) { + return i; + } + } + System.out.print("Selected card not found."); + return -1; + } + public void actionPerformed( ActionEvent e ) { - if( e.getSource() == addCard ) { + Object source = e.getSource(); + if( source == this ) { + int selected = selectedCard(); + int length = cards.getComponentCount(); + + switch( e.getActionCommand() ) { + + case KEY_ACTION_CARD_UP: { + System.out.print("Move card up.\n"); + moveCard( selected, max(selected-1, 0) ); + break; + } + + case KEY_ACTION_CARD_DOWN: { + System.out.print("Move card down.\n"); + moveCard( selected, min(selected+1, cards.getComponentCount()-1) ); + break; + } + + case KEY_ACTION_CARD_FULL_UP: { + System.out.print("Move card full up.\n"); + moveCard( selected, 0 ); + break; + } + + case KEY_ACTION_CARD_FULL_DOWN: { + System.out.print("Move card full down.\n"); + moveCard( selected, cards.getComponentCount()-1 ); + break; + } + + } + } else if( source == addCard ) { Card newCard = new Card(); newCard.titleSet("New card"); insertCard( newCard, -1 ); @@ -156,13 +248,14 @@ private class ColumnCardWidget extends JPanel { public long identifier; + public JTextArea title; public ColumnCardWidget( Card card ) { this.setLayout( new BorderLayout() ); identifier = card.identifierGet(); - JTextArea title = new JTextArea( card.titleGet() ); + title = new JTextArea( card.titleGet() ); title.setFont( new Font( "Monospaced", Font.BOLD, 16 ) ); @@ -193,6 +286,16 @@ title.addMouseListener( mouseListener ); } + + public boolean isSelected() + { + return title.isFocusOwner(); + } + + public void select() + { + title.requestFocusInWindow(); + } }