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);
}
}
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);
}
}
}
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());
}
}
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;
}
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;
}
Aggregations