view src/junotu/GUIToolbox.java @ 70:fc040f668d55

Database: Added 'databaseResaveAll()' function Not called from anywhere yet, but it's useful for introducing new fields to databases that don't have them. For example, in this case -- 'search' field.
author Fox
date Sat, 24 Dec 2022 12:16:19 +0100
parents 8db9285be2be
children 7a3fd865654a
line wrap: on
line source

package junotu;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.LayoutManager;

import java.awt.Component;
import java.awt.Container;

import javax.swing.SwingConstants;
import javax.swing.JPanel;
import javax.swing.Scrollable;

public class GUIToolbox {

    /** Source: https://stackoverflow.com/a/2814718 */
    public static class JPanelScrollable extends JPanel implements Scrollable {

	public boolean scrollVertical = false;

	public JPanelScrollable()
	{
	    super();
	}

	public JPanelScrollable( LayoutManager layout )
	{
	    super( layout );
	}
	
	public Dimension getPreferredScrollableViewportSize() {
	    return getPreferredSize();
	}
	
	public int getScrollableUnitIncrement( Rectangle visibleRect, int orientation, int direction ) {
	    return 10;
	}
	
	public int getScrollableBlockIncrement( Rectangle visibleRect, int orientation, int direction ) {
	    return ((orientation == SwingConstants.VERTICAL) ? visibleRect.height : visibleRect.width) - 10;
	}
	
	public boolean getScrollableTracksViewportWidth() {
	    return !scrollVertical;
	}
	
	public boolean getScrollableTracksViewportHeight() {
	    return scrollVertical;
	}
    }

    public static final int componentGetIndex( Component component )
    {
	if (component != null && component.getParent() != null) {
	    Container parent = component.getParent();
	    for( int i = 0; i < parent.getComponentCount(); i++ ) {
		if( parent.getComponent(i) == component )
		    return i;
	    }
	}
	
	return -1;
    }

    public static class CardEditLayout implements LayoutManager
    {
	public CardEditLayout() {}
	
	public Dimension minimumLayoutSize( Container parent )
	{
	    return new Dimension( 1, 1 );
	}

	/* TODO: Does this need to be precise? */
	public Dimension preferredLayoutSize( Container parent )
	{

	    Dimension preferred = new Dimension();
	    
	    Container parentparent = parent.getParent();
	    preferred.width = parentparent.getSize().width;

	    Component[] components = parent.getComponents();
	    assert( components.length == 3 );
	    
	    //Component title   = components[0];
	    Component content = components[1];
	    Component tags    = components[2];

	    content.setMaximumSize( new Dimension( preferred.width, Integer.MAX_VALUE ) );
	    tags.setMaximumSize( new Dimension( preferred.width, Integer.MAX_VALUE ) );

	    preferred.height = 32+8+4+Math.max(content.getPreferredSize().height, 32*12)+tags.getPreferredSize().height+512;
	    
	    return preferred;
	}
	
	public void addLayoutComponent( String name, Component comp ) {}
	public void removeLayoutComponent( Component comp ) {}
	
	public void layoutContainer( Container parent )
	{
	    Component[] components = parent.getComponents();
	    assert( components.length == 3 );
	    
	    Component title   = components[0];
	    Component content = components[1];
	    Component tags    = components[2];
	    
	    int width = (preferredLayoutSize( parent )).width;

	    content.setMaximumSize( new Dimension( width, Integer.MAX_VALUE ) );
	    tags.setMaximumSize( new Dimension( width, Integer.MAX_VALUE ) );

	    int contentHeight = Math.max(content.getPreferredSize().height, 32*12);
	    int tagsHeight = (tags.getPreferredSize()).height+512;

	    int y = 0;
	    title.setBounds( 0, y, width, 32+8 ); /* Underscores don't fit if I set height to font height exactly. :/ */
	    y += 32+8;
	    y += 4;
	    content.setBounds( 0, y, width, contentHeight );
	    y += contentHeight;
	    tags.setBounds( 0, y, width, tagsHeight );
	    y += tagsHeight;
	    
	}
    }
    
}