/* License see bottom */
package jpianotrain;
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 java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import jpianotrain.event.ApplicationContextListener;
import jpianotrain.util.ErrorHandler;
import jpianotrain.util.ResourceKeys;
import jpianotrain.util.UserConfiguration;
import org.apache.log4j.Logger;
/**
* Central station for common
* information.
*
* @since 0
* @author Alexander Methke
*/
public class ApplicationContext {
private static final Logger log=Logger.getLogger(ApplicationContext.class);
private ApplicationContext() {
contextListener=new ArrayList<ApplicationContextListener>();
UserConfiguration uc=UserConfiguration.getInstance();
keyHintColor=uc.getColorProperty(COLOR_HINTS, Constants.COLOR_HINTS);
keyWrongColor=uc.getColorProperty(COLOR_WRONG_KEY, Constants.COLOR_WRONG_KEY);
showWrongKey=uc.getBooleanProperty(SHOW_WRONG_KEY, true);
showHints=uc.getBooleanProperty(SHOW_HINTS, true);
}
public enum Mode {
MIDI_FILES,
RANDOM_NOTES,
LESSONS
}
public enum MidiStatus {
ON,
OFF
}
public enum StartMode {
ON_FIRST_KEY,
COUNT_IN
}
public Mode getMode() {
return mode;
}
public void setMode(Mode m) {
Mode oldMode=mode;
mode=m;
if (oldMode!=mode) {
fireModeChanged();
}
}
public StartMode getStartMode() {
return startMode;
}
public void setStartMode(StartMode m) {
StartMode oldMode=startMode;
startMode=m;
if (oldMode!=startMode) {
fireStartModeChanged();
}
}
public MidiStatus getMidiStatus() {
return midiStatus;
}
public void setMidiStatus(MidiStatus stat) {
MidiStatus oldStat=midiStatus;
midiStatus=stat;
if (oldStat!=stat) {
fireMidiStatusChanged();
}
}
public JFrame getDefaultDialogOwner() {
return defaultDialogOwner;
}
public void setDefaultDialogOwner(JFrame frame) {
defaultDialogOwner=frame;
}
public void addListener(ApplicationContextListener l) {
if (contextListener.contains(l)) {
return;
}
contextListener.add(l);
}
public void removeListener(ApplicationContextListener l) {
contextListener.remove(l);
}
/**
* If you're lazy, forward your message to
* {@link ErrorHandler} with this method.
*/
public void showMessage(ResourceKeys message) {
ErrorHandler.handleMessage(message);
}
public void setSpoolMidi(boolean flag) {
boolean oldStat=spoolMidi;
spoolMidi=flag;
if (flag ^ oldStat) {
fireSpoolChanged();
}
}
public boolean isSpoolMidi() {
return spoolMidi;
}
public int getActiveMidiChannel() {
return activeMidiChannel;
}
public void setActiveMidiChannel(int mc) {
activeMidiChannel=mc;
}
public boolean isShowHints() {
return showHints;
}
public void setShowHints(boolean showHints) {
this.showHints = showHints;
fireShowHintsChanged();
}
public boolean isShowWrongKey() {
return showWrongKey;
}
public void setShowWrongKey(boolean showWrongKey) {
log.debug("receiving show wrong key status: "+showWrongKey);
this.showWrongKey = showWrongKey;
fireShowWrongKeyChanged();
}
public Color getKeyWrongColor() {
return keyWrongColor;
}
public void setKeyWrongColor(Color keyWrongColor) {
this.keyWrongColor = keyWrongColor;
fireWrongColorChanged();
}
public Color getKeyHintColor() {
return keyHintColor;
}
public void setKeyHintColor(Color keyHintColor) {
this.keyHintColor = keyHintColor;
fireHintColorChanged();
}
public void handleError(Exception ex) {
ErrorHandler.handleError(ex);
}
public static ApplicationContext getInstance() {
if (instance==null) {
instance=new ApplicationContext();
}
return instance;
}
protected void fireModeChanged() {
for(ApplicationContextListener l:contextListener) {
l.modeChanged();
}
}
protected void fireSpoolChanged() {
for(ApplicationContextListener l:contextListener) {
l.spoolChanged();
}
}
protected void fireMidiStatusChanged() {
for(ApplicationContextListener l:contextListener) {
l.midiStatusChanged();
}
}
protected void fireStartModeChanged() {
for(ApplicationContextListener l:contextListener) {
l.startModeChanged();
}
}
protected void fireWrongColorChanged() {
for(ApplicationContextListener l:contextListener) {
l.wrongColorChanged();
}
}
protected void fireHintColorChanged() {
for(ApplicationContextListener l:contextListener) {
l.hintColorChanged();
}
}
protected void fireShowWrongKeyChanged() {
for(ApplicationContextListener l:contextListener) {
l.showWrongKeyChanged();
}
}
protected void fireShowHintsChanged() {
for(ApplicationContextListener l:contextListener) {
l.showHintsChanged();
}
}
private Mode mode=Constants.DEFAULT_MODE;
private StartMode startMode=Constants.DEFAULT_START_MODE;
private static ApplicationContext instance;
private boolean spoolMidi;
private boolean showHints;
private boolean showWrongKey;
private int activeMidiChannel;
private Color keyWrongColor;
private Color keyHintColor;
private JFrame defaultDialogOwner;
private MidiStatus midiStatus;
private List<ApplicationContextListener> contextListener;
}
/*
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).
*/