view src/junotu/TabColumns.java @ 43:0fa52bdb0673

TabColumns: Now able to remove columns via middle click.
author Fox
date Sun, 30 Oct 2022 02:06:02 +0200
parents a5bc0a1f173c
children bad4e8bb769e
line wrap: on
line source

package junotu;

/* TODO: Clean-up exports. */

import java.lang.RuntimeException;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;

import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;

import javax.swing.JPanel;
import javax.swing.Box;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;

import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;

import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;

import junotu.Main;
import junotu.Window.Tab;
import junotu.Card;

public class TabColumns extends JPanel implements MouseListener {

    final static int COLUMN_CONTENT_WIDTH = 256;
    final static int COLUMN_WIDTH = COLUMN_CONTENT_WIDTH+16;
    
    private class ColumnWidget extends JPanel implements ActionListener, MouseListener {
	JLabel title;
	JTextField titleEdit;
	Box cards;
	JButton addCard;

	public ColumnWidget()
	{
	    this.setLayout( new GridBagLayout() );
	    
	    title = new JLabel("");
	    cards = Box.createVerticalBox();
	    addCard = new JButton("+");

	    title.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) );
	    addCard.setFont( new Font( "Monospaced", Font.BOLD, 32 ) );

	    GridBagConstraints constraints = new GridBagConstraints();
	    constraints.anchor = GridBagConstraints.NORTHWEST;
	    constraints.fill = GridBagConstraints.HORIZONTAL;
	    constraints.weightx = 1.0;
	    constraints.weighty = 0.0;
	    constraints.gridx = 0;
	    constraints.gridy = 0;

	    this.add( cards, constraints );
	    constraints.gridy++;
	    this.add( addCard, constraints );
	    constraints.gridy++;
	    constraints.weighty = 1.0;
	    this.add( Box.createVerticalGlue(), constraints );

	    addCard.setPreferredSize( new Dimension( COLUMN_CONTENT_WIDTH, 64 ) );
	    addCard.setMaximumSize( new Dimension( COLUMN_CONTENT_WIDTH, 64 ) );
	    addCard.setAlignmentX( JButton.CENTER_ALIGNMENT );

	    //this.setPreferredSize( new Dimension( COLUMN_WIDTH, 384 ) );
	    this.setMaximumSize( new Dimension( COLUMN_WIDTH, 1000000 ) );

	    this.setBorder(
	        BorderFactory.createTitledBorder(
		    BorderFactory.createEtchedBorder(),
		    "Border title, but very very very long",
		    TitledBorder.LEADING,
		    TitledBorder.TOP,
		    new Font( "Monospaced", Font.BOLD, 16 )
		)
	    );

	    addCard.addActionListener(this);
	    
	}

	public void titleSet( String title_ )
	{
	    title.setText(title_);
	}

	public String titleGet()
	{
	    return title.getText();
	}
	
	public void insertCard( Card card, 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 );
	    cards.revalidate();
	}

	public void actionPerformed( ActionEvent e )
	{
	    if( e.getSource() == addCard ) {
		Card newCard = new Card();
		newCard.titleSet("New card");
		insertCard( newCard, -1 );
	    }
	}

	public void mouseClicked( MouseEvent e )
	{
	    if( e.getSource() instanceof ColumnCardWidget ) {
		if( e.getButton() == MouseEvent.BUTTON2 ) {
		    cards.remove( (ColumnCardWidget)e.getSource() );
		    cards.revalidate();
		}
	    }
	}
	
	public void mouseEntered( MouseEvent e ) {}
	public void mouseExited( MouseEvent e ) {}
	public void mousePressed( MouseEvent e ) {}
	public void mouseReleased( MouseEvent e ) {}
	
    }

    private class ColumnCardWidget extends JPanel {

	public long identifier;
	
	public ColumnCardWidget( Card card )
	{
	    this.setLayout( new BorderLayout() );
	    
	    identifier = card.identifierGet();
	    JTextArea title = new JTextArea( card.titleGet() );
	    
	    title.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );
		
	    this.setMinimumSize( new Dimension( COLUMN_CONTENT_WIDTH, 64 ) );
	    this.setPreferredSize( new Dimension( COLUMN_CONTENT_WIDTH, 64 ) );
	    this.setMaximumSize( new Dimension( COLUMN_CONTENT_WIDTH, 128 ) );

	    //title.setMinimumSize( new Dimension( 32, 32 ) );

	    this.setBorder( BorderFactory.createRaisedBevelBorder() );
            title.setEditable( true );
	    title.setLineWrap( true );
	    title.setWrapStyleWord( true );
	    title.setOpaque( false );

	    this.add( title, BorderLayout.CENTER );

	    MouseAdapter mouseListener = new MouseAdapter()
	    {
		@Override
		public void mouseClicked( MouseEvent e )
		{
		    e.setSource(ColumnCardWidget.this);
		    processMouseEvent(e);
		}
	    };

	    title.addMouseListener( mouseListener );
	    
	}
	
    }

    long columnsCard;
    JLabel title;
    Box columns;
    JScrollPane scroll;

    public TabColumns()
    {
	this.setLayout( new BorderLayout() );

	title = new JLabel("");
	columns = Box.createHorizontalBox();
	scroll = new JScrollPane( columns );

	title.setFont( new Font( "Monospaced", Font.PLAIN, 32 ) );

	this.add( title, BorderLayout.NORTH );
	this.add( scroll, BorderLayout.CENTER );

	/* TODO: DEBUG */
	columnsNew();
	
    }

    public void columnsNew()
    {
	title.setText("New board");
	columns.removeAll();
	for( int i = 0; i < 5; i++ ) {
	    insertColumn();
	}
    }

    public void columnsEdit( Card columnsCard )
    {
	
    }

    public void columnsSave()
    {
	
    }

    public void insertColumn()
    {
	ColumnWidget column = new ColumnWidget();
	column.titleSet("New column");
	column.addMouseListener(this);
	columns.add( Box.createHorizontalStrut(16) );
	columns.add(column);
    }

    public void mouseClicked( MouseEvent e )
    {
	if( e.getButton() == MouseEvent.BUTTON2 ) {
	    if( !(e.getSource() instanceof ColumnWidget) )
		return;

	    columns.remove( (ColumnWidget)e.getSource() );
	    /* TODO: FIXME: One of the columns is frequently (visually) not removed (until layout is manually refreshed). */
	    columns.revalidate();
	}
    }
    
    public void mouseEntered( MouseEvent e ) {}
    public void mouseExited( MouseEvent e ) {}
    public void mousePressed( MouseEvent e ) {}
    public void mouseReleased( MouseEvent e ) {}
    
}