001    /**
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: Apr 2, 2003
005     * Time: 7:54:26 PM
006     */
007    
008    package EVolve.util.settings;
009    
010    import java.io.File;
011    import java.io.FileWriter;
012    import java.io.IOException;
013    
014    import javax.swing.JDialog;
015    import javax.swing.JTextField;
016    
017    import EVolve.Scene;
018    
019    public abstract class Setting {
020        protected String iniFilename;
021        protected String tags[];
022        protected JTextField contents[];
023        protected JDialog dialog;
024    
025        protected String getFilenameWithPath(String name) {
026            boolean isRunningUnderWindows = System.getProperty("os.name").startsWith("Windows");
027            String userHome = System.getProperty("user.home");
028            String configFile = null;
029            if (userHome == null || userHome.trim().equals("")) {
030                // Fall back to current directory if undefined
031                userHome = ".";
032            }
033    
034            if (!userHome.endsWith(String.valueOf(Character.toString(File.separatorChar)))) {
035                userHome += File.separatorChar;
036            }
037    
038            if (isRunningUnderWindows) {
039                configFile = name + ".ini";
040            } else {
041                configFile = "." + name;
042            }
043            return userHome + configFile;
044        }
045    
046        protected void save() {
047            try {
048                FileWriter writer = new FileWriter(iniFilename);
049                for (int i=0; i<tags.length; i++) {
050                    writer.write(tags[i]+"\n");
051                    writer.write(contents[i].getText()+"\n");
052    
053                }
054                writer.flush();
055                writer.close();
056            } catch (IOException e) {
057                Scene.showErrorMessage("Unable to update file "+iniFilename);
058            }
059        }
060    }