/*
 *  Copyright 2011 RoboViz
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package config;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import rv.Configuration;
import rv.Globals;
import rv.Viewer;
import rv.util.SwingUtil;

public class RVConfigure extends JFrame {

    interface SaveListener {
        void configSaved(RVConfigure configProg);
    }

    Configuration                 config;
    final JButton                 saveButton;
    final ArrayList<SaveListener> listeners = new ArrayList<>();

    public RVConfigure() {

        try {
            config = Configuration.loadFromFile();
        } catch (Exception e) {
            e.printStackTrace();
            config = new Configuration();
        }

        Globals.setLookFeel();
        setTitle("RoboViz Configuration");
        setIconImage(Globals.getIcon());
        setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;

        c.gridx = 0;
        c.gridy = 0;
        add(new NetworkPanel(this), c);

        c.gridy = 1;
        add(new GraphicsPanel(this), c);

        c.gridy = 2;
        add(new GeneralPanel(this), c);

        c.gridy = 3;
        add(new TeamColorsPanel(this), c);

        c.gridy = 4;
        JPanel southPanel = new JPanel(new GridLayout(1, 2));
        add(southPanel, c);

        saveButton = new JButton("Save");
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                for (SaveListener l : listeners)
                    l.configSaved(RVConfigure.this);
                config.write();
            }
        });
        southPanel.add(saveButton);

        JButton startButton = new JButton("Start RoboViz");
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                RVConfigure.this.setVisible(false);
                Viewer.main(new String[] {});
            }
        });
        southPanel.add(startButton);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(550, 600);
        setResizable(false);
        getRootPane().setDefaultButton(startButton);
        pack();
        Point desiredLocation = new Point(config.graphics.frameX, config.graphics.frameY);
        if (config.graphics.centerFrame)
            desiredLocation.setLocation(0, 0);
        SwingUtil.centerOnScreenAtLocation(this, desiredLocation);
    }

    public static void main(String[] args) {
        new RVConfigure().setVisible(true);
    }
}

@@ -1807,21 +1807,41 @@
   c.
-weightx = 0.5
+anchor = GridBagConstraints.NORTH
 ;%0A%0A 
@@ -1901,15 +1901,16 @@
 new 
-Network
+Graphics
 Pane
@@ -1935,62 +1935,74 @@
     
-c.gridy = 1;%0A        add(new GraphicsPanel(this), c);%0A
+JPanel networkPanel = new NetworkPanel(this);%0A        c.gridx = 0;
 %0A   
@@ -2019,32 +2019,45 @@
 dy = 2;%0A        
+networkPanel.
 add(new GeneralP
@@ -2064,33 +2064,32 @@
 anel(this), c);%0A
-%0A
         c.gridy 
@@ -2093,32 +2093,45 @@
 dy = 3;%0A        
+networkPanel.
 add(new TeamColo
@@ -2141,33 +2141,32 @@
 anel(this), c);%0A
-%0A
         c.gridy 
@@ -2236,24 +2236,37 @@
 ));%0A        
+networkPanel.
 add(southPan
@@ -2270,24 +2270,94 @@
 Panel, c);%0A%0A
+        c.gridx = 1;%0A        c.gridy = 0;%0A        add(networkPanel);%0A%0A
         save
@@ -3123,21 +3123,23 @@
  set
-Size(550, 600
+Resizable(false
 );%0A 
@@ -3145,34 +3145,75 @@
         
-setResizable(false
+getContentPane().setBackground(networkPanel.getBackground()
 );%0A     

align some panels horizontally