Package main.settings.panels

Source Code of main.settings.panels.AbstractSettingPanel

package main.settings.panels;


import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import connection.server.ConnectionStateAdapter;

import main.settings.Settings;

import static main.ClientMain.*;


public abstract class AbstractSettingPanel extends ConnectionStateAdapter implements
    ActionListener, DocumentListener, ItemListener
{
  private JPanel space = new JPanel();
  private JPanel verticalLeftPadding = new JPanel();
  private JPanel verticalRightPadding = new JPanel();

  private JButton saveButton = new JButton("Save");
  private int rows = 0;
  private int columns = 0;
  private int maximumColumns = 0;

  protected AbstractSettingPanel()
  {
    getPanel().setLayout(new GridBagLayout());
    getConnectionManager().addListener(this);
  }

  private GridBagConstraints getVerticalPaddingConstraints(int col)
  {
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.FIRST_LINE_START;
    constraints.gridwidth = 1;
    constraints.gridheight = GridBagConstraints.REMAINDER;
    constraints.weighty = 0;
    constraints.weightx = 0.05;
    constraints.gridy = 1;
    constraints.gridx = col;
    return constraints;
  }

  private GridBagConstraints getHorizontalPaddingConstraints()
  {
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.FIRST_LINE_START;
    constraints.gridy = rows;
    constraints.gridx = 1;
    constraints.weighty = (rows == 1) ? 0.05 : 0.01;
    constraints.weightx = 0;
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    // constraints.gridheight = (rows == 1) ? ++rows : 1;
    constraints.gridheight = 1;
    return constraints;
  }

  private GridBagConstraints getSpaceConstraints()
  {
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.FIRST_LINE_START;
    constraints.gridwidth = GridBagConstraints.REMAINDER;
    constraints.gridheight = GridBagConstraints.REMAINDER;
    constraints.weighty = 1.0;
    constraints.weightx = 1.0;
    constraints.gridy = rows + 1;
    constraints.gridx = 1;
    return constraints;
  }

  private GridBagConstraints getNormalConstraints(int row, int col, double weight)
  {
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.FIRST_LINE_START;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weighty = 0.01;
    constraints.weightx = weight;
    constraints.gridy = row;
    constraints.gridx = col;
    return constraints;
  }

  private void addNewLineComponent(JComponent comp, double weight)
  {
    if (rows == 0)
    {
      getPanel().add(verticalLeftPadding, getVerticalPaddingConstraints(1));
    }
    getPanel().remove(space);
    rows++;
    getPanel().add(new JPanel(), getHorizontalPaddingConstraints());
    rows++;
    columns = 1;
    addComponent(comp, weight);
    getPanel().add(space, getSpaceConstraints());
  }

  protected void addComponent(JComponent comp, double weight)
  {
    columns++;
    getPanel().add(comp, getNormalConstraints(rows, columns, weight));
    if (columns > maximumColumns)
    {
      maximumColumns = columns;
      getPanel().remove(verticalRightPadding);
      getPanel().add(verticalRightPadding,
          getVerticalPaddingConstraints(maximumColumns + 1));
    }
  }

  protected void addSaveButton()
  {
    space.setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    saveButton.addActionListener(this);
    saveButton.setEnabled(false);
    space.add(saveButton, constraints);
  }

  protected JCheckBox addCheckBox(String name, boolean selected)
  {
    addNewLineComponent(new JLabel(name), 0.4);
    JCheckBox checkBox = new JCheckBox(selected ? "yes" : "no", selected);
    checkBox.addItemListener(this);
    addComponent(checkBox, 0.6);
    return checkBox;
  }

  protected JTextField addTextField(String name, String value)
  {
    addNewLineComponent(new JLabel(name), 0.4);
    JTextField textField = new JTextField(value);
    textField.getDocument().addDocumentListener(this);
    addComponent(textField, 0.6);
    return textField;
  }

  protected JLabel addLabelField(String name, String value)
  {
    addNewLineComponent(new JLabel(name), 0.4);
    JLabel label = new JLabel(value);
    addComponent(label, 0.6);
    return label;
  }

  public void actionPerformed(ActionEvent event)
  {
    setSettings();
    Settings.writeToFile();
    saveButton.setEnabled(checkFields() && checkChanges());
  }

  protected abstract void setSettings();

  protected boolean checkFields()
  {
    return true;
  }

  protected boolean checkChanges()
  {
    return true;
  }

  public void changedUpdate(DocumentEvent event)
  {
    saveButton.setEnabled(checkFields() && checkChanges());
  }

  public void insertUpdate(DocumentEvent event)
  {
    saveButton.setEnabled(checkFields() && checkChanges());
  }

  public void removeUpdate(DocumentEvent event)
  {
    saveButton.setEnabled(checkFields() && checkChanges());
  }

  public void itemStateChanged(ItemEvent event)
  {
    ((JCheckBox) event.getSource())
        .setText(event.getStateChange() == ItemEvent.SELECTED ? "yes" : "no");
    saveButton.setEnabled(checkFields() && checkChanges());
  }

  protected abstract JPanel getPanel();

  public static void intializePanels()
  {
    new ApplicationSettingPanel();
    new UpdateSettingPanel();
    new ConnectionSettingPanel();
    new MainSettingPanel();
    new SharingSettingPanel();
    new ThemeSettingPanel();
    new DebugSettingPanel();
  }

}
TOP

Related Classes of main.settings.panels.AbstractSettingPanel

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.