view src/junotu/Main.java @ 111:b93c6236c6cb

TabOptions: Minor progress
author Fox
date Thu, 15 Jun 2023 00:53:55 +0200
parents 33f090b497c8
children d63c1d41f364
line wrap: on
line source

package junotu;

import java.lang.RuntimeException;
import java.io.IOException;

import javax.swing.SwingUtilities;
import java.awt.Desktop;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.net.URI;

import junotu.Database;
import junotu.Window;
import junotu.Window.Tab;

public class Main {

	public static final String PROGRAM_NAME = "JUnotu";
	public static final int MAX_WINDOWS = 8;

	public static Database database;
	public static Window[] windows = new Window[MAX_WINDOWS];
	public static Desktop desktop;

	public static void main(String[] args) throws Exception
	{
		database = new Database();
		desktop = Desktop.getDesktop();

		//database.databaseResaveAll();

		SwingUtilities.invokeLater(
			new Runnable() {
				public void run()
				{
					windowAdd( Tab.SEARCH );
				}
			}
		);
	}

	public static Window windowAdd( Tab tab )
	{
		for( int i = 0; i < windows.length; i++ ) {
			if( windows[i] == null ) {
				windows[i] = new Window( tab );
				return windows[i];
			}
		}
		System.out.print("Reached window limit! (Maximum "+Integer.toString(MAX_WINDOWS)+" windows.)\n");
		return null;
	}

	public static void windowClose( Window window )
	{
		int openWindowCount = 0;
		for( int i = 0; i < windows.length; i++ ) {
			if( windows[i] == window ) {
				System.out.print( "Closing window slot "+Integer.toString(i)+": '"+window.getTitle()+"'.\n" );
				window.dispose();
				windows[i] = null;
			}
			if( windows[i] != null ) {
				openWindowCount++;
			}
		}
		if( openWindowCount == 0 ) {
			System.out.print( "No windows open, closing program..\n" );
			database.databaseCommit();
			System.exit(0);
		}
	}

	public static Window windowGetActive()
	{
		for( int i = 0; i < windows.length; i++ ) {
			if( windows[i] != null && windows[i].isActive() ) {
				return windows[i];
			}
		}
		return null;
	}

	public static void refreshSearches()
	{
		for( int i = 0; i < windows.length; i++ ) {
			if( windows[i] != null ) {
				Window window = windows[i];
				window.tabSearch.dirty = true;
				if( window.tabCurrent() == Tab.SEARCH ) {
					window.tabSearch.refreshSearch();
				}
			}
		}
	}

	public static void actionCardCreate( Window window )
	{
		//window = windowGetActive();
		window.tabSwitch( Tab.EDIT );
		window.tabEdit.cardCreate();
		System.out.print( "Opening edit tab for newly created card.\n" );
	}

	public static void actionCardEdit( Window window, long identifier )
	{
		//window = windowGetActive();
		Card card = database.cardGetByIdentifier( identifier );
		window.tabSwitch( Tab.EDIT );
		window.tabEdit.cardEdit( card );
		System.out.print( "Opening edit tab to edit '"+card.titleGet()+"'.\n" );
	}

	public static void actionCardSaved( Window window, Card card )
	{
		for( int i = 0; i < windows.length; i++ ) {
			if( windows[i] == window || windows[i] == null ) {
				continue;
			}
			if(
				windows[i].tabCurrent() == Tab.EDIT
				&& windows[i].tabEdit.newCard == false
				&& windows[i].tabEdit.card.identifierGet().longValue() == card.identifierGet().longValue()
			) {
				windows[i].tabEdit.markDirty();
			}
		}
		refreshSearches();
	}

	public static void actionCardLinkGhosts( Window window, Card card )
	{
		for( int i = 0; i < windows.length; i++ ) {
			if( windows[i] == window || windows[i] == null ) {
				continue;
			}
			if(
				windows[i].tabCurrent() == Tab.EDIT
				&& windows[i].tabEdit.newCard == true
				&& windows[i].tabEdit.ghost == true
				&& windows[i].tabEdit.card.identifierGet().longValue() == card.identifierGet().longValue()
			) {
				windows[i].tabEdit.linkGhost(card);
				continue;
			}
		}
	}

	public static void actionCardDeleted( Window window, Card card )
	{
		for( int i = 0; i < windows.length; i++ ) {
			if( windows[i] == window || windows[i] == null ) {
				continue;
			}
			if(
				windows[i].tabCurrent() == Tab.EDIT
				&& windows[i].tabEdit.newCard == false
				&& windows[i].tabEdit.card.identifierGet().longValue() == card.identifierGet().longValue()
			) {
				windows[i].tabEdit.markGhost();
			}
		}
		refreshSearches();
	}

	public static void actionOpenURI( String uri )
	{
		if( desktop == null ) {
			System.err.print("Failed to open URI: No 'Desktop' instance.\n");
			return;
		}
		try {
			if( desktop.isSupported( Desktop.Action.BROWSE ) ) {
				URI parsed = new URI(uri);
				desktop.browse(parsed);
			} else if( System.getProperty("os.name").equals("Linux") ) {
				System.out.print("URI: 'Browse' action is not supported, trying Linux-specific workarounds..\n");
				/* Try some common commands. Loosely inspired by 'https://stackoverflow.com/a/18004334'. */
				final String openCommands[] = {
					"xdg-open",
					//"gnome-open",
					//"kde-open",
				};
				String command[] = new String[2];
				command[1] = uri;
				for( int i = 0; i < openCommands.length; i++ ) {
					System.out.print("\tRunning '"+openCommands[i]+" "+uri+"'..\n");
					command[0] = openCommands[i];
					Process process;
					boolean success = false;
					try {
						process = Runtime.getRuntime().exec( command, null, null );
						if( process.exitValue() == 0 ) {
							success = true;
						}
					} catch( IOException e ) {
						/* I am assuming it means that the command is missing. */
					} catch( IllegalThreadStateException e ) {
						/* Process is still running. Count as success. */
						success = true;
					}
					if( success ) {
						System.out.print("URI: '"+openCommands[i]+"' seems to have worked.\n");
						return;
					}
				}
				System.err.print("Failed to open URI: All Linux-specific workarounds seem to have failed.\n");
			} else {
				System.err.print("Failed to open URI: 'Browse' action is not supported, and system-specific workarounds waren't triggered. (OS name: '"+System.getProperty("os.name")+"')\n");
			}
		} catch( Exception e ) {
			throw new RuntimeException(e);
		}
	}

	public static String clipboardGet()
	{
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		try {
			return (String)clipboard.getData( DataFlavor.stringFlavor );
		} catch( Exception e ) {
			return "";
		}
	}

	public static void clipboardSet( String string )
	{
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		StringSelection transferable = new StringSelection(string);
		System.out.print("Set system clipboard to '"+string+"'.\n");
		clipboard.setContents( transferable, transferable );
	}

}