Package de.timefinder.core

Source Code of de.timefinder.core.LifecycleAdvisor

/*
* 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;

import de.timefinder.data.algo.DataPoolSettings;
import de.timefinder.core.io.xml.XmlExport;
import de.timefinder.core.io.xml.XmlImport;
import de.timefinder.core.util.ApplicationSettings;
import de.timefinder.core.util.Translator;
import de.timefinder.data.DataPool;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.richclient.application.ApplicationWindow;
import org.springframework.richclient.application.config.ApplicationWindowConfigurer;
import org.springframework.richclient.application.config.DefaultApplicationLifecycleAdvisor;
import org.springframework.richclient.application.setup.SetupWizard;

/**
* Custom application lifecycle implementation that configures the application
*
* @author Peter Karich
*/
public class LifecycleAdvisor extends DefaultApplicationLifecycleAdvisor {

    private final Log logger = LogFactory.getLog(getClass());
    private ApplicationSettings appSettings;
    private DataPoolSettings dataSettings;
    private Translator tr;
    private DataPool dataPool;

    public void setSettings(ApplicationSettings set) {
        appSettings = set;
    }

    @Autowired
    public void setDataPoolSettings(DataPoolSettings settings) {
        dataSettings = settings;
    }

    @Autowired
    public void setTranslator(Translator tr) {
        this.tr = tr;
    }

    @Autowired
    public void setDataPool(DataPool dataPool) {
        this.dataPool = dataPool;
    }

    @Override
    public void onPreStartup() {
        if (appSettings.getShowStartupWizard()
                && getApplication().getApplicationContext().containsBean("setupWizard")) {
            SetupWizard setupWizard = (SetupWizard) getApplication().
                    getApplicationContext().getBean("setupWizard", SetupWizard.class);
            setupWizard.execute();
            appSettings.setShowStartupWizard(false);
            try {
                appSettings.save();
            } catch (IOException ex) {
                logger.error("Can't save state.", ex);
            }
        }
    }

    /**
     * Called when the application has fully started. This is after the initial
     * application window has been made visible.
     */
    @Override
    public void onPostStartup() {
    }

    /**
     * This method is called prior to the opening of an application window. Note
     * at this point the window control has not been created. This hook allows
     * programmatic control over the configuration of the window (by setting
     * properties on the configurer) and it provides a hook where code that
     * needs to be executed prior to the window opening can be plugged in (like
     * a startup wizard, for example).
     *
     * @param configurer The application window configurer
     */
    @Override
    public void onPreWindowOpen(ApplicationWindowConfigurer configurer) {

        // If you override this method, it is critical to allow the superclass
        // implementation to run as well.
        super.onPreWindowOpen(configurer);

        // Uncomment to hide the menubar, toolbar, or alter window size...
        // configurer.setShowMenuBar(false);
        // configurer.setShowToolBar(false);
        Rectangle rect = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
        configurer.setInitialSize(new Dimension(rect.width, rect.height));

        if (!appSettings.isAutoImport())
            return;

        File storageFile = appSettings.getStorageFile();
        String[] options = new String[]{tr.get("yesCommand.caption"),
            tr.get("noCommand.caption"),
            tr.get("autoImport.alwaysSkip")};
        int res = JOptionPane.showOptionDialog(null,
                tr.get("autoImport.question", storageFile),
                tr.get("autoImport"), JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
                null, options, tr.get("yesCommand.caption"));

        if (res == 1 || res == JOptionPane.CLOSED_OPTION)
            return;

        if (res == 2) {
            appSettings.setAutoImport(false);
            try {
                appSettings.save();
            } catch (IOException ex) {
                logger.fatal("Auto-import message will pop up next time again :-(", ex);
            }
            return;
        }

        try {
            new XmlImport(dataPool, dataSettings, new FileInputStream(storageFile)).doWork();
        } catch (IOException ex) {
            logger.fatal("Couldn't auto-import from:" + storageFile, ex);
        }
    }

    /**
     * Called just after the command context has been internalized. At this
     * point, all the commands for the window have been created and are
     * available for use. If you need to force the execution of a command prior
     * to the display of an application window (like a login command), this is
     * where you'd do it.
     *
     * @param window The window who's commands have just been created
     */
    @Override
    public void onCommandsCreated(ApplicationWindow window) {
    }

    /**
     * Called after the actual window control has been created.
     *
     * @param window The window being processed
     */
    @Override
    public void onWindowCreated(ApplicationWindow window) {
    }

    /**
     * Called immediately after making the window visible.
     *
     * @param window The window being processed
     */
    @Override
    public void onWindowOpened(ApplicationWindow window) {
    }

    /**
     * Called when the window is being closed. This hook allows control over
     * whether the window is allowed to close. By returning false from this
     * method, the window will not be closed.
     *
     * @return boolean indicator if window should be closed. <code>true</code>
     *         to allow the close, <code>false</code> to prevent the close.
     */
    @Override
    public boolean onPreWindowClose(ApplicationWindow window) {
        String question = tr.get("exitCommand.confirm.question");
        int ret = JOptionPane.showConfirmDialog(null,
                question, question, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (ret == JOptionPane.YES_OPTION) {
            File storageFile = appSettings.getStorageFile();
            try {
                new XmlExport(dataPool, dataSettings,
                        new FileOutputStream(storageFile)).doWork();
                logger.info("Auto-export to:" + storageFile);
            } catch (Exception ex) {
                logger.fatal("Couldn't auto-export to:" + storageFile, ex);
                getApplication().getActiveWindow().getStatusBar().
                        setErrorMessage("Couldn't import from " + storageFile
                        + ". " + ex.getLocalizedMessage());
            }
        }

        return true;
    }
}
TOP

Related Classes of de.timefinder.core.LifecycleAdvisor

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.