@ -2,16 +2,22 @@ package com.fr.design.gui.ifilechooser;
import com.fr.design.i18n.Toolkit ;
import com.fr.design.i18n.Toolkit ;
import com.fr.design.upm.UpmUtils ;
import com.fr.design.mainframe.DesignerContext ;
import com.fr.log.FineLoggerFactory ;
import com.fr.log.FineLoggerFactory ;
import com.fr.stable.StringUtils ;
import com.sun.javafx.application.PlatformImpl ;
import com.sun.javafx.application.PlatformImpl ;
import javafx.application.Platform ;
import javafx.scene.Scene ;
import javafx.scene.control.Label ;
import javafx.scene.layout.Background ;
import javafx.scene.layout.BackgroundFill ;
import javafx.scene.paint.Color ;
import javafx.stage.DirectoryChooser ;
import javafx.stage.DirectoryChooser ;
import javafx.stage.FileChooser ;
import javafx.stage.FileChooser ;
import javafx.stage.Window ;
import javafx.stage.Modality ;
import javafx.stage.Stage ;
import javafx.stage.StageStyle ;
import javax.swing.* ;
import javax.swing.* ;
import javax.swing.filechooser.FileNameExtensionFilter ;
import java.awt.* ;
import java.awt.* ;
import java.io.File ;
import java.io.File ;
import java.util.List ;
import java.util.List ;
@ -40,9 +46,17 @@ public class JavaFxNativeFileChooser implements FileChooserProvider {
@Override
@Override
public int showDialog ( Component parent ) {
public int showDialog ( Component parent ) {
final CountDownLatch latch = new CountDownLatch ( 1 ) ;
final CountDownLatch latch = new CountDownLatch ( 1 ) ;
PlatformImpl . startup ( new Runnable ( ) {
PlatformImpl . startup ( ( ) - > {
} ) ;
Platform . setImplicitExit ( false ) ;
Platform . runLater ( new Runnable ( ) {
@Override
@Override
public void run ( ) {
public void run ( ) {
Component fileChooserParent = parent ;
if ( fileChooserParent = = null ) {
fileChooserParent = DesignerContext . getDesignerFrame ( ) ;
}
Stage stage = showCoverStage ( fileChooserParent ) ;
try {
try {
if ( fileSelectionMode = = FileSelectionMode . FILE | | fileSelectionMode = = FileSelectionMode . MULTIPLE_FILE ) {
if ( fileSelectionMode = = FileSelectionMode . FILE | | fileSelectionMode = = FileSelectionMode . MULTIPLE_FILE ) {
FileChooser fileChooser = new FileChooser ( ) ;
FileChooser fileChooser = new FileChooser ( ) ;
@ -50,12 +64,12 @@ public class JavaFxNativeFileChooser implements FileChooserProvider {
fileChooser . getExtensionFilters ( ) . addAll ( filters ) ;
fileChooser . getExtensionFilters ( ) . addAll ( filters ) ;
fileChooser . setInitialDirectory ( currentDirectory ) ;
fileChooser . setInitialDirectory ( currentDirectory ) ;
if ( fileSelectionMode = = FileSelectionMode . FILE ) {
if ( fileSelectionMode = = FileSelectionMode . FILE ) {
File file = fileChooser . showOpenDialog ( null ) ;
File file = fileChooser . showOpenDialog ( stage ) ;
if ( file ! = null ) {
if ( file ! = null ) {
selectedFiles = new File [ ] { file } ;
selectedFiles = new File [ ] { file } ;
}
}
} else if ( fileSelectionMode = = FileSelectionMode . MULTIPLE_FILE ) {
} else if ( fileSelectionMode = = FileSelectionMode . MULTIPLE_FILE ) {
List < File > fileList = fileChooser . showOpenMultipleDialog ( null ) ;
List < File > fileList = fileChooser . showOpenMultipleDialog ( stage ) ;
if ( fileList ! = null ) {
if ( fileList ! = null ) {
selectedFiles = new File [ fileList . size ( ) ] ;
selectedFiles = new File [ fileList . size ( ) ] ;
fileList . toArray ( selectedFiles ) ;
fileList . toArray ( selectedFiles ) ;
@ -65,17 +79,70 @@ public class JavaFxNativeFileChooser implements FileChooserProvider {
DirectoryChooser directoryChooser = new DirectoryChooser ( ) ;
DirectoryChooser directoryChooser = new DirectoryChooser ( ) ;
directoryChooser . setTitle ( title ) ;
directoryChooser . setTitle ( title ) ;
directoryChooser . setInitialDirectory ( currentDirectory ) ;
directoryChooser . setInitialDirectory ( currentDirectory ) ;
File folder = directoryChooser . showDialog ( null ) ;
File folder = directoryChooser . showDialog ( stage ) ;
if ( folder ! = null ) {
if ( folder ! = null ) {
selectedFiles = new File [ ] { folder } ;
selectedFiles = new File [ ] { folder } ;
}
}
System . out . println ( folder ) ;
}
}
} catch ( Exception e ) {
} catch ( Exception e ) {
FineLoggerFactory . getLogger ( ) . error ( e , e . getMessage ( ) ) ;
FineLoggerFactory . getLogger ( ) . error ( e , e . getMessage ( ) ) ;
} finally {
} finally {
latch . countDown ( ) ;
latch . countDown ( ) ;
closeCoverStage ( stage ) ;
}
}
private void closeCoverStage ( Stage stage ) {
if ( stage ! = null ) {
stage . close ( ) ;
closeCoverStage ( ( Stage ) stage . getOwner ( ) ) ;
}
}
private Stage showCoverStage ( Component component ) {
try {
if ( component = = null )
return null ;
Stage parentStage = showCoverStage ( component . getParent ( ) ) ;
if ( component instanceof JDialog | | component instanceof JFrame ) {
return createStage ( component . getX ( ) , component . getY ( ) , component . getWidth ( ) , component . getHeight ( ) , parentStage ) ;
} else {
return parentStage ;
}
} catch ( Exception e ) {
FineLoggerFactory . getLogger ( ) . error ( e , e . getMessage ( ) ) ;
return null ;
}
}
private Stage createStage ( double x , double y , double w , double h , Stage parentStage ) {
try {
Stage stage = new Stage ( ) ;
stage . setX ( x ) ;
stage . setY ( y ) ;
stage . setWidth ( w ) ;
stage . setHeight ( h ) ;
stage . setOpacity ( 0 . 2 ) ;
stage . setResizable ( false ) ;
stage . initStyle ( StageStyle . UNDECORATED ) ;
Label label = new Label ( ) ;
label . setBackground (
new Background ( new BackgroundFill ( Color . color ( 0 . 78 , 0 . 78 , 0 . 80 , 0 . 5 ) , null , null ) ) ) ;
stage . setScene ( new Scene ( label ) ) ;
if ( parentStage ! = null ) {
stage . initOwner ( parentStage ) ;
stage . initModality ( Modality . WINDOW_MODAL ) ;
}
stage . show ( ) ;
return stage ;
} catch ( Exception e ) {
FineLoggerFactory . getLogger ( ) . error ( e , e . getMessage ( ) ) ;
return null ;
}
}
}
}
} ) ;
} ) ;