001package apps;
002
003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
004import java.awt.event.ActionEvent;
005import java.io.File;
006import javax.swing.AbstractAction;
007import javax.swing.BoxLayout;
008import javax.swing.JButton;
009import javax.swing.JComponent;
010import javax.swing.JFileChooser;
011import javax.swing.JFrame;
012import javax.swing.JLabel;
013import javax.swing.JPanel;
014import javax.swing.JTextField;
015import jmri.InstanceManager;
016import jmri.implementation.FileLocationsPreferences;
017import jmri.profile.Profile;
018import jmri.profile.ProfileManager;
019import jmri.swing.PreferencesPanel;
020import jmri.util.FileUtil;
021import org.openide.util.lookup.ServiceProvider;
022
023/**
024 * Provide GUI to configure the Default File Locations.
025 * <p>
026 * Provides GUI configuration for the default file locations by displaying
027 * textfields for the user to directly enter in their own path or a Set button
028 * is provided so that the user can select the path.
029 *
030 * @author Kevin Dickerson Copyright (C) 2010
031 */
032@ServiceProvider(service = PreferencesPanel.class)
033public final class FileLocationPane extends JPanel implements PreferencesPanel {
034
035    private boolean restartRequired = false;
036    private final JTextField scriptLocation = new JTextField();
037    private final JTextField userLocation = new JTextField();
038
039    public FileLocationPane() {
040        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
041
042        add(prefLocation());
043        add(scriptsLocation());
044
045        /*p = new JPanel();
046         JLabel throttle = new JLabel("Default Throttle Location");
047         p.add(throttle);
048         p.add(throttleLocation);
049         throttleLocation.setColumns(20);
050         throttleLocation.setText(jmri.jmrit.throttle.ThrottleFrame.getDefaultThrottleFolder());
051         add(p);*/
052    }
053
054    private JPanel scriptsLocation() {
055        JButton bScript = new JButton(ConfigBundle.getMessage("ButtonSetDots"));
056        final JFileChooser fcScript;
057        fcScript = new jmri.util.swing.JmriJFileChooser(FileUtil.getScriptsPath());
058
059        fcScript.setDialogTitle(ConfigBundle.getMessage("MessageSelectDirectory"));
060        fcScript.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
061        fcScript.setAcceptAllFileFilterUsed(false);
062        bScript.addActionListener(new OpenAction(fcScript, scriptLocation));
063        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
064        JPanel p = new JPanel();
065        JLabel scripts = new JLabel(ConfigBundle.getMessage("ScriptDir"));
066        p.add(scripts);
067        p.add(scriptLocation);
068        p.add(bScript);
069        scriptLocation.setColumns(30);
070        scriptLocation.setText(FileUtil.getScriptsPath());
071        return p;
072    }
073
074    private JPanel prefLocation() {
075        JPanel p = new JPanel();
076        JLabel users = new JLabel(ConfigBundle.getMessage("PrefDir"));
077        p.add(users);
078        p.add(userLocation);
079        userLocation.setColumns(30);
080        userLocation.setText(FileUtil.getUserFilesPath());
081
082        JButton bUser = new JButton(ConfigBundle.getMessage("ButtonSetDots"));
083        final JFileChooser fcUser;
084        fcUser = new jmri.util.swing.JmriJFileChooser(FileUtil.getUserFilesPath());
085
086        fcUser.setDialogTitle(ConfigBundle.getMessage("MessageSelectDirectory"));
087        fcUser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
088        fcUser.setAcceptAllFileFilterUsed(false);
089        bUser.addActionListener(new OpenAction(fcUser, userLocation));
090        p.add(bUser);
091        return p;
092    }
093
094    @Override
095    public String getPreferencesItem() {
096        return "FILELOCATIONS"; // NOI18N
097    }
098
099    @Override
100    public String getPreferencesItemText() {
101        return ConfigBundle.getMessage("MenuFileLocation"); // NOI18N
102    }
103
104    @Override
105    public String getTabbedPreferencesTitle() {
106        return ConfigBundle.getMessage("TabbedLayoutFileLocations"); // NOI18N
107    }
108
109    @Override
110    public String getLabelKey() {
111        return ConfigBundle.getMessage("LabelTabbedFileLocations"); // NOI18N
112    }
113
114    @Override
115    public JComponent getPreferencesComponent() {
116        return this;
117    }
118
119    @Override
120    public boolean isPersistant() {
121        return true;
122    }
123
124    @Override
125    public String getPreferencesTooltip() {
126        return null;
127    }
128
129    @Override
130    public void savePreferences() {
131        if (!FileUtil.getUserFilesPath().equals(this.userLocation.getText())) {
132            FileUtil.setUserFilesPath(ProfileManager.getDefault().getActiveProfile(), this.userLocation.getText());
133            this.restartRequired = true;
134        }
135        if (!FileUtil.getScriptsPath().equals(this.scriptLocation.getText())) {
136            FileUtil.setScriptsPath(ProfileManager.getDefault().getActiveProfile(), this.scriptLocation.getText());
137            this.restartRequired = true;
138        }
139        Profile profile = ProfileManager.getDefault().getActiveProfile();
140        if (profile != null) {
141            InstanceManager.getDefault(FileLocationsPreferences.class).savePreferences(profile);
142        }
143    }
144
145    @Override
146    public boolean isDirty() {
147        return (!FileUtil.getUserFilesPath().equals(this.userLocation.getText())
148                || !FileUtil.getScriptsPath().equals(this.scriptLocation.getText()));
149    }
150
151    @Override
152    public boolean isRestartRequired() {
153        return this.restartRequired;
154    }
155
156    @Override
157    public boolean isPreferencesValid() {
158        return true; // no validity checking performed
159    }
160
161    private class OpenAction extends AbstractAction {
162
163        JFileChooser chooser;
164        JTextField field;
165
166        OpenAction(JFileChooser chooser, JTextField field) {
167            this.chooser = chooser;
168            this.field = field;
169        }
170
171        @Override
172        @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "protected by if instanceof")
173        public void actionPerformed(ActionEvent e) {
174            // get the file
175            chooser.showOpenDialog(null);
176            if (chooser.getSelectedFile() == null) {
177                return; // cancelled
178            }
179            field.setText(chooser.getSelectedFile() + File.separator);
180            validate();
181            if (getTopLevelAncestor() != null && getTopLevelAncestor() instanceof JFrame) {
182                ((JFrame) getTopLevelAncestor()).pack();
183            }
184        }
185    }
186
187}