001package jmri.jmrit.roster;
002
003import java.awt.FlowLayout;
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 javax.swing.filechooser.FileFilter;
016
017import jmri.InstanceManager;
018import jmri.profile.Profile;
019import jmri.profile.ProfileManager;
020import jmri.swing.PreferencesPanel;
021import jmri.util.FileUtil;
022import jmri.util.swing.JmriJOptionPane;
023
024import org.openide.util.lookup.ServiceProvider;
025
026/**
027 * Provide GUI to configure Roster defaults.
028 *
029 *
030 * @author Bob Jacobsen Copyright (C) 2001, 2003, 2007
031 */
032@ServiceProvider(service = PreferencesPanel.class)
033public class RosterConfigPane extends JPanel implements PreferencesPanel {
034
035    JLabel filename;
036    JTextField owner = new JTextField(20);
037    JFileChooser fc;
038
039    public RosterConfigPane() {
040        fc = new jmri.util.swing.JmriJFileChooser(FileUtil.getUserFilesPath());
041        // filter to only show the roster.xml file
042        FileFilter filt = new FileFilter() {
043            @Override
044            public boolean accept(File f) {
045                if (f.getName().equals("roster.xml")) {
046                    return true;
047                } else {
048                    return f.isDirectory();
049                }
050            }
051
052            @Override
053            public String getDescription() {
054                return "roster.xml only";
055            }
056        };
057
058        fc.setDialogTitle(Bundle.getMessage("DialogTitleMove"));
059        fc.setFileFilter(filt);
060
061        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
062        JPanel p = new JPanel();
063        p.setLayout(new FlowLayout());
064        p.add(new JLabel(Bundle.getMessage("LabelMoveLocation")));
065
066        p.add(filename = new JLabel(Roster.getDefault().getRosterLocation()));
067        // don't show default location, so it's not deemed a user selection
068        // and saved
069        if (FileUtil.getUserFilesPath().equals(Roster.getDefault().getRosterLocation())) {
070            filename.setText("");
071        }
072        JButton b = new JButton(Bundle.getMessage("ButtonSetDots"));
073
074        b.addActionListener(new AbstractAction() {
075            @Override
076            public void actionPerformed(ActionEvent e) {
077                // prompt with instructions
078                if (JmriJOptionPane.OK_OPTION != JmriJOptionPane.showConfirmDialog(RosterConfigPane.this.getTopLevelAncestor(),
079                        Bundle.getMessage("DialogMsgMoveWarning"),
080                        Bundle.getMessage("DialogMsgMoveQuestion"),
081                        JmriJOptionPane.OK_CANCEL_OPTION
082                )) {
083                    return;
084                }
085
086                // get the file
087                fc.rescanCurrentDirectory();
088                fc.showOpenDialog(null);
089                if (fc.getSelectedFile() == null) {
090                    return; // cancelled
091                }
092                if (!fc.getSelectedFile().getName().equals("roster.xml")) {
093                    return; // wrong file
094                }
095                filename.setText(fc.getSelectedFile().getParent() + File.separator);
096                validate();
097                packTopFrame();
098            }
099        });
100        p.add(b);
101        b = new JButton(Bundle.getMessage("ButtonReset"));
102        b.addActionListener(new AbstractAction() {
103            @Override
104            public void actionPerformed(ActionEvent e) {
105                filename.setText("");
106                validate();
107                packTopFrame();
108            }
109        });
110        p.add(b);
111        add(p);
112
113        JPanel p2 = new JPanel();
114        p2.setLayout(new FlowLayout());
115        p2.add(new JLabel(Bundle.getMessage("LabelDefaultOwner")));
116        owner.setText(InstanceManager.getDefault(RosterConfigManager.class).getDefaultOwner());
117        p2.add(owner);
118        add(p2);
119    }
120    
121    private void packTopFrame(){
122        java.awt.Container co = getTopLevelAncestor();
123        if ( co instanceof JFrame ) {
124            ((JFrame) co).pack();
125        }
126    }
127
128    public String getDefaultOwner() {
129        return owner.getText();
130    }
131
132    public void setDefaultOwner(String defaultOwner) {
133        owner.setText(defaultOwner);
134    }
135
136    public String getSelectedItem() {
137        return filename.getText();
138    }
139
140    @Override
141    public String getPreferencesItem() {
142        return "ROSTER"; // NOI18N
143    }
144
145    @Override
146    public String getPreferencesItemText() {
147        return Bundle.getMessage("MenuItemRoster"); // NOI18N
148    }
149
150    @Override
151    public String getTabbedPreferencesTitle() {
152        return Bundle.getMessage("TabbedLayoutRoster"); // NOI18N
153    }
154
155    @Override
156    public String getLabelKey() {
157        return Bundle.getMessage("LabelTabbedLayoutRoster"); // NOI18N
158    }
159
160    @Override
161    public JComponent getPreferencesComponent() {
162        return this;
163    }
164
165    @Override
166    public boolean isPersistant() {
167        return true;
168    }
169
170    @Override
171    public String getPreferencesTooltip() {
172        return null;
173    }
174
175    @Override
176    public void savePreferences() {
177        Profile project = ProfileManager.getDefault().getActiveProfile();
178        RosterConfigManager manager = InstanceManager.getDefault(RosterConfigManager.class);
179        manager.setDefaultOwner(project, this.getDefaultOwner());
180        manager.setDirectory(project, this.getSelectedItem());
181        manager.savePreferences(project);
182    }
183
184    @Override
185    public boolean isDirty() {
186        return (this.isFileLocationChanged()
187                || !InstanceManager.getDefault(RosterConfigManager.class).getDefaultOwner().equals(this.getDefaultOwner()));
188    }
189
190    @Override
191    public boolean isRestartRequired() {
192        return this.isFileLocationChanged();
193    }
194
195    private boolean isFileLocationChanged() {
196        return (this.getSelectedItem() == null || this.getSelectedItem().isEmpty())
197                ? !Roster.getDefault().getRosterLocation().equals(FileUtil.getUserFilesPath())
198                : !Roster.getDefault().getRosterLocation().equals(this.getSelectedItem());
199    }
200
201    @Override
202    public boolean isPreferencesValid() {
203        return true; // no validity checking performed
204    }
205}