Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package edu.synth.state; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map.Entry; import java.util.TreeMap; import org.apache.commons.io.FileUtils; import edu.synth.logging.Messager; import edu.synth.settings.AbundanceSettings; import edu.synth.settings.ConvolveSettings; import edu.synth.settings.SynthSettings; import edu.synth.utils.Constants; import edu.synth.utils.FileWorker; import edu.synth.utils.FilenameFilter; /** * Singleton for ClearDIBHelper state * * @author */ public class SyntHelperState { private static volatile SyntHelperState clearDIBHelperState; private String workspacePath = ""; private SyntHelperState() { } public static SyntHelperState getInstance() { if (clearDIBHelperState == null) { synchronized (SyntHelperState.class) { if (clearDIBHelperState == null) clearDIBHelperState = new SyntHelperState(); } } return clearDIBHelperState; } private SynthSettings synthSettings = new SynthSettings(); private AbundanceSettings abundanceSettings = new AbundanceSettings(); private ConvolveSettings convolveSettings = new ConvolveSettings(); /** * @return the workspacePath */ public String getWorkspacePath() { return workspacePath; } /** * @param workspacePath * the workspacePath to set * @throws java.io.IOException */ public void setWorkspacePath(String workspacePath) throws IOException { this.workspacePath = workspacePath; handleWorkspace(new File(workspacePath)); } /** * @return the synthSettings */ public SynthSettings getSynthSettings() { return synthSettings; } /** * @param synthSettings * the synthSettings to set */ public void setSynthSettings(SynthSettings synthSettings) { this.synthSettings = synthSettings; } /** * @return the abundanceSettings */ public AbundanceSettings getAbundanceSettings() { return abundanceSettings; } /** * @param abundanceSettings * the abundanceSettings to set */ public void setAbundanceSettings(AbundanceSettings abundanceSettings) { this.abundanceSettings = abundanceSettings; } /** * @return the convolveSettings */ public ConvolveSettings getConvolveSettings() { return convolveSettings; } /** * @param convolveSettings * the convolveSettings to set */ public void setConvolveSettings(ConvolveSettings convolveSettings) { this.convolveSettings = convolveSettings; } private boolean handleFileList(File wDir, FilenameFilter filter, File dest) { String path = ""; boolean done = false; String[] files = wDir.list(filter); int max = files.length; if (max > 0) { for (int i = 0; i < max; i++) { path = wDir + File.separator + files[i]; File src = new File(path); if (src.exists()) try { FileUtils.copyFile(src, dest); done = true; Messager.publish("SYNTHelperState - handleFileList", path + " to " + dest.getPath() + " has been copied successfuly..."); break; } catch (IOException e) { Messager.publish("SYNTHelperState - handleFileList", (Exception) e); } } } return done; } private void handleWorkspace(File wDir) throws IOException { if (wDir.isDirectory()) { if (!handleFileList(wDir, new FilenameFilter(".abn"), new File(Constants.ABN_PATH))) { FileUtils.copyFile(new File(Constants.DEF_SOLAR_ABN), new File(Constants.ABN_PATH)); Messager.publish("SYNTHelperState - handleWorkspace", Constants.DEF_SOLAR_ABN + " to " + Constants.ABN_PATH + " has been copied successfuly... Default settings!"); } if (!handleFileList(wDir, new FilenameFilter("synt.inp"), new File(Constants.SYNTH_PATH))) { FileUtils.copyFile(new File(Constants.DEF_SYNTH_INP), new File(Constants.SYNTH_PATH)); Messager.publish("SYNTHelperState - handleWorkspace", Constants.DEF_SYNTH_INP + " to " + Constants.SYNTH_PATH + " has been copied successfuly... Default settings!"); } if (!handleFileList(wDir, new FilenameFilter("input.atl"), new File(Constants.MODEL_PATH))) Messager.publish("SYNTHelperState - handleWorkspace", "Kurucz's format model file is not found... Some error? Check workspace directory or file names there..."); if (!handleFileList(wDir, new FilenameFilter(".obs"), new File(Constants.OBS_DATA))) Messager.publish("SYNTHelperState - handleWorkspace", "Observational data file is not found... Some error? Check workspace directory or file names there..."); if (!handleFileList(wDir, new FilenameFilter("convolve.conf"), new File(Constants.CONV_PATH))) { FileUtils.copyFile(new File(Constants.DEF_CONV_CONF), new File(Constants.CONV_PATH)); Messager.publish("SYNTHelperState - handleWorkspace", Constants.DEF_CONV_CONF + " to " + Constants.CONV_PATH + " has been copied successfuly... Default settings!"); } } } public void resetLinePathList() throws IOException { String pathLns = new File(Constants.DEF_LNS).getCanonicalPath(); getSynthSettings().setBinaryFilesPaths(new LinkedHashSet<String>()); getSynthSettings().getBinaryFilesPaths().add(pathLns); } public void saveSettings() throws IOException { getSynthSettings().saveSettings(new File(Constants.SYNTH_PATH)); getConvolveSettings().saveSettings(new File(Constants.CONV_PATH)); getAbundanceSettings().saveSettings(new File(Constants.ABN_PATH)); Messager.publish("Saving Settings", "Settings have been saved successfuly..."); } public void copySettingsFilesToWorkspace() throws IOException { File src = new File(Constants.SYNTH_PATH); FileUtils.copyFile(src, new File(getWorkspacePath() + File.separator + src.getName())); src = new File(Constants.CONV_PATH); FileUtils.copyFile(src, new File(getWorkspacePath() + File.separator + src.getName())); src = new File(Constants.ABN_PATH); FileUtils.copyFile(src, new File(getWorkspacePath() + File.separator + src.getName())); Messager.publish("Export Settings Files to Workspace", "Files have been copied successfuly..."); } public void handleObsData() throws IOException { File obsFile = new File(Constants.OBS_DATA); TreeMap<Double, Double> obs = FileWorker.getSortedDoubleData(obsFile); synthSettings.setStartSynth(new BigDecimal(obs.firstKey()).setScale(0, RoundingMode.DOWN).intValue()); synthSettings.setEndSynth(new BigDecimal(obs.lastKey()).setScale(0, RoundingMode.UP).intValue()); List<String> strs = new ArrayList<String>(); for (Entry<Double, Double> ent : obs.entrySet()) strs.add(String.format(Locale.ENGLISH, "%1.4f %1.4f", ent.getKey(), ent.getValue())); FileWorker.write(obsFile, strs); } }