UI defaults inspector: support copy key/value to clipboard

This commit is contained in:
Karl Tauber
2021-01-09 11:13:17 +01:00
parent 00b4e0a6fd
commit 9113c31612
3 changed files with 105 additions and 3 deletions

View File

@@ -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

View File

@@ -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<String> 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 ---------------------------------------------------------

View File

@@ -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 )
} )
}
}