private void saveFiles()

in dbus-java-utils/src/main/java/org/freedesktop/dbus/viewer/FileSaver.java [51:115]


    private void saveFiles() {
        String overwritePolicy = null;
        final Iterator<TextFile> iterator = textFiles.iterator();
        while (iterator.hasNext()) {
            final TextFile textFile = iterator.next();
            String fileName = textFile.getFileName();
            File fileToSave = new File(parentDirectory, fileName);
            File parentFile = fileToSave.getParentFile();
            if (parentFile.exists() || parentFile.mkdirs()) {
                boolean doSave = !fileToSave.exists() || OVERWRITE_ALL.equals(overwritePolicy);
                if (!doSave && !SKIP_ALL.equals(overwritePolicy)) {
                    String[] selectionValues;
                    if (iterator.hasNext()) {
                        selectionValues = new String[] {
                                OVERWRITE, OVERWRITE_ALL, SKIP, SKIP_ALL, CANCEL
                        };
                    } else {
                        selectionValues = new String[] {
                                OVERWRITE, CANCEL
                        };
                    }
                    int option = JOptionPane.showOptionDialog(parentComponent, "File exists: " + fileName, "Save", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, selectionValues, null);
                    if (option == -1) {
                        break;
                    }
                    overwritePolicy = selectionValues[option];
                    if (CANCEL.equals(overwritePolicy)) {
                        break;
                    }

                    doSave = OVERWRITE.equals(overwritePolicy) || OVERWRITE_ALL.equals(overwritePolicy);
                }
                if (doSave) {
                    try {
                        String contents = textFile.getContents();
                        writeFile(fileToSave, contents);
                    } catch (final IOException ex) {
                        /* Can't access parent directory for saving */
                        final String errorMessage = "Could not save " + fileName + ": " + ex.getLocalizedMessage();
                        if (iterator.hasNext()) {

                            int confirm = JOptionPane.showConfirmDialog(parentComponent, errorMessage + ".\n" + "Try saving other files?", "Save Failed", JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);
                            if (confirm != JOptionPane.OK_OPTION) {
                                break;
                            }
                        } else {
                            JOptionPane.showMessageDialog(parentComponent, errorMessage + ".", "Save Failed", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            } else {

                final String errorMessage = "Could not access parent directory for " + fileName;
                if (iterator.hasNext()) {

                    int confirm = JOptionPane.showConfirmDialog(parentComponent, errorMessage + ".\n" + "Try saving other files?", "Save Failed", JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);
                    if (confirm != JOptionPane.OK_OPTION) {
                        break;
                    }
                } else {
                    JOptionPane.showMessageDialog(parentComponent, errorMessage + ".", "Save Failed", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }