/*
*
* Copyright 2012 Chad Preisler
*
* This file is part of jriaffeBlog.
*
* jriaffeBlog 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
* (at your option) any later version.
*
* jriaffeBlog 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 jriaffeBlog. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.jriaffe.blog;
import jriaffe.client.NotificationCenter;
import jriaffe.client.StartupPanelConfig;
import jriaffe.client.ui.components.ApplicationWindow;
import javax.swing.*;
import java.io.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.inet.jortho.FileUserDictionary;
import com.inet.jortho.SpellChecker;
import org.jriaffe.blog.service.ImageImporterService;
/**
* Created by IntelliJ IDEA.
* User: preisler
* Date: 4/21/11
* Time: 9:55 PM
*/
public class Startup implements StartupPanelConfig {
public static final String LICENSE_DECISION = "LICENCE_DECISION";
public static final String LICENSE_ACCEPTED = "licenseAccepted";
public static final String DISK_DIRECTORY = "disk";
public static final String JRIAFFE_APP_DIR = "jriaffeBlog";
public static final String SYSTEM_PROPS = "system.properties";
public static final String usersHomeDirectory = System.getProperty("user.home") + System.getProperty("file.separator");
public static final String appStorageDirectory = usersHomeDirectory +
JRIAFFE_APP_DIR + System.getProperty("file.separator") +
DISK_DIRECTORY + System.getProperty("file.separator");
public static final String ORG_JRIAFFE_BLOG_RESOURCES_LICENSE_HTML = "/org/jriaffe/blog/resources/LICENSE.html";
//public static final String templateDirectory = appStorageDirectory + System.getProperty("file.separator") + "templates"
// + System.getProperty("file.separator");
private NotificationCenter notify = NotificationCenter.getDefaultNotificationCenter();
private Properties appProps = new Properties();
private File propsFile = new File(appStorageDirectory + SYSTEM_PROPS);
private boolean licenseAccepted = false;
static {
// Create user dictionary in the current working directory of your application
SpellChecker.setUserDictionaryProvider(new FileUserDictionary());
// Load the configuration from the file dictionaries.cnf and
// use the current locale or the first language as default
SpellChecker.registerDictionaries(new File("disk/dictionaries.cnf"), null);
// Load the HSQL Database Engine JDBC driver
//try {
// Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//} catch (ClassNotFoundException e) {
// Logger.getLogger(Startup.class.getName()).log(Level.SEVERE, "Could not load HSQL DB driver.");
// System.exit(0);
//}
}
public Startup() {
notify.postNotification(ApplicationWindow.CHANGE_VERTICAL_SCROLL_SPEED, 20);
notify.postNotification(ApplicationWindow.CHANGE_HORIZONTAL_SCROLL_SPEED, 20);
NavigationButtonBar buttonBar = new NavigationButtonBar();
notify.postNotification(ApplicationWindow.NOTIFY_ADDBUTTON_BAR, buttonBar);
appProps.setProperty(LICENSE_ACCEPTED, "false");
File localStorage = new File(appStorageDirectory);
if (!localStorage.exists()) {
localStorage.mkdirs();
}
try {
if (!propsFile.exists()) {
storeAppProps();
} else {
appProps.load(new FileReader(propsFile));
}
} catch (IOException ex) {
Logger.getLogger(Startup.class.getName()).log(Level.SEVERE, "Could not write the license file.");
System.exit(0);
}
ImageImporterService service = new ImageImporterService();
try {
notify.addObserver(service, "startImport", ImageImporterService.START_IMPORT, null);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public static boolean readLicenseAccepted() {
File propsFile = new File(appStorageDirectory + SYSTEM_PROPS);
Properties appProps = new Properties();
boolean accepted = false;
try {
appProps.load(new FileReader(propsFile));
} catch (IOException e) {
Logger.getLogger(Startup.class.getName()).log(Level.WARNING, "Could not read properties file.");
}
String ap = appProps.getProperty(LICENSE_ACCEPTED);
if (ap.equals("true")) {
accepted = true;
}
return accepted;
}
public JPanel getStartUpPanel() {
JPanel view = null;
String licenseAcceptedValue = appProps.getProperty(LICENSE_ACCEPTED);
if (licenseAcceptedValue.equals("true")) {
licenseAccepted = true;
}
if (licenseAccepted) {
view = new SplashPanel();
} else {
java.net.URL licenseURL = LicensePanel.class.getResource(
ORG_JRIAFFE_BLOG_RESOURCES_LICENSE_HTML);
JPanel licensePanel = new LicensePanel(licenseURL, true, true);
try {
notify.addObserver(this, "licenseAccepted", LICENSE_DECISION, licensePanel);
} catch (NoSuchMethodException ex) {
Logger.getLogger(Startup.class.getName()).log(Level.SEVERE, null, ex);
}
view = licensePanel;
}
return view;
}
public boolean retain() {
return false;
}
private void storeAppProps() throws IOException {
OutputStream out = new FileOutputStream(propsFile);
appProps.store(out, "Changing the licenseAccepted to True means that you accept the GPL license for this application.");
out.close();
}
public void licenseAccepted(String message, Object note) {
Boolean accepted = false;
if (note instanceof LicensePanel) {
LicensePanel panel = (LicensePanel)note;
accepted = panel.getDecision();
}
licenseAccepted = accepted;
if (licenseAccepted) {
notify.postNotification(NavigationButtonBar.ENABLE_BUTTONS, this);
}
appProps.setProperty(LICENSE_ACCEPTED, accepted.toString());
try {
storeAppProps();
} catch (IOException ex) {
Logger.getLogger(Startup.class.getName()).log(Level.SEVERE, "Could not write license decision.");
System.exit(0);
}
}
}