Example usage for javax.swing JFileChooser setAccessory

List of usage examples for javax.swing JFileChooser setAccessory

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "Sets the accessory component on the JFileChooser.")
public void setAccessory(JComponent newAccessory) 

Source Link

Document

Sets the accessory component.

Usage

From source file:org.yccheok.jstock.gui.Utils.java

public static FileEx promptSavePortfolioCSVAndExcelJFileChooser(final String suggestedFileName) {
    final JStockOptions jStockOptions = JStock.instance().getJStockOptions();
    final JFileChooser chooser = new JFileChooser(jStockOptions.getLastFileIODirectory());
    final FileNameExtensionFilter csvFilter = new FileNameExtensionFilter("CSV Documents (*.csv)", "csv");
    final FileNameExtensionFilter xlsFilter = new FileNameExtensionFilter("Microsoft Excel (*.xls)", "xls");
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.addChoosableFileFilter(csvFilter);
    chooser.addChoosableFileFilter(xlsFilter);

    final org.yccheok.jstock.gui.file.PortfolioSelectionJPanel portfolioSelectionJPanel = new org.yccheok.jstock.gui.file.PortfolioSelectionJPanel();
    chooser.setAccessory(portfolioSelectionJPanel);
    chooser.addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener() {

        @Override/*from w  ww.ja  v  a  2  s  .  c  om*/
        public void propertyChange(PropertyChangeEvent evt) {
            final boolean flag = ((FileNameExtensionFilter) evt.getNewValue()).equals(csvFilter);
            portfolioSelectionJPanel.setEnabled(flag);
            chooser.setSelectedFile(chooser.getFileFilter().getDescription().equals(csvFilter.getDescription())
                    ? new File(portfolioSelectionJPanel.getSuggestedFileName())
                    : new File(suggestedFileName));
        }

    });
    portfolioSelectionJPanel.addJRadioButtonsActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            chooser.setSelectedFile(new File(portfolioSelectionJPanel.getSuggestedFileName()));
        }

    });
    final java.util.Map<String, FileNameExtensionFilter> map = new HashMap<String, FileNameExtensionFilter>();
    map.put(csvFilter.getDescription(), csvFilter);
    map.put(xlsFilter.getDescription(), xlsFilter);

    final FileNameExtensionFilter filter = map.get(jStockOptions.getLastSavedFileNameExtensionDescription());
    if (filter != null) {
        chooser.setFileFilter(filter);
    }

    // Only enable portfolioSelectionJPanel, if CSV is being selected.
    portfolioSelectionJPanel
            .setEnabled(chooser.getFileFilter().getDescription().equals(csvFilter.getDescription()));
    chooser.setSelectedFile(chooser.getFileFilter().getDescription().equals(csvFilter.getDescription())
            ? new File(portfolioSelectionJPanel.getSuggestedFileName())
            : new File(suggestedFileName));

    while (true) {
        final int returnVal = chooser.showSaveDialog(JStock.instance());
        if (returnVal != JFileChooser.APPROVE_OPTION) {
            return null;
        }

        File file = chooser.getSelectedFile();
        if (file == null) {
            return null;
        }

        // Ensure the saved file is in correct extension. If user provide correct
        // file extension explicitly, leave it as is. If not, mutate the filename.
        final String extension = Utils.getFileExtension(file);
        if (extension.equals("csv") == false && extension.equals("xls") == false) {
            if (chooser.getFileFilter().getDescription().equals(csvFilter.getDescription())) {
                file = new File(file.getAbsolutePath() + ".csv");
            } else if (chooser.getFileFilter().getDescription().equals(xlsFilter.getDescription())) {
                file = new File(file.getAbsolutePath() + ".xls");
            } else {
                // Impossible.
                return null;
            }
        }

        if (file.exists()) {
            final String output = MessageFormat
                    .format(MessagesBundle.getString("question_message_replace_old_template"), file.getName());

            final int result = javax.swing.JOptionPane.showConfirmDialog(JStock.instance(), output,
                    MessagesBundle.getString("question_title_replace_old"),
                    javax.swing.JOptionPane.YES_NO_OPTION, javax.swing.JOptionPane.QUESTION_MESSAGE);
            if (result != javax.swing.JOptionPane.YES_OPTION) {
                continue;
            }
        }

        final String parent = chooser.getSelectedFile().getParent();
        if (parent != null) {
            jStockOptions.setLastFileIODirectory(parent);
        }

        if (Utils.getFileExtension(file).equals("csv")) {
            jStockOptions.setLastFileNameExtensionDescription(csvFilter.getDescription());
        } else if (Utils.getFileExtension(file).equals("xls")) {
            jStockOptions.setLastFileNameExtensionDescription(xlsFilter.getDescription());
        } else {
            // Impossible.
            return null;
        }

        return new FileEx(file, portfolioSelectionJPanel.getType());
    }
}

From source file:plugin.notes.gui.NotesView.java

/**
 *  obtains an Image for input using a custom JFileChooser dialog
 *
 *@param  startDir  Directory to open JFielChooser to
 *@param  exts      Extensions to search for
 *@param  desc      Description for files
 *@return           File pointing to the selected image
 *///from   w w w .  j av  a  2 s  .  com
private File getImageFromChooser(String startDir, String[] exts, String desc) {
    JFileChooser jImageDialog = new JFileChooser();
    jImageDialog.setCurrentDirectory(new File(startDir));
    jImageDialog.setAccessory(new ImageFileChooserPreview(jImageDialog));
    jImageDialog.setDialogType(JFileChooser.CUSTOM_DIALOG);
    jImageDialog.setFileFilter(new FileNameExtensionFilter(desc, exts));
    jImageDialog.setDialogTitle("Select an Image to Insert");

    int optionSelected = jImageDialog.showDialog(this, "Insert");

    if (optionSelected == JFileChooser.APPROVE_OPTION) {
        return jImageDialog.getSelectedFile();
    }

    return null;
}

From source file:uk.ac.liverpool.narrative.SolutionGraphics.java

@Override
public void run() {
    // set up action cost filter
    if (actionCostsFilePath != null && (new File(actionCostsFilePath)).exists()) {
        CharacterActionSolutionFilter tc = new CharacterActionSolutionFilter();
        if (actionCostsFilePath != null && (new File(actionCostsFilePath)).exists()) {
            tc.setActionCosts(BranchingStoryGenerator.readCosts(actionCostsFilePath,
                    BranchingStoryGenerator.actionCostType));
        }/*w w w.  j  a va  2 s.c  o  m*/
        if (characterCostsFilePath != null && (new File(characterCostsFilePath)).exists()) {
            tc.setCharacterCosts(BranchingStoryGenerator.readCosts(characterCostsFilePath,
                    BranchingStoryGenerator.characterCostType));
        }
        if (authorDirection != null && (new File(authorDirection)).exists()) {
            tc.setAuthorDirection(
                    BranchingStoryGenerator.readCosts(authorDirection, BranchingStoryGenerator.authorCostType)
                            .get(BranchingStoryGenerator.authorCostType));
        }
        sg.filters.add(tc);
        tc.setThreshold(threshold);
    }
    if (character != null) {
        charFilter = new CharacterFilter(character);
    }

    if (fc) {
        JFileChooser fc = new JFileChooser(BranchingStoryGenerator.DestinationFolder);
        fc.setAccessory(new SolutionPreview(fc));
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Solution files", "sol");
        fc.setFileFilter(filter);
        fc.setMultiSelectionEnabled(true);
        fc.showOpenDialog(vvv);
        File[] files = fc.getSelectedFiles();
        for (File f : files) {
            try {
                sg.readSolutionFile(f);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    } else {
        for (File f : BranchingStoryGenerator.DestinationFolder.listFiles()) {
            if (!f.isFile() || !f.getName().endsWith(".sol")) {
                continue;
            }
            try {
                sg.readSolutionFile(f);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    System.out.println("done");

}

From source file:uk.co.modularaudio.componentdesigner.mainframe.actions.SaveAsFileAction.java

@Override
public void actionPerformed(final ActionEvent e) {
    log.debug("SaveFileAsAction called");

    try {/*from  w w  w  . j  av  a  2 s. c o  m*/
        final JFileChooser saveFileChooser = new JFileChooser();
        final String rackDataModelName = fc.getRackDataModelName();
        final CDFileSaveAccessory fileSaveAccessory = new CDFileSaveAccessory(rackDataModelName);
        saveFileChooser.setAccessory(fileSaveAccessory);
        final String patchesDir = upc.getUserPreferencesMVCController().getModel().getUserPatchesModel()
                .getValue();
        saveFileChooser.setCurrentDirectory(new File(patchesDir));
        final int retVal = saveFileChooser.showSaveDialog(mainFrame);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            final File f = saveFileChooser.getSelectedFile();
            if (f != null) {
                final String rackName = fileSaveAccessory.getFileName();
                if (log.isDebugEnabled()) {
                    log.debug(
                            "Attempting to save to file as " + f.getAbsolutePath() + " with name " + rackName);
                }

                fc.saveRackToFile(f.getAbsolutePath(), rackName);
            }
        }
    } catch (final Exception ex) {
        final String msg = "Exception caught performing save file as action: " + ex.toString();
        log.error(msg, ex);
    }
}

From source file:uk.co.modularaudio.mads.subrack.ui.SubRackMadUiInstance.java

public void saveSubRack(final Component parent)
        throws DatastoreException, IOException, RecordNotFoundException, MAConstraintViolationException {
    final JFileChooser saveFileChooser = new JFileChooser();
    final CDFileSaveAccessory cdSaveFileNameAccessory = new CDFileSaveAccessory(
            rackService.getRackName(subRackDataModel));
    saveFileChooser.setAccessory(cdSaveFileNameAccessory);

    final String subRackPatchDir = userPreferencesService.getUserSubRackPatchesDir();
    saveFileChooser.setCurrentDirectory(new File(subRackPatchDir));
    final int retVal = saveFileChooser.showSaveDialog(parent);
    if (retVal == JFileChooser.APPROVE_OPTION) {
        final File f = saveFileChooser.getSelectedFile();
        if (f != null) {
            if (log.isDebugEnabled()) {
                log.debug("Attempting to save patch to file " + f.getAbsolutePath() + " with name "
                        + cdSaveFileNameAccessory.getFileName());
            }//from  w w  w .  j a  v a  2 s .c  o  m
            rackService.setRackName(subRackDataModel, cdSaveFileNameAccessory.getFileName());
            rackMarshallingService.saveSubRackToFile(subRackDataModel, f.getAbsolutePath());
            // Only set dirty to false after successful save
            rackService.setRackDirty(subRackDataModel, false);
        }
    }
}