Example usage for javax.swing JFileChooser isDirectorySelectionEnabled

List of usage examples for javax.swing JFileChooser isDirectorySelectionEnabled

Introduction

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

Prototype

@BeanProperty(bound = false)
public boolean isDirectorySelectionEnabled() 

Source Link

Document

Convenience call that determines if directories are selectable based on the current file selection mode.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    boolean enabled = chooser.isDirectorySelectionEnabled();

    chooser.showOpenDialog(null);/*from w  w  w . ja  v  a2 s .c  o  m*/
}

From source file:app.RunApp.java

/**
 * Action of Save datasets button on Transformation tab
 * //w  ww .  j  av  a2  s.  c o m
 * @param evt Event
 */
private void jButtonSaveDatasetsTransActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveDatasetsTransActionPerformed
    try {
        if (dataset == null) {
            JOptionPane.showMessageDialog(null, "You must load a dataset.", "alert", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if ((transformedDatasets == null || transformedDatasets.isEmpty())) {
            JOptionPane.showMessageDialog(null, "You must click on Start before.", "alert",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }

        JFileChooser fc = new JFileChooser();

        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int returnVal = fc.showSaveDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();

            String dataName = datasetName.substring(0, datasetName.length() - 5);

            if (fc.isDirectorySelectionEnabled()) {
                if (radioBRTrans.isSelected()) {
                    for (int i = 0; i < transformedDatasets.size(); i++) {
                        ArffSaver saver = new ArffSaver();
                        saver.setInstances(transformedDatasets.get(i));
                        saver.setFile(new File(
                                file.getAbsolutePath() + "/" + dataName + "_BRTransformed_" + i + ".arff"));
                        saver.writeBatch();
                    }
                } else if (radioLPTrans.isSelected()) {
                    ArffSaver saver = new ArffSaver();
                    saver.setInstances(transformedDatasets.get(0));
                    saver.setFile(
                            new File(file.getAbsolutePath() + "/" + dataName + "_LPTransformed" + ".arff"));
                    saver.writeBatch();
                } else if (radioRemoveLabelsTrans.isSelected()) {
                    ArffSaver saver = new ArffSaver();
                    saver.setInstances(transformedDatasets.get(0));
                    saver.setFile(new File(
                            file.getAbsolutePath() + "/" + dataName + "_RemoveAllLabelsTransformed" + ".arff"));
                    saver.writeBatch();
                } else if (radioIncludeLabelsTrans.isSelected()) {
                    ArffSaver saver = new ArffSaver();
                    saver.setInstances(transformedDatasets.get(0));
                    saver.setFile(new File(
                            file.getAbsolutePath() + "/" + dataName + "_IncludeLabelsTransformed" + ".arff"));
                    saver.writeBatch();
                }

                JOptionPane.showMessageDialog(null, "All files have been saved.", "Successful",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        }
    } catch (HeadlessException | IOException e) {
        JOptionPane.showMessageDialog(null, "An error ocurred while saving the dataset files.", "alert",
                JOptionPane.ERROR_MESSAGE);
        Logger.getLogger(RunApp.class.getName()).log(Level.SEVERE, null, e);
    }
}

From source file:app.RunApp.java

/**
 * Save MVML datasets/*w w w .  j a va 2s. c o  m*/
 * 
 * @return Positive number if successfull and negative otherwise
 */
private int saveMultiView() {
    if (dataset == null) {
        JOptionPane.showMessageDialog(null, "You must load a dataset.", "alert", JOptionPane.ERROR_MESSAGE);
        return -1;
    }

    int[] selecteds = jTable2.getSelectedRows();
    if (selecteds.length == 0) {
        JOptionPane.showMessageDialog(null, "You must select at least one view.", "alert",
                JOptionPane.ERROR_MESSAGE);
        return -1;
    }

    MultiLabelInstances mvData;

    int attSize = 0;
    for (int n : selecteds) {
        attSize += views.get("View " + (n + 1)).length;
    }
    int[] attToKeep = new int[attSize];
    Integer[] view_i;
    int j = 0;
    for (int n : selecteds) {
        view_i = views.get("View " + (n + 1));
        for (int k : view_i) {
            attToKeep[j] = k;
            j++;
        }
    }

    FeatureSelector fs = new FeatureSelector(dataset, attSize);
    mvData = fs.keepAttributes(attToKeep);

    try {
        String format = jComboBox_SaveFormat1.getSelectedItem().toString();

        JFileChooser fc = new JFileChooser();

        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        String xmlPath;

        int returnVal = fc.showSaveDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();

            if (fc.isDirectorySelectionEnabled()) {
                String preprocessedType = "-views";

                for (int n : selecteds) {
                    preprocessedType += "_" + (n + 1);
                }

                BufferedWriter bwTrain;
                try {
                    String dataName = datasetName.substring(0, datasetName.length() - 5);

                    int sumNotSelected = 0;
                    Hashtable<String, Integer[]> v = new Hashtable<>();
                    for (int i = 0; i < views.size(); i++) {
                        if (Utils.contains(selecteds, i)) {
                            Integer[] A = views.get("View " + (i + 1));
                            for (int a = 0; a < A.length; a++) {
                                A[a] -= sumNotSelected;
                            }
                            v.put("View " + (i + 1), A);
                        } else {
                            sumNotSelected += views.get("View " + (i + 1))[views.get("View " + (i + 1)).length
                                    - 1] - views.get("View " + (i + 1))[0] + 1;
                        }
                    }

                    String viewsString = "-V:";
                    for (int n : selecteds) {
                        attSize += v.get("View " + (n + 1)).length;
                        viewsString += viewsIntervals.get("View " + (n + 1)) + "!";
                        //viewsString += v.get("View " + (n+1))[0] + "-" + v.get("View " + (n+1))[v.get("View " + (n+1)).length-1] + "!";
                    }
                    viewsString += ";";
                    viewsString = viewsString.replace("!;", "");

                    if (format.toLowerCase().contains("meka")) {
                        String dataPath = file.getAbsolutePath() + "/" + dataName + preprocessedType + ".arff";

                        bwTrain = new BufferedWriter(new FileWriter(dataPath));
                        PrintWriter wrTrain = new PrintWriter(bwTrain);

                        DataIOUtils.saveMVMekaDataset(wrTrain, mvData, dataName, viewsString);

                        wrTrain.close();
                        bwTrain.close();
                    } else {
                        String dataPath = file.getAbsolutePath() + "/" + dataName + preprocessedType + ".arff";
                        xmlPath = file.getAbsolutePath() + "/" + dataName + preprocessedType + ".xml";

                        System.out.println("dataPath: " + dataPath);

                        bwTrain = new BufferedWriter(new FileWriter(dataPath));
                        PrintWriter wrTrain = new PrintWriter(bwTrain);

                        DataIOUtils.saveDatasetMV(wrTrain, mvData, dataName, viewsString);

                        wrTrain.close();
                        bwTrain.close();

                        BufferedWriter bwXml = new BufferedWriter(new FileWriter(xmlPath));
                        PrintWriter wrXml = new PrintWriter(bwXml);

                        DataIOUtils.saveXMLFile(wrXml, mvData);

                        wrXml.close();
                        bwXml.close();
                    }

                    JOptionPane.showMessageDialog(null, "All files have been saved.", "Successful",
                            JOptionPane.INFORMATION_MESSAGE);
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(null,
                            "The selected folder to save the dataset does not exist. \nPlease select a correct folder.",
                            "alert", JOptionPane.ERROR_MESSAGE);
                    Logger.getLogger(RunApp.class.getName()).log(Level.SEVERE, null, ex);
                    return -1;
                }
                Toolkit.getDefaultToolkit().beep();
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "An error ocurred while saving the dataset files.", "alert",
                JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
        return -1;
    }

    return 1;
}

From source file:app.RunApp.java

/**
 * Action of Save datasets button in Preprocess tab
 * /*www.  j  a v a 2 s  .c o m*/
 * @param evt Event
 */
private void jButtonSaveDatasetsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveDatasetsActionPerformed
    try {
        /*
        If only FS is selected, save FS dataset
        If any splitting method is selected, save the splitted datasets (those are FS too if it has been selected)
        */

        String format = jComboBoxSaveFormat.getSelectedItem().toString();

        if (dataset == null) {
            JOptionPane.showMessageDialog(null, "You must load a dataset.", "alert", JOptionPane.ERROR_MESSAGE);
            return;
        }
        if (!(radioNoFS.isSelected() && radioNoIS.isSelected() && radioNoSplit.isSelected())) {
            if ((trainDatasets.isEmpty() && testDatasets.isEmpty()) && (radioRandomCV.isSelected()
                    || radioIterativeStratifiedCV.isSelected() || radioLPStratifiedCV.isSelected())) {
                JOptionPane.showMessageDialog(null, "You must click on Start before.", "alert",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
            if ((trainDataset == null && testDataset == null) && (radioIterativeStratifiedHoldout.isSelected()
                    || radioRandomHoldout.isSelected() || radioLPStratifiedHoldout.isSelected())) {
                JOptionPane.showMessageDialog(null, "You must click on Start before.", "alert",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
            if ((preprocessedDataset == null) && (radioBRFS.isSelected() || radioRandomFS.isSelected())) {
                JOptionPane.showMessageDialog(null, "You must click on Start before.", "alert",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
        }

        //JFileChooser save
        JFileChooser fc = new JFileChooser();

        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        String trainPath, testPath, xmlPath;

        int returnVal = fc.showSaveDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();

            if (fc.isDirectorySelectionEnabled()) {
                String dataName = datasetName.substring(0, datasetName.length() - 5);

                if (radioRandomIS.isSelected()) {
                    dataName += "-randomIS";
                }
                if (radioRandomFS.isSelected()) {
                    dataName += "-randomFS";
                } else if (radioBRFS.isSelected()) {
                    dataName += "-BRFS";
                }
                if (radioRandomCV.isSelected() || radioRandomHoldout.isSelected()) {
                    dataName += "-random";
                } else if (radioIterativeStratifiedCV.isSelected()
                        || radioIterativeStratifiedHoldout.isSelected()) {
                    dataName += "-iterative";
                } else if (radioLPStratifiedCV.isSelected() || radioLPStratifiedHoldout.isSelected()) {
                    dataName += "-LP";
                }
                if ((radioNoIS.isSelected()) && (radioNoFS.isSelected()) && (radioNoSplit.isSelected())) {
                    if (format.toLowerCase().contains("meka")) {
                        dataName += "-mekaConverted";
                    } else if (format.toLowerCase().contains("mulan")) {
                        dataName += "-mulanConverted";
                    }
                }

                //Check if none were selected -> Dataset conversion
                if (radioNoFS.isSelected() && radioNoIS.isSelected() && radioNoSplit.isSelected()) {
                    BufferedWriter bwTrain;
                    try {
                        if (format.toLowerCase().contains("meka")) {
                            String dataPath = file.getAbsolutePath() + "/" + dataName + ".arff";

                            bwTrain = new BufferedWriter(new FileWriter(dataPath));
                            PrintWriter wrTrain = new PrintWriter(bwTrain);

                            DataIOUtils.saveMekaDataset(wrTrain, dataset, dataset.getDataSet().relationName());

                            wrTrain.close();
                            bwTrain.close();
                        } else {
                            String dataPath = file.getAbsolutePath() + "/" + dataName + ".arff";
                            xmlPath = file.getAbsolutePath() + "/" + dataName + ".xml";

                            bwTrain = new BufferedWriter(new FileWriter(dataPath));
                            PrintWriter wrTrain = new PrintWriter(bwTrain);

                            DataIOUtils.saveDataset(wrTrain, dataset, dataset.getDataSet().relationName());

                            wrTrain.close();
                            bwTrain.close();

                            BufferedWriter bwXml = new BufferedWriter(new FileWriter(xmlPath));
                            PrintWriter wrXml = new PrintWriter(bwXml);

                            DataIOUtils.saveXMLFile(wrXml, dataset);

                            wrXml.close();
                            bwXml.close();
                        }

                        JOptionPane.showMessageDialog(null, "All files have been saved.", "Successful",
                                JOptionPane.INFORMATION_MESSAGE);

                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null, "An error ocurred while saving the dataset files.",
                                "alert", JOptionPane.ERROR_MESSAGE);
                        Logger.getLogger(RunApp.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                //check if only FS and/or IS is selected
                if ((radioBRFS.isSelected() || radioRandomFS.isSelected() || radioRandomIS.isSelected())
                        && radioNoSplit.isSelected())//Feature and/or instance selection
                {

                    BufferedWriter bwTrain;
                    try {

                        if (format.toLowerCase().contains("meka")) {
                            String dataPath = file.getAbsolutePath() + "/" + dataName + ".arff";

                            bwTrain = new BufferedWriter(new FileWriter(dataPath));
                            PrintWriter wrTrain = new PrintWriter(bwTrain);

                            if (radioNoFS.isSelected()) {
                                DataIOUtils.saveMekaDataset(wrTrain, preprocessedDataset,
                                        preprocessedDataset.getDataSet().relationName());
                            } else {
                                DataIOUtils.saveMekaDataset(wrTrain, preprocessedDataset, dataName);
                            }

                            wrTrain.close();
                            bwTrain.close();
                        } else {
                            String dataPath = file.getAbsolutePath() + "/" + dataName + ".arff";
                            xmlPath = file.getAbsolutePath() + "/" + dataName + ".xml";

                            bwTrain = new BufferedWriter(new FileWriter(dataPath));
                            PrintWriter wrTrain = new PrintWriter(bwTrain);

                            if (radioNoFS.isSelected()) {
                                DataIOUtils.saveDataset(wrTrain, preprocessedDataset,
                                        preprocessedDataset.getDataSet().relationName());
                            } else {
                                DataIOUtils.saveDataset(wrTrain, preprocessedDataset, dataName);
                            }

                            wrTrain.close();
                            bwTrain.close();

                            BufferedWriter bw_xml = new BufferedWriter(new FileWriter(xmlPath));
                            PrintWriter wr_xml = new PrintWriter(bw_xml);

                            DataIOUtils.saveXMLFile(wr_xml, preprocessedDataset);

                            wr_xml.close();
                            bw_xml.close();
                        }

                        JOptionPane.showMessageDialog(null, "All files have been saved.", "Successful",
                                JOptionPane.INFORMATION_MESSAGE);

                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null, "An error ocurred while saving the dataset files.",
                                "alert", JOptionPane.ERROR_MESSAGE);
                        Logger.getLogger(RunApp.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                if (radioIterativeStratifiedHoldout.isSelected() || radioRandomHoldout.isSelected()
                        || radioLPStratifiedHoldout.isSelected()) //holdout
                {
                    BufferedWriter bwTrain;
                    try {
                        trainPath = file.getAbsolutePath() + "/" + dataName + "_train.arff";
                        testPath = file.getAbsolutePath() + "/" + dataName + "_test.arff";
                        xmlPath = file.getAbsolutePath() + "/" + dataName + ".xml";

                        if (format.toLowerCase().contains("meka")) {
                            bwTrain = new BufferedWriter(new FileWriter(trainPath));
                            PrintWriter wrTrain = new PrintWriter(bwTrain);

                            if (radioNoFS.isSelected()) {
                                DataIOUtils.saveMekaDataset(wrTrain, trainDataset,
                                        trainDataset.getDataSet().relationName());
                            } else {
                                DataIOUtils.saveMekaDataset(wrTrain, trainDataset, dataName);
                            }

                            wrTrain.close();
                            bwTrain.close();

                            BufferedWriter bwTest = new BufferedWriter(new FileWriter(testPath));
                            PrintWriter wrTest = new PrintWriter(bwTest);

                            if (radioNoFS.isSelected()) {
                                DataIOUtils.saveMekaDataset(wrTest, testDataset,
                                        testDataset.getDataSet().relationName());
                            } else {
                                DataIOUtils.saveMekaDataset(wrTest, testDataset, dataName);
                            }

                            wrTest.close();
                            bwTest.close();
                        } else {
                            bwTrain = new BufferedWriter(new FileWriter(trainPath));
                            PrintWriter wrTrain = new PrintWriter(bwTrain);

                            if (radioNoFS.isSelected()) {
                                DataIOUtils.saveDataset(wrTrain, trainDataset,
                                        trainDataset.getDataSet().relationName());
                            } else {
                                DataIOUtils.saveDataset(wrTrain, trainDataset, dataName);
                            }

                            wrTrain.close();
                            bwTrain.close();

                            BufferedWriter bwTest = new BufferedWriter(new FileWriter(testPath));
                            PrintWriter wrTest = new PrintWriter(bwTest);

                            if (radioNoFS.isSelected()) {
                                DataIOUtils.saveDataset(wrTest, testDataset,
                                        testDataset.getDataSet().relationName());
                            } else {
                                DataIOUtils.saveDataset(wrTest, testDataset, dataName);
                            }

                            wrTest.close();
                            bwTest.close();

                            BufferedWriter bwXml = new BufferedWriter(new FileWriter(xmlPath));
                            PrintWriter wrXml = new PrintWriter(bwXml);

                            DataIOUtils.saveXMLFile(wrXml, trainDataset);

                            wrXml.close();
                            bwXml.close();
                        }

                        JOptionPane.showMessageDialog(null, "All files have been saved.", "Successful",
                                JOptionPane.INFORMATION_MESSAGE);

                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null, "An error ocurred while saving the dataset files.",
                                "alert", JOptionPane.ERROR_MESSAGE);
                        Logger.getLogger(RunApp.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }

                else if (radioIterativeStratifiedCV.isSelected() || radioRandomCV.isSelected()
                        || radioLPStratifiedCV.isSelected())//CROSS VALIDATION
                {
                    try {

                        if (format.toLowerCase().contains("meka")) {
                            DataIOUtils.saveMekaDataset(trainDatasets, file.getAbsolutePath(), dataName,
                                    "_train");
                            DataIOUtils.saveMekaDataset(testDatasets, file.getAbsolutePath(), dataName,
                                    "_test");
                            /*
                            if(radioNoFS.isSelected() && radioNoIS.isSelected()){
                            DataIOUtils.saveMekaDataset(trainDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5), "_train");
                            DataIOUtils.saveMekaDataset(testDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5), "_test");
                            }
                            if(radioNoFS.isSelected()){
                            DataIOUtils.saveMekaDataset(trainDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5), "_train");
                            DataIOUtils.saveMekaDataset(testDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5), "_test");
                            }
                            else{
                            DataIOUtils.saveMekaDatasetsNoViews(trainDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5), "_train");
                            DataIOUtils.saveMekaDatasetsNoViews(testDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5), "_test");
                            }
                            */
                        } else {
                            /*
                            if(radioNoFS.isSelected() && radioNoIS.isSelected()){
                            DataIOUtils.saveDatasets(trainDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5), "_train");
                            DataIOUtils.saveDatasets(testDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5), "_test");
                            xmlPath = file.getAbsolutePath()+"/"+datasetName.substring(0,datasetName.length()-5)+".xml";
                            }
                            else if(radioNoFS.isSelected()){
                            DataIOUtils.saveDatasets(trainDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5), "_train");
                            DataIOUtils.saveDatasets(testDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5),  "_test");
                            xmlPath = file.getAbsolutePath()+"/"+datasetName.substring(0,datasetName.length()-5) + ".xml";
                            }
                            else{
                            DataIOUtils.saveMVDatasets(trainDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5), "_train");
                            DataIOUtils.saveMVDatasets(testDatasets,file.getAbsolutePath(), datasetName.substring(0,datasetName.length()-5),  "_test");
                            xmlPath = file.getAbsolutePath()+"/"+datasetName.substring(0,datasetName.length()-5)+ ".xml";
                            }
                            */
                            DataIOUtils.saveDatasets(trainDatasets, file.getAbsolutePath(), dataName, "_train");
                            DataIOUtils.saveDatasets(testDatasets, file.getAbsolutePath(), dataName, "_test");
                            xmlPath = file.getAbsolutePath() + "/" + dataName + ".xml";

                            BufferedWriter bwXml = new BufferedWriter(new FileWriter(xmlPath));
                            PrintWriter wrXml = new PrintWriter(bwXml);

                            DataIOUtils.saveXMLFile(wrXml, trainDatasets.get(0));

                            wrXml.close();
                            bwXml.close();
                        }

                        JOptionPane.showMessageDialog(null, "All files have been saved.", "Successful",
                                JOptionPane.INFORMATION_MESSAGE);

                    } catch (IOException | HeadlessException e1) {
                        JOptionPane.showMessageDialog(null, "An error ocurred while saving the dataset files.",
                                "alert", JOptionPane.ERROR_MESSAGE);
                        e1.printStackTrace();
                    }
                }

                Toolkit.getDefaultToolkit().beep();
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "An error ocurred while saving the dataset files.", "alert",
                JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
    }
}