Example usage for javax.swing JFileChooser setCurrentDirectory

List of usage examples for javax.swing JFileChooser setCurrentDirectory

Introduction

In this page you can find the example usage for javax.swing JFileChooser setCurrentDirectory.

Prototype

@BeanProperty(preferred = true, description = "The directory that the JFileChooser is showing files of.")
public void setCurrentDirectory(File dir) 

Source Link

Document

Sets the current directory.

Usage

From source file:org.gumtree.vis.awt.JChartPanel.java

@Override
public void doSaveAs() throws IOException {

    JFileChooser fileChooser = new JFileChooser();
    String currentDirectory = System.getProperty(StaticValues.SYSTEM_SAVE_PATH_LABEL);
    if (currentDirectory != null) {
        File savePath = new File(currentDirectory);
        if (savePath.exists() && savePath.isDirectory()) {
            fileChooser.setCurrentDirectory(savePath);
        }//from w  ww .java  2 s.  com
    }
    ExtensionFileFilter ascFilter = new ExtensionFileFilter("Text_Files", ".txt");
    ExtensionFileFilter jpgFilter = new ExtensionFileFilter("JPG_Image_Files", ".jpg");
    ExtensionFileFilter pngFilter = new ExtensionFileFilter("PNG_Image_Files", ".png");
    fileChooser.addChoosableFileFilter(pngFilter);
    fileChooser.addChoosableFileFilter(jpgFilter);
    fileChooser.addChoosableFileFilter(ascFilter);
    fileChooser.setFileFilter(jpgFilter);
    int option = fileChooser.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION) {
        String filename = fileChooser.getSelectedFile().getPath();
        String selectedDescription = fileChooser.getFileFilter().getDescription();
        String fileExtension = StaticValues.DEFAULT_IMAGE_FILE_EXTENSION;
        if (selectedDescription.toLowerCase().contains("png")) {
            fileExtension = "png";
            if (!filename.toLowerCase().endsWith(".png")) {
                filename = filename + ".png";
            }
        } else if (selectedDescription.toLowerCase().contains("jpg")) {
            fileExtension = "jpg";
            if (!filename.toLowerCase().endsWith(".jpg")) {
                filename = filename + ".jpg";
            }
        } else if (selectedDescription.toLowerCase().contains("text")) {
            fileExtension = "txt";
            if (!filename.toLowerCase().endsWith(".txt")) {
                filename = filename + ".txt";
            }
        }
        File selectedFile = new File(filename);
        int confirm = JOptionPane.YES_OPTION;
        if (selectedFile.exists()) {
            confirm = JOptionPane.showConfirmDialog(this, selectedFile.getName() + " exists, overwrite?",
                    "Confirm Overwriting", JOptionPane.YES_NO_OPTION);
        }
        if (confirm == JOptionPane.YES_OPTION) {
            saveTo(filename, fileExtension);
            System.setProperty(StaticValues.SYSTEM_SAVE_PATH_LABEL, fileChooser.getSelectedFile().getParent());
        }
    }
}

From source file:com.openbravo.pos.util.JRViewer411.java

void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
    // Add your handling code here:

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setLocale(this.getLocale());
    fileChooser.updateUI();//from  ww w . j a v a 2 s .co  m
    for (int i = 0; i < saveContributors.size(); i++) {
        fileChooser.addChoosableFileFilter(saveContributors.get(i));
    }

    if (saveContributors.contains(lastSaveContributor)) {
        fileChooser.setFileFilter(lastSaveContributor);
    } else if (saveContributors.size() > 0) {
        fileChooser.setFileFilter(saveContributors.get(0));
    }

    if (lastFolder != null) {
        fileChooser.setCurrentDirectory(lastFolder);
    }

    int retValue = fileChooser.showSaveDialog(this);
    if (retValue == JFileChooser.APPROVE_OPTION) {
        FileFilter fileFilter = fileChooser.getFileFilter();
        File file = fileChooser.getSelectedFile();

        lastFolder = file.getParentFile();

        JRSaveContributor contributor = null;

        if (fileFilter instanceof JRSaveContributor) {
            contributor = (JRSaveContributor) fileFilter;
        } else {
            int i = 0;
            while (contributor == null && i < saveContributors.size()) {
                contributor = saveContributors.get(i++);
                if (!contributor.accept(file)) {
                    contributor = null;
                }
            }

            if (contributor == null) {
                contributor = new JRPrintSaveContributor(getLocale(), this.resourceBundle);
            }
        }

        lastSaveContributor = contributor;

        try {
            contributor.save(jasperPrint, file);
        } catch (JRException e) {
            if (log.isErrorEnabled()) {
                log.error("Save error.", e);
            }
            JOptionPane.showMessageDialog(this, getBundleString("error.saving"));
        }
    }
}

From source file:com.dfki.av.sudplan.ui.MainFrame.java

private void miOpenDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miOpenDataActionPerformed
    try {/*  ww w.  ja  va 2s  .  co m*/
        String cmd = evt.getActionCommand();
        final JFileChooser fc = new JFileChooser();

        FileFilter fileFilter;
        // Set file filter..
        if (cmd.equalsIgnoreCase(miOpenKMLFile.getActionCommand())) {
            fileFilter = new FileNameExtensionFilter("KML/KMZ File", "kml", "kmz");
        } else if (cmd.equalsIgnoreCase(miAddGeoTiff.getActionCommand())) {
            fileFilter = new FileNameExtensionFilter("GeoTiff File ( *.tif, *.tiff)", "tif", "tiff");
        } else if (cmd.equalsIgnoreCase(miAddShape.getActionCommand())) {
            fileFilter = new FileNameExtensionFilter("ESRI Shapefile (*.shp)", "shp", "SHP");
        } else if (cmd.equalsIgnoreCase(miAddShapeZip.getActionCommand())) {
            fileFilter = new FileNameExtensionFilter("ESRI Shapefile ZIP (*.zip)", "zip", "ZIP");
        } else {
            log.warn("No valid action command.");
            fileFilter = null;
        }
        fc.setFileFilter(fileFilter);

        // Set latest working directory...
        XMLConfiguration xmlConfig = Configuration.getXMLConfiguration();
        String path = xmlConfig.getString("sudplan3D.working.dir");
        File dir;
        if (path != null) {
            dir = new File(path);
            if (dir.exists()) {
                fc.setCurrentDirectory(dir);
            }
        }

        // Show dialog...
        int ret = fc.showOpenDialog(this);

        // Save currently selected working directory...
        dir = fc.getCurrentDirectory();
        path = dir.getAbsolutePath();
        xmlConfig.setProperty("sudplan3D.working.dir", path);

        if (ret != JFileChooser.APPROVE_OPTION) {
            return;
        }

        if (cmd.equalsIgnoreCase(miOpenKMLFile.getActionCommand())) {
            wwPanel.addKMLLayer(fc.getSelectedFile());
        } else if (cmd.equalsIgnoreCase(miAddGeoTiff.getActionCommand())) {
            wwPanel.addGeoTiffLayer(fc.getSelectedFile());
        } else if (cmd.equalsIgnoreCase(miAddShape.getActionCommand())
                || cmd.equalsIgnoreCase(miAddShapeZip.getActionCommand())) {
            IVisAlgorithm algo = VisAlgorithmFactory.newInstance(VisPointCloud.class.getName());
            if (algo != null) {
                wwPanel.addLayer(fc.getSelectedFile(), algo, null);
            } else {
                log.error("VisAlgorithm {} not supported.", VisPointCloud.class.getName());
                JOptionPane.showMessageDialog(this, "Algorithm not supported.", "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        } else {
            log.warn("No valid action command.");
        }
    } catch (Exception ex) {
        log.error(ex.toString());
        JOptionPane.showMessageDialog(this, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}

From source file:org.pgptool.gui.ui.decryptone.DecryptOnePm.java

public SaveFileChooserDialog getTargetFileChooser() {
    if (targetFileChooser == null) {
        targetFileChooser = new SaveFileChooserDialog(findRegisteredWindowIfAny(), "action.chooseTargetFile",
                "action.choose", appProps, "DecryptionTargetChooser") {
            @Override/*w w w  . ja  va2 s  .c o  m*/
            protected String onDialogClosed(String filePathName, JFileChooser ofd) {
                String ret = super.onDialogClosed(filePathName, ofd);
                if (ret != null) {
                    targetFile.setValueByOwner(ret);
                }
                return ret;
            }

            @Override
            protected void suggestTarget(JFileChooser ofd) {
                String sourceFileStr = sourceFile.getValue();
                if (StringUtils.hasText(targetFile.getValue())) {
                    // Case 1: Based on current target
                    use(ofd, targetFile.getValue());
                } else if (decryptionDialogParameters != null
                        && decryptionDialogParameters.getTargetFile() != null) {
                    if (decryptionDialogParameters.getSourceFile().equals(sourceFileStr)) {
                        // exact match
                        use(ofd, decryptionDialogParameters.getTargetFile());
                    } else {
                        // case when suggested parameters are provided for
                        // neighbor
                        use(ofd, madeUpTargetFileName(FilenameUtils
                                .getFullPathNoEndSeparator(decryptionDialogParameters.getTargetFile())));
                    }
                } else if (StringUtils.hasText(sourceFileStr) && new File(sourceFileStr).exists()) {
                    use(ofd, madeUpTargetFileName(FilenameUtils.getFullPathNoEndSeparator(sourceFileStr)));
                }
            }

            private void use(JFileChooser ofd, String filePathName) {
                ofd.setCurrentDirectory(new File(FilenameUtils.getFullPathNoEndSeparator(filePathName)));
                ofd.setSelectedFile(new File(filePathName));
            }
        };
    }
    return targetFileChooser;
}

From source file:neg.JRViewer.java

void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
    // Add your handling code here:

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setLocale(this.getLocale());
    fileChooser.updateUI();/*from  ww w.  j av a 2s .  c o m*/
    for (int i = 0; i < saveContributors.size(); i++) {
        fileChooser.addChoosableFileFilter((JRSaveContributor) saveContributors.get(i));
    }

    if (saveContributors.contains(lastSaveContributor)) {
        fileChooser.setFileFilter(lastSaveContributor);
    } else if (saveContributors.size() > 0) {
        fileChooser.setFileFilter((JRSaveContributor) saveContributors.get(0));
    }

    if (lastFolder != null) {
        fileChooser.setCurrentDirectory(lastFolder);
    }

    int retValue = fileChooser.showSaveDialog(this);
    if (retValue == JFileChooser.APPROVE_OPTION) {
        FileFilter fileFilter = fileChooser.getFileFilter();
        File file = fileChooser.getSelectedFile();

        lastFolder = file.getParentFile();

        JRSaveContributor contributor = null;

        if (fileFilter instanceof JRSaveContributor) {
            contributor = (JRSaveContributor) fileFilter;
        } else {
            int i = 0;
            while (contributor == null && i < saveContributors.size()) {
                contributor = (JRSaveContributor) saveContributors.get(i++);
                if (!contributor.accept(file)) {
                    contributor = null;
                }
            }

            if (contributor == null) {
                contributor = new JRPrintSaveContributor(getLocale(), this.resourceBundle);
            }
        }

        lastSaveContributor = contributor;

        try {
            contributor.save(jasperPrint, file);
        } catch (JRException e) {
            if (log.isErrorEnabled())
                log.error("Save error.", e);

            JOptionPane.showMessageDialog(this, getBundleString("error.saving"));
        }
    }
}

From source file:cn.pholance.datamanager.common.components.JRViewer.java

void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
    // Add your handling code here:

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setLocale(this.getLocale());
    fileChooser.updateUI();/*from  w w  w  .j a  va 2  s  .c  o m*/
    for (int i = 0; i < saveContributors.size(); i++) {
        fileChooser.addChoosableFileFilter(saveContributors.get(i));
    }

    if (saveContributors.contains(lastSaveContributor)) {
        fileChooser.setFileFilter(lastSaveContributor);
    } else if (saveContributors.size() > 0) {
        fileChooser.setFileFilter(saveContributors.get(0));
    }

    if (lastFolder != null) {
        fileChooser.setCurrentDirectory(lastFolder);
    }

    int retValue = fileChooser.showSaveDialog(this);
    if (retValue == JFileChooser.APPROVE_OPTION) {
        FileFilter fileFilter = fileChooser.getFileFilter();
        File file = fileChooser.getSelectedFile();

        lastFolder = file.getParentFile();

        JRSaveContributor contributor = null;

        if (fileFilter instanceof JRSaveContributor) {
            contributor = (JRSaveContributor) fileFilter;
        } else {
            int i = 0;
            while (contributor == null && i < saveContributors.size()) {
                contributor = saveContributors.get(i++);
                if (!contributor.accept(file)) {
                    contributor = null;
                }
            }

            if (contributor == null) {
                contributor = new JRPrintSaveContributor(this.jasperReportsContext, getLocale(),
                        this.resourceBundle);
            }
        }

        lastSaveContributor = contributor;

        try {
            contributor.save(jasperPrint, file);
        } catch (JRException e) {
            if (log.isErrorEnabled()) {
                log.error("Save error.", e);
            }
            JOptionPane.showMessageDialog(this, getBundleString("error.saving"));
        }
    }
}

From source file:org.pgptool.gui.ui.encryptone.EncryptOnePm.java

public SaveFileChooserDialog getTargetFileChooser() {
    if (targetFileChooser == null) {
        targetFileChooser = new SaveFileChooserDialog(findRegisteredWindowIfAny(), "action.chooseTargetFile",
                "action.choose", appProps, "EncryptionTargetChooser") {
            @Override/*from   w w w.  j  a va 2s.com*/
            protected String onDialogClosed(String filePathName, JFileChooser ofd) {
                String ret = super.onDialogClosed(filePathName, ofd);
                if (ret != null) {
                    targetFile.setValueByOwner(ret);
                }
                return ret;
            }

            @Override
            protected void onFileChooserPostConstrct(JFileChooser ofd) {
                ofd.setAcceptAllFileFilterUsed(false);
                ofd.addChoosableFileFilter(new FileNameExtensionFilter("GPG Files (.pgp)", "pgp"));
                // NOTE: Should we support other extensions?....
                ofd.addChoosableFileFilter(ofd.getAcceptAllFileFilter());
                ofd.setFileFilter(ofd.getChoosableFileFilters()[0]);
            }

            @Override
            protected void suggestTarget(JFileChooser ofd) {
                String sourceFileStr = sourceFile.getValue();
                if (StringUtils.hasText(targetFile.getValue())) {
                    use(ofd, targetFile.getValue());
                } else if (encryptionDialogParameters != null
                        && encryptionDialogParameters.getTargetFile() != null) {
                    if (encryptionDialogParameters.getSourceFile().equals(sourceFile.getValue())) {
                        use(ofd, encryptionDialogParameters.getTargetFile());
                    } else {
                        use(ofd, madeUpTargetFileName(sourceFile.getValue(), FilenameUtils
                                .getFullPathNoEndSeparator(encryptionDialogParameters.getTargetFile())));
                    }
                } else if (StringUtils.hasText(sourceFileStr) && new File(sourceFileStr).exists()) {
                    String basePath = FilenameUtils.getFullPathNoEndSeparator(sourceFileStr);
                    ofd.setCurrentDirectory(new File(basePath));
                    ofd.setSelectedFile(new File(madeUpTargetFileName(sourceFileStr, basePath)));
                }
            }

            private void use(JFileChooser ofd, String filePathName) {
                ofd.setCurrentDirectory(new File(FilenameUtils.getFullPathNoEndSeparator(filePathName)));
                ofd.setSelectedFile(new File(filePathName));
            }
        };
    }
    return targetFileChooser;
}

From source file:net.rptools.maptool.launcher.MapToolLauncher.java

private JPanel buildBasicPanel() {
    final JPanel p = new JPanel();
    p.setLayout(new BorderLayout());

    // BASIC:  Top panel
    final JPanel logoPanel = new JPanel();
    logoPanel.setLayout(new FlowLayout());
    logoPanel.setBorder(/*from  w  w  w  . j  a  v  a  2  s. c  om*/
            new TitledBorder(new LineBorder(Color.BLACK), CopiedFromOtherJars.getText("msg.logoPanel.border"))); //$NON-NLS-1$

    jlMTLogo.setIcon(icon);
    logoPanel.add(jlMTLogo);

    // BASIC:  Middle panel
    final JPanel memPanel = new JPanel();
    memPanel.setLayout(new GridLayout(3, 2));
    memPanel.setBorder(new LineBorder(Color.WHITE));

    jtfMaxMem.setHorizontalAlignment(SwingConstants.RIGHT);
    jtfMaxMem.setInfo(CopiedFromOtherJars.getText("msg.info.javaMaxMem", DEFAULT_MAXMEM)); //$NON-NLS-1$ 
    jtfMaxMem.setToolTipText(CopiedFromOtherJars.getText("msg.tooltip.javaMaxMem")); //$NON-NLS-1$
    jtfMaxMem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            jtfMaxMemActionPerformed(evt);
        }
    });
    jtfMaxMem.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent evt) {
            jtfMaxMemFocusLost(evt);
        }

        @Override
        public void focusGained(FocusEvent evt) {
            jtfMaxMemFocusLost(evt);
        }
    });
    jtfMaxMem.addKeyListener(new InputValidator());

    jtfMinMem.setHorizontalAlignment(SwingConstants.RIGHT);
    jtfMinMem.setInfo(CopiedFromOtherJars.getText("msg.info.javaMinMem", DEFAULT_MINMEM)); //$NON-NLS-1$ 
    jtfMinMem.setToolTipText(CopiedFromOtherJars.getText("msg.tooltip.javaMinMem")); //$NON-NLS-1$
    jtfMinMem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            jtfMinMemActionPerformed(evt);
        }
    });
    jtfMinMem.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent evt) {
            jtfMinMemFocusLost(evt);
        }

        @Override
        public void focusGained(FocusEvent evt) {
            jtfMinMemFocusLost(evt);
        }
    });
    jtfMinMem.addKeyListener(new InputValidator());

    jtfStackSize.setHorizontalAlignment(SwingConstants.RIGHT);
    jtfStackSize.setInfo(CopiedFromOtherJars.getText("msg.info.javaStackSize", DEFAULT_STACKSIZE)); //$NON-NLS-1$ 
    jtfStackSize.setToolTipText(CopiedFromOtherJars.getText("msg.tooltip.javaStackSize")); //$NON-NLS-1$
    jtfStackSize.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            jtfStackSizeActionPerformed(evt);
        }
    });
    jtfStackSize.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent evt) {
            jtfStackSizeFocusLost(evt);
        }

        @Override
        public void focusGained(FocusEvent evt) {
            jtfStackSizeFocusLost(evt);
        }
    });
    jtfStackSize.addKeyListener(new InputValidator());

    memPanel.add(jtfMaxMem);
    memPanel.add(jtfMinMem);
    memPanel.add(jtfStackSize);

    // BASIC:  Bottom panel
    final JPanel southPanel = new JPanel();
    southPanel.setLayout(new BorderLayout());

    final JPanel cbPanel = new JPanel();
    cbPanel.setLayout(new GridLayout(2, 1));
    cbPanel.setBorder(new LineBorder(Color.GRAY));

    jcbPromptUser.setSelected(true);
    jcbPromptUser.setText(CopiedFromOtherJars.getText("msg.info.promptAtNextLaunch")); //$NON-NLS-1$
    jcbPromptUser.setToolTipText(CopiedFromOtherJars.getText("msg.tooltip.promptAtNextLaunch")); //$NON-NLS-1$
    jcbPromptUser.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent e) {
            promptUser = jcbPromptUser.isSelected();
        }
    });

    jbMTJar.setText(jbMTJarText);
    jbMTJar.setToolTipText(CopiedFromOtherJars.getText("msg.tooltip.registerMapToolJar")); //$NON-NLS-1$
    jbMTJar.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            FileFilter filter = new FileNameExtensionFilter(
                    CopiedFromOtherJars.getText("msg.chooser.javaExecutable"), "jar"); //$NON-NLS-1$ //$NON-NLS-2$
            jfc.addChoosableFileFilter(filter);
            jfc.setFileFilter(filter);
            if (IS_MAC) {
                filter = new FileNameExtensionFilter(
                        CopiedFromOtherJars.getText("msg.chooser.appleApplicationBundle"), "app"); //$NON-NLS-1$ //$NON-NLS-2$
                jfc.addChoosableFileFilter(filter);
            }
            jfc.setCurrentDirectory(mapToolJarDir);

            final int returnVal = jfc.showOpenDialog(jbMTJar);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                final File f = jfc.getSelectedFile();
                final String fileName = f.getName();
                if (IS_MAC && fileName.endsWith(".app")) { //$NON-NLS-1$
                    File jarDir = new File(f.getParentFile(), fileName);
                    if (jarDir.isDirectory()) {
                        jarDir = new File(jarDir, "Contents/Resources/Java"); //$NON-NLS-1$
                        if (jarDir.isDirectory()) {
                            mapToolJarDir = jarDir;
                            mapToolJarName = fileName.replace(".app", ".jar"); //$NON-NLS-1$ //$NON-NLS-2$
                        } else {
                            logMsg(Level.SEVERE,
                                    "{0} does not contain 'Contents/Resources/Java' like it should!", //$NON-NLS-1$
                                    "msg.chooser.badAppLocation", jarDir); //$NON-NLS-1$
                            return;
                        }
                    } else {
                        logMsg(Level.SEVERE, "{0} is not a directory and it should be!", //$NON-NLS-1$
                                "msg.chooser.badAppLocation", jarDir); //$NON-NLS-1$
                        return;
                    }
                } else {
                    mapToolJarName = fileName;
                    mapToolJarDir = f.getParentFile();
                }
                logMsg(Level.INFO, f.toString(), null);
                jbMTJar.setText(fileName.replace(".jar", EMPTY)); //$NON-NLS-1$
                if (fileName.toLowerCase().startsWith("maptool-")) {
                    // We expect the name matches 'maptool-1.3.b89.jar'
                    mapToolVersion = " " + fileName.substring(8, 11);
                } else {
                    logMsg(Level.SEVERE, "Cannot determine MapTool version number from JAR filename: {0}", //$NON-NLS-1$
                            "msg.info.noMapToolVersion", fileName);
                    mapToolVersion = EMPTY;
                }
                jbLaunch.setEnabled(true);
                updateCommand();
                jbLaunch.requestFocusInWindow();
            }
        }
    });

    cbPanel.add(jcbPromptUser);
    cbPanel.add(jbMTJar);

    southPanel.add(cbPanel, BorderLayout.CENTER);

    p.add(memPanel, BorderLayout.CENTER);
    p.add(logoPanel, BorderLayout.NORTH);
    p.add(southPanel, BorderLayout.SOUTH);
    p.setBorder(new LineBorder(Color.BLACK));
    return p;
}

From source file:mondrian.gui.Workbench.java

private void openMenuItemActionPerformed(ActionEvent evt) {
    JFileChooser jfc = new JFileChooser();
    try {/*from w w  w  .ja  va 2 s  . c  o m*/
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
            public boolean accept(File pathname) {
                return pathname.getName().toLowerCase().endsWith(".xml") || pathname.isDirectory();
            }

            public String getDescription() {
                return getResourceConverter().getString("workbench.open.schema.file.type",
                        "Mondrian Schema files (*.xml)");
            }
        });

        String lastUsed = getWorkbenchProperty(LAST_USED1_URL);

        if (lastUsed != null) {
            jfc.setCurrentDirectory(new File(new URI(lastUsed)));
        }
    } catch (Exception ex) {
        LOGGER.error("Could not set file chooser", ex);
    }
    MondrianProperties.instance();
    if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        try {
            setLastUsed(jfc.getSelectedFile().getName(), jfc.getSelectedFile().toURI().toURL().toString());
        } catch (MalformedURLException e) {
            LOGGER.error(e);
        }

        openSchemaFrame(jfc.getSelectedFile(), false);
    }
}

From source file:ffx.ui.MainPanel.java

/**
 * <p>/* ww w . ja  v  a 2s.c  o  m*/
 * createKeyFile</p>
 *
 * @param system a {@link ffx.ui.FFXSystem} object.
 * @return a boolean.
 */
public boolean createKeyFile(FFXSystem system) {
    String message = new String("Please select a parameter file " + "and a TINKER Key file will be created.");
    String params = (String) JOptionPane.showInputDialog(this, message, "Parameter File",
            JOptionPane.QUESTION_MESSAGE, null, keywordPanel.getParamFiles(), null);
    if (params != null) {
        if (params.equalsIgnoreCase("Use an existing TINKER Key file")) {
            JFileChooser fc = resetFileChooser();
            fc.setDialogTitle("Choose a KEY File");
            fc.setCurrentDirectory(pwd);
            fc.setSelectedFile(null);
            fc.setFileFilter(keyFileFilter);
            int result = fc.showOpenDialog(this);
            if (result == JFileChooser.APPROVE_OPTION) {
                File keyfile = fc.getSelectedFile();
                if (keyfile.exists()) {
                    Hashtable<String, Keyword> keywordHash = KeyFilter.open(keyfile);
                    if (keywordHash != null) {
                        system.setKeywords(keywordHash);
                    } else {
                        return false;
                    }
                    system.setKeyFile(keyfile);
                    system.setForceField(null);
                    return true;
                }
            }
        } else {
            File tempFile = system.getFile();
            if (tempFile.getParentFile().canWrite()) {
                String path = system.getFile().getParent() + File.separatorChar;
                String keyFileName = system.getName() + ".key";
                File keyfile = new File(path + keyFileName);
                try {
                    FileWriter fw = new FileWriter(keyfile);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write("\n");
                    bw.write("# Force Field Selection\n");
                    String tempParm = keywordPanel.getParamPath(params);
                    if (tempParm.indexOf(" ") > 0) {
                        tempParm = "\"" + keywordPanel.getParamPath(params) + "\"";
                    }
                    bw.write("PARAMETERS        " + tempParm + "\n");
                    bw.close();
                    fw.close();
                    Hashtable<String, Keyword> keywordHash = KeyFilter.open(keyfile);
                    if (keywordHash != null) {
                        system.setKeywords(keywordHash);
                    } else {
                        return false;
                    }
                    system.setKeyFile(keyfile);
                    system.setForceField(null);
                    return true;
                } catch (Exception e) {
                    logger.warning("" + e);
                    message = new String("There was an error creating " + keyfile.getAbsolutePath());
                    JOptionPane.showMessageDialog(this, message);
                }
            } else {
                message = new String(
                        "Could not create a Key file because " + pwd.getAbsolutePath() + " is not writable");
                JOptionPane.showMessageDialog(this, message);
            }
        }
    }
    return false;
}