view src/junotu/TabSimpleSearch.java @ 14:d90a9b1065d1

Rewrote 'Card' class with tag support
author Fox
date Sat, 09 Apr 2022 16:40:55 +0200
parents 587b69a38915
children c9961a1e1479
line wrap: on
line source

package junotu;

import java.lang.RuntimeException;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

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

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JScrollPane;

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

public class TabSimpleSearch extends JPanel {

    private JTextField field;
    private Box results;
    
    public TabSimpleSearch()
    {
	this.setLayout( new BorderLayout() );

	JPanel top = new JPanel( new BorderLayout() );
	field = new JTextField();
	JButton create = new JButton("+");
	results = Box.createVerticalBox();
	JScrollPane scroll = new JScrollPane( results );

	field.setFont( new Font( "Monospaced", Font.PLAIN, 16 ) );
	create.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );

	scroll.getVerticalScrollBar().setUnitIncrement(128);

	field.setPreferredSize( new Dimension(32, 32) );
	
	this.add( top, BorderLayout.NORTH );
	top.add( field, BorderLayout.CENTER );
	top.add( create, BorderLayout.EAST );

	this.add( scroll, BorderLayout.CENTER );

	scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

	search();

	create.addActionListener(
				 new ActionListener()
				 {
				     @Override
				     public void actionPerformed( ActionEvent e )
				     {
					 buttonClickedCreate();
				     }
				 }
	);

	field.getDocument().addDocumentListener(
						new DocumentListener()
						{
						    @Override
						    public void changedUpdate( DocumentEvent e )
						    {
							updateTitle();
							search();
						    }
						    @Override
						    public void removeUpdate( DocumentEvent e )
						    {
							updateTitle();
							search();
						    }
						    @Override
						    public void insertUpdate( DocumentEvent e )
						    {
							updateTitle();
							search();
						    }
						}
						);
	
    }

    public void search() {
	
	Card[] cards;

	try {
	    String text = field.getText();
	    if( text.length() > 0 ) {
		cards = Main.database.searchSimple( field.getText() );
	    } else {
		cards = Main.database.searchTopRecent( 32 );
	    }
	} catch( Exception e ) {
	    throw new RuntimeException(e);
	}

	System.out.print("Search: Found "+cards.length+" matches.\n");
	
	/* TODO: Reuse widgets. */
	results.removeAll();
	for( Card card : cards ) {
	    CardWidget cardWidget = new CardWidget( card );
	    results.add( cardWidget );
	}
	results.validate();
	results.repaint();
    }

    private void updateTitle()
    {
	Window window = (Window)this.getTopLevelAncestor();

	String text = field.getText();

	if( text.length() > 0 ) {
	    window.setTitle( window.preferredTitle( "Search: "+text ) );
	} else {
	    window.setTitle( window.preferredTitle( "Search" ) );
	}
	
    }
    
    private void buttonClickedCreate()
    {
	Window window = (Window)this.getTopLevelAncestor();
	Main.actionCardCreate( window );
    }
    
}