Package jpianotrain.gui

Source Code of jpianotrain.gui.GUIPanel

/* License see bottom*/
package jpianotrain.gui;

import static jpianotrain.util.ConfigurationKeys.COLOR_HINTS;
import static jpianotrain.util.ConfigurationKeys.COLOR_WRONG_KEY;
import static jpianotrain.util.ConfigurationKeys.SHOW_HINTS;
import static jpianotrain.util.ConfigurationKeys.SHOW_WRONG_KEY;
import static jpianotrain.util.ResourceKeys.LABEL_SHOW_HINTS;
import static jpianotrain.util.ResourceKeys.LABEL_SHOW_WRONG_KEY;
import static jpianotrain.util.ResourceKeys.TITLE_HINT_COLOR;
import static jpianotrain.util.ResourceKeys.TITLE_WRONG_KEY_COLOR;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;

import jpianotrain.ApplicationContext;
import jpianotrain.Constants;
import jpianotrain.util.ResourceFactory;
import jpianotrain.util.UserConfiguration;

import org.apache.log4j.Logger;

/**
* Configuration for gui appearance.
*
* @author methke01
* @since 0
*/
public class GUIPanel extends ConfigurationPanel {
  private static final Logger log=Logger.getLogger(ConfigurationPanel.class);
 
  public GUIPanel() {
    super();
    createUI();
  }
 
  protected void createUI() {
    GridBagConstraints gbc=new GridBagConstraints();
    setLayout(new GridBagLayout());
    gbc.gridx=0;
    gbc.gridy=0;
    gbc.insets=new Insets(2,2,2,2);
    gbc.anchor=GridBagConstraints.EAST;
   
    add(new JLabel(ResourceFactory.getString(LABEL_SHOW_HINTS)), gbc);
    gbc.gridy++;
    add(new JLabel(ResourceFactory.getString(LABEL_SHOW_WRONG_KEY)), gbc);
   
    gbc.gridx++;
    gbc.gridy=0;
    showHints=new JCheckBox();
    showHints.addActionListener(this);
    add(showHints, gbc);

    gbc.gridy++;
    showWrongKey=new JCheckBox();
    showWrongKey.addActionListener(this);
    add(showWrongKey, gbc);
   
    gbc.gridx++;
    gbc.gridy=0;
    gbc.fill=GridBagConstraints.HORIZONTAL;
    keyHintColor=new JTextField("        ");
    keyHintColor.setEditable(false);
    add(keyHintColor, gbc);
   
    gbc.gridy++;
    keyWrongColor=new JTextField("        ");
    keyWrongColor.setEditable(false);
    add(keyWrongColor, gbc);
   
    gbc.gridx++;
    gbc.gridy=0;
    gbc.fill=GridBagConstraints.NONE;
    hintChooser=new JButton("...");
    hintChooser.addActionListener(this);
    add(hintChooser, gbc);
   
    gbc.gridy++;
    wrongKeyChooser=new JButton("...");
    wrongKeyChooser.addActionListener(this);
    add(wrongKeyChooser, gbc);
  }

  @Override
  public void initData() {
    UserConfiguration uc=UserConfiguration.getInstance();
    showWrongKey.setSelected(uc.getBooleanProperty(SHOW_WRONG_KEY, true));
    showHints.setSelected(uc.getBooleanProperty(SHOW_HINTS, true));
   
    keyWrongColor.setBackground(uc.getColorProperty(COLOR_WRONG_KEY, Constants.COLOR_WRONG_KEY));
    keyHintColor.setBackground(uc.getColorProperty(COLOR_HINTS, Constants.COLOR_HINTS));
    updateEnabledStatus();
  }
 
  @Override
  public void saveData() {
    UserConfiguration uc=UserConfiguration.getInstance();
    ApplicationContext ctx=ApplicationContext.getInstance();
   
    log.debug("sending show wrong key status: "+showWrongKey.isSelected());
    ctx.setKeyWrongColor(keyWrongColor.getBackground());
    ctx.setShowWrongKey(showWrongKey.isSelected());
   
    ctx.setKeyHintColor(keyHintColor.getBackground());
    ctx.setShowHints(showHints.isSelected());
   
    uc.putProperty(COLOR_WRONG_KEY, keyWrongColor.getBackground());
    uc.putProperty(COLOR_HINTS, keyHintColor.getBackground());
    uc.putProperty(SHOW_WRONG_KEY, keyWrongColor.isEnabled());
    uc.putProperty(SHOW_HINTS, keyHintColor.isEnabled());
  }
 
  @Override
  public void rejectData() {
   
  }
 
  public void actionPerformed(ActionEvent e) {
    Object src=e.getSource();
    if (src==wrongKeyChooser) {
      Color c=JColorChooser.showDialog(this,
          ResourceFactory.getString(TITLE_WRONG_KEY_COLOR),
          keyWrongColor.getBackground());
      if (c!=null) {
        keyWrongColor.setBackground(c);
      }
    } else if (src==hintChooser) {
      Color c=JColorChooser.showDialog(this,
          ResourceFactory.getString(TITLE_HINT_COLOR),
          keyHintColor.getBackground());
      if (c!=null) {
        keyHintColor.setBackground(c);
      }
    } else if (src==showWrongKey ||
        src==showHints) {
      updateEnabledStatus();
    }
  }
 
  private void updateEnabledStatus() {
    wrongKeyChooser.setEnabled(showWrongKey.isSelected());
    keyWrongColor.setEnabled(showWrongKey.isSelected());
   
    hintChooser.setEnabled(showHints.isSelected());
    keyHintColor.setEnabled(showHints.isSelected());
  }
 
  private JButton wrongKeyChooser;
  private JButton hintChooser;
 
  private JCheckBox showWrongKey;
  private JCheckBox showHints;
 
  private JTextField keyWrongColor;
  private JTextField keyHintColor;
}

/*
    Copyright (C) 2008  Alexander Methke

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program (gplv3.txt).
*/
TOP

Related Classes of jpianotrain.gui.GUIPanel

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.