Package de.chris_soft.nanodoa

Source Code of de.chris_soft.nanodoa.ConfigurationWizard

/**
* 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.util.Properties;
import javax.swing.JTextField;
import de.chris_soft.nanoarchive.ArchiveKeys;
import de.chris_soft.nanodoa.web.DocumentServerKeys;
import de.chris_soft.utilities.swing.wizard.Wizard;
import de.chris_soft.utilities.swing.wizard.WizardCheckBoxInputListener;
import de.chris_soft.utilities.swing.wizard.WizardPanel;
import de.chris_soft.utilities.swing.wizard.WizardTextInputListener;

/**
* Configuration wizard for NanoDoA.
* @author Christian Packenius.
*/
public class ConfigurationWizard implements MailKeys, ArchiveKeys, DocumentServerKeys {
  final Wizard wizard;

  /**
   * Constructor.
   */
  public ConfigurationWizard() {
    wizard = new Wizard(null, "NanoDoA Configuration Wizard", null);
    addIntroductionPanelToWizard();
    addMailPanelToWizard();
    addArchivePanelToWizard();
    addDocumentHttpServerPanelToWizard();
  }

  /**
   *
   */
  private void addIntroductionPanelToWizard() {
    StringBuilder introText = new StringBuilder();
    introText.append("NanoDoA is a small document archive system for private use.");
    introText.append("\r\nPlease fill the following fields for configuration.");
    introText.append("\r\n\r\nThank you for using NanoDoA.");
    WizardPanel p1 = new WizardPanel(wizard, "Introduction");
    p1.addLabel(introText.toString());
  }

  private void addMailPanelToWizard() {
    final WizardPanel p2 = new WizardPanel(wizard, "Mail document") {
      private static final long serialVersionUID = -1111708493729196988L;

      /**
       * @see de.chris_soft.utilities.swing.wizard.WizardPanel#isNextButtonEnabled()
       */
      @Override
      public boolean isNextButtonEnabled() {
        if (!Boolean.parseBoolean(wizard.getContent(PROP_KEY_SHALL_SEND_MAIL, "true"))) {
          return true;
        }
        if (!wizard.getContent(PROP_KEY_SENDER_MAIL_ADDRESS, "").contains("@")) {
          return false;
        }
        if (wizard.getContent(PROP_KEY_SENDER_MAIL_PW, "").length() == 0) {
          return false;
        }
        if (wizard.getContent(PROP_KEY_SENDER_MAIL_LOGIN, "").length() == 0) {
          return false;
        }
        if (wizard.getContent(PROP_KEY_SENDER_POP3_SERVER, "").length() == 0) {
          return false;
        }
        if (wizard.getContent(PROP_KEY_SENDER_SMTP_PORT, "").length() == 0) {
          return false;
        }
        if (wizard.getContent(PROP_KEY_SENDER_SMTP_SERVER, "").length() == 0) {
          return false;
        }
        if (!wizard.getContent(PROP_KEY_RECEIVER_MAIL_ADDRESS, "").contains("@")) {
          return false;
        }
        return true;
      }
    };

    // Set default values if missing.
    wizard.getContent(PROP_KEY_SHALL_SEND_MAIL, "true");
    wizard.getContent(PROP_KEY_SET_FULLTEXT_INTO_MAIL_BODY, "true");

    p2.addCheckBoxInput("Send mail per document", PROP_KEY_SHALL_SEND_MAIL, new WizardCheckBoxInputListener() {
      @Override
      public void checkInputChanged(String key, boolean checked) {
        p2.getComponentByName(PROP_KEY_SENDER_MAIL_ADDRESS).setEnabled(checked);
        p2.getComponentByName(PROP_KEY_SENDER_MAIL_LOGIN).setEnabled(checked);
        p2.getComponentByName(PROP_KEY_SENDER_MAIL_PW).setEnabled(checked);
        p2.getComponentByName(PROP_KEY_SENDER_SMTP_PORT).setEnabled(checked);
        p2.getComponentByName(PROP_KEY_SENDER_SMTP_SERVER).setEnabled(checked);
        p2.getComponentByName(PROP_KEY_SENDER_POP3_SERVER).setEnabled(checked);
        p2.getComponentByName(PROP_KEY_RECEIVER_MAIL_ADDRESS).setEnabled(checked);
        p2.getComponentByName(PROP_KEY_SET_FULLTEXT_INTO_MAIL_BODY).setEnabled(checked);
        p2.getComponentByName(PROP_KEY_ADD_DOCUMENT_AS_ATTACHMENT).setEnabled(checked);
      }
    });
    p2.addTextInput("Sender mail address", PROP_KEY_SENDER_MAIL_ADDRESS, new WizardTextInputListener() {
      @Override
      public void textInputChanged(String key, String textContent) {
        final String lowerContent = textContent.toLowerCase();
        ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_MAIL_LOGIN)).setText(textContent);
        if (lowerContent.endsWith("@googlemail.com") || lowerContent.endsWith("@gmail.com")) {
          ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_SMTP_SERVER)).setText("smtp.googlemail.com");
          ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_SMTP_PORT)).setText("587");
          ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_POP3_SERVER)).setText("pop.googlemail.com");
        }
        else if (lowerContent.contains("@gmx.")) {
          ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_SMTP_SERVER)).setText("mail.gmx.net");
          ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_SMTP_PORT)).setText("587");
          ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_POP3_SERVER)).setText("pop.gmx.net");
        }
        else if (lowerContent.endsWith("@aol.com")) {
          ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_SMTP_SERVER)).setText("smtp.de.aol.com");
          ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_SMTP_PORT)).setText("587");
          ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_POP3_SERVER)).setText("pop.aol.com");
          String onlyLoginName = lowerContent.substring(0, lowerContent.length() - 8);
          ((JTextField) p2.getComponentByName(PROP_KEY_SENDER_MAIL_LOGIN)).setText(onlyLoginName);
        }
      }
    });
    p2.addTextInput("Sender mail login", PROP_KEY_SENDER_MAIL_LOGIN, null);
    p2.addTextInput("Sender mail password", PROP_KEY_SENDER_MAIL_PW, null);
    p2.addTextInput("Sender SMTP server", PROP_KEY_SENDER_SMTP_SERVER, null);
    p2.addTextInput("Sender SMTP port", PROP_KEY_SENDER_SMTP_PORT, null);
    p2.addTextInput("Sender POP3 server", PROP_KEY_SENDER_POP3_SERVER, null);
    p2.addTextInput("Receiver mail address", PROP_KEY_RECEIVER_MAIL_ADDRESS, null);
    p2.addCheckBoxInput("Set document full text into mail body", PROP_KEY_SET_FULLTEXT_INTO_MAIL_BODY, null);
    p2.addCheckBoxInput("Add PDF document as attachment", PROP_KEY_ADD_DOCUMENT_AS_ATTACHMENT, null);
  }

  private void addArchivePanelToWizard() {
    final WizardPanel p3 = new WizardPanel(wizard, "Archive") {
      private static final long serialVersionUID = 7026397794090207792L;

      @Override
      public boolean isNextButtonEnabled() {
        if (wizard.getContent(PROP_KEY_ARCHIVE_DIRECTORY_PATH, "").isEmpty()) {
          return false;
        }
        if (wizard.getContent(PROP_KEY_OBSERVE_DIRECTORIES, "").isEmpty()) {
          return false;
        }
        return true;
      }
    };

    p3.addDirectoryInput("Archive directory", PROP_KEY_ARCHIVE_DIRECTORY_PATH, null);
    p3.addDirectoryListInput("Directories to observe for documents", PROP_KEY_OBSERVE_DIRECTORIES, null);
  }

  private void addDocumentHttpServerPanelToWizard() {
    final WizardPanel p4 = new WizardPanel(wizard, "Document (HTTP) Server") {
      private static final long serialVersionUID = 7026397794090207792L;

      @Override
      public boolean isNextButtonEnabled() {
        if (!Boolean.parseBoolean(wizard.getContent(PROP_KEY_DOCUMENT_SERVER_SHALL_USE, "false"))) {
          return true;
        }
        if (wizard.getContent(PROP_KEY_DOCUMENT_SERVER_NAME, "").isEmpty()) {
          return false;
        }
        if (wizard.getContent(PROP_KEY_DOCUMENT_SERVER_PORT, "").isEmpty()) {
          return false;
        }
        return true;
      }
    };

    wizard.getContent(PROP_KEY_DOCUMENT_SERVER_SHALL_USE, "false");
    wizard.getContent(PROP_KEY_DOCUMENT_SERVER_NAME, "localhost");
    wizard.getContent(PROP_KEY_DOCUMENT_SERVER_PORT, "23871");
    wizard.getContent(PROP_KEY_DOCUMENT_SERVER_PASSWORD, "");

    p4.addCheckBoxInput("Use HTTP document server", PROP_KEY_DOCUMENT_SERVER_SHALL_USE, new WizardCheckBoxInputListener() {
      @Override
      public void checkInputChanged(String key, boolean checked) {
        p4.getComponentByName(PROP_KEY_DOCUMENT_SERVER_PORT).setEnabled(checked);
      }
    });
    p4.addTextInput("Server name (for mail usage, e.g.\"www.domainname.xyz\")", PROP_KEY_DOCUMENT_SERVER_NAME, null);
    p4.addTextInput("Server port", PROP_KEY_DOCUMENT_SERVER_PORT, null);
    p4.addPasswordInput("Password (for document retrieval)", PROP_KEY_DOCUMENT_SERVER_PASSWORD, null);
  }

  /**
   * Start the wizard and get back the properties from user input.
   * @return Properties object from user input.
   */
  public Properties start() {
    return wizard.start();
  }
}
TOP

Related Classes of de.chris_soft.nanodoa.ConfigurationWizard

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.