Package gui

Source Code of gui.OptionFrame

/*
* Hamsam - Instant Messaging API
* Copyright (C) 2003 Raghu K
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package gui;

import gui.tools.PropertiesManager;
import gui.tools.Traces;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class OptionFrame extends JDialog implements ActionListener
{
  private PropertiesManager propertiesMgr;
  private BorderLayout mainLayout = new BorderLayout();

  //Buttons
  private JButton ok;
  private JButton cancel;
  private JPanel buttonPanel = new JPanel();

  //Option Panel
  private Box optionPanel = new Box(BoxLayout.Y_AXIS);

  //Language option
  private JCheckBox french;
  private JCheckBox english;
  private JPanel languagePanel = new JPanel();
  private ButtonGroup languageGroup = new ButtonGroup();
  private MainFrame owner;

  //Proxy option
  private JComboBox proxyType;
  private JTextField proxyHost;
  private JTextField proxyPort;
  private JPanel proxyPanel = new JPanel();

  public OptionFrame(MainFrame owner, PropertiesManager p)
  {
    super(owner, true);
    this.owner = owner;
    this.propertiesMgr = p;
    this.init();
  }

  private void init()
  {
    this.setTitle(propertiesMgr.getValue("optionFrameTitle"));
    this.getContentPane().setLayout(mainLayout);

    ok = new JButton(this.propertiesMgr.getValue("validOptionButton"));
    ok.addActionListener(this);
    cancel = new JButton(this.propertiesMgr.getValue("cancelOptionButton"));
    cancel.addActionListener(this);

    //Construct language panel option.
    french = new JCheckBox(this.propertiesMgr.getValue("frenchOption"));
    english = new JCheckBox(this.propertiesMgr.getValue("englishOption"));
    languagePanel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    languageGroup.add(french);
    languageGroup.add(english);
    c.gridy = 0;
    languagePanel.add(french, c);
    c.gridy = 1;
    languagePanel.add(english, c);
    PropertiesManager genProp = owner.getGeneralPropertiesManager();
    if(genProp.getValue("language").equals("fr"))
      french.setSelected(true);
    else
      english.setSelected(true);
    languagePanel.setBorder(new TitledBorder(propertiesMgr.getValue("languageBorder")));

    //Construct proxy panel.
    proxyType = new JComboBox();
    proxyType.addItem("No Proxy");
    proxyType.addItem("HTTP");
    proxyType.addItem("Socks 4");
    proxyType.addItem("Socks 5");
    proxyType.setSelectedIndex(Integer.parseInt(genProp.getValue("proxyType")));
    proxyHost = new JTextField();
    proxyHost.setText(genProp.getValue("proxyHost"));
    proxyPort = new JTextField();
    proxyPort.setText(genProp.getValue("proxyPort"));

    proxyPanel.setLayout(new GridBagLayout());
    c.gridy = 0;
    proxyPanel.add(proxyType, c);
    c.gridy = 1;
    proxyPanel.add(proxyHost, c);
    c.gridy = 2;
    proxyPanel.add(proxyPort, c);
    proxyPanel.setBorder(new TitledBorder(propertiesMgr.getValue("proxyBorder")));

    //Construct option panel
    optionPanel.add(languagePanel);
    optionPanel.add(proxyPanel);

    //Construct button panel
    buttonPanel.add(ok);
    buttonPanel.add(cancel);

    //construct frame
    this.getContentPane().add(optionPanel, BorderLayout.CENTER);
    this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
  }

  public void actionPerformed(ActionEvent evt)
  {
    Object source = evt.getSource();
    if(source == ok)
    {
      Traces.printTraces("Options recorded.");
      String sel = "en";
      if(french.isSelected())
        sel = "fr";
      PropertiesManager genProp = owner.getGeneralPropertiesManager();
      genProp.setValue("language", sel);
      sel = "" + proxyType.getSelectedIndex();
      genProp.setValue("proxyType", sel);
      sel = proxyHost.getText();
      genProp.setValue("proxyHost", sel);
      sel = proxyPort.getText();
      genProp.setValue("proxyPort", sel);
      genProp.saveProperties("");
      this.dispose();
    }
    else if (source == cancel)
    {
      Traces.printTraces("Options cancelled.");
      this.dispose();
    }
  }
}
TOP

Related Classes of gui.OptionFrame

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.