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.awt.Dimension;
018    import java.awt.event.ComponentAdapter;
019    import java.awt.event.ComponentEvent;
020    import java.awt.event.WindowAdapter;
021    import java.awt.event.WindowEvent;
022    
023    import javax.swing.JFrame;
024    
025    import com.valhalla.gui.Standard;
026    import com.valhalla.jbother.plugins.events.ExitingEvent;
027    import com.valhalla.pluginmanager.PluginChain;
028    import com.valhalla.settings.Settings;
029    
030    /**
031     * Provides a containing frame for the BuddyList panel when in ICQ mode
032     * 
033     * @author Adam Olsen
034     */
035    public class BuddyListContainerFrame extends JFrame {
036        private BuddyList buddyList;
037    
038        /**
039         * The default constructor
040         * 
041         * @param buddyList
042         *            the buddy list panel that should be contained in this fram
043         */
044        public BuddyListContainerFrame(BuddyList buddyList) {
045            super("JBother");
046            this.buddyList = buddyList;
047            setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
048            setIconImage(Standard.getImage("frameicon.png"));
049            initComponents();
050            getContentPane().add(buddyList);
051            pack();
052    
053            setPreferredDimensions();
054            setPreferredLocation();
055            addComponentListener(new ComponentAdapter() {
056                public void componentMoved(ComponentEvent e) {
057                    BuddyList.getInstance().saveSettings();
058                }
059            });
060        }
061    
062        /**
063         * Sets up the components for this container
064         */
065        private void initComponents() {
066            addWindowListener(new WindowAdapter() {
067                public void windowClosing(WindowEvent e) {
068                    ExitingEvent event = new ExitingEvent(buddyList);
069                    PluginChain.fireEvent(event);
070                    if (event.getExit()) {
071                        buddyList.signOff();
072                        buddyList.quitHandler();
073                    }
074                }
075    
076            });
077        }
078    
079        /**
080         * Load saved settings from the last session and set the buddy list to the
081         * sizes that were saved.
082         */
083        protected void setPreferredDimensions() {
084            String width = Settings.getInstance().getProperty("buddyListWidth");
085            String height = Settings.getInstance().getProperty("buddyListHeight");
086    
087            Dimension dim = new Dimension(100, 100);
088    
089            try {
090                dim.setSize(Integer.parseInt(width), Integer.parseInt(height));
091            } catch (NumberFormatException ex) {
092                ex.printStackTrace();
093            }
094    
095            setSize(dim);
096        }
097    
098        /**
099         * Loads the saved settings from any previous settings
100         */
101        protected void setPreferredLocation() {
102            //load the settings from the settings file
103            String xString = Settings.getInstance().getProperty("buddyListX");
104            String yString = Settings.getInstance().getProperty("buddyListY");
105    
106            if (xString == null)
107                xString = "100";
108            if (yString == null)
109                yString = "100";
110    
111            double x = 100;
112            double y = 100;
113    
114            try {
115                x = Double.parseDouble(xString);
116                y = Double.parseDouble(yString);
117            } catch (NumberFormatException e) {
118            } catch (NullPointerException e) {
119            }
120    
121            if (x < -50.0)
122                x = 100.0;
123            if (y < -50.0)
124                y = 100.0;
125    
126            setLocation((int) x, (int) y);
127        }
128    
129    }