001package jmri.jmrix.loconet.soundloader;
002
003import java.awt.FlowLayout;
004import java.io.File;
005import java.io.IOException;
006
007import javax.swing.AbstractAction;
008import javax.swing.BoxLayout;
009import javax.swing.JButton;
010import javax.swing.JFileChooser;
011import javax.swing.JFrame;
012import javax.swing.JPanel;
013
014import jmri.util.swing.JmriJOptionPane;
015
016/**
017 * Frame for editing Digitrax SPJ files.
018 * <p>
019 * This is just an enclosure for the EditorPane, which does the real work.
020 * <p>
021 * This handles file read/write.
022 *
023 * @author Bob Jacobsen Copyright (C) 2006, 2007, 2008, 2010
024 */
025public class EditorPane extends jmri.jmrix.loconet.swing.LnPanel {
026
027    // GUI member declarations
028    EditorFilePane pane;
029
030    JButton open;
031    JButton save;
032
033    @Override
034    public String getHelpTarget() {
035        return "package.jmri.jmrix.loconet.soundloader.EditorFrame"; // NOI18N
036    }
037
038    @Override
039    public String getTitle() {
040        return getTitle(Bundle.getMessage("MenuItemSoundEditor"));
041    }
042
043    public EditorPane() {
044        super();
045
046        // general GUI config
047        super.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
048
049        // add file button
050        open = new JButton(Bundle.getMessage("ButtonOpen"));
051        open.addActionListener(new AbstractAction() {
052            @Override
053            public void actionPerformed(java.awt.event.ActionEvent e) {
054                selectInputFile();
055            }
056        });
057
058        save = new JButton(Bundle.getMessage("ButtonSave"));
059        save.addActionListener(new AbstractAction() {
060            @Override
061            public void actionPerformed(java.awt.event.ActionEvent e) {
062                selectSaveFile();
063            }
064        });
065        super.add(save);
066        save.setEnabled(false);
067
068        JPanel p = new JPanel();
069        p.setLayout(new FlowLayout());
070        p.add(open);
071        p.add(save);
072        super.add(p);
073
074        // for now, for debugging, load the file
075        // from a fixed name
076        // pane = new EditorPane("ac4400.spj");
077        // pane = new EditorPane("ac4400-silence.spj");
078        // pane = new EditorPane("java/test/jmri/jmrix/loconet/spjfile/sd38_2.spj");
079        //add(pane);
080    }
081
082    private static JFileChooser chooser;  // shared across all of these
083
084    private synchronized static void setChooser( JFileChooser jfc ){
085        chooser = jfc;
086    }
087    
088    void selectInputFile() {
089        if (chooser == null) {
090            setChooser( jmri.jmrit.XmlFile.userFileChooser()) ;
091        }
092        EditorPane.chooser.rescanCurrentDirectory();
093        int retVal = EditorPane.chooser.showOpenDialog(this);
094        if (retVal != JFileChooser.APPROVE_OPTION) {
095            return;  // give up if no file selected
096        }
097        // success, open the file
098        addFile(EditorPane.chooser.getSelectedFile());
099    }
100
101    void selectSaveFile() {
102        if (chooser == null) {
103            setChooser( new jmri.util.swing.JmriJFileChooser(System.getProperty("user.dir"))); // NOI18N
104        }
105        int retVal = EditorPane.chooser.showSaveDialog(this);
106        if (retVal != JFileChooser.APPROVE_OPTION) {
107            return;  // give up if no file selected
108        }
109        // success, open the file
110        try {
111            saveFile(EditorPane.chooser.getSelectedFile().getPath());
112        } catch (IOException e) {
113            // failed, warn user
114            JmriJOptionPane.showMessageDialog(this, "Error during save: " + e,
115                    "Save failed!", JmriJOptionPane.WARNING_MESSAGE);
116        }
117    }
118
119    void addFile(File name) {
120        if (pane != null) {
121            // already defined
122            return;
123        }
124        pane = new EditorFilePane(name);
125        add(pane);
126        open.setEnabled(false);
127        save.setEnabled(true);
128        revalidate();
129        // major resize, repack
130        java.awt.Container co = getTopLevelAncestor();
131        if ( co instanceof JFrame ) {
132            ((JFrame) co).pack();
133        }
134    }
135
136    void saveFile(String name) throws IOException {
137        pane.saveFile(name);
138    }
139
140    @Override
141    public void dispose() {
142        if (pane != null) {
143            pane.dispose();
144        }
145        super.dispose();
146    }
147}