View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    * 
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.view.swing.dialog;
12  
13  import com.jsql.util.I18nUtil;
14  import com.jsql.view.swing.util.MediatorHelper;
15  
16  import javax.swing.*;
17  
18  /**
19   * File chooser for supporting 'file already exists'.
20   */
21  public class ReplaceFileChooser extends JFileChooser {
22      
23      /**
24       * Create a file chooser with a replace confirm dialog.
25       */
26      public ReplaceFileChooser(String currentDirectoryPath) {
27          // Unhandled NoSuchMethodError #82561 on constructor: NoSuchMethodError
28          // Unhandled InternalError #93015 on constructor: InvocationTargetException
29          super(currentDirectoryPath);
30      }
31      
32      @Override
33      public void approveSelection() {
34          if (this.getDialogType() == JFileChooser.SAVE_DIALOG) {
35              var file = this.getSelectedFile();
36              if (file.exists()) {
37                  int result = JOptionPane.showConfirmDialog(
38                      MediatorHelper.frame(),
39                      String.format(
40                          "%s %s",
41                          this.getSelectedFile().getName(),
42                          I18nUtil.valueByKey("SAVE_TAB_CONFIRM_LABEL")
43                      ),
44                      I18nUtil.valueByKey("SAVE_TAB_CONFIRM_TITLE"),
45                      JOptionPane.YES_NO_OPTION
46                  );
47                  switch (result) {
48                      case JOptionPane.YES_OPTION:
49                          super.approveSelection();
50                          return;
51                      case JOptionPane.NO_OPTION:
52                      case JOptionPane.CLOSED_OPTION:
53                          return;
54                      case JOptionPane.CANCEL_OPTION:
55                          this.cancelSelection();
56                          return;
57                      default:
58                          break;
59                  }
60              } else {
61                  super.approveSelection();
62              }
63          }
64      }
65  }