001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.main;
020:
021: import java.io.File;
022: import java.util.Locale;
023:
024: import com.jeta.forms.logger.FormsLogger;
025: import com.jeta.open.registry.JETARegistry;
026: import com.jeta.swingbuilder.app.AppResourceLoader;
027: import com.jeta.swingbuilder.app.ApplicationStateStore;
028: import com.jeta.swingbuilder.common.ComponentNames;
029: import com.jeta.swingbuilder.debug.DebugLogger;
030: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
031: import com.jeta.swingbuilder.interfaces.resources.ResourceLoader;
032:
033: /**
034: * This class is responsible for initializing all components needed for the
035: * Abeille Forms application.
036: *
037: * @author Jeff Tassin
038: */
039: public class FormsInitializer {
040: private DebugLogger m_debug_logger;
041:
042: /**
043: * Checks that the home directory is present. If not, tries to create it.
044: *
045: * @param home
046: * @return
047: */
048: private boolean checkHome(String home) {
049: boolean bresult = false;
050: try {
051: File f = new File(home);
052: f.mkdir();
053: bresult = true;
054: } catch (Exception e) {
055: }
056: return bresult;
057: }
058:
059: /**
060: * Initializes the application
061: *
062: * @param args
063: */
064: public void initialize(String[] args) {
065: // now try to load properties
066: try {
067: String tshome = null;
068: String language = null;
069: String country = null;
070: if (args != null) {
071: for (int x = 0; x < args.length; x++) {
072: try {
073: if (args[x].equals("-language"))
074: language = args[x + 1];
075: else if (args[x].equals("-country"))
076: country = args[x + 1];
077: } catch (Exception e) {
078: e.printStackTrace();
079: }
080: }
081: }
082: if (tshome == null) {
083: tshome = System.getProperty("user.home");
084: if (tshome != null && tshome.length() > 0) {
085: char c = tshome.charAt(tshome.length() - 1);
086: if (c != '\\' && c != '/')
087: tshome = tshome + File.separatorChar
088: + ".abeilleforms13";
089: else
090: tshome = tshome + ".abeilleforms13";
091: }
092: }
093:
094: if (checkHome(tshome)) {
095: /** the jeta framework */
096: AppResourceLoader loader = new AppResourceLoader(
097: tshome, "jeta.resources/images/");
098: JETARegistry
099: .rebind(ResourceLoader.COMPONENT_ID, loader);
100:
101: /**
102: * Initialize the components needed by the forms sub-system.
103: */
104: com.jeta.forms.defaults.DefaultInitializer.initialize();
105:
106: initializeLocale(language, country);
107: // load global application user settings
108: ApplicationStateStore appstore = new ApplicationStateStore(
109: "application");
110: JETARegistry.rebind(
111: ComponentNames.APPLICATION_STATE_STORE,
112: appstore);
113: System.setProperty("abeilleforms.home", tshome);
114: System.setProperty("abeilleforms.version",
115: com.jeta.forms.support.AbeilleForms
116: .getVersionEx());
117:
118: /**
119: * Initialize the components needed by the swing builder system.
120: */
121: com.jeta.swingbuilder.defaults.DefaultInitializer
122: .initialize();
123:
124: /**
125: * register the help manager
126: */
127: JETARegistry.rebind(
128: ComponentNames.APPLICATION_HELP_FACTORY,
129: new AbeilleHelpFactory());
130: } else {
131: System.out
132: .println("Unable to establish home directory: "
133: + tshome);
134: System.exit(0);
135: }
136:
137: FormDesignerUtils.getEnvVars(true);
138: } catch (Exception e) {
139: e.printStackTrace();
140: }
141: FormsLogger.debug("FormsInitializer.completed.... ");
142:
143: }
144:
145: /**
146: * Gets the language and country from the command line and sets as the
147: * default locale
148: *
149: * @param language
150: * @param country
151: */
152: void initializeLocale(String language, String country) {
153: Locale locale = null;
154: if (language != null && country != null)
155: locale = new Locale(language, country);
156: else
157: locale = Locale.getDefault();
158:
159: com.jeta.open.i18n.I18NHelper oi18n = com.jeta.open.i18n.I18NHelper
160: .getInstance();
161: oi18n.setLocale(locale);
162: oi18n
163: .loadBundle("com.jeta.swingbuilder.resources.MessagesBundle");
164:
165: }
166:
167: private void startDebugConsole() {
168: try {
169: m_debug_logger = new DebugLogger("localhost");
170: } catch (Exception e) {
171: e.printStackTrace();
172: }
173: }
174:
175: /**
176: * LoggerHandler for writing log message to console.
177: *
178: * @temp
179: */
180: /*
181: * public class DebugHandler extends Handler { public DebugHandler() {
182: * setFormatter( new java.util.logging.SimpleFormatter() ); }
183: *
184: * public boolean isLoggable(LogRecord record) { return true; }
185: *
186: * public void close() { }
187: *
188: * public void flush() { }
189: *
190: * public void publish( LogRecord record ) { if ( m_debug_logger != null )
191: * m_debug_logger.sendRequest( record.getMessage() ); } }
192: */
193:
194: }
|