Initial code check in continued

This commit is contained in:
dennis.sheirer
2014-04-26 19:48:24 +00:00
parent 960a6f9952
commit 3dfedcf8d1
474 changed files with 76613 additions and 0 deletions

View File

@@ -0,0 +1,190 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* 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. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package settings;
import java.awt.Color;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
public class ColorSetting extends Setting
{
private static final int NO_TRANSLUCENCY = 255;
private static final int SPECTRUM_TRANSLUCENCY = 128;
private static final int CONFIG_TRANSLUCENCY = 60;
private String mRGB;
private int mAlpha;
private ColorSettingName mColorSettingName = ColorSettingName.UNKNOWN;
public ColorSetting()
{
setColor( mColorSettingName.getDefaultColor() );
}
public ColorSetting( ColorSettingName name )
{
setColor( name.getDefaultColor() );
setColorSettingName( name );
}
@XmlAttribute
public ColorSettingName getColorSettingName()
{
return mColorSettingName;
}
public void setColorSettingName( ColorSettingName name )
{
mColorSettingName = name;
}
@XmlAttribute
public String getRgb()
{
return mRGB;
}
public void setRgb( String value )
{
mRGB = value;
}
@XmlAttribute
public int getAlpha()
{
return mAlpha;
}
public void setAlpha( int value )
{
mAlpha = value;
}
@XmlTransient
public Color getColor()
{
Color temp = Color.decode( mRGB );
return new Color( temp.getRed(),
temp.getGreen(),
temp.getBlue(),
mAlpha );
}
public void setColor( Color color )
{
mRGB = Integer.toHexString( color.getRGB() );
mRGB = "#" + mRGB.substring( 2, mRGB.length() );
mAlpha = color.getAlpha();
}
public static Color getTranslucent( Color color, int translucency )
{
return new Color( color.getRed(),
color.getGreen(),
color.getBlue(),
translucency );
}
public enum ColorSettingName
{
CHANNEL_CONFIG( getTranslucent( Color.LIGHT_GRAY, CONFIG_TRANSLUCENCY ),
"Channel", "Channel Color" ),
CHANNEL_CONFIG_PROCESSING( getTranslucent( Color.GREEN, CONFIG_TRANSLUCENCY ),
"Channel Processing", "Processing Channel Color" ),
CHANNEL_CONFIG_SELECTED( getTranslucent( Color.BLUE, CONFIG_TRANSLUCENCY ),
"Channel Selected", "Selected Channel Color" ),
CHANNEL_STATE_BACKGROUND( Color.BLACK, "Background",
"Channel State Background" ),
CHANNEL_STATE_GRADIENT_TOP_CALL( Color.BLACK,
"Call Gradient Top", "Channel Call State Gradient Top" ),
CHANNEL_STATE_GRADIENT_MIDDLE_CALL( Color.BLUE,
"Call Gradient Middle", "Channel Call State Gradient Middle" ),
CHANNEL_STATE_GRADIENT_TOP_CONTROL( Color.BLACK,
"Control Gradient Top", "Channel Control State Gradient Top" ),
CHANNEL_STATE_GRADIENT_MIDDLE_CONTROL( new Color( 0xC64F00 ),
"Control Gradient Middle", "Channel Control State Gradient Middle" ),
CHANNEL_STATE_GRADIENT_TOP_DATA( Color.BLACK,
"Data Gradient Top", "Channel Data State Gradient Top" ),
CHANNEL_STATE_GRADIENT_MIDDLE_DATA( Color.BLUE,
"Data Gradient Middle", "Channel Data State Gradient Middle" ),
CHANNEL_STATE_GRADIENT_TOP_FADE( Color.BLACK,
"Fade Gradient Top", "Channel Fade State Gradient Top" ),
CHANNEL_STATE_GRADIENT_MIDDLE_FADE( Color.DARK_GRAY,
"Fade Gradient Middle", "Channel Fade State Gradient Middle" ),
CHANNEL_STATE_GRADIENT_TOP_IDLE( Color.BLACK,
"Idle Gradient Top", "Channel Idle State Gradient Top" ),
CHANNEL_STATE_GRADIENT_MIDDLE_IDLE( Color.DARK_GRAY,
"Idle Gradient Middle", "Channel Idle State Gradient Middle" ),
CHANNEL_STATE_GRADIENT_TOP_NO_TUNER( Color.RED,
"No Tuner Gradient Top", "Channel No Tuner State Gradient Top" ),
CHANNEL_STATE_GRADIENT_MIDDLE_NO_TUNER( new Color( 0x990000 ),
"No Tuner Gradient Middle", "Channel No Tuner State Gradient Middle" ),
CHANNEL_STATE_LABEL_DETAILS( Color.LIGHT_GRAY,
"Details", "Details Label Color" ),
CHANNEL_STATE_LABEL_DECODER( Color.GREEN,
"Decoder", "Decoder Label Color" ),
CHANNEL_STATE_LABEL_AUX_DECODER( Color.YELLOW,
"Aux Decoder", "Aux Decoder Label Color" ),
CHANNEL_STATE_SELECTED_CHANNEL( Color.YELLOW,
"Selected Channel Indicator", "Selected Channel Indicator Color" ),
SPECTRUM_BACKGROUND( Color.BLACK,
"Background", "Spectrum Background Color" ),
SPECTRUM_CURSOR( Color.ORANGE,
"Cursor", "Spectrum Cursor Color" ),
SPECTRUM_GRADIENT_BOTTOM( getTranslucent( Color.GREEN, SPECTRUM_TRANSLUCENCY ),
"Gradient Bottom", "Spectrum Gradient Bottom Color" ),
SPECTRUM_GRADIENT_TOP( getTranslucent( Color.WHITE, SPECTRUM_TRANSLUCENCY ),
"Gradient Top", "Spectrum Gradient Top Color" ),
SPECTRUM_LINE( getTranslucent( Color.LIGHT_GRAY, SPECTRUM_TRANSLUCENCY ),
"Line", "Spectrum Lines and Text Color" ),
UNKNOWN( Color.RED, "Unknown", "Unknown Setting Color" );
private Color mDefaultColor;
private String mLabel;
private String mDialogTitle;
private ColorSettingName( Color defaultColor,
String label,
String dialogTitle )
{
mDefaultColor = defaultColor;
mLabel = label;
mDialogTitle = dialogTitle;
}
public Color getDefaultColor()
{
return mDefaultColor;
}
public String getLabel()
{
return mLabel;
}
public String getDialogTitle()
{
return mDialogTitle;
}
}
}

View File

@@ -0,0 +1,71 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* 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. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package settings;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JColorChooser;
import javax.swing.JMenuItem;
import settings.ColorSetting.ColorSettingName;
import util.ColorIcon;
/**
* JMenuItem for selecting a color and automatically setting (saving) the
* color selection in the settings manager
*/
public class ColorSettingMenuItem extends JMenuItem
{
private static final long serialVersionUID = 1L;
private ColorSettingName mColorSettingName;
private SettingsManager mSettingsManager;
private Color mCurrentColor;
public ColorSettingMenuItem( SettingsManager settingsManager,
ColorSettingName colorSettingName )
{
super( colorSettingName.getLabel() );
mSettingsManager = settingsManager;
mColorSettingName = colorSettingName;
mCurrentColor = mSettingsManager
.getColorSetting( mColorSettingName ).getColor();
this.setIcon( new ColorIcon( mCurrentColor ) );
addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
Color newColor = JColorChooser.showDialog( ColorSettingMenuItem.this,
mColorSettingName.getDialogTitle(),
mCurrentColor );
if( newColor != null )
{
mSettingsManager.setColorSetting( mColorSettingName, newColor );
}
}
} );
}
}

View File

@@ -0,0 +1,57 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* 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. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package settings;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import settings.ColorSetting.ColorSettingName;
/**
* JMenuItem for selecting a color and automatically setting (saving) the
* color selection in the settings manager
*/
public class ColorSettingResetAllMenuItem extends JMenuItem
{
private static final long serialVersionUID = 1L;
private ColorSettingName mColorSettingName;
private SettingsManager mSettingsManager;
private Color mCurrentColor;
public ColorSettingResetAllMenuItem( SettingsManager settingsManager,
ColorSettingName colorSettingName )
{
super( "Reset - " + colorSettingName.getLabel() );
mSettingsManager = settingsManager;
mColorSettingName = colorSettingName;
addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
mSettingsManager.resetAllColorSettings();
}
} );
}
}

View File

@@ -0,0 +1,57 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* 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. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package settings;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import settings.ColorSetting.ColorSettingName;
/**
* JMenuItem for selecting a color and automatically setting (saving) the
* color selection in the settings manager
*/
public class ColorSettingResetMenuItem extends JMenuItem
{
private static final long serialVersionUID = 1L;
private ColorSettingName mColorSettingName;
private SettingsManager mSettingsManager;
private Color mCurrentColor;
public ColorSettingResetMenuItem( SettingsManager settingsManager,
ColorSettingName colorSettingName )
{
super( "Reset - " + colorSettingName.getLabel() );
mSettingsManager = settingsManager;
mColorSettingName = colorSettingName;
addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
mSettingsManager.resetColorSetting( mColorSettingName );
}
} );
}
}

View File

@@ -0,0 +1,52 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* 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. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package settings;
import java.io.File;
public class FileSetting extends Setting
{
private String mPath;
public FileSetting()
{
super();
}
public FileSetting( String name, String path )
{
super( name );
setPath( path );
}
public String getPath()
{
return mPath;
}
public void setPath( String value )
{
mPath = value;
}
public File getFile()
{
return new File( mPath );
}
}

View File

@@ -0,0 +1,79 @@
package settings;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import org.jdesktop.swingx.mapviewer.GeoPosition;
public class MapViewSetting extends Setting
{
private double mLatitude;
private double mLongitude;
private int mZoom;
public MapViewSetting()
{
/* Empty constructor for JAXB */
}
public MapViewSetting( String name, double latitude, double longitude, int zoom )
{
super( name );
mLatitude = latitude;
mLongitude = longitude;
mZoom = zoom;
}
public MapViewSetting( String name, GeoPosition position, int zoom )
{
super( name );
mLatitude = position.getLatitude();
mLongitude = position.getLongitude();
mZoom = zoom;
}
@XmlAttribute
public double getLatitude()
{
return mLatitude;
}
public void setLatitude( double latitude )
{
mLatitude = latitude;
}
@XmlAttribute
public double getLongitude()
{
return mLongitude;
}
public void setLongitude( double longitude )
{
mLongitude = longitude;
}
@XmlAttribute
public int getZoom()
{
return mZoom;
}
public void setZoom( int zoom )
{
mZoom = zoom;
}
@XmlTransient
public GeoPosition getGeoPosition()
{
return new GeoPosition( mLatitude, mLongitude );
}
public void setGeoPosition( GeoPosition position )
{
mLatitude = position.getLatitude();
mLongitude = position.getLongitude();
}
}

45
src/settings/Setting.java Normal file
View File

@@ -0,0 +1,45 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* 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. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package settings;
import javax.xml.bind.annotation.XmlAttribute;
public class Setting
{
protected String mName;
public Setting()
{
}
public Setting( String name )
{
mName = name;
}
@XmlAttribute
public String getName()
{
return mName;
}
public void setName( String name )
{
mName = name;
}
}

View File

@@ -0,0 +1,24 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* 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. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package settings;
public interface SettingChangeListener
{
public void settingChanged( Setting setting );
public void settingDeleted( Setting setting );
}

295
src/settings/Settings.java Normal file
View File

@@ -0,0 +1,295 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* 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. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package settings;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import map.DefaultIcon;
import map.MapIcon;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import settings.ColorSetting.ColorSettingName;
import source.tuner.TunerConfiguration;
import source.tuner.TunerConfigurationAssignment;
import source.tuner.TunerType;
import source.tuner.fcd.proV1.FCD1TunerConfiguration;
import source.tuner.fcd.proplusV2.FCD2TunerConfiguration;
import source.tuner.rtl.e4k.E4KTunerConfiguration;
import source.tuner.rtl.r820t.R820TTunerConfiguration;
/**
* Support for persisting via JAXB all configured SDRTrunk settings
*/
@XmlSeeAlso( { ColorSetting.class,
DefaultIcon.class,
E4KTunerConfiguration.class,
FCD1TunerConfiguration.class,
FCD2TunerConfiguration.class,
FileSetting.class,
MapViewSetting.class,
MapIcon.class,
R820TTunerConfiguration.class,
Setting.class,
TunerConfiguration.class,
TunerConfigurationAssignment.class} )
@XmlRootElement( name = "SDRTrunk_settings" )
public class Settings
{
private ArrayList<Setting> mSettings = new ArrayList<Setting>();
private ArrayList<TunerConfiguration> mTunerConfiguration =
new ArrayList<TunerConfiguration>();
private ArrayList<TunerConfigurationAssignment> mConfigurationAssignments =
new ArrayList<TunerConfigurationAssignment>();
public Settings()
{
}
@XmlElement( name="tuner_configuration" )
public ArrayList<TunerConfiguration> getTunerConfigurations()
{
return mTunerConfiguration;
}
public void setTunerConfigurations( ArrayList<TunerConfiguration> configs )
{
mTunerConfiguration = configs;
}
public void addTunerConfiguration( TunerConfiguration config )
{
mTunerConfiguration.add( config );
}
public void removeTunerConfiguration( TunerConfiguration config )
{
mTunerConfiguration.remove( config );
}
public ArrayList<TunerConfiguration> getTunerConfigurations( TunerType type )
{
ArrayList<TunerConfiguration> configs =
new ArrayList<TunerConfiguration>();
for( TunerConfiguration config: mTunerConfiguration )
{
if( config.getTunerType() == type )
{
configs.add( config );
}
}
return configs;
}
@XmlElement( name="setting" )
public ArrayList<Setting> getSettings()
{
return mSettings;
}
public void setSettings( ArrayList<Setting> settings )
{
mSettings = settings;
}
public Setting getSetting( String name )
{
for( Setting setting: mSettings )
{
if( setting.getName().contentEquals( name ) )
{
return setting;
}
}
return null;
}
public void addSetting( Setting setting )
{
mSettings.add( setting );
}
public void removeSetting( Setting setting )
{
mSettings.remove( setting );
}
public DefaultIcon getDefaultIcon()
{
for( Setting setting: mSettings )
{
if( setting instanceof DefaultIcon )
{
return (DefaultIcon)setting;
}
}
return null;
}
public ColorSetting getColorSetting( ColorSettingName name )
{
for( Setting setting: mSettings )
{
if( setting instanceof ColorSetting &&
((ColorSetting)setting).getColorSettingName() == name )
{
return (ColorSetting)setting;
}
}
return null;
}
public List<ColorSetting> getColorSettings()
{
ArrayList<ColorSetting> colors = new ArrayList<ColorSetting>();
for( Setting setting: mSettings )
{
if( setting instanceof ColorSetting )
{
colors.add( (ColorSetting)setting );
}
}
return colors;
}
public FileSetting getFileSetting( String name )
{
for( Setting setting: mSettings )
{
if( setting instanceof FileSetting &&
setting.getName().contentEquals( name ) )
{
return (FileSetting)setting;
}
}
return null;
}
public MapIcon getMapIcon( String name )
{
for( Setting setting: mSettings )
{
if( setting instanceof MapIcon &&
setting.getName().contentEquals( name ) )
{
return (MapIcon)setting;
}
}
return null;
}
public ArrayList<MapIcon> getMapIcons()
{
ArrayList<MapIcon> icons = new ArrayList<MapIcon>();
for( Setting setting: mSettings )
{
if( setting instanceof MapIcon )
{
icons.add( (MapIcon)setting );
}
}
return icons;
}
@XmlElement( name="tuner_configuration_assignment" )
public ArrayList<TunerConfigurationAssignment> getSelectedTunerConfiguration()
{
return mConfigurationAssignments;
}
public void setSelectedTunerConfiguration( ArrayList<TunerConfigurationAssignment> configs )
{
mConfigurationAssignments = configs;
}
public void addSelectedTunerConfiguration( TunerConfigurationAssignment config )
{
mConfigurationAssignments.add( config );
}
public void removeSelectedTunerConfiguration( TunerConfigurationAssignment config )
{
mConfigurationAssignments.remove( config );
}
public TunerConfigurationAssignment getConfigurationAssignment( TunerType type, String uniqueID )
{
for( TunerConfigurationAssignment config: mConfigurationAssignments )
{
if( config.getTunerType() == type &&
uniqueID.contentEquals( config.getUniqueID() ) )
{
return config;
}
}
return null;
}
public void setConfigurationAssignment( TunerType type,
String uniqueID,
String tunerConfigurationName )
{
TunerConfigurationAssignment config = getConfigurationAssignment( type, uniqueID );
if( config == null )
{
config = new TunerConfigurationAssignment();
config.setTunerType( type );
config.setUniqueID( uniqueID );
addSelectedTunerConfiguration( config );
}
config.setTunerConfigurationName( tunerConfigurationName );
}
public MapViewSetting getMapViewSetting( String name )
{
for( Setting setting: mSettings )
{
if( setting instanceof MapViewSetting &&
setting.getName().contentEquals( name ) )
{
return (MapViewSetting)setting;
}
}
return null;
}
}

View File

@@ -0,0 +1,818 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
*
* 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. If not, see <http://www.gnu.org/licenses/>
******************************************************************************/
package settings;
import java.awt.Color;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.ImageIcon;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import log.Log;
import map.DefaultIcon;
import map.MapIcon;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import properties.SystemProperties;
import settings.ColorSetting.ColorSettingName;
import source.tuner.TunerConfiguration;
import source.tuner.TunerConfigurationAssignment;
import source.tuner.TunerType;
import source.tuner.fcd.proV1.FCD1TunerConfiguration;
import source.tuner.fcd.proplusV2.FCD2TunerConfiguration;
import source.tuner.rtl.e4k.E4KTunerConfiguration;
import source.tuner.rtl.r820t.R820TTunerConfiguration;
public class SettingsManager
{
private Settings mSettings = new Settings();
private ArrayList<SettingChangeListener> mListeners =
new ArrayList<SettingChangeListener>();
public static final String DEFAULT_ICON = "No Icon";
public String mCurrentDefaultIconName = DEFAULT_ICON;
private HashMap<String,MapIcon> mIcons = new HashMap<String,MapIcon>();
private HashMap<String,ImageIcon> mResizedIcons =
new HashMap<String,ImageIcon>();
public SettingsManager()
{
init();
}
/**
* Loads settings from the current settings file, or the default settings file,
* as specified in the current SDRTrunk system settings
*/
private void init()
{
SystemProperties props = SystemProperties.getInstance();
Path settingsFolder = props.getApplicationFolder( "settings" );
String defaultSettingsFile =
props.get( "settings.defaultFilename", "settings.xml" );
String settingsFile =
props.get( "settings.currentFilename", defaultSettingsFile );
load( settingsFolder.resolve( settingsFile ) );
refreshIcons();
}
/**
* Reloads the icon hash map, and sets up the default iconology. If the
* saved default icon name is not part of the current set of icons, we
* set the default icon to the standard system default name, which should
* be guaranteed to be available.
*/
public void refreshIcons()
{
mIcons.clear();
mCurrentDefaultIconName = DEFAULT_ICON;
DefaultIcon defaultIcon = getDefaultIcon();
ArrayList<MapIcon> icons = mSettings.getMapIcons();
icons.addAll( getStandardIcons() );
for( MapIcon icon: icons )
{
mIcons.put( icon.getName(), icon );
if( icon.getName().contentEquals( defaultIcon.getName() ) )
{
mCurrentDefaultIconName = defaultIcon.getName();
icon.setDefaultIcon( true );
}
else
{
icon.setDefaultIcon( false );
}
}
}
public Settings getSettings()
{
return mSettings;
}
public void setSettings( Settings settings )
{
mSettings = settings;
}
public Setting getSetting( String name )
{
return mSettings.getSetting( name );
}
/**
* Returns the current setting, or if the setting doesn't exist
* returns a newly created setting with the specified parameters
*/
public ColorSetting getColorSetting( ColorSettingName name )
{
ColorSetting setting = mSettings.getColorSetting( name );
if( setting == null )
{
setting = new ColorSetting( name );
addSetting( setting );
}
return setting;
}
/**
* Fetches the current setting and applies the parameter(s) to it. Creates
* the setting if it does not exist
*/
public void setColorSetting( ColorSettingName name, Color color )
{
ColorSetting setting = getColorSetting( name );
setting.setColor( color );
broadcastSettingChange( setting );
save();
}
public void resetColorSetting( ColorSettingName name )
{
setColorSetting( name, name.getDefaultColor() );
}
public void resetAllColorSettings()
{
for( ColorSetting color: mSettings.getColorSettings() )
{
resetColorSetting( color.getColorSettingName() );
}
}
/**
* Returns the current setting, or if the setting doesn't exist
* returns a newly created setting with the specified parameters
*/
public FileSetting getFileSetting( String name, String defaultPath )
{
FileSetting setting = mSettings.getFileSetting( name );
if( setting == null )
{
setting = new FileSetting( name, defaultPath );
addSetting( setting );
}
return setting;
}
/**
* Fetches the current setting and applies the parameter(s) to it. Creates
* the setting if it does not exist
*/
public void setFileSetting( String name, String path )
{
FileSetting setting = getFileSetting( name, path );
setting.setPath( path );
broadcastSettingChange( setting );
save();
}
public MapIcon[] getMapIcons()
{
MapIcon[] returnIcons = new MapIcon[mIcons.values().size() ];
return mIcons.values().toArray( returnIcons );
}
private ArrayList<MapIcon> getStandardIcons()
{
ArrayList<MapIcon> icons = new ArrayList<MapIcon>();
MapIcon defaultIcon = new MapIcon( DEFAULT_ICON, "images/no_icon.png", false );
icons.add( defaultIcon );
MapIcon ambulance =
new MapIcon( "Ambulance", "images/ambulance.png", false );
icons.add( ambulance );
MapIcon blockTruck =
new MapIcon( "Block Truck", "images/concrete_block_truck.png", false );
icons.add( blockTruck );
MapIcon cwid = new MapIcon( "CWID", "images/cwid.png", false );
icons.add( cwid );
MapIcon dispatcher =
new MapIcon( "Dispatcher", "images/dispatcher.png", false );
icons.add( dispatcher );
MapIcon dumpTruck =
new MapIcon( "Dump Truck", "images/dump_truck_red.png", false );
icons.add( dumpTruck );
MapIcon garbageTruck =
new MapIcon( "Garbage Truck", "images/garbage_truck.png", false );
icons.add( garbageTruck );
MapIcon loader = new MapIcon( "Loader", "images/loader.png", false );
icons.add( loader );
MapIcon police =
new MapIcon( "Police", "images/police.png", false );
icons.add( police );
MapIcon propaneTruck =
new MapIcon( "Propane Truck", "images/propane_truck.png", false );
icons.add( propaneTruck );
MapIcon schoolBus =
new MapIcon( "School Bus", "images/school_bus.png", false );
icons.add( schoolBus );
MapIcon taxi =
new MapIcon( "Taxi", "images/taxi.png", false );
icons.add( taxi );
MapIcon train =
new MapIcon( "Train", "images/train.png", false );
icons.add( train );
MapIcon transportBus =
new MapIcon( "Transport Bus", "images/opt_bus.png", false );
icons.add( transportBus );
MapIcon van =
new MapIcon( "Van", "images/van.png", false );
icons.add( van );
return icons;
}
/**
* Deletes the map icon
*/
public void deleteMapIcon( MapIcon icon )
{
MapIcon existing = mIcons.get( icon.getName() );
if( existing != null )
{
mIcons.remove( icon.getName() );
mSettings.removeSetting( existing );
broadcastSettingDeleted( existing );
save();
}
}
/**
* Adds the icon, overwriting any existing icon with the same name
* @param icon
*/
public void addMapIcon( MapIcon icon )
{
MapIcon existing = mIcons.get( icon.getName() );
if( existing == null )
{
MapIcon newIcon = new MapIcon( icon.getName(), icon.getPath() );
mIcons.put( newIcon.getName(), newIcon );
addSetting( newIcon );
}
else
{
existing.setName( icon.getName() );
existing.setPath( icon.getPath() );
broadcastSettingChange( existing );
save();
}
}
public void updatMapIcon( MapIcon icon, String newName, String newPath )
{
MapIcon existing = mSettings.getMapIcon( icon.getName() );
if( existing == null )
{
addMapIcon( new MapIcon( newName, newPath ) );
}
else
{
existing.setName( newName );
existing.setPath( newPath );
broadcastSettingChange( existing );
save();
}
}
/**
* Returns named map icon scaled to height, if necessary.
* @param name - name of icon
* @param heigh - height of icon in pixels
*
* @return - ImageIcon or null
*/
public ImageIcon getImageIcon( String name, int height )
{
String mergedName = name + height;
if( mResizedIcons.containsKey( mergedName ) )
{
return mResizedIcons.get( mergedName );
}
MapIcon mapIcon = mIcons.get( name );
if( mapIcon != null )
{
if( mapIcon.getImageIcon().getIconHeight() > height )
{
ImageIcon scaled = getScaledIcon( mapIcon.getImageIcon(), height );
mResizedIcons.put( mergedName, scaled );
return scaled;
}
else
{
mResizedIcons.put( mergedName, mapIcon.getImageIcon() );
}
}
/* Use the default Icon */
String mergedDefault = mCurrentDefaultIconName + height;
if( mResizedIcons.containsKey( mergedDefault ) )
{
return mResizedIcons.get( mergedDefault );
}
else
{
MapIcon defaultIcon = mIcons.get( mCurrentDefaultIconName );
if( defaultIcon != null )
{
if( defaultIcon.getImageIcon().getIconHeight() > height )
{
ImageIcon scaledDefault = getScaledIcon( defaultIcon.getImageIcon(), height );
mResizedIcons.put( mergedDefault, scaledDefault );
return scaledDefault;
}
else
{
mResizedIcons.put( mergedDefault, defaultIcon.getImageIcon() );
return defaultIcon.getImageIcon();
}
}
}
/* Something happened ... the above should always return an icon */
Log.error( "SettingsManager - couldn't return an icon named [" +
name + "] of heigh [" + height + "]" );
return null;
}
private ImageIcon getScaledIcon( ImageIcon original, int height )
{
double scale = (double)original.getIconHeight() / (double)height;
int scaledWidth = (int)( (double)original.getIconWidth() / scale );
Image scaledImage = original.getImage().getScaledInstance( scaledWidth,
height, java.awt.Image.SCALE_SMOOTH );
return new ImageIcon( scaledImage );
}
public MapIcon getMapIcon( String name )
{
if( mIcons.containsKey( name ) )
{
return mIcons.get( name );
}
/* Return the default */
if( mIcons.containsKey( mCurrentDefaultIconName ) )
{
return mIcons.get( mCurrentDefaultIconName );
}
/* Return the real default */
return mIcons.get( DEFAULT_ICON );
}
/**
* Returns the named map icon, or creates it with the default path
* @param name - name of the icon
* @param defaultPath - path to icon image file
* @return - map icon
*/
public MapIcon getMapIcon( String name, String defaultPath )
{
MapIcon icon = mSettings.getMapIcon( name );
if( icon == null )
{
icon = new MapIcon( name, defaultPath );
addSetting( icon );
}
return icon;
}
public DefaultIcon getDefaultIcon()
{
DefaultIcon icon = mSettings.getDefaultIcon();
if( icon == null )
{
icon = new DefaultIcon( DEFAULT_ICON );
addSetting( icon );
}
return icon;
}
public void setDefaultIcon( MapIcon icon )
{
if( mIcons.containsValue( icon ) )
{
MapIcon currentDefault = mIcons.get( mCurrentDefaultIconName );
currentDefault.setDefaultIcon( false );
icon.setDefaultIcon( true );
mCurrentDefaultIconName = icon.getName();
DefaultIcon defaultIconSetting = getDefaultIcon();
defaultIconSetting.setName( icon.getName() );
save();
broadcastSettingChange( defaultIconSetting );
}
}
/**
* Adds the setting and stores the set of settings
* @param setting
*/
private void addSetting( Setting setting )
{
mSettings.addSetting( setting );
save();
broadcastSettingChange( setting );
}
public ArrayList<TunerConfiguration> getTunerConfigurations( TunerType type )
{
ArrayList<TunerConfiguration> configs = getSettings()
.getTunerConfigurations( type );
if( configs.isEmpty() )
{
configs.add( addNewTunerConfiguration( type, "Default" ) );
}
return configs;
}
public void deleteTunerConfiguration( TunerConfiguration config )
{
getSettings().removeTunerConfiguration( config );
save();
}
public TunerConfiguration addNewTunerConfiguration( TunerType type,
String name )
{
switch( type )
{
case ELONICS_E4000:
E4KTunerConfiguration e4KConfig =
new E4KTunerConfiguration( name );
getSettings().addTunerConfiguration( e4KConfig );
save();
return e4KConfig;
case FUNCUBE_DONGLE_PRO:
FCD1TunerConfiguration config =
new FCD1TunerConfiguration( name );
getSettings().addTunerConfiguration( config );
save();
return config;
case FUNCUBE_DONGLE_PRO_PLUS:
FCD2TunerConfiguration configPlus =
new FCD2TunerConfiguration( name );
getSettings().addTunerConfiguration( configPlus );
save();
return configPlus;
case RAFAELMICRO_R820T:
R820TTunerConfiguration r820TConfig =
new R820TTunerConfiguration( name );
getSettings().addTunerConfiguration( r820TConfig );
save();
return r820TConfig;
default:
throw new IllegalArgumentException( "TunerConfiguration"
+ "Directory - tuner type is unrecognized [" +
type.toString() + "]" );
}
}
public TunerConfigurationAssignment getSelectedTunerConfiguration(
TunerType type, String address )
{
return mSettings.getConfigurationAssignment( type, address );
}
public void setSelectedTunerConfiguration( TunerType type,
String address, TunerConfiguration config )
{
mSettings.setConfigurationAssignment( type, address, config.getName() );
save();
}
public MapViewSetting getMapViewSetting( String name, GeoPosition position, int zoom )
{
MapViewSetting loc = mSettings.getMapViewSetting( name );
if( loc != null )
{
return loc;
}
else
{
MapViewSetting newLoc = new MapViewSetting( name, position, zoom );
addSetting( newLoc );
return newLoc;
}
}
public void setMapViewSetting( String name, GeoPosition position, int zoom )
{
MapViewSetting loc = getMapViewSetting( name, position, zoom );
loc.setGeoPosition( position );
loc.setZoom( zoom );
save();
}
public void save()
{
JAXBContext context = null;
SystemProperties props = SystemProperties.getInstance();
Path settingsPath = props.getApplicationFolder( "settings" );
String settingsDefault = props.get( "settings.defaultFilename",
"settings.xml" );
String settingsCurrent = props.get( "settings.currentFilename",
settingsDefault );
Path filePath = settingsPath.resolve( settingsCurrent );
File outputFile = new File( filePath.toString() );
try
{
if( !outputFile.exists() )
{
outputFile.createNewFile();
}
}
catch( Exception e )
{
Log.error( "SettingsManager - couldn't create file to save "
+ "settings [" + filePath.toString() + "]" );
}
OutputStream out = null;
try
{
out = new FileOutputStream( outputFile );
try
{
context = JAXBContext.newInstance( Settings.class );
Marshaller m = context.createMarshaller();
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
m.marshal( mSettings, out );
}
catch ( JAXBException e )
{
Log.error( "SettingsManager - jaxb exception while saving " +
"settings: " + e.getLocalizedMessage() );
}
}
catch ( Exception e )
{
Log.error( "SettingsManager - coulcn't open outputstream to " +
"save settings [" + filePath.toString() + "]" );
}
finally
{
if( out != null )
{
try
{
out.close();
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
}
/**
* Erases current settings and loads settings from the settingsPath filename,
* if it exists.
*/
public void load( Path settingsPath )
{
if( Files.exists( settingsPath ) )
{
Log.info( "SettingsManager - loading settings file [" +
settingsPath.toString() + "]" );
JAXBContext context = null;
InputStream in = null;
try
{
in = new FileInputStream( settingsPath.toString() );
try
{
context = JAXBContext.newInstance( Settings.class );
Unmarshaller m = context.createUnmarshaller();
mSettings = (Settings)m.unmarshal( in );
}
catch ( JAXBException e )
{
Log.error( "SettingsManager - jaxb exception while loading " +
"settings: " + e.getLocalizedMessage() );
e.printStackTrace();
}
}
catch ( Exception e )
{
Log.error( "SettingsManager - coulcn't open inputstream to " +
"load settings [" + settingsPath.toString() + "]" );
}
finally
{
if( in != null )
{
try
{
in.close();
}
catch ( IOException e )
{
Log.error( "SettingsManager - exception while closing " +
"the settings file inputstream reader - " +
e.getLocalizedMessage() );
}
}
}
}
else
{
Log.info( "SettingsManager - settings does not exist [" +
settingsPath.toString() + "]" );
}
if( mSettings == null )
{
mSettings = new Settings();
}
}
public void broadcastSettingChange( Setting setting )
{
Iterator<SettingChangeListener> it = mListeners.iterator();
while( it.hasNext() )
{
SettingChangeListener listener = it.next();
if( listener == null )
{
it.remove();
}
else
{
listener.settingChanged( setting );
}
}
}
public void broadcastSettingDeleted( Setting setting )
{
Iterator<SettingChangeListener> it = mListeners.iterator();
while( it.hasNext() )
{
SettingChangeListener listener = it.next();
if( listener == null )
{
it.remove();
}
else
{
listener.settingDeleted( setting );
}
}
}
public void addListener( SettingChangeListener listener )
{
mListeners.add( listener );
}
public void removeListener( SettingChangeListener listener )
{
mListeners.remove( listener );
}
}