From 9113c31612d99b422f2f84586347d6c482ccfd97 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 9 Jan 2021 11:13:17 +0100 Subject: [PATCH] UI defaults inspector: support copy key/value to clipboard --- CHANGELOG.md | 6 +- .../extras/FlatUIDefaultsInspector.java | 80 ++++++++++++++++++- .../extras/FlatUIDefaultsInspector.jfd | 22 +++++ 3 files changed, 105 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43aeca58..f8e57582 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,10 @@ FlatLaf Change Log buttons. - TextComponent: Clip placeholder text if it does not fit into visible area. (PR #229) -- Extras: Support embedding UI defaults inspector into any window (see - `FlatUIDefaultsInspector.createInspectorPanel()`. +- Extras: UI defaults inspector: + - Support embedding UI defaults inspector panel into any window. See + `FlatUIDefaultsInspector.createInspectorPanel()`. + - Copy selected keys and values into clipboard via context menu. #### Fixed bugs diff --git a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatUIDefaultsInspector.java b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatUIDefaultsInspector.java index 5a628fdf..9849f805 100644 --- a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatUIDefaultsInspector.java +++ b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatUIDefaultsInspector.java @@ -17,6 +17,7 @@ package com.formdev.flatlaf.extras; import java.awt.*; +import java.awt.datatransfer.StringSelection; import java.awt.event.*; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; @@ -325,7 +326,7 @@ public class FlatUIDefaultsInspector return items.toArray( new Item[items.size()] ); } - private void updateWindowTitle( JFrame frame ) { + private static void updateWindowTitle( JFrame frame ) { String title = frame.getTitle(); String sep = " - "; int sepIndex = title.indexOf( sep ); @@ -400,6 +401,49 @@ public class FlatUIDefaultsInspector return "(other)"; } + private void tableMousePressed( MouseEvent e ) { + if( !SwingUtilities.isRightMouseButton( e ) ) + return; + + int row = table.rowAtPoint( e.getPoint() ); + if( row >= 0 && !table.isRowSelected( row ) ) + table.setRowSelectionInterval( row, row ); + } + + private void copyKey() { + copyToClipboard( 0 ); + } + + private void copyValue() { + copyToClipboard( 1 ); + } + + private void copyKeyAndValue() { + copyToClipboard( -1 ); + } + + private void copyToClipboard( int column ) { + int[] rows = table.getSelectedRows(); + if( rows.length == 0 ) + return; + + StringBuilder buf = new StringBuilder(); + for( int i = 0; i < rows.length; i++ ) { + if( i > 0 ) + buf.append( '\n' ); + + if( column < 0 || column == 0 ) + buf.append( table.getValueAt( rows[i], 0 ) ); + if( column < 0 ) + buf.append( " = " ); + if( column < 0 || column == 1 ) + buf.append( table.getValueAt( rows[i], 1 ) ); + } + + Toolkit.getDefaultToolkit().getSystemClipboard().setContents( + new StringSelection( buf.toString() ), null ); + } + private void initComponents() { // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents panel = new JPanel(); @@ -410,6 +454,10 @@ public class FlatUIDefaultsInspector valueTypeField = new JComboBox<>(); scrollPane = new JScrollPane(); table = new JTable(); + tablePopupMenu = new JPopupMenu(); + copyKeyMenuItem = new JMenuItem(); + copyValueMenuItem = new JMenuItem(); + copyKeyAndValueMenuItem = new JMenuItem(); //======== panel ======== { @@ -472,10 +520,36 @@ public class FlatUIDefaultsInspector //---- table ---- table.setAutoCreateRowSorter(true); + table.setComponentPopupMenu(tablePopupMenu); + table.addMouseListener(new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + tableMousePressed(e); + } + }); scrollPane.setViewportView(table); } panel.add(scrollPane, BorderLayout.CENTER); } + + //======== tablePopupMenu ======== + { + + //---- copyKeyMenuItem ---- + copyKeyMenuItem.setText("Copy Key"); + copyKeyMenuItem.addActionListener(e -> copyKey()); + tablePopupMenu.add(copyKeyMenuItem); + + //---- copyValueMenuItem ---- + copyValueMenuItem.setText("Copy Value"); + copyValueMenuItem.addActionListener(e -> copyValue()); + tablePopupMenu.add(copyValueMenuItem); + + //---- copyKeyAndValueMenuItem ---- + copyKeyAndValueMenuItem.setText("Copy Key and Value"); + copyKeyAndValueMenuItem.addActionListener(e -> copyKeyAndValue()); + tablePopupMenu.add(copyKeyAndValueMenuItem); + } // JFormDesigner - End of component initialization //GEN-END:initComponents } @@ -488,6 +562,10 @@ public class FlatUIDefaultsInspector private JComboBox valueTypeField; private JScrollPane scrollPane; private JTable table; + private JPopupMenu tablePopupMenu; + private JMenuItem copyKeyMenuItem; + private JMenuItem copyValueMenuItem; + private JMenuItem copyKeyAndValueMenuItem; // JFormDesigner - End of variables declaration //GEN-END:variables //---- class Item --------------------------------------------------------- diff --git a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatUIDefaultsInspector.jfd b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatUIDefaultsInspector.jfd index 9c90fb66..731c5284 100644 --- a/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatUIDefaultsInspector.jfd +++ b/flatlaf-extras/src/main/java/com/formdev/flatlaf/extras/FlatUIDefaultsInspector.jfd @@ -66,6 +66,8 @@ new FormModel { add( new FormComponent( "javax.swing.JTable" ) { name: "table" "autoCreateRowSorter": true + "componentPopupMenu": new FormReference( "tablePopupMenu" ) + addEvent( new FormEvent( "java.awt.event.MouseListener", "mousePressed", "tableMousePressed", true ) ) } ) }, new FormLayoutConstraints( class java.lang.String ) { "value": "Center" @@ -74,5 +76,25 @@ new FormModel { "location": new java.awt.Point( 0, 0 ) "size": new java.awt.Dimension( 400, 300 ) } ) + add( new FormContainer( "javax.swing.JPopupMenu", new FormLayoutManager( class javax.swing.JPopupMenu ) ) { + name: "tablePopupMenu" + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "copyKeyMenuItem" + "text": "Copy Key" + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "copyKey", false ) ) + } ) + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "copyValueMenuItem" + "text": "Copy Value" + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "copyValue", false ) ) + } ) + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "copyKeyAndValueMenuItem" + "text": "Copy Key and Value" + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "copyKeyAndValue", false ) ) + } ) + }, new FormLayoutConstraints( null ) { + "location": new java.awt.Point( 0, 370 ) + } ) } }