Search in sources :

Example 91 with JFileChooser

use of javax.swing.JFileChooser in project JMRI by JMRI.

the class NceConsistRestore method run.

@Override
public void run() {
    // Get file to read from
    JFileChooser fc = new JFileChooser(FileUtil.getUserFilesPath());
    fc.addChoosableFileFilter(new textFilter());
    int retVal = fc.showOpenDialog(null);
    if (retVal != JFileChooser.APPROVE_OPTION) {
        // Canceled
        return;
    }
    if (fc.getSelectedFile() == null) {
        // Canceled
        return;
    }
    File f = fc.getSelectedFile();
    BufferedReader in;
    try {
        in = new BufferedReader(new FileReader(f));
    } catch (FileNotFoundException e) {
        return;
    }
    // create a status frame
    JPanel ps = new JPanel();
    jmri.util.JmriJFrame fstatus = new jmri.util.JmriJFrame("Consist Restore");
    fstatus.setLocationRelativeTo(null);
    fstatus.setSize(200, 100);
    fstatus.getContentPane().add(ps);
    ps.add(textConsist);
    ps.add(consistNumber);
    textConsist.setText("Consist line number:");
    textConsist.setVisible(true);
    consistNumber.setVisible(true);
    // Now read the file and check the consist address
    waiting = 0;
    // in case we break out early
    fileValid = false;
    // for user status messages
    int consistNum = 0;
    // load the start address of the NCE consist memory
    int curConsist = CS_CONSIST_MEM;
    // NCE Consist data
    byte[] consistData = new byte[CONSIST_LNTH];
    String line = " ";
    while (true) {
        try {
            line = in.readLine();
        } catch (IOException e) {
            break;
        }
        consistNumber.setText(Integer.toString(consistNum++));
        if (line == null) {
            // while loop does not break out quick enough
            log.error("NCE consist file terminator :0000 not found");
            break;
        }
        if (log.isDebugEnabled()) {
            log.debug("consist " + line);
        }
        // check that each line contains the NCE memory address of the consist
        String consistAddr = ":" + Integer.toHexString(curConsist);
        String[] consistLine = line.split(" ");
        // check for end of consist terminator
        if (consistLine[0].equalsIgnoreCase(":0000")) {
            // success!
            fileValid = true;
            break;
        }
        if (!consistAddr.equalsIgnoreCase(consistLine[0])) {
            log.error("Restore file selected is not a vaild backup file");
            log.error("Consist memory address in restore file should be " + consistAddr + " Consist address read " + consistLine[0]);
            break;
        }
        // consist file found, give the user the choice to continue
        if (curConsist == CS_CONSIST_MEM) {
            if (JOptionPane.showConfirmDialog(null, "Restore file found!  Restore can take over a minute, continue?", "NCE Consist Restore", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
                break;
            }
        }
        fstatus.setVisible(true);
        // now read the entire line from the file and create NCE message
        for (int i = 0; i < 8; i++) {
            // i = word index, j = byte index
            int j = i << 1;
            byte[] b = StringUtil.bytesFromHexString(consistLine[i + 1]);
            consistData[j] = b[0];
            consistData[j + 1] = b[1];
        }
        NceMessage m = writeNceConsistMemory(curConsist, consistData);
        tc.sendNceMessage(m, this);
        curConsist += CONSIST_LNTH;
        // wait for write to NCE CS to complete
        if (waiting > 0) {
            synchronized (this) {
                try {
                    wait(20000);
                } catch (InterruptedException e) {
                    // retain if needed later
                    Thread.currentThread().interrupt();
                }
            }
        }
        // failed
        if (waiting > 0) {
            log.error("timeout waiting for reply");
            break;
        }
    }
    try {
        in.close();
    } catch (IOException e) {
    }
    // kill status panel
    fstatus.dispose();
    if (fileValid) {
        JOptionPane.showMessageDialog(null, "Successful Restore!", "NCE Consist", JOptionPane.INFORMATION_MESSAGE);
    } else {
        JOptionPane.showMessageDialog(null, "Restore failed. Check console for error messages. \r\n" + "If operating at 19,200 baud, try 9600 baud.", "NCE Consist", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : JPanel(javax.swing.JPanel) NceMessage(jmri.jmrix.nce.NceMessage) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) JFileChooser(javax.swing.JFileChooser) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 92 with JFileChooser

use of javax.swing.JFileChooser in project JMRI by JMRI.

the class CsvExportAction method startLogging.

void startLogging(ActionEvent e) {
    System.out.println("" + e);
    ((JMenuItem) (e.getSource())).setText("Stop CSV Export Reading...");
    // initialize chooser
    if (fileChooser == null) {
        fileChooser = new JFileChooser();
    } else {
        fileChooser.rescanCurrentDirectory();
    }
    // get file
    int retVal = fileChooser.showSaveDialog(mParent);
    if (retVal == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        if (log.isDebugEnabled()) {
            log.debug("start to log to file " + file);
        }
        try {
            str = new PrintStream(new FileOutputStream(file));
            Distributor.instance().addReadingListener(this);
            logging = true;
        } catch (IOException ex) {
            log.error("Error opening file: " + ex);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) JFileChooser(javax.swing.JFileChooser) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JMenuItem(javax.swing.JMenuItem) File(java.io.File)

Example 93 with JFileChooser

use of javax.swing.JFileChooser in project processdash by dtuma.

the class FileSystemLOCDiffPanel method browseFile.

protected void browseFile(JTextField dest) {
    if (fileChooser == null) {
        fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    }
    if (fileChooser.showOpenDialog(configPanel) == JFileChooser.APPROVE_OPTION) {
        File f = fileChooser.getSelectedFile();
        if (f != null)
            dest.setText(f.getPath());
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) File(java.io.File)

Example 94 with JFileChooser

use of javax.swing.JFileChooser in project processdash by dtuma.

the class FileSelectionField method createFileChooser.

protected JFileChooser createFileChooser() {
    JFileChooser result = new JFileChooser();
    result.setFileSelectionMode(fileSelectionMode);
    return result;
}
Also used : JFileChooser(javax.swing.JFileChooser)

Example 95 with JFileChooser

use of javax.swing.JFileChooser in project processdash by dtuma.

the class WBSTabPanel method getFileChooser.

private JFileChooser getFileChooser() {
    if (fileChooser == null) {
        fileChooser = new JFileChooser();
        fileChooser.setFileFilter(TABFILE_FILTER);
        fileChooser.setMultiSelectionEnabled(false);
    }
    return fileChooser;
}
Also used : JFileChooser(javax.swing.JFileChooser)

Aggregations

JFileChooser (javax.swing.JFileChooser)797 File (java.io.File)571 IOException (java.io.IOException)185 FileFilter (javax.swing.filechooser.FileFilter)109 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)96 ActionEvent (java.awt.event.ActionEvent)59 ActionListener (java.awt.event.ActionListener)48 JButton (javax.swing.JButton)44 JPanel (javax.swing.JPanel)40 Component (java.awt.Component)39 FileOutputStream (java.io.FileOutputStream)39 FileNotFoundException (java.io.FileNotFoundException)30 JMenuItem (javax.swing.JMenuItem)29 Point (java.awt.Point)28 ArrayList (java.util.ArrayList)28 FileWriter (java.io.FileWriter)25 Dimension (java.awt.Dimension)24 PrintWriter (java.io.PrintWriter)24 JLabel (javax.swing.JLabel)22 JFrame (javax.swing.JFrame)21