Example usage for javax.swing JFileChooser setFileFilter

List of usage examples for javax.swing JFileChooser setFileFilter

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "Sets the File Filter used to filter out files of type.")
public void setFileFilter(FileFilter filter) 

Source Link

Document

Sets the current file filter.

Usage

From source file:org.zaproxy.zap.extension.autoupdate.ExtensionAutoUpdate.java

private ZapMenuItem getMenuItemLoadAddOn() {
    if (menuItemLoadAddOn == null) {
        menuItemLoadAddOn = new ZapMenuItem("cfu.file.menu.loadaddon", KeyStroke.getKeyStroke(KeyEvent.VK_L,
                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
        menuItemLoadAddOn.addActionListener(new java.awt.event.ActionListener() {
            @Override/*from w ww .ja v  a 2s .c  o m*/
            public void actionPerformed(java.awt.event.ActionEvent e) {
                try {
                    JFileChooser chooser = new JFileChooser(
                            Model.getSingleton().getOptionsParam().getUserDirectory());
                    File file = null;
                    chooser.setFileFilter(new FileFilter() {
                        @Override
                        public boolean accept(File file) {
                            if (file.isDirectory()) {
                                return true;
                            } else if (file.isFile() && file.getName().endsWith(".zap")) {
                                return true;
                            }
                            return false;
                        }

                        @Override
                        public String getDescription() {
                            return Constant.messages.getString("file.format.zap.addon");
                        }
                    });
                    int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
                    if (rc == JFileChooser.APPROVE_OPTION) {
                        file = chooser.getSelectedFile();
                        if (file == null) {
                            return;
                        }
                        installLocalAddOn(file);
                    }
                } catch (Exception e1) {
                    logger.error(e1.getMessage(), e1);
                }
            }
        });
    }
    return menuItemLoadAddOn;
}

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

private void compareSessions() {
    JFileChooser chooser = new JFileChooser(Model.getSingleton().getOptionsParam().getUserDirectory());
    File file = null;/*from  ww w  .  j a va2 s. c  om*/
    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".session")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("file.format.zap.session");
        }
    });
    int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        try {
            file = chooser.getSelectedFile();
            if (file == null) {
                return;
            }
            Model cmpModel = new Model();
            Session session = cmpModel.getSession();

            //log.info("opening session file " + file.getAbsolutePath());
            //WaitMessageDialog waitMessageDialog = View.getSingleton().getWaitMessageDialog("Loading session file.  Please wait ...");
            cmpModel.openSession(file, this);

            // TODO support other implementations in the future
            Database db = new ParosDatabase();
            db.open(file.getAbsolutePath());

            Map<String, String> curMap = new HashMap<>();
            Map<String, String> cmpMap = new HashMap<>();

            // Load the 2 sessions into 2 maps
            this.buildHistoryMap(Model.getSingleton().getDb().getTableHistory(), curMap);
            this.buildHistoryMap(db.getTableHistory(), cmpMap);

            File outputFile = this.getOutputFile();

            if (outputFile != null) {
                // Write the result to the specified file
                try {
                    TreeSet<String> sset = new TreeSet<>();
                    // Combine the keys for both maps
                    sset.addAll(curMap.keySet());
                    sset.addAll(cmpMap.keySet());

                    StringBuilder sb = new StringBuilder(500);
                    sb.append("<?xml version=\"1.0\"?>");
                    sb.append(CRLF);
                    sb.append("<report>");
                    sb.append(CRLF);
                    sb.append("<session-names>");
                    sb.append(CRLF);
                    sb.append("<session1>");
                    sb.append(Model.getSingleton().getSession().getSessionName());
                    sb.append("</session1>");
                    sb.append(CRLF);
                    sb.append("<session2>");
                    sb.append(session.getSessionName());
                    sb.append("</session2>");
                    sb.append(CRLF);
                    sb.append("</session-names>");
                    sb.append(CRLF);

                    Iterator<String> iter = sset.iterator();
                    while (iter.hasNext()) {
                        sb.append("<urlrow>");
                        sb.append(CRLF);
                        String key = iter.next();
                        String method = key.substring(0, key.indexOf(" "));
                        String url = key.substring(key.indexOf(" ") + 1);

                        sb.append("<method>");
                        sb.append(method);
                        sb.append("</method>");
                        sb.append(CRLF);

                        sb.append("<url>");
                        sb.append(url);
                        sb.append("</url>");
                        sb.append(CRLF);

                        sb.append("<code1>");
                        if (curMap.containsKey(key)) {
                            sb.append(curMap.get(key));
                        } else {
                            sb.append("---");
                        }
                        sb.append("</code1>");
                        sb.append(CRLF);

                        sb.append("<code2>");
                        if (cmpMap.containsKey(key)) {
                            sb.append(cmpMap.get(key));
                        } else {
                            sb.append("---");
                        }
                        sb.append("</code2>");
                        sb.append(CRLF);

                        sb.append("</urlrow>");
                        sb.append(CRLF);
                    }

                    sb.append("</report>");
                    sb.append(CRLF);

                    ReportGenerator.stringToHtml(sb.toString(), Constant.getZapInstall() + File.separator
                            + "xml" + File.separator + "reportCompare.xsl", outputFile.getAbsolutePath());

                    try {
                        DesktopUtils.openUrlInBrowser(outputFile.toURI());
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                        View.getSingleton()
                                .showMessageDialog(MessageFormat.format(
                                        Constant.messages.getString("report.complete.warning"),
                                        new Object[] { outputFile.getAbsolutePath() }));
                    }

                } catch (Exception e1) {
                    log.warn(e1.getMessage(), e1);
                }
            }

            //waitMessageDialog.setVisible(true);

        } catch (Exception e) {
            log.warn(e.getMessage(), e);
        }
    }
}

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//from ww  w. j a va  2s .  c  om
        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.customFire.CustomFireDialog.java

/**
 * //from   w ww .  ja v a 2  s  . c o m
 * @return JPanel - customPanel with saved injectionList 
 */
private JPanel getSerCustomPanel() {
    customPanel = getCustomPanel();
    ArrayList highlightList = new ArrayList<>();
    ArrayList textAreaList = new ArrayList<>();
    JFileChooser chooser = new JFileChooser(Constant.getZapHome());
    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".ser")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("customFire.custom.file.format.csp.ser");
        }
    });
    File file = null;
    chooser.setDialogTitle("Open Custom Vectors");
    int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null || !file.getName().equalsIgnoreCase("Custom Vectors.ser")) {
            View.getSingleton().showWarningDialog(
                    Constant.messages.getString("customFire.custom.tech.ser.mismatch.error"));
            return customPanel;
        }
        try {
            FileInputStream fis = new FileInputStream(file.getPath());
            ObjectInputStream ois = new ObjectInputStream(fis);
            textAreaList = (ArrayList<String>) ois.readObject();
            ois.close();
            fis.close();
            highlightList = (ArrayList) textAreaList.get(0);
            String httpReq = (String) textAreaList.get(1);

            customPanel = getSavedCustomPanel(highlightList, httpReq);
            reset = true;

        } catch (IOException | ClassNotFoundException e1) {
            View.getSingleton()
                    .showWarningDialog(Constant.messages.getString("customFire.custom.ser.load.error"));
            return customPanel;
        }
    }
    return customPanel;
}

From source file:org.zaproxy.zap.extension.customFire.CustomFireDialog.java

/**
 * /* w  w w .j ava  2 s  .co  m*/
 * @return TechnologyTreePanel `
 */
private TechnologyTreePanel getTechTree() {
    TechnologyTreePanel ttp = new TechnologyTreePanel(
            Constant.messages.getString("customFire.custom.tab.tech.node"));
    JFileChooser chooser = new JFileChooser(Constant.getZapHome());
    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".ser")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("customFire.custom.file.format.csp.ser");
        }
    });
    File file = null;
    chooser.setDialogTitle("Open Technology");
    int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null || !file.getName().equalsIgnoreCase("Tech.ser")) {
            View.getSingleton().showWarningDialog(
                    Constant.messages.getString("customFire.custom.tech.ser.mismatch.error"));
            return ttp;
        }
        try {
            FileInputStream fis = new FileInputStream(file.getPath());
            ObjectInputStream ois = new ObjectInputStream(fis);
            ttp = (TechnologyTreePanel) ois.readObject();
            ttp.addTechTreeListener(ttp, true);
            ois.close();
            fis.close();

        } catch (IOException | ClassNotFoundException e1) {
            View.getSingleton()
                    .showWarningDialog(Constant.messages.getString("customFire.custom.ser.load.error"));
        }
    }

    techTree = ttp;

    return techTree;
}

From source file:org.zaproxy.zap.extension.customFire.CustomFireDialog.java

/**
 * Not used as of now/*  w w w  . j a va  2s.  c om*/
 * @return ScriptTreePanel `
 */
public ScriptTreePanel getSTree() {
    //if (sTree == null) {

    ScriptTreePanel stp = new ScriptTreePanel(Constant.messages.getString("customFire.custom.tab.script.node"));
    JFileChooser chooser = new JFileChooser(Constant.getZapHome());
    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".ser")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("customFire.custom.file.format.csp.ser");
        }
    });
    File file = null;
    chooser.setDialogTitle("Open Scripts settings");
    int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null || !file.getName().equalsIgnoreCase("Scripts.ser")) {
            View.getSingleton().showWarningDialog(
                    Constant.messages.getString("customFire.custom.scripts.ser.mismatch.error"));
            return stp;
        }
        try {
            FileInputStream fis = new FileInputStream(file.getPath());
            ObjectInputStream ois = new ObjectInputStream(fis);
            stp = (ScriptTreePanel) ois.readObject();
            stp.addScriptTreeListener(stp, true);
            ois.close();
            fis.close();

        } catch (IOException | ClassNotFoundException e1) {
            View.getSingleton()
                    .showWarningDialog(Constant.messages.getString("customFire.custom.ser.load.error"));
        }
    }

    sTree = stp;
    return sTree;
}

From source file:org.zaproxy.zap.extension.customFire.CustomFireDialog.java

/**
 *    Gets Input Vectors tab// w  w  w  .j av  a2 s .  co  m
 *  void `
 */
private ScannerParam getScannerParam() {

    ScannerParam sp = new ScannerParam();
    JFileChooser chooser = new JFileChooser(Constant.getZapHome());
    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".ser")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("customFire.custom.file.format.csp.ser");
        }
    });
    File file = null;
    chooser.setDialogTitle("Open Input Vectors");
    int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null || !file.getName().equalsIgnoreCase("Input Vectors.ser")) {
            View.getSingleton().showWarningDialog(
                    Constant.messages.getString("customFire.custom.inputVectors.ser.mismatch.error"));
            return scannerParam;
        }
        try {
            FileInputStream fis = new FileInputStream(file.getPath());
            ObjectInputStream ois = new ObjectInputStream(fis);
            sp = (ScannerParam) ois.readObject();
            ois.close();
            fis.close();
            return sp;

        } catch (IOException | ClassNotFoundException e1) {
            View.getSingleton()
                    .showWarningDialog(Constant.messages.getString("customFire.custom.ser.load.error"));
            return scannerParam;
        }
    }

    return scannerParam;
}

From source file:org.zaproxy.zap.extension.customFire.CustomFireDialog.java

/**
 * Saves custom vectors tab/* w w  w . j  a  va2  s.  c  o  m*/
 * @param list
 * @param httpRequest void `
 */
protected void saveCustomVectors(ArrayList list, String httpRequest) {

    JFileChooser chooser = new JFileChooser(Constant.getPoliciesDir());
    File file = new File(Constant.getZapHome(), "Custom Vectors.ser");
    chooser.setSelectedFile(file);

    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".ser")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("customFire.custom.file.format.csp.ser");
        }
    });
    int rc = chooser.showSaveDialog(View.getSingleton().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null) {
            return;
        }

        ArrayList woi = new ArrayList<>();
        woi.add(list);
        woi.add(httpRequest);

        try {

            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(woi);
            oos.close();
            fos.close();
            View.getSingleton()
                    .showMessageDialog(Constant.messages.getString("customFire.custom.ser.saveCV.success"));

        } catch (IOException e1) {
            e1.printStackTrace();
            View.getSingleton()
                    .showWarningDialog(Constant.messages.getString("customFire.custom.ser.saveCV.error"));
            return;
        }
    }
}

From source file:org.zaproxy.zap.extension.customFire.CustomFireDialog.java

/**
 * Saves input vectors tab/* ww w  .  j  a  v  a 2  s  . c  o  m*/
 * @param scannerParam void `
 */
protected void saveScannerParam(ScannerParam scannerParam) {

    JFileChooser chooser = new JFileChooser(Constant.getPoliciesDir());
    File file = new File(Constant.getZapHome(), "Input Vectors.ser");
    chooser.setSelectedFile(file);

    chooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else if (file.isFile() && file.getName().endsWith(".ser")) {
                return true;
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constant.messages.getString("customFire.custom.file.format.csp.ser");
        }
    });
    int rc = chooser.showSaveDialog(View.getSingleton().getMainFrame());
    if (rc == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (file == null) {
            return;
        }
        try {

            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(scannerParam);
            oos.close();
            fos.close();
            View.getSingleton()
                    .showMessageDialog(Constant.messages.getString("customFire.custom.ser.saveIV.success"));

        } catch (IOException e1) {
            View.getSingleton()
                    .showWarningDialog(Constant.messages.getString("customFire.custom.ser.saveIV.error"));
            return;
        }
    }
    if (rc == JFileChooser.CANCEL_OPTION) {
        chooser.setVisible(false);
        return;
    }
}

From source file:org.zaproxy.zap.extension.customFire.PolicyManagerDialog.java

private JButton getImportButton() {
    if (this.importButton == null) {
        this.importButton = new JButton(
                Constant.messages.getString("customFire.custom.policymgr.button.import"));
        this.importButton.addActionListener(new ActionListener() {
            @Override/*  ww  w.j a  v  a 2 s  .c o  m*/
            public void actionPerformed(ActionEvent e) {
                // Default to ZAP home dir - we dont want to import/export to the policy dir
                JFileChooser chooser = new JFileChooser(Constant.getZapHome());
                chooser.setFileFilter(new FileFilter() {
                    @Override
                    public boolean accept(File file) {
                        if (file.isDirectory()) {
                            return true;
                        } else if (file.isFile() && file.getName().endsWith(".policy")) {
                            return true;
                        }
                        return false;
                    }

                    @Override
                    public String getDescription() {
                        return Constant.messages.getString("file.format.zap.policy");
                    }
                });
                File file = null;
                int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
                if (rc == JFileChooser.APPROVE_OPTION) {
                    file = chooser.getSelectedFile();
                    if (file == null) {
                        return;
                    }
                    try {
                        extension.getPolicyManager().importPolicy(file);
                        policyNamesChanged();
                    } catch (ConfigurationException | IOException e1) {
                        logger.error(e1.getMessage(), e1);
                        View.getSingleton().showWarningDialog(
                                Constant.messages.getString("customFire.custom.policy.load.error"));
                    }
                }
            }
        });
    }
    return this.importButton;
}