Package de.timefinder.core.util

Source Code of de.timefinder.core.util.ApplicationSettings

/*
* This file is part of the TimeFinder project.
* Visit http://www.timefinder.de for more information.
* Copyright 2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.timefinder.core.util;

import de.timefinder.core.io.xml.XmlSettings;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.richclient.application.Application;

/**
* @author Peter Karich, peat_hal ‘at’ users ‘dot’ sourceforge ‘dot’ net
*/
public class ApplicationSettings extends XmlSettings {

    private static ApplicationContext ctx;
    private static File timefinderDirectory;
    private File settingsFile;
    private File layoutFile;
    private File logPropertiesFile;

    public ApplicationSettings() {
        initDefault();
    }

    @Override
    public File getSettingsFile() {
        if (settingsFile == null)
            settingsFile = new File(getTimefinderDirectory(), "settings.xml");

        return settingsFile;
    }

    protected void initDefault() {
        setStorageFile(new File(getTimefinderDirectory(), "storage.xml"));
        setShowStartupWizard(true);
        setAutoImport(true);
    }

    public File getLayoutFile() {
        if (layoutFile == null) {
            try {
                layoutFile = new File(getTimefinderDirectory(), "layout.xml");
                layoutFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(ApplicationSettings.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return layoutFile;
    }

    public File getLogPropertiesFile() {
        if (logPropertiesFile == null) {
            try {
                logPropertiesFile = new File(getTimefinderDirectory(), "log4j.properties");
                if (!logPropertiesFile.exists()) {
                    logPropertiesFile.createNewFile();

                    // copy from initial properties file from classpath
                    InputStream in = getClass().getResourceAsStream("/log4j.properties");
                    OutputStream out = new FileOutputStream(logPropertiesFile);
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    in.close();
                    out.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(ApplicationSettings.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return logPropertiesFile;
    }
    public final static String COMMANDS_CONTEXT = "/de/timefinder/core/ui/commands-context.xml";
    public final static String APP_CONTEXT = "/de/timefinder/core/ctx/application-context.xml";

    public static ApplicationContext getApplicationContext() {
        if (Application.isLoaded())
            ctx = Application.instance().getApplicationContext();
        else
            ctx = new ClassPathXmlApplicationContext(new String[]{APP_CONTEXT});
        return ctx;
    }

    public static File getTimefinderDirectory() {
        if (timefinderDirectory == null) {
            try {
                timefinderDirectory = new File(System.getProperty("user.home")
                        + File.separatorChar + ".timefinder");
            } catch (Exception ex) {
                timefinderDirectory = new File(".timefinder");
            }
            timefinderDirectory.mkdir();
        }
        return timefinderDirectory;
    }

    public boolean getShowStartupWizard() {
        return (Boolean) getObject("showStartupWizard");
    }

    public void setShowStartupWizard(boolean b) {
        setObject("showStartupWizard", b);
    }

    public void setAutoImport(boolean b) {
        setObject("autoImport", b);
    }

    public boolean isAutoImport() {
        return (Boolean) getObject("autoImport");
    }

    public File getStorageFile() {
        return (File) getObject("storageFile");
    }

    public void setStorageFile(File file) {
        setObject("storageFile", file);
    }
}
TOP

Related Classes of de.timefinder.core.util.ApplicationSettings

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.