001package jmri.util.startup; 002 003import java.awt.Component; 004import java.io.IOException; 005 006import javax.swing.JFileChooser; 007 008import jmri.InstanceManager; 009 010import org.slf4j.Logger; 011import org.slf4j.LoggerFactory; 012 013/** 014 * Provide an abstract StartupModelFactory with common methods for factories 015 * that manipulate models that open files. 016 * 017 * @author Randall Wood 018 */ 019public abstract class AbstractFileModelFactory implements StartupModelFactory { 020 021 private JFileChooser chooser = null; 022 private final static Logger log = LoggerFactory.getLogger(AbstractFileModelFactory.class); 023 024 @Override 025 public String getDescription() { 026 return Bundle.getMessage(this.getModelClass().getCanonicalName()); 027 } 028 029 /** 030 * This factory simply displays a {@link javax.swing.JFileChooser} to allow 031 * users to configure the action. Subclasses to performAction the correct file 032 chooser by implementing this method. 033 * 034 * @return a configured file chooser. 035 */ 036 abstract protected JFileChooser setFileChooser(); 037 038 @Override 039 public String getActionText() { 040 return Bundle.getMessage("EditableStartupAction", this.getDescription()); 041 } 042 043 @Override 044 public void editModel(StartupModel model, Component parent) { 045 if (this.getModelClass().isInstance(model)) { 046 if (this.chooser == null) { 047 this.chooser = this.setFileChooser(); 048 this.chooser.setDialogTitle(this.getDescription()); 049 this.chooser.setDialogType(JFileChooser.CUSTOM_DIALOG); 050 } 051 if (this.chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) { 052 try { 053 String name = model.getName(); 054 if (name == null || !name.equals(this.chooser.getSelectedFile().getCanonicalPath())) { 055 model.setName(this.chooser.getSelectedFile().getCanonicalPath()); 056 InstanceManager.getDefault(StartupActionsManager.class).setRestartRequired(); 057 } 058 } catch (IOException ex) { 059 log.error("File {} does not exist.", this.chooser.getSelectedFile()); 060 } 061 } 062 } 063 } 064 065 @Override 066 public void initialize() { 067 // nothing to do 068 } 069}