/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.gui.customcontrols;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import net.java.sip.communicator.impl.gui.*;
import net.java.sip.communicator.impl.gui.utils.*;
import net.java.sip.communicator.service.configuration.*;
import net.java.sip.communicator.util.*;

public abstract class SIPCommFrame extends JFrame
{
    private Logger logger = Logger.getLogger(SIPCommFrame.class);
    
    ActionMap amap;
    InputMap imap;
    
    public SIPCommFrame()
    {
        this.setIconImage(
            ImageLoader.getImage(ImageLoader.SIP_COMMUNICATOR_LOGO));
        
        // In order to have the same icon when using option panes
        JOptionPane.getRootFrame().setIconImage(
                ImageLoader.getImage(ImageLoader.SIP_COMMUNICATOR_LOGO));

        this.addWindowListener(new FrameWindowAdapter());
        
        amap = this.getRootPane().getActionMap();

        amap.put("close", new CloseAction());
        
        imap = this.getRootPane().getInputMap(
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
        
    }
    
    /**
     * The action invoked when user presses Escape key.
     */
    private class CloseAction extends AbstractAction
    {
        public void actionPerformed(ActionEvent e)
        {
            saveSizeAndLocation();
            close(true);
        }        
    }
    
    /**
     * Adds a key - action pair for this frame.
     * 
     * @param keyStroke the key combination
     * @param action the action which will be executed when user presses the
     * given key combination
     */
    protected void addKeyBinding(KeyStroke keyStroke, Action action)
    {
        String actionID = action.getClass().getName();
        
        amap.put(actionID, action);
        
        imap.put(keyStroke, actionID);
    }
    
    /**
     * Before closing the application window saves the current size and position
     * through the <tt>ConfigurationService</tt>.
     */
    public class FrameWindowAdapter extends WindowAdapter
    {
        public void windowClosing(WindowEvent e) {
            saveSizeAndLocation();
            close(false);
        }
    }

    /**
     * Saves the size and the location of this frame through the
     * <tt>ConfigurationService</tt>.
     */
    private void saveSizeAndLocation()
    {
        ConfigurationService configService
            = GuiActivator.getConfigurationService();
    
        String className = this.getClass().getName();
                
        try {
            configService.setProperty(
                className + ".width",
                new Integer(getWidth()));
    
            configService.setProperty(
                className + ".height",
                new Integer(getHeight()));
    
            configService.setProperty(
                className + ".x",
                new Integer(getX()));
    
            configService.setProperty(
                className + ".y",
                new Integer(getY()));
        }
        catch (PropertyVetoException e1) {
            logger.error("The proposed property change "
                    + "represents an unacceptable value");
        }
    }    

    /**
     * Sets window size and position.
     */
    public void setSizeAndLocation() {
        ConfigurationService configService
            = GuiActivator.getConfigurationService();

        String className = this.getClass().getName();
        
        String widthString = configService.getString(
            className + ".width");

        String heightString = configService.getString(
            className + ".height");

        String xString = configService.getString(
            className + ".x");

        String yString = configService.getString(
            className + ".y");

        int width = 0;
        int height = 0;
        
        if(widthString != null && heightString != null)
        {   
            width = new Integer(widthString).intValue();
            height = new Integer(heightString).intValue();
            
            if(width > 0 && height > 0)
                this.setSize(width, height);
        }
        
        if(xString != null && yString != null)
        {   
            this.setLocation(new Integer(xString).intValue(),
                new Integer(yString).intValue());
        }        
        else {            
            this.setCenterLocation();
        }
    }
    
    /**
     * Positions this window in the center of the screen.
     */
    private void setCenterLocation()
    {
        this.setLocation(
                Toolkit.getDefaultToolkit().getScreenSize().width/2
                    - this.getWidth()/2,
                Toolkit.getDefaultToolkit().getScreenSize().height/2
                    - this.getHeight()/2
                );
    }

    
    /**
     * Overwrites the setVisible method in order to set the size and the
     * position of this window before showing it.
     */
    public void setVisible(boolean isVisible)
    {
        if(isVisible) {
            this.pack();
            this.setSizeAndLocation();
        }
        
        super.setVisible(isVisible);
    }
    
    /**
     * Overwrites the dispose method in order to save the size and the position
     * of this window before closing it.
     */
    public void dispose()
    {
        super.dispose();
        
        this.saveSizeAndLocation();
    }
    
    /**
     * All functions implemented in this method will be invoked when user
     * presses the Escape key. 
     */
    protected abstract void close(boolean isEscaped);
}

@@ -510,16 +510,20 @@
 ommFrame
+%0A   
  extends
@@ -530,16 +530,16 @@
  JFrame%0A
-
 %7B%0A    pr
@@ -772,33 +772,25 @@
 TOR_LOGO));%0A
-        
 %0A
+
         // I
@@ -1021,33 +1021,25 @@
 Adapter());%0A
-        
 %0A
+
         amap
@@ -1123,25 +1123,17 @@
 ion());%0A
-        
 %0A
+
         
@@ -1310,36 +1310,23 @@
 lose%22);%0A
-        %0A
     %7D%0A
-    
 %0A
+
     /**%0A
@@ -1574,32 +1574,24 @@
 );%0A        %7D
-        
 %0A    %7D%0A    %0A
@@ -2305,16 +2305,24 @@
 Event e)
+%0A       
  %7B%0A     
@@ -2343,32 +2343,33 @@
 eAndLocation();%0A
+%0A
             clos
@@ -3407,20 +3407,16 @@
  %7D%0A    %7D
-    
 %0A%0A    /*
@@ -3499,16 +3499,20 @@
 cation()
+%0A   
  %7B%0A     
@@ -4577,22 +4577,17 @@
 else
- %7B    
+%0A
         
 %0A   
@@ -4574,32 +4574,33 @@
    else%0A        
+%7B
 %0A            thi
@@ -5219,32 +5219,32 @@
 sVisible)%0A    %7B%0A
-
         if(isVis
@@ -5248,16 +5248,24 @@
 Visible)
+%0A       
  %7B%0A     

format code