changeset 73:d1faf5db459c

TabEdit: Started on tag context menu, and started on 'open as URI' option
author Fox
date Sat, 07 Jan 2023 00:58:11 +0100
parents 3f25b75e6dac
children 1d37914defe0
files src/junotu/Main.java src/junotu/TabEdit.java
diffstat 2 files changed, 77 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/junotu/Main.java	Fri Jan 06 23:49:28 2023 +0100
+++ b/src/junotu/Main.java	Sat Jan 07 00:58:11 2023 +0100
@@ -3,6 +3,8 @@
 import java.lang.RuntimeException;
 
 import javax.swing.SwingUtilities;
+import java.awt.Desktop;
+import java.net.URI;
 
 import junotu.Database;
 import junotu.Window;
@@ -15,10 +17,12 @@
 
     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();
 	
 	SwingUtilities.invokeLater(
             new Runnable() {
@@ -101,5 +105,18 @@
 	window.tabEdit.cardEdit( card );
 	System.out.print( "Opening edit tab to edit '"+card.titleGet()+"'.\n" );
     }
+
+    public static void actionOpenURI( String uri )
+    {
+	if( desktop == null ) {
+	    return;
+	}
+	try {
+	    URI parsed = new URI(uri);
+	    desktop.browse(parsed);
+	} catch( Exception e ) {
+	    throw new RuntimeException(e);
+	}
+    }
     
 }
--- a/src/junotu/TabEdit.java	Fri Jan 06 23:49:28 2023 +0100
+++ b/src/junotu/TabEdit.java	Sat Jan 07 00:58:11 2023 +0100
@@ -8,6 +8,8 @@
 import java.awt.event.KeyEvent;
 import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
 
 import javax.swing.KeyStroke;
 import javax.swing.SwingUtilities;
@@ -25,6 +27,8 @@
 import javax.swing.JTextArea;
 import javax.swing.JScrollPane;
 import javax.swing.JScrollBar;
+import javax.swing.JPopupMenu;
+import javax.swing.JMenuItem;
 
 import junotu.Main;
 import junotu.Database;
@@ -32,9 +36,9 @@
 import junotu.GUIToolbox;
 import junotu.Card;
 
-public class TabEdit extends JPanel implements ActionListener {
+public class TabEdit extends JPanel implements ActionListener, MouseListener {
 
-    private class TagWidget extends JButton implements ActionListener {
+    private class TagWidget extends JButton {
 	
 	public String tag;
 	public Object value;
@@ -45,8 +49,6 @@
 	    this.value = value;
 	    this.setToolTipText("Click to edit this tag. Use ':' to delimit name and value.");
 
-	    addActionListener(this);
-	    
 	    update();
 	}
 
@@ -58,12 +60,6 @@
 		setText( tag );
 	    }	    
 	}
-
-	public void actionPerformed( ActionEvent e )
-	{
-	    /* Not exactly sure how this works, but it does. */
-	    tagEdit( this );
-	}
 	
     }
 
@@ -88,6 +84,9 @@
     private JButton editAsBoard;
     private JButton save;
 
+    private JPopupMenu tagMenu;
+    private JMenuItem tagMenu_OpenUri;
+
     
     public TabEdit()
     {
@@ -107,6 +106,9 @@
 	editAsBoard = new JButton("As board");
 	save        = new JButton("Save");
 
+	tagMenu = new JPopupMenu("Tag menu");
+	tagMenu_OpenUri = tagMenu.add("Open as URI");
+
 	tags.setLayout( new FlowLayout() );
 
 	title.setFont( new Font( "Monospaced", Font.PLAIN, 32 ) );
@@ -143,6 +145,8 @@
 	save.addActionListener(this);
 	addTag.addActionListener(this);
 
+	tagMenu_OpenUri.addActionListener(this);
+
 	editedTagField.addFocusListener(
 					new FocusListener()
 					{
@@ -190,6 +194,8 @@
 	save.setToolTipText("Save and go back. Shift-click to save without exiting. Can also use [CTRL]+[S].");
 
 	addTag.setToolTipText("Add new tag.");
+
+	tagMenu_OpenUri.setToolTipText("Open tag's value as URI; most commonly a file path ('file://') or URL ('http://').");
 	
     }
 
@@ -260,12 +266,16 @@
     private void updateTags()
     {
 	tags.removeAll();
+	TagWidget newWidget;
 	for( String tag : card.tagNames() ) {
 	    if( tag.startsWith("_") ) {
 		continue;
 	    }
 	    for( Object value : card.tagValues( tag ) ) {
-		tags.add( new TagWidget( tag, value ) );
+		newWidget = new TagWidget( tag, value );
+		tags.add( newWidget );
+		newWidget.addActionListener(this);
+		newWidget.addMouseListener(this);
 	    }
 	}
 	tags.add( addTag );
@@ -348,8 +358,11 @@
 	    
 	} else { /* Adding new tag (value). */
 	    if( !newTag.equals("") ) {
+		TagWidget newWidget = new TagWidget( newTag, newValue );
 		card.tagValueAdd( newTag, newValue );
-		tags.add( new TagWidget( newTag, newValue ), GUIToolbox.componentGetIndex( addTag )-1 );
+		tags.add( newWidget, GUIToolbox.componentGetIndex( addTag )-1 );
+		newWidget.addActionListener(this);
+		newWidget.addMouseListener(this);
 		logTagChange( "", null, newTag, newValue );
 	    } else {
 		logTagChange( "", null, "", null );
@@ -491,6 +504,18 @@
 	}
     }
 
+    private boolean possiblyShowTagContextMenu( MouseEvent e )
+    {
+	if( e.isPopupTrigger() ) {
+	    TagWidget tagWidget = (TagWidget)e.getSource();
+	    tagMenu_OpenUri.setEnabled( tagWidget.value instanceof String );
+	    tagMenu.show( tagWidget, e.getX(), e.getY() );
+	    return true;
+	} else {
+	    return false;
+	}
+    }
+
     public void actionPerformed( ActionEvent e )
     {
 	Object source = e.getSource();
@@ -520,6 +545,29 @@
 	    buttonClickedSave( noSwitch );
 	} else if( source == addTag ) {
 	    tagAdd();
+	} else if( source instanceof TagWidget ) {
+	    tagEdit( (TagWidget)source );
+	} else if( source == tagMenu_OpenUri ) {
+	    TagWidget tagWidget = (TagWidget)tagMenu.getInvoker();
+	    Main.actionOpenURI( (String)tagWidget.value );
+	}
+    }
+
+    public void mouseClicked( MouseEvent e ) {}
+    public void mouseEntered( MouseEvent e ) {}
+    public void mouseExited( MouseEvent e ) {}
+    
+    public void mousePressed( MouseEvent e )
+    {
+	if( possiblyShowTagContextMenu(e) ) {
+	    return;
+	}
+    }
+
+    public void mouseReleased( MouseEvent e )
+    {
+	if( possiblyShowTagContextMenu(e) ) {
+	    return;
 	}
     }