Package jpianotrain.gui

Source Code of jpianotrain.gui.StatusPanel

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

import static jpianotrain.util.ResourceKeys.LABEL_MIDI;
import static jpianotrain.util.ResourceKeys.LABEL_SHOW_HINTS;
import static jpianotrain.util.ResourceKeys.LABEL_SHOW_WRONG_KEY;
import static jpianotrain.util.ResourceKeys.LABEL_SPOOL_MIDI;
import static jpianotrain.util.ResourceKeys.MESSAGE_OFF;
import static jpianotrain.util.ResourceKeys.MESSAGE_ON;
import static jpianotrain.util.ResourceKeys.TOOLTIP_MODE_STATUS;

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

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import jpianotrain.ApplicationContext;
import jpianotrain.event.ApplicationContextListener;
import jpianotrain.util.ResourceFactory;

import org.apache.log4j.Logger;

/**
* Status panel to display some information at
* the bottom of main frame.
*
* @since 0
* @author Alexander Methke
*/
public class StatusPanel extends JPanel
             implements ApplicationContextListener {
  private static final Logger log=Logger.getLogger(StatusPanel.class);
  public StatusPanel() {
    ApplicationContext.getInstance().addListener(this);
    createUI();
  }

  private void createUI() {
    setLayout(new GridBagLayout());
    GridBagConstraints gbc=new GridBagConstraints();
    ApplicationContext ctx=ApplicationContext.getInstance();
   
    JPanel p=new JPanel();
    gbc.weightx=1.0;
    gbc.insets=new Insets(2,3,2,3);
    add(p, gbc);
    gbc.weightx=0;
    gbc.gridy=0;
    // remember to fill from right to
    // left.
    gbc.anchor=GridBagConstraints.EAST;
    modeField=createField();
    modeField.setText(ctx.getMode().toString());
    modeField.setToolTipText(ResourceFactory.getMessage(
      TOOLTIP_MODE_STATUS,
      ApplicationContext.getInstance().getMode()));
    gbc.gridx++;
    add(modeField, gbc);

    midiField=createField();
    midiField.setText(ResourceFactory.getString(LABEL_MIDI)+
      " "+ResourceFactory.getString(MESSAGE_OFF));
    gbc.gridx++;
    add(midiField, gbc);
   
    spoolField=createField();
    spoolField.setText(ResourceFactory.getString(LABEL_SPOOL_MIDI)+
      " "+ResourceFactory.getString(MESSAGE_OFF));
    gbc.gridx++;
    add(spoolField, gbc);

    startModeField=createField();
    startModeField.setText(ctx.getStartMode().toString());
    gbc.gridx++;
    add(startModeField, gbc);
   
    hintField=createField();
    hintField.setText(ResourceFactory.getString(LABEL_SHOW_HINTS));
    hintField.setEnabled(ctx.isShowHints());
    hintColorChanged();
    gbc.gridx++;
    add(hintField);
   
    wrongField=createField();
    wrongField.setText(ResourceFactory.getString(LABEL_SHOW_WRONG_KEY));
    wrongField.setEnabled(ctx.isShowWrongKey());
    wrongColorChanged();
    gbc.gridx++;
    add(wrongField);
    updateEnabledState();
  }

  private JTextField createField() {
    JTextField tf=new JTextField();
    tf.setEditable(false);
    tf.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    return tf;
  }

// ApplicationContextListener
  public void modeChanged() {
    modeField.setText(ApplicationContext.getInstance().getMode().toString());
    modeField.setToolTipText(ResourceFactory.getMessage(
      TOOLTIP_MODE_STATUS,
      ApplicationContext.getInstance().getMode()));
  }

  public void spoolChanged() {
    ApplicationContext ctx=ApplicationContext.getInstance();
    if (ctx.isSpoolMidi()) {
      spoolField.setText(ResourceFactory.getString(LABEL_SPOOL_MIDI)+
          " "+ResourceFactory.getString(MESSAGE_ON));
    } else {
      spoolField.setText(ResourceFactory.getString(LABEL_SPOOL_MIDI)+
          " "+ResourceFactory.getString(MESSAGE_OFF));
    }
  }
 
  public void midiStatusChanged() {
    ApplicationContext ctx=ApplicationContext.getInstance();
    if (ctx.getMidiStatus()==ApplicationContext.MidiStatus.ON) {
      midiField.setText(ResourceFactory.getString(LABEL_MIDI)+
          " "+ResourceFactory.getString(MESSAGE_ON));
    } else {
      midiField.setText(ResourceFactory.getString(LABEL_MIDI)+
          " "+ResourceFactory.getString(MESSAGE_OFF));
    }
  }
 
  public void startModeChanged() {
    startModeField.setText(ApplicationContext.getInstance().getStartMode().toString());
  }
 
  public void hintColorChanged() {
    Color c=ApplicationContext.getInstance().getKeyHintColor();
    hintField.setBackground(c);
    if (c.getRed()+c.getGreen()+c.getBlue()>265) {
      hintField.setForeground(Color.black);
    } else {
      hintField.setForeground(Color.white);
    }
  }
 
  public void wrongColorChanged() {
    Color c=ApplicationContext.getInstance().getKeyWrongColor();
    wrongField.setBackground(c);
    if (c.getRed()+c.getGreen()+c.getBlue()>265) {
      wrongField.setForeground(Color.black);
    } else {
      wrongField.setForeground(Color.white);
    }
  }
 
  public void showHintsChanged() {
    hintField.setEnabled(ApplicationContext.getInstance().isShowHints());
    updateEnabledState();
  }
 
  public void showWrongKeyChanged() {
    log.debug("updating wrong key changed status to: "+
        ApplicationContext.getInstance().isShowWrongKey());
    wrongField.setEnabled(ApplicationContext.getInstance().isShowWrongKey());
    updateEnabledState();
  }

  /**
   * Disabled color fields should appear
   * gray/black instead of colored nervous
   * eye catching candy blocks.
   */
  private void updateEnabledState() {
    if (!hintField.isEnabled()) {
      hintField.setBackground(Color.gray);
      hintField.setForeground(Color.black);
    }
    if (!wrongField.isEnabled()) {
      wrongField.setBackground(Color.gray);
      wrongField.setForeground(Color.black);
    }
  }
 
  private JTextField wrongField;
  private JTextField hintField;
  private JTextField modeField;
  private JTextField midiField;
  private JTextField spoolField;
  private JTextField startModeField;
}
/*
    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.StatusPanel

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.