Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2020 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Predicate;
import java.util.prefs.Preferences;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.icons.FlatAbstractIcon;
import com.formdev.flatlaf.ui.FlatBorder;
import com.formdev.flatlaf.ui.FlatEmptyBorder;
import com.formdev.flatlaf.ui.FlatLineBorder;
import com.formdev.flatlaf.ui.FlatMarginBorder;
import com.formdev.flatlaf.ui.FlatUIUtils;
import com.formdev.flatlaf.util.DerivedColor;
import com.formdev.flatlaf.util.GrayFilter;
import com.formdev.flatlaf.util.HSLColor;
import com.formdev.flatlaf.util.LoggingFacade;
import com.formdev.flatlaf.util.ScaledEmptyBorder;
import com.formdev.flatlaf.util.UIScale;
/**
* A simple UI defaults inspector that shows a window with all UI defaults used
* in current look and feel.
*
* To use it in an application install it with:
*
* FlatUIDefaultsInspector.install( "ctrl shift alt Y" );
*
* This can be done e.g. in the main() method and allows enabling (and disabling)
* the UI defaults inspector with the given keystroke.
*
* @author Karl Tauber
*/
public class FlatUIDefaultsInspector
{
private static final int KEY_MODIFIERS_MASK = InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK | InputEvent.ALT_DOWN_MASK | InputEvent.META_DOWN_MASK;
private static JFrame inspectorFrame;
private final PropertyChangeListener lafListener = this::lafChanged;
private final PropertyChangeListener lafDefaultsListener = this::lafDefaultsChanged;
private boolean refreshPending;
private Properties derivedColorKeys;
/**
* Installs a key listener into the application that allows enabling and disabling
* the UI inspector with the given keystroke (e.g. "ctrl shift alt Y").
*
* @param activationKeys a keystroke (e.g. "ctrl shift alt Y")
*/
public static void install( String activationKeys ) {
KeyStroke keyStroke = KeyStroke.getKeyStroke( activationKeys );
Toolkit.getDefaultToolkit().addAWTEventListener( e -> {
if( e.getID() == KeyEvent.KEY_RELEASED &&
((KeyEvent)e).getKeyCode() == keyStroke.getKeyCode() &&
(((KeyEvent)e).getModifiersEx() & KEY_MODIFIERS_MASK) == (keyStroke.getModifiers() & KEY_MODIFIERS_MASK) )
{
show();
}
}, AWTEvent.KEY_EVENT_MASK );
}
public static void show() {
if( inspectorFrame != null ) {
ensureOnScreen( inspectorFrame );
inspectorFrame.toFront();
return;
}
inspectorFrame = new FlatUIDefaultsInspector().createFrame();
inspectorFrame.setVisible( true );
}
public static void hide() {
if( inspectorFrame != null )
inspectorFrame.dispose();
}
/**
* Creates a UI defaults inspector panel that can be embedded into any window.
*/
public static JComponent createInspectorPanel() {
return new FlatUIDefaultsInspector().panel;
}
private FlatUIDefaultsInspector() {
initComponents();
panel.setBorder( new ScaledEmptyBorder( 10, 10, 10, 10 ) );
filterPanel.setBorder( new ScaledEmptyBorder( 0, 0, 10, 0 ) );
// initialize filter
filterField.getDocument().addDocumentListener( new DocumentListener() {
@Override
public void removeUpdate( DocumentEvent e ) {
filterChanged();
}
@Override
public void insertUpdate( DocumentEvent e ) {
filterChanged();
}
@Override
public void changedUpdate( DocumentEvent e ) {
filterChanged();
}
} );
delegateKey( KeyEvent.VK_UP, "unitScrollUp" );
delegateKey( KeyEvent.VK_DOWN, "unitScrollDown" );
delegateKey( KeyEvent.VK_PAGE_UP, "scrollUp" );
delegateKey( KeyEvent.VK_PAGE_DOWN, "scrollDown" );
// initialize table
table.setModel( new ItemsTableModel( getUIDefaultsItems() ) );
table.setDefaultRenderer( String.class, new KeyRenderer() );
table.setDefaultRenderer( Item.class, new ValueRenderer() );
table.getRowSorter().setSortKeys( Collections.singletonList(
new RowSorter.SortKey( 0, SortOrder.ASCENDING ) ) );
// restore column widths
Preferences prefs = getPrefs();
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn( 0 ).setPreferredWidth( prefs.getInt( "column1width", 100 ) );
columnModel.getColumn( 1 ).setPreferredWidth( prefs.getInt( "column2width", 100 ) );
PropertyChangeListener columnWidthListener = e -> {
if( "width".equals( e.getPropertyName() ) ) {
prefs.putInt( "column1width", columnModel.getColumn( 0 ).getWidth() );
prefs.putInt( "column2width", columnModel.getColumn( 1 ).getWidth() );
}
};
columnModel.getColumn( 0 ).addPropertyChangeListener( columnWidthListener );
columnModel.getColumn( 1 ).addPropertyChangeListener( columnWidthListener );
// restore filter
String filter = prefs.get( "filter", "" );
String valueType = prefs.get( "valueType", null );
if( filter != null && !filter.isEmpty() )
filterField.setText( filter );
if( valueType != null )
valueTypeField.setSelectedItem( valueType );
panel.addPropertyChangeListener( "ancestor", e -> {
if( e.getNewValue() != null ) {
UIManager.addPropertyChangeListener( lafListener );
UIManager.getDefaults().addPropertyChangeListener( lafDefaultsListener );
} else {
UIManager.removePropertyChangeListener( lafListener );
UIManager.getDefaults().removePropertyChangeListener( lafDefaultsListener );
}
} );
// register F5 key to refresh
panel.registerKeyboardAction(
e -> refresh(),
KeyStroke.getKeyStroke( KeyEvent.VK_F5, 0, false ),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
}
private JFrame createFrame() {
JFrame frame = new JFrame();
frame.setTitle( "UI Defaults Inspector" );
frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
frame.addWindowListener( new WindowAdapter() {
@Override
public void windowClosed( WindowEvent e ) {
inspectorFrame = null;
}
@Override
public void windowClosing( WindowEvent e ) {
saveWindowBounds( frame );
}
@Override
public void windowDeactivated( WindowEvent e ) {
saveWindowBounds( frame );
}
} );
updateWindowTitle( frame );
frame.getContentPane().add( panel, BorderLayout.CENTER );
// restore window bounds
Preferences prefs = getPrefs();
int x = prefs.getInt( "x", -1 );
int y = prefs.getInt( "y", -1 );
int width = prefs.getInt( "width", UIScale.scale( 600 ) );
int height = prefs.getInt( "height", UIScale.scale( 800 ) );
frame.setSize( width, height );
if( x != -1 && y != -1 ) {
frame.setLocation( x, y );
ensureOnScreen( frame );
} else
frame.setLocationRelativeTo( null );
// register ESC key to close frame
((JComponent)frame.getContentPane()).registerKeyboardAction(
e -> frame.dispose(),
KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0, false ),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
return frame;
}
private void delegateKey( int keyCode, String actionKey ) {
KeyStroke keyStroke = KeyStroke.getKeyStroke( keyCode, 0 );
String actionMapKey = "delegate-" + actionKey;
filterField.getInputMap().put( keyStroke, actionMapKey );
filterField.getActionMap().put( actionMapKey, new AbstractAction() {
@Override
public void actionPerformed( ActionEvent e ) {
Action action = scrollPane.getActionMap().get( actionKey );
if( action != null ) {
action.actionPerformed( new ActionEvent( scrollPane,
e.getID(), actionKey, e.getWhen(), e.getModifiers() ) );
}
}
} );
}
private static void ensureOnScreen( JFrame frame ) {
Rectangle frameBounds = frame.getBounds();
boolean onScreen = false;
for( GraphicsDevice screen : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices() ) {
GraphicsConfiguration gc = screen.getDefaultConfiguration();
Rectangle screenBounds = FlatUIUtils.subtractInsets( gc.getBounds(),
Toolkit.getDefaultToolkit().getScreenInsets( gc ) );
if( frameBounds.intersects( screenBounds ) ) {
onScreen = true;
break;
}
}
if( !onScreen )
frame.setLocationRelativeTo( null );
}
private void lafChanged( PropertyChangeEvent e ) {
if( "lookAndFeel".equals( e.getPropertyName() ) )
refresh();
}
private void lafDefaultsChanged( PropertyChangeEvent e ) {
if( refreshPending )
return;
refreshPending = true;
EventQueue.invokeLater( () -> {
refresh();
refreshPending = false;
} );
}
private void refresh() {
ItemsTableModel model = (ItemsTableModel) table.getModel();
model.setItems( getUIDefaultsItems() );
JFrame frame = (JFrame) SwingUtilities.getAncestorOfClass( JFrame.class, panel );
if( frame != null )
updateWindowTitle( frame );
}
private Item[] getUIDefaultsItems() {
UIDefaults defaults = UIManager.getDefaults();
UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults();
Set> defaultsSet = defaults.entrySet();
ArrayList items = new ArrayList<>( defaultsSet.size() );
HashSet