001    /*
002     *  Copyright (C) 2003 Adam Olsen
003     *  This program is free software; you can redistribute it and/or modify
004     *  it under the terms of the GNU General Public License as published by
005     *  the Free Software Foundation; either version 1, or (at your option)
006     *  any later version.
007     *  This program is distributed in the hope that it will be useful,
008     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
009     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
010     *  GNU General Public License for more details.
011     *  You should have received a copy of the GNU General Public License
012     *  along with this program; if not, write to the Free Software
013     *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014     */
015    package com.valhalla.jbother;
016    
017    import java.io.*;
018    
019    import javax.swing.*;
020    import java.awt.*;
021    import javax.swing.plaf.*;
022    
023    import com.valhalla.gui.*;
024    import com.valhalla.jbother.*;
025    import com.valhalla.settings.*;
026    import java.util.*;
027    import java.text.*;
028    import com.valhalla.pluginmanager.*;
029    
030    /**
031     * Sets default Settings (if this is the first run of JBother), loads command line arguments and
032     * settings from the settings file, sets the L&F.
033     *
034     * @author     Adam Olsen (arolsen@gmail.com)
035     * @created    November 30, 2004
036     * @version    1.0
037     */
038    public class JBotherLoader
039    {
040            private static PluginLoader loader = PluginLoader.getInstance();
041    
042            /**
043             * This is the main class, it basically just provides a loading point for the login
044             * screen - and also allows arguments to be passed from the command line.
045             *
046             * @param  args  arguments passed via the command line
047             * @see          com.valhalla.settings.Arguments
048             */
049            public static void startJBother( String args[] )
050            {
051                    // initialize the argument holder
052                    Arguments.setArguments( args );
053    
054                    if( System.getProperty( "mrj.version" ) != null )
055                    {
056                            System.setProperty( "apple.laf.useScreenMenuBar", "true" );
057                            System.setProperty( "com.apple.mrj.application.apple.menu.about.name", "JBother" );
058                    }
059    
060                    String profile = ProfileManager.getDefaultProfile();
061                    if( profile == null || profile.equals( "" ) )
062                    {
063                            profile = "default";
064                    }
065    
066                    JBother.profileDir = JBother.settingsDir + File.separatorChar + "profiles" + File.separatorChar + profile;
067    
068                    Settings.getInstance().loadSettings( JBother.profileDir, "settings.properties" );
069                    loadSettings();
070    
071                    // does any Jbother version change tasks
072                    JBVersionManager.check();
073    
074                    com.valhalla.Logger.setLogFile( JBother.settingsDir + File.separatorChar + "jbother.log" );
075    
076                    if( Arguments.getInstance().getProperty( "webstart" ) == null )
077                    {
078                            loadPlugins();
079                    }
080    
081                    if( Settings.getInstance().getProperty( "username" ) == null ||
082                                    Settings.getInstance().getProperty( "password" ) == null )
083                    {
084                            ProfileEditorDialog dialog = new ProfileEditorDialog( null, profile );
085                            dialog.setExitOnClose( true );
086                            dialog.getDefaultBox().setSelected( true );
087                            dialog.show();
088    
089                            return;
090                    }
091    
092                    if( Arguments.getInstance().getProperty( "prof" ) != null )
093                    {
094                            ProfileManager m = new ProfileManager();
095                            m.setExitOnClose( true );
096                    }
097                    else
098                    {
099                            launch();
100                    }
101            }
102    
103            /**
104             *  Loads some JBother settings
105             */
106            public static void loadSettings()
107            {
108                    loadLAF();
109    
110                    String fontString = Settings.getInstance().getProperty( "applicationFont" );
111                    if( fontString == null )
112                    {
113                            fontString = "Default-PLAIN-12";
114                    }
115    
116                    Font newFont = Font.decode( fontString );
117    
118                    setupFont( newFont );
119            }
120    
121            /**
122             * Loads the Look And Feel requested in the settings
123             */
124            public static void loadLAF()
125            {
126                    String laf = Settings.getInstance().getProperty( "lookAndFeel" );
127                    if( Arguments.getInstance().getProperty( "laf" ) != null )
128                    {
129                            laf = Arguments.getInstance().getProperty( laf );
130                    }
131    
132                    UIManager.put( "ClassLoader", loader );
133                    if( laf != null && Arguments.getInstance().getProperty( "notheme" ) == null )
134                    {
135                            try
136                            {
137                                    Class lafClass = loader.loadClass( laf );
138                                    UIManager.setLookAndFeel( (LookAndFeel)lafClass.newInstance() );
139                            }
140                            catch( Exception e )
141                            {
142                                    com.valhalla.Logger.debug( "Could not load look and feel settings.\n" + e.getMessage() );
143                            }
144                    }
145            }
146    
147            /**
148             * Finds the available plugins and uses the PluginLoader to load them
149             * Once a plugin is loaded, it's init() method is called so it can
150             * execute initial code and register for various events in JBother
151             */
152            public static void loadPlugins()
153            {
154                    ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
155    
156                    loader.findPlugins( JBother.settingsDir + File.separatorChar + "plugins" );
157                    loader.loadPlugins();
158    
159                    ArrayList invalids = loader.getInvalidPlugins();
160                    for( int i = 0; i < invalids.size(); i++ )
161                    {
162                            String name = (String)invalids.get( i );
163                            Standard.warningMessage( null, resources.getString( "pluginError" ),
164                                            MessageFormat.format( resources.getString( "pluginErrorMessage" ), new Object[]{name} ) );
165                    }
166            }
167    
168            /**
169             * Sets the font for the entire application
170             *
171             * @param  font  the font to use
172             */
173            public static void setupFont( Font font )
174            {
175                    LookAndFeel lf = javax.swing.UIManager.getLookAndFeel();
176                    UIDefaults uid = lf.getDefaults();
177                    Enumeration k = uid.keys();
178                    while( k.hasMoreElements() )
179                    {
180                            Object key = k.nextElement();
181                            Object val = javax.swing.UIManager.get( key );
182                            if( val instanceof FontUIResource )
183                            {
184                                    FontUIResource fuir = (FontUIResource)val;
185                                    javax.swing.UIManager.put( key, new FontUIResource( font ) );
186                            }
187                    }
188            }
189    
190            /**
191             * launches the profile defined as default
192             */
193            public static void launch()
194            {
195                    String profile = ProfileManager.getDefaultProfile();
196                    if( profile == null || profile.equals( "" ) )
197                    {
198                            profile = "default";
199                    }
200                    ProfileManager.loadProfile( profile );
201            }
202    }
203