package com.timepath.plaf.x.filechooser;

import com.timepath.plaf.OS;
import com.timepath.plaf.win.JnaFileChooser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * @author TimePath
 */
public class NativeFileChooser extends BaseFileChooser {

    private static final Logger LOG = Logger.getLogger(NativeFileChooser.class.getName());

    public NativeFileChooser() {
    }

    @NotNull
    private static BaseFileChooser which() {
        if (OS.isWindows()) {
            return new JnaFileChooser();
        }
        if (OS.isMac()) {
//            return new AWTFileChooser(); // FIXME
        }
        if (OS.isLinux()) {
            String de = System.getenv("XDG_CURRENT_DESKTOP");
            if (de != null) {
                return "KDE".equalsIgnoreCase(de) ? new KDialogFileChooser() : new ZenityFileChooser();
            }
        }
        return new SwingFileChooser();
    }

    @Nullable
    @Override
    public File[] choose() throws IOException {
        return getChooser().choose();
    }

    @NotNull
    private BaseFileChooser getChooser() {
        @NotNull BaseFileChooser chooser = which();
        chooser.setApproveButtonText(approveButtonText)
                .setTitle(dialogTitle)
                .setDialogType(dialogType)
                .setDirectory(directory)
                .setFileName(fileName)
                .setFileMode(fileMode)
                .setMultiSelectionEnabled(multiSelectionEnabled)
                .setParent(parent);
        for (ExtensionFilter ef : filters) {
            chooser.addFilter(ef);
        }
        return chooser;
    }
}

@@ -120,167 +120,166 @@
 ort 
-org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.util.logging.Logger
+java.io.File;%0Aimport java.io.IOException;%0Aimport java.util.logging.Logger;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable
 ;%0A%0A/
@@ -549,24 +549,42 @@
 r which() %7B%0A
+        try %7B%0A    
         if (
@@ -597,24 +597,28 @@
 indows()) %7B%0A
+    
             
@@ -650,34 +650,42 @@
 ();%0A        
-%7D%0A
+    %7D%0A//  
         
 if (OS.isMac
@@ -664,32 +664,34 @@
   %7D%0A//          
+  
 if (OS.isMac()) 
@@ -694,16 +694,20 @@
 ()) %7B%0A//
+    
         
@@ -748,16 +748,22 @@
 / FIXME%0A
+//    
         
@@ -764,32 +764,36 @@
       %7D%0A        
+   
+ 
 if (OS.isLinux()
@@ -792,24 +792,28 @@
 sLinux()) %7B%0A
+    
             
@@ -870,24 +870,28 @@
             
+   
+ 
 if (de != nu
@@ -892,24 +892,28 @@
  != null) %7B%0A
+    
             
@@ -1008,33 +1008,95 @@
 ();%0A            
+  
-%7D
+  %7D%0A            %7D%0A        %7D catch (final Throwable ignored) %7B
 %0A        %7D%0A     

Handle NativeFileChooser instantiation exceptions