Example usage for java.awt FileDialog getFile

List of usage examples for java.awt FileDialog getFile

Introduction

In this page you can find the example usage for java.awt FileDialog getFile.

Prototype

public String getFile() 

Source Link

Document

Gets the selected file of this file dialog.

Usage

From source file:livecanvas.mesheditor.MeshEditor.java

private void open(File file) {
    if (file == null) {
        FileDialog fd = new FileDialog(JOptionPane.getFrameForComponent(MeshEditor.this), "Load");
        fd.setVisible(true);/*from   w  w  w .  ja  v  a2  s .  c  o m*/
        String file_str = fd.getFile();
        if (file_str == null) {
            return;
        }
        file = new File(fd.getDirectory() + "/" + file_str);
    }
    try {
        StringWriter out = new StringWriter();
        InputStreamReader in = new InputStreamReader(new FileInputStream(file));
        Utils.copy(in, out);
        in.close();
        out.close();
        clear();
        JSONObject doc = new JSONObject(out.toString());
        Layer rootLayer = Layer.fromJSON(doc.getJSONObject("rootLayer"));
        layersView.setRootLayer(rootLayer);
        rootLayer.setCanvas(canvas);
        for (Layer layer : rootLayer.getSubLayersRecursively()) {
            layer.setCanvas(canvas);
        }
        JSONObject canvasJSON = doc.optJSONObject("canvas");
        if (canvasJSON != null) {
            canvas.fromJSON(canvasJSON);
        }
        canvas.setCurrLayer(rootLayer);
    } catch (Exception e1) {
        e1.printStackTrace();
        String msg = "An error occurred while trying to load.";
        JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(MeshEditor.this), msg, "Error",
                JOptionPane.ERROR_MESSAGE);
    }
    repaint();
}

From source file:livecanvas.mesheditor.MeshEditor.java

private void save(File file) {
    if (!canSave()) {
        return;//from   ww  w  .jav a  2s.  c o  m
    }
    if (file == null) {
        FileDialog fd = new FileDialog(JOptionPane.getFrameForComponent(MeshEditor.this), "Save");
        fd.setVisible(true);
        String file_str = fd.getFile();
        if (file_str == null) {
            return;
        }
        file = new File(fd.getDirectory() + "/" + file_str);
    }
    try {
        Layer rootLayer = layersView.getRootLayer();
        for (Layer l : rootLayer.getSubLayersRecursively()) {
            Path path = l.getPath();
            if (path.isFinalized()) {
                Mesh mesh = path.getMesh();
                if (mesh.getControlPointsCount() < 2) {
                    String msg = "At least one mesh has less than 2 control points.\n"
                            + "You may not be able to use it for animation. Do you\n"
                            + "still want to continue?";
                    if (JOptionPane.showConfirmDialog(this, msg, "Warning", JOptionPane.YES_NO_OPTION,
                            JOptionPane.WARNING_MESSAGE) != JOptionPane.YES_OPTION) {
                        return;
                    } else {
                        break;
                    }
                }
            }
        }
        PrintWriter out = new PrintWriter(new FileOutputStream(file));
        JSONObject doc = new JSONObject();
        doc.put("rootLayer", rootLayer.toJSON());
        doc.put("canvas", canvas.toJSON());
        doc.write(out);
        out.close();
    } catch (Exception e1) {
        e1.printStackTrace();
        String msg = "An error occurred while trying to load.";
        JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(MeshEditor.this), msg, "Error",
                JOptionPane.ERROR_MESSAGE);
    }
    repaint();
}

From source file:br.upe.ecomp.dosa.view.mainwindow.MainWindowActions.java

@Override
protected void exportButtonActionPerformed(ActionEvent evt) {
    String outputDirectory = null;
    String outputFile = null;/*w w  w  .ja v  a  2 s.c  o  m*/
    String measurement = (String) measurementLineResultComboBox.getSelectedItem();
    Integer step = Integer.parseInt(stepLineResultTextField.getText());

    FileDialog fileopen = new FileDialog(new Frame(), "Open Results Directory", FileDialog.SAVE);
    fileopen.setModalityType(ModalityType.DOCUMENT_MODAL);
    fileopen.setVisible(true);

    if (fileopen.getFile() != null) {
        outputDirectory = fileopen.getDirectory();
        outputFile = fileopen.getFile();
    }

    if (outputDirectory != null && outputFile != null) {
        new CSVParser().parse(outputDirectory, outputFile, resultFiles, measurement, step);
    }
}

From source file:com.declarativa.interprolog.gui.Ini.java

public void GraphAnalysis() {
    String fileToLoad, folder;//from ww w .  j  a  v  a2s  . c  om
    File filetoreconsult = null;
    FileDialog d = new FileDialog(this, "Consult file...");
    d.setVisible(true);
    fileToLoad = d.getFile();
    folder = d.getDirectory();
    if (fileToLoad != null) {
        ArgBuildManager manag = new ArgBuildManager();
        manag.GraphFileRules(folder, fileToLoad);//Call to the ArgBuildManager!
        /*
         filetoreconsult = new File(folder, fileToLoad);
         if (engine.consultAbsolute(filetoreconsult)) {
         addToReloaders(filetoreconsult, "consult");
         }
         */
        //System.out.println("file:"+filetoreconsult.);

    }
}

From source file:Forms.CreateGearForm.java

private Boolean saveSpec(GearSpec spec) {

    //Get top level frame
    JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(MasterPanel);

    //Create dialog for choosing gearspec file
    FileDialog fd = new FileDialog(topFrame, "Save .gearspec file", FileDialog.SAVE);
    fd.setDirectory(System.getProperty("user.home"));
    // Gets the name that is specified for the spec in the beginning
    fd.setFile(spec.getName() + ".gearspec");
    fd.setVisible(true);/*from w w w  . j  ava 2  s  . co  m*/
    //Get file
    String filename = fd.getFile();
    if (filename == null) {
        System.out.println("You cancelled the choice");
        return false;
    } else {
        System.out.println("You chose " + filename);

        //Get spec file
        File specFile = new File(fd.getDirectory() + Utils.pathSeparator() + filename);

        //Serialize spec to string
        String gearString = gson.toJson(spec);

        try {
            //If it exists, set it as the selected file path
            if (specFile.exists()) {
                FileUtils.forceDelete(specFile);
            }

            //Write new spec
            FileUtils.write(specFile, gearString);
        } catch (IOException e) {
            e.printStackTrace();
            showSaveErrorDialog();
            return false;
        }

        return true;
    }
}

From source file:JDAC.JDAC.java

public void saveData() {
    DateFormat df = new SimpleDateFormat("ddmmyy_HHmm");

    FileDialog fd = new FileDialog(this, "Export as...", FileDialog.SAVE);

    fd.setFile("Data_" + df.format(new Date()) + ".csv");
    fd.setVisible(true);//from   w w  w . j ava 2s .com

    if (fd.getFile() == null) {
        setStatus("Export CSV canceled");
        //export canceled
        return;
    }

    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(fd.getDirectory() + fd.getFile()));
        out.write(dataToCSVstring());
        out.close();
        setStatus("Export CSV successful");
    } catch (IOException e) {
        JOptionPane.showMessageDialog(this, "" + "Error while exporting data...\n"
                + "If the error persists please contact the system administrator");
        return;
    }
}

From source file:JDAC.JDAC.java

public void exportPNG() {
    DateFormat df = new SimpleDateFormat("ddmmyy_HHmm");

    FileDialog fd = new FileDialog(this, "Export as...", FileDialog.SAVE);

    fd.setFile("Chart_" + df.format(new Date()) + ".png");
    fd.setVisible(true);//from w  ww.j  a v a  2  s .c  o  m

    if (fd.getFile() == null) {
        setStatus("Export PNG canceled");
        //export canceled
        return;
    }

    mChart.setTitle(currentSensor);

    try {
        ChartUtilities.saveChartAsPNG(new File(fd.getDirectory() + fd.getFile()), mChart,
                Definitions.exportPNGwidth, Definitions.exportPNGheight);
        setStatus("Export PNG successful");
    } catch (IOException e) {
        JOptionPane.showMessageDialog(this, "" + "Error while exporting chart...\n"
                + "If the error persists please contact the system administrator");
        mChart.setTitle(mainChartPreviewTitle);
        return;
    }

    mChart.setTitle(mainChartPreviewTitle);
}

From source file:jpad.MainEditor.java

public void saveAs_OSX_Nix() {
    String fileToSaveTo = null;//from w  w  w.j  a  v  a  2 s .co  m
    FilenameFilter awtFilter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            String lowercaseName = name.toLowerCase();
            if (lowercaseName.endsWith(".txt")) {
                return true;
            } else {
                return false;
            }
        }
    };
    FileDialog fd = new FileDialog(this, "Save Text File", FileDialog.SAVE);
    fd.setDirectory(System.getProperty("java.home"));
    if (curFile == null)
        fd.setFile("Untitled.txt");
    else
        fd.setFile(curFile);
    fd.setFilenameFilter(awtFilter);
    fd.setVisible(true);
    if (fd.getFile() != null)
        fileToSaveTo = fd.getDirectory() + fd.getFile();
    else {
        fileToSaveTo = fd.getFile();
        return;
    }

    curFile = fileToSaveTo;
    JRootPane root = this.getRootPane();
    root.putClientProperty("Window.documentFile", new File(curFile));
    hasChanges = false;
    hasSavedToFile = true;
}

From source file:edu.ku.brc.specify.tools.schemalocale.SchemaLocalizerFrame.java

/**
 * Export data //w w  w.  j a va 2  s . c o m
 */
protected void exportSingleLocale() {
    statusBar.setText(getResourceString("SchemaLocalizerFrame.EXPORTING")); //$NON-NLS-1$
    statusBar.paintImmediately(statusBar.getBounds());

    schemaLocPanel.getAllDataFromUI();

    if (localizableIO.hasChanged()) {
        if (UIRegistry.askYesNoLocalized("SAVE", "CANCEL",
                UIRegistry.getResourceString("SchemaLocalizerFrame.NEEDS_SAVING"),
                "SAVE") == JOptionPane.YES_NO_OPTION) {
            localizableIO.save();
        } else {
            return;
        }
    }

    Vector<Locale> stdLocales = SchemaI18NService.getInstance().getStdLocaleList(false);
    final JList list = new JList(stdLocales);
    list.setCellRenderer(new DefaultListCellRenderer() {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value != null) {
                setText(((Locale) value).getDisplayName());
            }
            return this;
        }
    });

    PanelBuilder pb = new PanelBuilder(new FormLayout("f:p:g", "f:p:g"));
    pb.add(UIHelper.createScrollPane(list), (new CellConstraints()).xy(1, 1));
    pb.setDefaultDialogBorder();

    final CustomDialog dlg = new CustomDialog((Frame) UIRegistry.getTopWindow(),
            getResourceString("SchemaLocalizerFrame.CHOOSE_LOCALE"), true, pb.getPanel());
    list.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                dlg.getOkBtn().setEnabled(list.getSelectedValue() != null);
            }
        }
    });
    dlg.createUI();
    dlg.getOkBtn().setEnabled(false);

    UIHelper.centerAndShow(dlg);
    if (!dlg.isCancelled()) {
        Locale locale = (Locale) list.getSelectedValue();
        if (locale != null) {
            FileDialog fileDlg = new FileDialog((Frame) UIRegistry.getTopWindow(),
                    getResourceString("SchemaLocalizerFrame.SVFILENAME"), FileDialog.SAVE);
            fileDlg.setVisible(true);
            String fileName = fileDlg.getFile();
            if (StringUtils.isNotEmpty(fileName)) {
                File outFile = new File(fileDlg.getDirectory() + File.separator + fileName);
                boolean savedOK = localizableIO.exportSingleLanguageToDirectory(outFile, locale);
                if (savedOK) {
                    String msg = UIRegistry.getLocalizedMessage("SchemaLocalizerFrame.EXPORTEDTO",
                            locale.getDisplayName(), outFile.getAbsolutePath());
                    UIRegistry.displayInfoMsgDlg(msg);
                } else {
                    String msg = UIRegistry.getLocalizedMessage("SchemaLocalizerFrame.EXPORTING_ERR",
                            outFile.getAbsolutePath());
                    UIRegistry.showError(msg);
                }

            }
        }
    }
}

From source file:br.upe.ecomp.dosa.view.mainwindow.MainWindowActions.java

private void openTestScenario() {
    FileDialog fileopen = new FileDialog(this, "Open Test Scenario", FileDialog.LOAD);
    fileopen.setFilenameFilter(new FilenameFilter() {
        @Override/*from w ww  .  jav  a 2 s.  c  o  m*/
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith("xml");
        }
    });

    fileopen.setVisible(true);

    if (fileopen.getFile() != null) {
        filePath = fileopen.getDirectory();
        fileName = fileopen.getFile();

        algorithm = AlgorithmXMLParser.read(filePath, fileName);
        configureTree();
        updateToolBarButtonsState();
        ((CardLayout) panelTestScenario.getLayout()).last(panelTestScenario);
    }
}