use of javax.swing.JFileChooser in project JMRI by JMRI.
the class DccSpeedProfile method openExportFile.
private static void openExportFile() {
JFileChooser fileChooser = new JFileChooser(FileUtil.getUserFilesPath());
String fileName = null;
// get filename
// start at current file, show dialog
int retVal = fileChooser.showSaveDialog(null);
// handle selection or cancel
if (retVal == JFileChooser.APPROVE_OPTION) {
fileName = fileChooser.getSelectedFile().getPath();
}
try {
// Create a print writer based on the file, so we can print to it.
out = new FileOutputStream(fileName);
p = new PrintWriter(out, true);
} catch (IOException ex) {
if (log.isDebugEnabled()) {
log.debug("Problem creating output stream " + ex);
}
}
if (out == null) {
log.error("Null File Output Stream");
}
if (p == null) {
log.error("Null Print Writer");
}
}
use of javax.swing.JFileChooser in project JMRI by JMRI.
the class EditorTableDataModel method replWavButtonPressed.
void replWavButtonPressed(Object value, int row, int col) {
if (chooser == null) {
chooser = new JFileChooser(FileUtil.getUserFilesPath());
}
chooser.rescanCurrentDirectory();
int retVal = chooser.showOpenDialog(null);
if (retVal != JFileChooser.APPROVE_OPTION) {
// give up if no file selected
return;
}
// load file
jmri.jmrit.sound.WavBuffer buff;
try {
buff = new jmri.jmrit.sound.WavBuffer(chooser.getSelectedFile());
} catch (Exception e) {
log.error("Exception loading file: " + e);
return;
}
// store to memory
file.getHeader(row + 1).setContent(buff.getByteArray(), buff.getDataStart(), buff.getDataSize());
// update rest of header
file.getHeader(row + 1).setName(chooser.getSelectedFile().getName());
// mark table changes in other rows
fireTableRowsUpdated(row, row);
}
use of javax.swing.JFileChooser in project JMRI by JMRI.
the class LoaderPane method selectInputFile.
void selectInputFile() {
String name = inputFileName.getText();
if (name.equals("")) {
name = FileUtil.getUserFilesPath();
}
if (chooser == null) {
chooser = new JFileChooser(name);
}
// clear out in case of failure
inputFileName.setText("");
int retVal = chooser.showOpenDialog(this);
if (retVal != JFileChooser.APPROVE_OPTION) {
// give up if no file selected
return;
}
inputFileName.setText(chooser.getSelectedFile().getName());
readButton.setEnabled(true);
readButton.setToolTipText(res.getString("TipReadEnabled"));
loadButton.setEnabled(false);
loadButton.setToolTipText(res.getString("TipLoadDisabled"));
status.setText(res.getString("StatusReadFile"));
}
use of javax.swing.JFileChooser in project JMRI by JMRI.
the class NceMacroBackup method run.
@Override
public void run() {
// get file to write to
JFileChooser fc = new JFileChooser(FileUtil.getUserFilesPath());
fc.addChoosableFileFilter(new textFilter());
File fs = new File("NCE macro backup.txt");
fc.setSelectedFile(fs);
int retVal = fc.showSaveDialog(null);
if (retVal != JFileChooser.APPROVE_OPTION) {
// Canceled
return;
}
if (fc.getSelectedFile() == null) {
// Canceled
return;
}
File f = fc.getSelectedFile();
if (fc.getFileFilter() != fc.getAcceptAllFileFilter()) {
// append .txt to file name if needed
String fileName = f.getAbsolutePath();
String fileNameLC = fileName.toLowerCase();
if (!fileNameLC.endsWith(".txt")) {
fileName = fileName + ".txt";
f = new File(fileName);
}
}
if (f.exists()) {
if (JOptionPane.showConfirmDialog(null, "File " + f.getName() + " already exists, overwrite it?", "Overwrite file?", JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION) {
return;
}
}
PrintWriter fileOut;
try {
fileOut = new PrintWriter(new BufferedWriter(new FileWriter(f)), true);
} catch (IOException e) {
return;
}
if (JOptionPane.showConfirmDialog(null, "Backup can take over a minute, continue?", "NCE Macro Backup", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
fileOut.close();
return;
}
// create a status frame
JPanel ps = new JPanel();
jmri.util.JmriJFrame fstatus = new jmri.util.JmriJFrame("Macro Backup");
fstatus.setLocationRelativeTo(null);
fstatus.setSize(200, 100);
fstatus.getContentPane().add(ps);
ps.add(textMacro);
ps.add(macroNumber);
textMacro.setText("Macro number:");
textMacro.setVisible(true);
macroNumber.setVisible(true);
// now read NCE CS macro memory and write to file
// reset in case there was a previous error
waiting = 0;
// assume we're going to succeed
fileValid = true;
for (int macroNum = 0; macroNum < NUM_MACRO; macroNum++) {
macroNumber.setText(Integer.toString(macroNum));
fstatus.setVisible(true);
getNceMacro(macroNum);
if (!fileValid) {
// break out of for loop
macroNum = NUM_MACRO;
}
if (fileValid) {
StringBuffer buf = new StringBuffer();
buf.append(":" + Integer.toHexString(CS_MACRO_MEM + (macroNum * MACRO_LNTH)));
for (int i = 0; i < MACRO_LNTH; i++) {
buf.append(" " + StringUtil.twoHexFromInt(nceMacroData[i++]));
buf.append(StringUtil.twoHexFromInt(nceMacroData[i]));
}
if (log.isDebugEnabled()) {
log.debug("macro " + buf.toString());
}
fileOut.println(buf.toString());
}
}
if (fileValid) {
// NCE file terminator
String line = ":0000";
fileOut.println(line);
}
// Write to disk and close file
fileOut.flush();
fileOut.close();
// kill status panel
fstatus.dispose();
if (fileValid) {
JOptionPane.showMessageDialog(null, "Successful Backup!", "NCE Macro", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Backup failed", "NCE Macro", JOptionPane.ERROR_MESSAGE);
}
}
use of javax.swing.JFileChooser in project JMRI by JMRI.
the class NceConsistBackup method run.
@Override
public void run() {
// get file to write to
JFileChooser fc = new JFileChooser(FileUtil.getUserFilesPath());
fc.addChoosableFileFilter(new textFilter());
File fs = new File("NCE consist backup.txt");
fc.setSelectedFile(fs);
int retVal = fc.showSaveDialog(null);
if (retVal != JFileChooser.APPROVE_OPTION) {
// Canceled
return;
}
if (fc.getSelectedFile() == null) {
// Canceled
return;
}
File f = fc.getSelectedFile();
if (fc.getFileFilter() != fc.getAcceptAllFileFilter()) {
// append .txt to file name if needed
String fileName = f.getAbsolutePath();
String fileNameLC = fileName.toLowerCase();
if (!fileNameLC.endsWith(".txt")) {
fileName = fileName + ".txt";
f = new File(fileName);
}
}
if (f.exists()) {
if (JOptionPane.showConfirmDialog(null, "File " + f.getName() + " already exists, overwrite it?", "Overwrite file?", JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION) {
return;
}
}
PrintWriter fileOut;
try {
fileOut = new PrintWriter(new BufferedWriter(new FileWriter(f)), true);
} catch (IOException e) {
return;
}
if (JOptionPane.showConfirmDialog(null, "Backup can take over a minute, continue?", "NCE Consist Backup", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
fileOut.close();
return;
}
// create a status frame
JPanel ps = new JPanel();
jmri.util.JmriJFrame fstatus = new jmri.util.JmriJFrame("Consist Backup");
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 NCE CS consist memory and write to file
// reset in case there was a previous error
waiting = 0;
// assume we're going to succeed
fileValid = true;
for (int consistNum = 0; consistNum < workingNumConsists; consistNum++) {
consistNumber.setText(Integer.toString(consistNum));
fstatus.setVisible(true);
getNceConsist(consistNum);
if (!fileValid) {
// break out of for loop
consistNum = workingNumConsists;
}
if (fileValid) {
StringBuffer buf = new StringBuffer();
buf.append(":" + Integer.toHexString(NceCmdStationMemory.CabMemorySerial.CS_CONSIST_MEM + (consistNum * CONSIST_LNTH)));
for (int i = 0; i < CONSIST_LNTH; i++) {
buf.append(" " + StringUtil.twoHexFromInt(nceConsistData[i++]));
buf.append(StringUtil.twoHexFromInt(nceConsistData[i]));
}
if (log.isDebugEnabled()) {
log.debug("consist " + buf.toString());
}
fileOut.println(buf.toString());
}
}
if (fileValid) {
// NCE file terminator
String line = ":0000";
fileOut.println(line);
}
// Write to disk and close file
fileOut.flush();
fileOut.close();
// kill status panel
fstatus.dispose();
if (fileValid) {
JOptionPane.showMessageDialog(null, "Successful Backup!", "NCE Consist", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Backup failed", "NCE Consist", JOptionPane.ERROR_MESSAGE);
}
}
Aggregations