Skip to content

Commit

Permalink
FileChooser: wrap shortcuts in scroll pane (issue #828)
Browse files Browse the repository at this point in the history
added tooltips to shortcuts
disabled group hover effect on shortcuts
  • Loading branch information
DevCharly committed Jun 21, 2024
1 parent 0c0d4bf commit c95e95e
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 32 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ FlatLaf Change Log

#### New features and improvements

- Button and ToggleButton: Added missing border colors for pressed and selected
states. (issue #848)
- Button and ToggleButton: Added border colors for pressed and selected states.
(issue #848)
- Label: Support painting background with rounded corners. (issue #842)
- Popup: Fixed flicker of popups (e.g. tooltips) while they are moving (e.g.
following mouse pointer). (issues #832 and #672)
- FileChooser: Wrap shortcuts in scroll pane. (issue #828)
- Theme Editor: On macOS, use larger window title bar. (PR #779)

#### Fixed bugs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.awt.Image;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
Expand All @@ -32,6 +33,7 @@
import java.util.ArrayList;
import java.util.function.Function;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
Expand All @@ -46,6 +48,7 @@
import javax.swing.JTable;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.filechooser.FileSystemView;
Expand Down Expand Up @@ -164,6 +167,7 @@ public class FlatFileChooserUI
{
private final FlatFileView fileView = new FlatFileView();
private FlatShortcutsPanel shortcutsPanel;
private JScrollPane shortcutsScrollPane;

public static ComponentUI createUI( JComponent c ) {
return new FlatFileChooserUI( (JFileChooser) c );
Expand All @@ -183,7 +187,10 @@ public void installComponents( JFileChooser fc ) {
FlatShortcutsPanel panel = createShortcutsPanel( fc );
if( panel.getComponentCount() > 0 ) {
shortcutsPanel = panel;
fc.add( shortcutsPanel, BorderLayout.LINE_START );
shortcutsScrollPane = new JScrollPane( shortcutsPanel,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
shortcutsScrollPane.setBorder( BorderFactory.createEmptyBorder() );
fc.add( shortcutsScrollPane, BorderLayout.LINE_START );
fc.addPropertyChangeListener( shortcutsPanel );
}
}
Expand All @@ -196,6 +203,7 @@ public void uninstallComponents( JFileChooser fc ) {
if( shortcutsPanel != null ) {
fc.removePropertyChangeListener( shortcutsPanel );
shortcutsPanel = null;
shortcutsScrollPane = null;
}
}

Expand Down Expand Up @@ -324,7 +332,7 @@ protected FlatShortcutsPanel createShortcutsPanel( JFileChooser fc ) {
public Dimension getPreferredSize( JComponent c ) {
Dimension prefSize = super.getPreferredSize( c );
Dimension minSize = getMinimumSize( c );
int shortcutsPanelWidth = (shortcutsPanel != null) ? shortcutsPanel.getPreferredSize().width : 0;
int shortcutsPanelWidth = (shortcutsScrollPane != null) ? shortcutsScrollPane.getPreferredSize().width : 0;
return new Dimension(
Math.max( prefSize.width, minSize.width + shortcutsPanelWidth ),
Math.max( prefSize.height, minSize.height ) );
Expand Down Expand Up @@ -401,7 +409,7 @@ public Icon getIcon( File f ) {
/** @since 2.3 */
public static class FlatShortcutsPanel
extends JToolBar
implements PropertyChangeListener
implements PropertyChangeListener, Scrollable
{
private final JFileChooser fc;

Expand All @@ -420,6 +428,7 @@ public FlatShortcutsPanel( JFileChooser fc ) {
super( JToolBar.VERTICAL );
this.fc = fc;
setFloatable( false );
putClientProperty( FlatClientProperties.STYLE, "hoverButtonGroupBackground: null" );

buttonSize = UIScale.scale( getUIDimension( "FileChooser.shortcuts.buttonSize", 84, 64 ) );
iconSize = getUIDimension( "FileChooser.shortcuts.iconSize", 32, 32 );
Expand Down Expand Up @@ -461,7 +470,7 @@ else if( icon != null )
icon = new ShortcutIcon( icon, iconSize.width, iconSize.height );

// create button
JToggleButton button = createButton( name, icon );
JToggleButton button = createButton( name, icon, file.toString() );
File f = file;
button.addActionListener( e -> {
fc.setCurrentDirectory( f );
Expand All @@ -487,8 +496,10 @@ private Dimension getUIDimension( String key, int defaultWidth, int defaultHeigh
return size;
}

protected JToggleButton createButton( String name, Icon icon ) {
/** @since 3.5 */
protected JToggleButton createButton( String name, Icon icon, String toolTip ) {
JToggleButton button = new JToggleButton( name, icon );
button.setToolTipText( toolTip );
button.setVerticalTextPosition( SwingConstants.BOTTOM );
button.setHorizontalTextPosition( SwingConstants.CENTER );
button.setAlignmentX( Component.CENTER_ALIGNMENT );
Expand Down Expand Up @@ -566,6 +577,8 @@ protected void directoryChanged( File file ) {
buttonGroup.clearSelection();
}

//---- interface PropertyChangeListener ----

@Override
public void propertyChange( PropertyChangeEvent e ) {
switch( e.getPropertyName() ) {
Expand All @@ -574,6 +587,41 @@ public void propertyChange( PropertyChangeEvent e ) {
break;
}
}

//---- interface Scrollable ----

@Override
public Dimension getPreferredScrollableViewportSize() {
if( getComponentCount() > 0 ) {
Insets insets = getInsets();
int height = (getComponent( 0 ).getPreferredSize().height * 5) + insets.top + insets.bottom;
return new Dimension( getPreferredSize().width, height );
}
return getPreferredSize();
}

@Override
public int getScrollableUnitIncrement( Rectangle visibleRect, int orientation, int direction ) {
if( orientation == SwingConstants.VERTICAL && getComponentCount() > 0 )
return getComponent( 0 ).getPreferredSize().height;

return getScrollableBlockIncrement( visibleRect, orientation, direction ) / 10;
}

@Override
public int getScrollableBlockIncrement( Rectangle visibleRect, int orientation, int direction ) {
return (orientation == SwingConstants.VERTICAL) ? visibleRect.height : visibleRect.width;
}

@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}

@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}

//---- class ShortcutIcon -------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
public class FlatFileChooserTest
extends FlatTestPanel
{
private static ShortcutsCount shortcutsCount = ShortcutsCount.home;

public static void main( String[] args ) {
// Locale.setDefault( Locale.FRENCH );
// Locale.setDefault( Locale.GERMAN );
Expand All @@ -52,8 +54,21 @@ public static void main( String[] args ) {
FlatTestFrame frame = FlatTestFrame.create( args, "FlatFileChooserTest" );

UIManager.put( "FileChooser.shortcuts.filesFunction", (Function<File[], File[]>) files -> {
if( shortcutsCount == null )
return files;
if( shortcutsCount == ShortcutsCount.empty )
return new File[0];

ArrayList<File> list = new ArrayList<>( Arrays.asList( files ) );
list.add( 0, new File( System.getProperty( "user.home" ) ) );
if( shortcutsCount == ShortcutsCount.home )
list.add( 0, new File( System.getProperty( "user.home" ) ) );
else {
File home = new File( System.getProperty( "user.home" ) );
File[] homeFiles = home.listFiles();
int count = shortcutsCount.value;
for( int i = 0; i < count; i++ )
list.add( i < homeFiles.length ? homeFiles[i] : new File( "Dummy " + i ) );
}
return list.toArray( new File[list.size()] );
} );

Expand All @@ -78,6 +93,8 @@ public static void main( String[] args ) {
dialogTypeField.init( DialogType.class, false );
fileSelectionModeField.init( FileSelectionMode.class, false );
localesField.init( Locales.class, false );
shortcutsCountField.init( ShortcutsCount.class, true );
shortcutsCountField.setSelectedValue( shortcutsCount );

showControlButtonsCheckBox.setSelected( fileChooser1.getControlButtonsAreShown() );
multiSelectionCheckBox.setSelected( fileChooser1.isMultiSelectionEnabled() );
Expand Down Expand Up @@ -197,6 +214,11 @@ private void localesChanged() {
} );
}

private void shortcutsCountChanged() {
shortcutsCount = shortcutsCountField.getSelectedValue();
fileChooser1.updateUI();
}

private void printShortcutFiles() {
printFiles( JavaCompatibility2.getChooserShortcutPanelFiles( fileChooser1.getFileSystemView() ) );
}
Expand Down Expand Up @@ -263,6 +285,8 @@ private void initComponents() {
printRootsButton = new JButton();
JLabel localesLabel = new JLabel();
localesField = new FlatTestEnumSelector<>();
JLabel label12 = new JLabel();
shortcutsCountField = new FlatTestEnumSelector<>();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
JLabel label3 = new JLabel();
Expand Down Expand Up @@ -291,6 +315,7 @@ private void initComponents() {
"[]" +
"[]" +
"[]" +
"[]" +
"[]para" +
"[]"));

Expand Down Expand Up @@ -432,49 +457,57 @@ private void initComponents() {
localesField.addActionListener(e -> localesChanged());
add(localesField, "cell 1 8 2 1");

//---- label12 ----
label12.setText("Shortcuts:");
add(label12, "cell 0 9");

//---- shortcutsCountField ----
shortcutsCountField.addActionListener(e -> shortcutsCountChanged());
add(shortcutsCountField, "cell 1 9 2 1");

//---- label1 ----
label1.setText("icons:");
add(label1, "cell 0 9");
add(label1, "cell 0 10");

//---- label2 ----
label2.setIcon(UIManager.getIcon("FileView.directoryIcon"));
add(label2, "cell 1 9 2 1");
add(label2, "cell 1 10 2 1");

//---- label3 ----
label3.setIcon(UIManager.getIcon("FileView.fileIcon"));
add(label3, "cell 1 9 2 1");
add(label3, "cell 1 10 2 1");

//---- label4 ----
label4.setIcon(UIManager.getIcon("FileView.computerIcon"));
add(label4, "cell 1 9 2 1");
add(label4, "cell 1 10 2 1");

//---- label5 ----
label5.setIcon(UIManager.getIcon("FileView.hardDriveIcon"));
add(label5, "cell 1 9 2 1");
add(label5, "cell 1 10 2 1");

//---- label6 ----
label6.setIcon(UIManager.getIcon("FileView.floppyDriveIcon"));
add(label6, "cell 1 9 2 1");
add(label6, "cell 1 10 2 1");

//---- label7 ----
label7.setIcon(UIManager.getIcon("FileChooser.newFolderIcon"));
add(label7, "cell 1 9 2 1");
add(label7, "cell 1 10 2 1");

//---- label8 ----
label8.setIcon(UIManager.getIcon("FileChooser.upFolderIcon"));
add(label8, "cell 1 9 2 1");
add(label8, "cell 1 10 2 1");

//---- label9 ----
label9.setIcon(UIManager.getIcon("FileChooser.homeFolderIcon"));
add(label9, "cell 1 9 2 1");
add(label9, "cell 1 10 2 1");

//---- label10 ----
label10.setIcon(UIManager.getIcon("FileChooser.detailsViewIcon"));
add(label10, "cell 1 9 2 1");
add(label10, "cell 1 10 2 1");

//---- label11 ----
label11.setIcon(UIManager.getIcon("FileChooser.listViewIcon"));
add(label11, "cell 1 9 2 1");
add(label11, "cell 1 10 2 1");
// JFormDesigner - End of component initialization //GEN-END:initComponents
}

Expand All @@ -500,6 +533,7 @@ private void initComponents() {
private JButton printComboBoxFilesButton;
private JButton printRootsButton;
private FlatTestEnumSelector<Locales> localesField;
private FlatTestEnumSelector<ShortcutsCount> shortcutsCountField;
// JFormDesigner - End of variables declaration //GEN-END:variables

//---- enum DialogType ----------------------------------------------------
Expand Down Expand Up @@ -553,4 +587,26 @@ enum Locales {
this.value = value;
}
}

//---- enum ShortcutsCount ------------------------------------------------

enum ShortcutsCount {
empty( -1 ),
home( -2 ),
zero( 0 ),
one( 1 ),
two( 2 ),
three( 3 ),
four( 4 ),
five( 5 ),
ten( 10 ),
twenty( 20 ),
thirty( 30 );

public final int value;

ShortcutsCount( int value ) {
this.value = value;
}
}
}
Loading

0 comments on commit c95e95e

Please sign in to comment.