System File Chooser: implemented modality for GtkFileChooserDialog on Linux

This commit is contained in:
Karl Tauber
2025-01-04 12:22:14 +01:00
parent a303cd2dec
commit 641fada6c4
5 changed files with 226 additions and 46 deletions

View File

@@ -17,10 +17,12 @@
package com.formdev.flatlaf.testing;
import static com.formdev.flatlaf.ui.FlatNativeLinuxLibrary.*;
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.SecondaryLoop;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
@@ -75,6 +77,26 @@ public class FlatSystemFileChooserLinuxTest
}
private void openOrSave( boolean open, boolean direct ) {
Window frame = SwingUtilities.windowForComponent( this );
if( ownerFrameRadioButton.isSelected() )
openOrSave( open, direct, frame );
else if( ownerDialogRadioButton.isSelected() ) {
JDialog dialog = new JDialog( frame, "Dummy Modal Dialog", Dialog.DEFAULT_MODALITY_TYPE );
dialog.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
dialog.addWindowListener( new WindowAdapter() {
@Override
public void windowOpened( WindowEvent e ) {
openOrSave( open, direct, dialog );
}
} );
dialog.setSize( 1200, 1000 );
dialog.setLocationRelativeTo( this );
dialog.setVisible( true );
} else
openOrSave( open, direct, null );
}
private void openOrSave( boolean open, boolean direct, Window owner ) {
String title = n( titleField.getText() );
String okButtonLabel = n( okButtonLabelField.getText() );
String currentName = n( currentNameField.getText() );
@@ -103,7 +125,7 @@ public class FlatSystemFileChooserLinuxTest
int fileTypeIndex = fileTypeIndexSlider.getValue();
if( direct ) {
String[] files = FlatNativeLinuxLibrary.showFileChooser( open,
String[] files = FlatNativeLinuxLibrary.showFileChooser( owner, open,
title, okButtonLabel, currentName, currentFolder,
optionsSet.get(), optionsClear.get(), fileTypeIndex, fileTypes );
@@ -113,7 +135,7 @@ public class FlatSystemFileChooserLinuxTest
String[] fileTypes2 = fileTypes;
new Thread( () -> {
String[] files = FlatNativeLinuxLibrary.showFileChooser( open,
String[] files = FlatNativeLinuxLibrary.showFileChooser( owner, open,
title, okButtonLabel, currentName, currentFolder,
optionsSet.get(), optionsClear.get(), fileTypeIndex, fileTypes2 );
@@ -198,6 +220,11 @@ public class FlatSystemFileChooserLinuxTest
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
ownerLabel = new JLabel();
ownerFrameRadioButton = new JRadioButton();
ownerDialogRadioButton = new JRadioButton();
ownerNullRadioButton = new JRadioButton();
ownerSpacer = new JPanel(null);
titleLabel = new JLabel();
titleField = new JTextField();
panel1 = new JPanel();
@@ -239,12 +266,31 @@ public class FlatSystemFileChooserLinuxTest
"[]" +
"[]" +
"[]" +
"[]" +
"[grow,fill]"));
//---- ownerLabel ----
ownerLabel.setText("owner");
add(ownerLabel, "cell 0 0");
//---- ownerFrameRadioButton ----
ownerFrameRadioButton.setText("JFrame");
ownerFrameRadioButton.setSelected(true);
add(ownerFrameRadioButton, "cell 1 0");
//---- ownerDialogRadioButton ----
ownerDialogRadioButton.setText("JDialog");
add(ownerDialogRadioButton, "cell 1 0");
//---- ownerNullRadioButton ----
ownerNullRadioButton.setText("null");
add(ownerNullRadioButton, "cell 1 0");
add(ownerSpacer, "cell 1 0,growx");
//---- titleLabel ----
titleLabel.setText("title");
add(titleLabel, "cell 0 0");
add(titleField, "cell 1 0");
add(titleLabel, "cell 0 1");
add(titleField, "cell 1 1");
//======== panel1 ========
{
@@ -288,26 +334,26 @@ public class FlatSystemFileChooserLinuxTest
local_onlyCheckBox.setText("local_only");
panel1.add(local_onlyCheckBox, "cell 0 5");
}
add(panel1, "cell 2 0 1 6,aligny top,growy 0");
add(panel1, "cell 2 1 1 6,aligny top,growy 0");
//---- okButtonLabelLabel ----
okButtonLabelLabel.setText("okButtonLabel");
add(okButtonLabelLabel, "cell 0 1");
add(okButtonLabelField, "cell 1 1");
add(okButtonLabelLabel, "cell 0 2");
add(okButtonLabelField, "cell 1 2");
//---- currentNameLabel ----
currentNameLabel.setText("currentName");
add(currentNameLabel, "cell 0 2");
add(currentNameField, "cell 1 2");
add(currentNameLabel, "cell 0 3");
add(currentNameField, "cell 1 3");
//---- currentFolderLabel ----
currentFolderLabel.setText("currentFolder");
add(currentFolderLabel, "cell 0 3");
add(currentFolderField, "cell 1 3");
add(currentFolderLabel, "cell 0 4");
add(currentFolderField, "cell 1 4");
//---- fileTypesLabel ----
fileTypesLabel.setText("fileTypes");
add(fileTypesLabel, "cell 0 4");
add(fileTypesLabel, "cell 0 5");
//---- fileTypesField ----
fileTypesField.setEditable(true);
@@ -317,11 +363,11 @@ public class FlatSystemFileChooserLinuxTest
"Text Files,*.txt,null,PDF Files,*.pdf,null,All Files,*,null",
"Text and PDF Files,*.txt,*.pdf,null"
}));
add(fileTypesField, "cell 1 4");
add(fileTypesField, "cell 1 5");
//---- fileTypeIndexLabel ----
fileTypeIndexLabel.setText("fileTypeIndex");
add(fileTypeIndexLabel, "cell 0 5");
add(fileTypeIndexLabel, "cell 0 6");
//---- fileTypeIndexSlider ----
fileTypeIndexSlider.setMaximum(10);
@@ -329,27 +375,27 @@ public class FlatSystemFileChooserLinuxTest
fileTypeIndexSlider.setValue(0);
fileTypeIndexSlider.setPaintLabels(true);
fileTypeIndexSlider.setSnapToTicks(true);
add(fileTypeIndexSlider, "cell 1 5");
add(fileTypeIndexSlider, "cell 1 6");
//---- openButton ----
openButton.setText("Open...");
openButton.addActionListener(e -> open());
add(openButton, "cell 0 6 3 1");
add(openButton, "cell 0 7 3 1");
//---- saveButton ----
saveButton.setText("Save...");
saveButton.addActionListener(e -> save());
add(saveButton, "cell 0 6 3 1");
add(saveButton, "cell 0 7 3 1");
//---- openDirectButton ----
openDirectButton.setText("Open (no-thread)...");
openDirectButton.addActionListener(e -> openDirect());
add(openDirectButton, "cell 0 6 3 1");
add(openDirectButton, "cell 0 7 3 1");
//---- saveDirectButton ----
saveDirectButton.setText("Save (no-thread)...");
saveDirectButton.addActionListener(e -> saveDirect());
add(saveDirectButton, "cell 0 6 3 1");
add(saveDirectButton, "cell 0 7 3 1");
//======== filesScrollPane ========
{
@@ -358,11 +404,22 @@ public class FlatSystemFileChooserLinuxTest
filesField.setRows(8);
filesScrollPane.setViewportView(filesField);
}
add(filesScrollPane, "cell 0 7 3 1,growx");
add(filesScrollPane, "cell 0 8 3 1,growx");
//---- ownerButtonGroup ----
ButtonGroup ownerButtonGroup = new ButtonGroup();
ownerButtonGroup.add(ownerFrameRadioButton);
ownerButtonGroup.add(ownerDialogRadioButton);
ownerButtonGroup.add(ownerNullRadioButton);
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
private JLabel ownerLabel;
private JRadioButton ownerFrameRadioButton;
private JRadioButton ownerDialogRadioButton;
private JRadioButton ownerNullRadioButton;
private JPanel ownerSpacer;
private JLabel titleLabel;
private JTextField titleField;
private JPanel panel1;

View File

@@ -6,19 +6,52 @@ new FormModel {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "ltr,insets dialog,hidemode 3"
"$columnConstraints": "[left][grow,fill][fill]"
"$rowConstraints": "[][][][][][][][grow,fill]"
"$rowConstraints": "[][][][][][][][][grow,fill]"
} ) {
name: "this"
add( new FormComponent( "javax.swing.JLabel" ) {
name: "ownerLabel"
"text": "owner"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
} )
add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "ownerFrameRadioButton"
"text": "JFrame"
"$buttonGroup": new FormReference( "ownerButtonGroup" )
"selected": true
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0"
} )
add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "ownerDialogRadioButton"
"text": "JDialog"
"$buttonGroup": new FormReference( "ownerButtonGroup" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0"
} )
add( new FormComponent( "javax.swing.JRadioButton" ) {
name: "ownerNullRadioButton"
"text": "null"
"$buttonGroup": new FormReference( "ownerButtonGroup" )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0"
} )
add( new FormComponent( "com.jformdesigner.designer.wrapper.HSpacer" ) {
name: "ownerSpacer"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0,growx"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "titleLabel"
"text": "title"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
"value": "cell 0 1"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "titleField"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 0"
"value": "cell 1 1"
} )
add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "insets 2,hidemode 3"
@@ -67,46 +100,46 @@ new FormModel {
"value": "cell 0 5"
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 0 1 6,aligny top,growy 0"
"value": "cell 2 1 1 6,aligny top,growy 0"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "okButtonLabelLabel"
"text": "okButtonLabel"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 1"
"value": "cell 0 2"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "okButtonLabelField"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 1"
"value": "cell 1 2"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "currentNameLabel"
"text": "currentName"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 2"
"value": "cell 0 3"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "currentNameField"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 2"
"value": "cell 1 3"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "currentFolderLabel"
"text": "currentFolder"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 3"
"value": "cell 0 4"
} )
add( new FormComponent( "javax.swing.JTextField" ) {
name: "currentFolderField"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 3"
"value": "cell 1 4"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "fileTypesLabel"
"text": "fileTypes"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4"
"value": "cell 0 5"
} )
add( new FormComponent( "javax.swing.JComboBox" ) {
name: "fileTypesField"
@@ -119,13 +152,13 @@ new FormModel {
addElement( "Text and PDF Files,*.txt,*.pdf,null" )
}
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 4"
"value": "cell 1 5"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "fileTypeIndexLabel"
"text": "fileTypeIndex"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 5"
"value": "cell 0 6"
} )
add( new FormComponent( "javax.swing.JSlider" ) {
name: "fileTypeIndexSlider"
@@ -135,35 +168,35 @@ new FormModel {
"paintLabels": true
"snapToTicks": true
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 5"
"value": "cell 1 6"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "openButton"
"text": "Open..."
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "open", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 6 3 1"
"value": "cell 0 7 3 1"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "saveButton"
"text": "Save..."
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "save", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 6 3 1"
"value": "cell 0 7 3 1"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "openDirectButton"
"text": "Open (no-thread)..."
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "openDirect", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 6 3 1"
"value": "cell 0 7 3 1"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "saveDirectButton"
"text": "Save (no-thread)..."
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "saveDirect", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 6 3 1"
"value": "cell 0 7 3 1"
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "filesScrollPane"
@@ -172,11 +205,16 @@ new FormModel {
"rows": 8
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 7 3 1,growx"
"value": "cell 0 8 3 1,growx"
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
"size": new java.awt.Dimension( 690, 630 )
} )
add( new FormNonVisual( "javax.swing.ButtonGroup" ) {
name: "ownerButtonGroup"
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 640 )
} )
}
}