Added a 'copy link location' popup to ModInfoArea links
This commit is contained in:
parent
5d95ec072d
commit
eb31a6b58d
1 changed files with 45 additions and 0 deletions
|
@ -4,8 +4,13 @@ import java.awt.Color;
|
||||||
import java.awt.Cursor;
|
import java.awt.Cursor;
|
||||||
import java.awt.Desktop;
|
import java.awt.Desktop;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.datatransfer.StringSelection;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import javax.swing.AbstractAction;
|
||||||
|
import javax.swing.JPopupMenu;
|
||||||
import javax.swing.JScrollPane;
|
import javax.swing.JScrollPane;
|
||||||
import javax.swing.JTextPane;
|
import javax.swing.JTextPane;
|
||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
|
@ -37,7 +42,10 @@ public class ModInfoArea extends JScrollPane {
|
||||||
public static Color COLOR_HYPER = Color.BLUE;
|
public static Color COLOR_HYPER = Color.BLUE;
|
||||||
public static final StyleContext DEFAULT_STYLES = ModInfoArea.getDefaultStyleContext();
|
public static final StyleContext DEFAULT_STYLES = ModInfoArea.getDefaultStyleContext();
|
||||||
|
|
||||||
|
private JPopupMenu linkPopup = new JPopupMenu();
|
||||||
|
|
||||||
private Statusbar statusbar = null;
|
private Statusbar statusbar = null;
|
||||||
|
private String lastClickedLinkTarget = null;
|
||||||
|
|
||||||
private JTextPane textPane;
|
private JTextPane textPane;
|
||||||
private StyledDocument doc;
|
private StyledDocument doc;
|
||||||
|
@ -62,12 +70,32 @@ public class ModInfoArea extends JScrollPane {
|
||||||
browseWorks = true;
|
browseWorks = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
linkPopup.add( new AbstractAction( "Copy link address" ) {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed( ActionEvent ae ) {
|
||||||
|
if ( lastClickedLinkTarget != null ) {
|
||||||
|
Toolkit.getDefaultToolkit().getSystemClipboard().setContents( new StringSelection( lastClickedLinkTarget ), null );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
MouseInputAdapter hyperlinkListener = new MouseInputAdapter() {
|
MouseInputAdapter hyperlinkListener = new MouseInputAdapter() {
|
||||||
private Cursor defaultCursor = new Cursor( Cursor.DEFAULT_CURSOR );
|
private Cursor defaultCursor = new Cursor( Cursor.DEFAULT_CURSOR );
|
||||||
private Cursor linkCursor = new Cursor( Cursor.HAND_CURSOR );
|
private Cursor linkCursor = new Cursor( Cursor.HAND_CURSOR );
|
||||||
private boolean wasOverLink = false;
|
private boolean wasOverLink = false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mousePressed( MouseEvent e ) {
|
||||||
|
if ( e.isConsumed() ) return;
|
||||||
|
if ( e.isPopupTrigger() ) showMenu( e );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseReleased( MouseEvent e ) {
|
||||||
|
if ( e.isConsumed() ) return;
|
||||||
|
if ( e.isPopupTrigger() ) showMenu( e );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked( MouseEvent e ) {
|
public void mouseClicked( MouseEvent e ) {
|
||||||
if ( e.isConsumed() ) return;
|
if ( e.isConsumed() ) return;
|
||||||
|
@ -108,6 +136,23 @@ public class ModInfoArea extends JScrollPane {
|
||||||
wasOverLink = false;
|
wasOverLink = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void showMenu( MouseEvent e ) {
|
||||||
|
AttributeSet tmpAttr = doc.getCharacterElement( textPane.viewToModel( e.getPoint() ) ).getAttributes();
|
||||||
|
Object targetObj = tmpAttr.getAttribute( ATTR_HYPERLINK_TARGET );
|
||||||
|
if ( targetObj != null ) { // Link menu.
|
||||||
|
textPane.requestFocus();
|
||||||
|
|
||||||
|
lastClickedLinkTarget = targetObj.toString();
|
||||||
|
|
||||||
|
int nx = e.getX();
|
||||||
|
if ( nx > 500 ) nx = nx - linkPopup.getSize().width;
|
||||||
|
|
||||||
|
linkPopup.show( e.getComponent(), nx, e.getY() - linkPopup.getSize().height );
|
||||||
|
|
||||||
|
e.consume();
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
textPane.addMouseListener( hyperlinkListener );
|
textPane.addMouseListener( hyperlinkListener );
|
||||||
textPane.addMouseMotionListener( hyperlinkListener );
|
textPane.addMouseMotionListener( hyperlinkListener );
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue