Package de.chris_soft.nanodoa

Source Code of de.chris_soft.nanodoa.StartUp

/**
* NanoDoA - File based document archive
*
* Copyright (C) 2011-2012 Christian Packenius, christian.packenius@googlemail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package de.chris_soft.nanodoa;

import java.awt.AWTException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import de.chris_soft.nanoarchive.ArchiveKeys;
import de.chris_soft.nanoarchive.DerbyArchive;
import de.chris_soft.nanodoa.gui.AppWindow;
import de.chris_soft.nanodoa.gui.TrayIconAndMenu;
import de.chris_soft.nanodoa.misc.ConfigurationWizard;
import de.chris_soft.nanodoa.misc.DocumentInputManagement;
import de.chris_soft.nanodoa.misc.MailKeys;
import de.chris_soft.nanodoa.misc.SystemUpdateThread;
import de.chris_soft.nanodoa.misc.TempFilesDeletionThread;
import de.chris_soft.utilities.AppProperties;
import de.chris_soft.utilities.FileUtils;
import de.chris_soft.utilities.LogUtils;
import de.chris_soft.utilities.StringLists;
import de.chris_soft.utilities.swing.SwingUtils;

/**
* System start of NanoDoA application.
* @author Christian Packenius.
*/
public final class StartUp implements MailKeys {
  /**
   * Start all subsystems for NanoDoA.
   * @return <i>true</i> if (and only if) all sub systems has been successfully
   *         started.
   */
  protected static boolean startSystem() {
    // Load (or create default) properties.
    if (!checkAppProperties()) {
      return false;
    }
    AppProperties.setPropertiesFile(NanoDoAMain.nanodoaPropertiesFile);

    // Try to create tray icon.
    try {
      God.trayIAM = new TrayIconAndMenu();
    }
    catch (AWTException exception) {
      SwingUtils.showError(null, "Couldn't create tray icon!");
      LogUtils.log(exception);
      return false;
    }

    // Open database.
    String archivePath = AppProperties.getProperty(ArchiveKeys.PROP_KEY_ARCHIVE_DIRECTORY_PATH);
    if (archivePath == null) {
      archivePath = ".";
    }
    try {
      God.archive = new DerbyArchive(new File(archivePath));
    }
    catch (Exception exception) {
      SwingUtils.showError(null, "Couldn't open database!");
      LogUtils.log(exception);
      God.trayIAM.removeSystemTrayIcon();
      return false;
    }

    // Document input thread starting.
    try {
      God.documentInputManagement = new DocumentInputManagement();
    }
    catch (IOException exception) {
      SwingUtils.showError(null, "Couldn't open database!");
      LogUtils.log(exception);
      God.trayIAM.removeSystemTrayIcon();
      God.archive.shutdown();
      return false;
    }
    setDocumentInputManagementDirectories();

    // Create application window.
    God.appWindow = new AppWindow();

    // Start document input thread.
    God.documentInputManagement.start();

    // Start thread that deletes old files.
    new TempFilesDeletionThread();

    // Start thread for searching for new versions of NanoDoA.
    new SystemUpdateThread();

    return true;
  }

  private static void setDocumentInputManagementDirectories() {
    List<File> list = new ArrayList<File>();
    for (String s : StringLists.string2list(AppProperties.getProperty(ArchiveKeys.PROP_KEY_OBSERVE_DIRECTORIES))) {
      list.add(new File(s));
    }
    God.documentInputManagement.setDestinationDirectories(list);
  }

  private static boolean checkAppProperties() {
    if (!NanoDoAMain.nanodoaPropertiesFile.exists()) {
      Properties props = getPropertiesFromConfigurationWizard();
      if (props == null) {
        SwingUtils.showError(null, "Couldn't load properties!");
        return false;
      }
      try {
        FileUtils.storeProperties(NanoDoAMain.nanodoaPropertiesFile, props);
      }
      catch (IOException exception) {
        SwingUtils.showError(null, "Couldn't load (store) properties!");
        LogUtils.log(exception);
        return false;
      }
    }
    return true;
  }

  private static Properties getPropertiesFromConfigurationWizard() {
    ConfigurationWizard cw = new ConfigurationWizard();
    return cw.start();
  }
}
TOP

Related Classes of de.chris_soft.nanodoa.StartUp

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.