Example usage for javax.swing JFileChooser getCurrentDirectory

List of usage examples for javax.swing JFileChooser getCurrentDirectory

Introduction

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

Prototype

public File getCurrentDirectory() 

Source Link

Document

Returns the current directory.

Usage

From source file:org.trzcinka.intellitrac.view.toolwindow.tickets.ticket_editor.BaseTicketEditorForm.java

public BaseTicketEditorForm() {
    ticketsModel.getCurrentTicketModel().addListener(this);
    submitChangesButton.addActionListener(retrieveSubmitButtonActionListener());
    changeHistoryButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Window window = SwingUtilities.getWindowAncestor(rootComponent);
            TicketChangesHistoryPopup dialog = new TicketChangesHistoryPopup(window,
                    ticketsModel.getCurrentTicketModel().getCurrentTicket().getChanges());
            dialog.pack();// www.ja  va 2  s .com
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
            dialog.dispose();
        }
    });
    downloadButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Attachment attachment = (Attachment) attachmentsList.getSelectedValue();
            OutputStream stream = null;
            if (attachment != null) {
                try {
                    byte[] body = gateway.retrieveAttachment(
                            ticketsModel.getCurrentTicketModel().getCurrentTicket().getId(),
                            attachment.getFileName());
                    JFileChooser fc = new JFileChooser();
                    File dir = fc.getCurrentDirectory();
                    fc.setSelectedFile(new File(dir, attachment.getFileName()));
                    int save = fc.showSaveDialog(rootComponent);

                    if (save == JFileChooser.APPROVE_OPTION) {
                        File file = fc.getSelectedFile();
                        stream = new FileOutputStream(file);
                        IOUtils.write(body, stream);
                    }
                } catch (ConnectionFailedException e1) {
                    TracGatewayLocator.handleConnectionProblem();
                } catch (IOException e1) {
                    logger.error("Could not save file", e1);
                } finally {
                    IOUtils.closeQuietly(stream);
                }
            }
        }
    });
    showDescriptionButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Attachment attachment = (Attachment) attachmentsList.getSelectedValue();
            if (attachment != null) {
                JOptionPane popup = new AttachmentDescriptionPopup(attachment.getDescription());
                JDialog dialog = popup.createDialog(null,
                        MessageFormat.format(
                                bundle.getString("tool_window.tickets.ticket_editor.attachments.popup_title"),
                                attachment.getFileName()));
                dialog.setVisible(true);
                dialog.dispose();
            }
        }
    });
    attachmentsList.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            Attachment selected = (Attachment) attachmentsList.getSelectedValue();
            if (selected == null) {
                downloadButton.setEnabled(false);
                showDescriptionButton.setEnabled(false);
            } else {
                downloadButton.setEnabled(true);
                if (!(StringUtils.isEmpty(selected.getDescription()))) {
                    showDescriptionButton.setEnabled(true);
                }
            }
        }
    });
    newAttachmentButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Window window = SwingUtilities.getWindowAncestor(rootComponent);
            NewAttachmentPopup dialog = new NewAttachmentPopup(window,
                    ticketsModel.getCurrentTicketModel().getCurrentTicket().getId());
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
            dialog.dispose();
            synchronizeTicket();
        }
    });
    synchronizeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            synchronizeTicket();
        }
    });
}

From source file:org.zaproxy.zap.extension.compare.ExtensionCompare.java

private File getOutputFile() {

    JFileChooser chooser = new JFileChooser(getModel().getOptionsParam().getUserDirectory());
    chooser.setFileFilter(new FileFilter() {
        @Override/*  ww w  . j a  v a 2  s . c o  m*/
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().toLowerCase().endsWith(".htm")) {
                return true;
            } else if (file.isFile() && file.getName().toLowerCase().endsWith(".html")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("file.format.html");
        }
    });

    File file = null;
    int rc = chooser.showSaveDialog(getView().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null) {
            return file;
        }
        getModel().getOptionsParam().setUserDirectory(chooser.getCurrentDirectory());
        String fileNameLc = file.getAbsolutePath().toLowerCase();
        if (!fileNameLc.endsWith(".htm") && !fileNameLc.endsWith(".html")) {
            file = new File(file.getAbsolutePath() + ".html");
        }
        return file;

    }
    return file;
}

From source file:org.zaproxy.zap.extension.history.PopupMenuExportURLs.java

private File getOutputFile() {

    JFileChooser chooser = new JFileChooser(extension.getModel().getOptionsParam().getUserDirectory());
    chooser.setFileFilter(new FileFilter() {
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().toLowerCase().endsWith(".txt")) {
                return true;
            } else if (file.isFile() && file.getName().toLowerCase().endsWith(".htm")) {
                return true;
            } else if (file.isFile() && file.getName().toLowerCase().endsWith(".html")) {
                return true;
            }//w  ww.  j a  v  a  2s. c  om
            return false;
        }

        public String getDescription() {
            return Constant.messages.getString("file.format.textOrHtml");
        }
    });

    File file = null;
    int rc = chooser.showSaveDialog(extension.getView().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null) {
            return file;
        }
        extension.getModel().getOptionsParam().setUserDirectory(chooser.getCurrentDirectory());
        String fileName = file.getAbsolutePath().toLowerCase();
        if (!fileName.endsWith(".txt") && !fileName.endsWith(".htm") && !fileName.endsWith(".html")) {
            fileName += ".txt";
            file = new File(fileName);
        }
        return file;

    }
    return file;
}

From source file:org.zaproxy.zap.extension.openapi.ExtensionOpenApi.java

private ZapMenuItem getMenuImportLocalOpenApi() {
    if (menuImportLocalOpenApi == null) {
        menuImportLocalOpenApi = new ZapMenuItem("openapi.topmenu.import.importopenapi");
        menuImportLocalOpenApi//from ww  w.jav a  2  s  .c om
                .setToolTipText(Constant.messages.getString("openapi.topmenu.import.importopenapi.tooltip"));

        menuImportLocalOpenApi.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                // Prompt for a OpenApi file.
                final JFileChooser chooser = new JFileChooser(
                        Model.getSingleton().getOptionsParam().getUserDirectory());
                int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
                if (rc == JFileChooser.APPROVE_OPTION) {
                    Model.getSingleton().getOptionsParam().setUserDirectory(chooser.getCurrentDirectory());
                    importOpenApiDefinition(chooser.getSelectedFile(), true);
                }
            }
        });
    }
    return menuImportLocalOpenApi;
}

From source file:quake3mapfixer.Pk3Fixer.java

private void fileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileChooserActionPerformed
    Boolean old = UIManager.getBoolean("FileChooser.readOnly");
    UIManager.put("FileChooser.readOnly", Boolean.TRUE);
    JFileChooser chooser = new JFileChooser();
    UIManager.put("FileChooser.readOnly", old);
    FileNameExtensionFilter pk3filter = new FileNameExtensionFilter("pk3 files (*.pk3)", "pk3");
    chooser.setFileFilter(pk3filter);//from  w  ww  .ja  v  a2s. c  om
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.setDialogTitle("Open pk3 file");
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        currentDirectory = chooser.getCurrentDirectory().toString();
        selectedFile = chooser.getSelectedFile().toString();
        absolutePath = chooser.getSelectedFile().getName();
        areaFileName.setText(selectedFile);
        fileFixer.setEnabled(true);
    }

}

From source file:ru.gelin.fictionbook.reader.actions.OpenAction.java

/**
 *  Shows file chosing dialog and returns choosed file or null if
 *  file was not selected./*  w w  w .j  av  a  2s.c o  m*/
 */
File chooseFile() {
    File result = null;
    JFileChooser chooser = new JFileChooser();
    if (currentDirectory != null) {
        chooser.setCurrentDirectory(currentDirectory);
    }
    chooser.addChoosableFileFilter(new FBFileFilter());
    int returnValue = chooser.showOpenDialog(getParentComponent());
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        result = chooser.getSelectedFile();
        if (log.isInfoEnabled()) {
            log.info(result + " file is selected");
        }
    }
    currentDirectory = chooser.getCurrentDirectory(); //save current dialog directory
    return result;
}

From source file:uk.chromis.pos.imports.JPanelCSVImport.java

private void jbtnDbDriverLibActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDbDriverLibActionPerformed
    resetFields();//from  w w  w  . j  a  v  a2 s .  c o  m
    setWorker();
    // If CSV.last_file is null then use c:\ otherwise use saved dir
    JFileChooser chooser = new JFileChooser(last_folder == null ? "C:\\" : last_folder);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("csv files", "csv");
    chooser.setFileFilter(filter);
    chooser.showOpenDialog(null);
    File csvFile = chooser.getSelectedFile();
    // check if a file was selected        
    if (csvFile == null) {
        return;
    }

    File current_folder = chooser.getCurrentDirectory();
    // If we have a file lets save the directory for later use if it's different from the old
    if (last_folder == null || !last_folder.equals(current_folder.getAbsolutePath())) {
        AppConfig.getInstance().setProperty("CSV.last_folder", current_folder.getAbsolutePath());
        last_folder = current_folder.getAbsolutePath();
        try {
            AppConfig.getInstance().save();
        } catch (IOException ex) {
            Logger.getLogger(JPanelCSVImport.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    String csv = csvFile.getName();
    if (!(csv.trim().equals(""))) {
        csvFileName = csvFile.getAbsolutePath();
        jFileName.setText(csvFileName);
    }
}

From source file:zsk.JFCMainClient.java

/**
 * process events of ActionListener/*  w w  w.ja v a2  s.  c  o m*/
 * 
 */
public void actionPerformed(final ActionEvent e) {
    if (e.getSource().equals(frame.textinputfield)) {
        if (!e.getActionCommand().equals("")) {
            if (e.getActionCommand().matches(szYTREGEX))
                addYTURLToList(e.getActionCommand());
            else {
                addTextToConsole(e.getActionCommand());
                cli(e.getActionCommand().toLowerCase());
            }
        }
        synchronized (frame.textinputfield) {
            frame.textinputfield.setText("");
        }
        return;
    }

    // let the user choose another dir
    if (e.getSource().equals(frame.directorybutton)) {
        debugoutput("frame.directorybutton");
        JFileChooser fc = new JFileChooser();
        fc.setMultiSelectionEnabled(false);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        synchronized (frame.directorytextfield) {
            // we have to set current directory here because it gets lost when fc is lost
            fc.setCurrentDirectory(new File(frame.directorytextfield.getText()));
        }
        debugoutput("current dir: ".concat(fc.getCurrentDirectory().getAbsolutePath()));
        if (fc.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
            return;
        }
        String snewdirectory = fc.getSelectedFile().getAbsolutePath();
        // append file.seperator if last character is not file.seperator (the user choosed a directory other than root)
        snewdirectory.concat(snewdirectory.endsWith(System.getProperty("file.separator")) ? ""
                : System.getProperty("file.separator"));
        File ftest = new File(snewdirectory);
        if (ftest.exists()) {
            if (ftest.isDirectory()) {
                synchronized (frame.directorytextfield) {
                    frame.directorytextfield.setText(snewdirectory);
                }
                config.setProperty("savefolder", snewdirectory);
                debugoutput("new current dir: ".concat(fc.getCurrentDirectory().getAbsolutePath()));
                output("new current dir: ".concat(fc.getCurrentDirectory().getAbsolutePath()));
            } else {
                output("not a directory: ".concat(snewdirectory));
            }
        } else {
            output("directory does not exist: ".concat(snewdirectory));
        }
        return;
    }

    // let the user choose another download resolution
    if (e.getActionCommand().equals(JFCMainClient.frame.hdbutton.getActionCommand())
            || e.getActionCommand().equals(JFCMainClient.frame.stdbutton.getActionCommand())
            || e.getActionCommand().equals(JFCMainClient.frame.ldbutton.getActionCommand())) {
        debugoutput("trying: ".concat(e.getActionCommand()));
        output("trying: ".concat(e.getActionCommand().toUpperCase()));

        return;
    }

    // let the user choose another video format
    if (e.getActionCommand().equals(JFCMainClient.frame.mpgbutton.getActionCommand())
            || e.getActionCommand().equals(JFCMainClient.frame.flvbutton.getActionCommand())
            || e.getActionCommand().equals(JFCMainClient.frame.webmbutton.getActionCommand())) {
        debugoutput("trying: ".concat(e.getActionCommand()));
        output("trying: ".concat(e.getActionCommand().toUpperCase()
                .concat("  ".concat(isgerman() ? "(Dateien werden immer nach MIME-Typ benannt!)"
                        : "(files will always be named according to MIME type!)"))));
        return;
    }

    if (e.getActionCommand().equals("quit")) {
        addTextToConsole(isgerman()
                ? "Programmende ausgewhlt - beende DownloadThreads, Vorgang kann eine Weile dauern!"
                : "quit requested - signaling donwload threads to terminate, this may take a while!");
        // seems to have to effect:
        //JFCMainClient.frame.repaint();
        JFCMainClient.shutdownAppl();
        return;
    }
    debugoutput("unknown action. ".concat(e.getSource().toString()));
    output("unbekannte Aktion");

}