view src/junotu/ColumnCardWidget.java @ 121:cc1df2f32f2b

TabColumnBoard and TabCalendarBoard cards now expand vertically to accomodate longer titless
author Fox
date Sat, 25 Nov 2023 15:18:53 +0100
parents 6a78c671a7cf
children
line wrap: on
line source

package junotu;

import java.lang.RuntimeException;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import java.awt.Component;
import javax.swing.JTextArea;

import javax.swing.BorderFactory;

import junotu.Main;
import junotu.Card;
import junotu.TabColumnBoard;

class ColumnCardWidget extends JPanel {
	
	final static int CARD_MINIMUM_HEIGHT = 64;
	
	public boolean newCard;
	public long identifier;
	public JTextArea title;
	
	public ColumnCardWidget( Card card )
	{
		this.setLayout( new BorderLayout() );
		
		title = new JTextArea("");
		
		title.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );
		
		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 );
		
		newCard = card == null;
		if( !newCard ) {
			identifier = card.identifierGet().longValue();
			title.setText(card.titleGet());
		}
		
	}
	
	public Dimension getMinimumSize()
	{
		Dimension d = super.getMinimumSize();
		d.height = Math.max( CARD_MINIMUM_HEIGHT, d.height );
		return d;
	}
	
	public Dimension getMaximumSize()
	{
		Dimension d = super.getMaximumSize();
		d.height = Math.max( CARD_MINIMUM_HEIGHT, d.height );
		return d;
	}
	
	public Dimension getPreferredSize()
	{
		Dimension d = super.getPreferredSize();
		d.height = Math.max( CARD_MINIMUM_HEIGHT, d.height );
		return d;
	}
	
	public void save()
	{
		try {
			if( newCard ) {
				Card card = new Card();
				card.titleSet( title.getText() );
				card.tagValueSetOnly( Card.TAG_BOARD_COLUMN_CARD, Card.VALUE_BOARD_COLUMN_CARD_ONLY );
				
				identifier = Main.database.cardAdd(card);
				newCard = false;
				
			} else {
				Card card = Main.database.cardGetByIdentifier(identifier);
				
				if( card == null ) {
					throw new RuntimeException("Null card on update try.");
				}
				
				card.titleSet( title.getText() );
				Main.database.cardUpdate( card, true );
				
			}
		} catch( Exception e ) {
			throw new RuntimeException(e);
		}
	}
	
	public void delete()
	{
		if( newCard ) {
			return;
		}
		
		Main.database.cardDeleteByIdentifier(identifier, true);
	}
	
	public boolean isSelected()
	{
		return title.isFocusOwner();
	}
	
	public void select()
	{
		title.requestFocusInWindow();
	}
	
}