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

318
src/gui/SDRTrunk.java Normal file
View File

@@ -0,0 +1,318 @@
/*******************************************************************************
* 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 gui;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import log.Log;
import net.miginfocom.swing.MigLayout;
import properties.SystemProperties;
import source.tuner.TunerSelectionListener;
import spectrum.SpectralDisplayPanel;
import util.TimeStamp;
import com.jidesoft.swing.JideSplitPane;
import controller.ControllerPanel;
import controller.ResourceManager;
public class SDRTrunk
{
private ResourceManager mResourceManager;
private ControllerPanel mControllerPanel;
private SpectralDisplayPanel mSpectralPanel;
private JFrame mMainGui = new JFrame();
public SDRTrunk()
{
//Setup the application home directory
Path home = getHomePath();
//Load properties file
if( home != null )
{
loadProperties( home );
}
//Start the logger
Log.init();
//Log current properties setting
SystemProperties.getInstance().logCurrentSettings();
/**
* Construct the resource manager now, so that it can use the system
* properties that were just loaded
*/
mResourceManager = new ResourceManager();
//Initialize the GUI
initGUI();
Log.info( "Starting the main gui" );
//Start the gui
EventQueue.invokeLater( new Runnable()
{
public void run()
{
try
{
mMainGui.setVisible( true );
}
catch( Exception e )
{
e.printStackTrace();
}
}
} );
}
/**
* Launch the application.
*/
public static void main( String[] args )
{
new SDRTrunk();
}
/**
* Initialize the contents of the frame.
*/
private void initGUI()
{
mMainGui.setLayout( new MigLayout( "insets 0 0 0 0 ",
"[grow,fill]",
"[grow,fill]") );
mControllerPanel = new ControllerPanel( mResourceManager );
mSpectralPanel = new SpectralDisplayPanel( mResourceManager );
//Register spectral panel to receive tuner selection events
mControllerPanel.getController()
.addListener( (TunerSelectionListener)mSpectralPanel );
//Add spectrum panel to receive channel change events
mResourceManager.getChannelManager().addListener( mSpectralPanel );
//init() the controller to load tuners and playlists
mControllerPanel.getController().init();
//Fire first tuner selection so it is displayed in the spectrum/waterfall
mControllerPanel.getController().fireFirstTunerSelected();
/**
* Setup main JFrame window
*/
mMainGui.setTitle( "SDRTrunk" );
mMainGui.setBounds( 100, 100, 1280, 800 );
mMainGui.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
mSpectralPanel.setPreferredSize( new Dimension( 1280, 400 ) );
JideSplitPane splitPane = new JideSplitPane( JideSplitPane.VERTICAL_SPLIT );
splitPane.setDividerSize( 5 );
splitPane.add( "Denny", mSpectralPanel );
splitPane.add( "Denny2", mControllerPanel );
mMainGui.add( splitPane, "cell 0 0,span,grow");
/**
* Menu items
*/
JMenuBar menuBar = new JMenuBar();
mMainGui.setJMenuBar( menuBar );
JMenu fileMenu = new JMenu( "File" );
menuBar.add( fileMenu );
JMenuItem settingsMenu = new JMenuItem( "Icon Manager" );
settingsMenu.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent arg0 )
{
mControllerPanel.getController().showIconManager();
}
} );
fileMenu.add( settingsMenu );
fileMenu.add( new JSeparator() );
JMenuItem exitMenu = new JMenuItem( "Exit" );
exitMenu.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
System.exit( 0 );
}
}
);
fileMenu.add( exitMenu );
JMenuItem screenCaptureItem = new JMenuItem( "Screen Capture" );
screenCaptureItem.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent arg0 )
{
try
{
Robot robot = new Robot();
final BufferedImage image =
robot.createScreenCapture( mMainGui.getBounds() );
SystemProperties props = SystemProperties.getInstance();
Path capturePath = props.getApplicationFolder( "screen_captures" );
if( !Files.exists( capturePath ) )
{
try
{
Files.createDirectory( capturePath );
}
catch ( IOException e )
{
Log.error( "Couldn't create 'screen_captures' "
+ "subdirectory in the " +
"SDRTrunk application directory" );
}
}
String filename = TimeStamp.getTimeStamp( "_" ) +
"_screen_capture.png";
final Path captureFile = capturePath.resolve( filename );
EventQueue.invokeLater( new Runnable()
{
@Override
public void run()
{
try
{
ImageIO.write( image, "png",
captureFile.toFile() );
}
catch ( IOException e )
{
Log.error( "Couldn't write screen capture to "
+ "file [" + captureFile.toString() + "]" );
}
}} );
}
catch ( AWTException e )
{
Log.error( "Exception while taking screen capture - " +
e.getLocalizedMessage() );
}
}
} );
menuBar.add( screenCaptureItem );
}
/**
* Loads the application properties file from the user's home directory,
* creating the properties file for the first-time, if necessary
*/
private void loadProperties( Path homePath )
{
Path propsPath = homePath.resolve( "SDRTrunk.properties" );
if( !Files.exists( propsPath ) )
{
try
{
Log.info( "SDRTrunk - creating application properties file [" +
propsPath.toAbsolutePath() + "]" );
Files.createFile( propsPath );
}
catch ( IOException e )
{
Log.error( "SDRTrunk - couldn't create application properties "
+ "file [" + propsPath.toAbsolutePath() );
}
}
if( Files.exists( propsPath ) )
{
SystemProperties.getInstance().load( propsPath );
}
else
{
Log.error( "SDRTrunk - couldn't find or recreate the SDRTrunk " +
"application properties file" );
}
}
/**
* Gets (or creates) the SDRTRunk application home directory.
*
* Note: the user can change this setting to allow log files and other
* files to reside elsewhere on the file system.
*/
private Path getHomePath()
{
Path homePath = FileSystems.getDefault()
.getPath( System.getProperty( "user.home" ), "SDRTrunk" );
if( !Files.exists( homePath ) )
{
try
{
Files.createDirectory( homePath );
Log.info( "SDRTrunk - created application home directory [" +
homePath.toString() + "]" );
}
catch ( Exception e )
{
homePath = null;
Log.error( "SDRTrunk: exception while creating SDRTrunk home " +
"directory in the user's home directory" );
}
}
return homePath;
}
}

View File

@@ -0,0 +1,423 @@
/*******************************************************************************
* 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 gui.control;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import log.Log;
import net.miginfocom.swing.MigLayout;
import source.tuner.FrequencyChangeListener;
public class JFrequencyControl extends JPanel implements FrequencyChangeListener
{
private static final long serialVersionUID = 1L;
private ArrayList<FrequencyChangeListener> mListeners =
new ArrayList<FrequencyChangeListener>();
private Color mHighlightColor = Color.YELLOW;
private int mFrequency;
private Cursor mBlankCursor;
private HashMap<Integer,Digit> mDigits = new HashMap<Integer,Digit>();
public JFrequencyControl()
{
init();
}
public JFrequencyControl( int value )
{
this();
mFrequency = value;
}
private void init()
{
setLayout( new MigLayout( "", "[]0[]", "" ) );
Font font = new Font( Font.MONOSPACED, Font.BOLD, 30 );
for( int x = 9; x >= 0; x-- )
{
Digit digit = null;
try
{
digit = new Digit( x );
}
catch ( ParseException e )
{
Log.error( "JFrequencyControl - parse exception "
+ "constructing a digit - " + e.getLocalizedMessage() );
}
if( digit != null )
{
mDigits.put( x, digit );
add( digit );
digit.setFont( font );
if( x == 6 )
{
JLabel period = new JLabel( "." );
add( period );
}
}
}
add( new JLabel( " MHz" ) );
/**
* Create a blank cursor to use when the mouse is over the digits
*/
//Create an empty byte array
byte[] imageByte = new byte[ 0 ];
//Create image for cursor using empty array
Image cursorImage = Toolkit.getDefaultToolkit().createImage( imageByte );
mBlankCursor = Toolkit.getDefaultToolkit()
.createCustomCursor( cursorImage, new Point( 0, 0 ), "cursor" );
revalidate();
}
/**
* Receives a frequency change event invoked by another control. We don't
* rebroadcast this event, just set the control to indicate the new frequency.
*/
@Override
public void frequencyChanged( int frequency, int bandwidth )
{
setFrequency( frequency, false );
}
/**
* Loads the frequency into the display and optionally fires a change event
* to all registered listeners
*/
public void setFrequency( int frequency, boolean fireChangeEvent )
{
mFrequency = frequency;
for( Digit digit: mDigits.values() )
{
digit.setFrequency( frequency, fireChangeEvent );
}
}
public int getFrequency()
{
return mFrequency;
}
private void updateFrequency()
{
int frequency = 0;
for( Digit digit: mDigits.values() )
{
frequency += digit.getFrequency();
}
mFrequency = frequency;
}
private void fireFrequencyChanged()
{
updateFrequency();
Iterator<FrequencyChangeListener> it = mListeners.iterator();
while( it.hasNext() )
{
it.next().frequencyChanged( mFrequency, -1 );
}
}
public void addListener( FrequencyChangeListener listener )
{
mListeners.add( listener );
}
public void removeListener( FrequencyChangeListener listener )
{
mListeners.remove( listener );
}
public class Digit extends JTextField
{
private static final long serialVersionUID = 1L;
private int mPower = 0;
private int mValue = 0;
private Digit( int position ) throws ParseException
{
super( "0" );
mPower = position;
Listener listener = new Listener();
this.addKeyListener( listener );
this.addMouseListener( listener );
this.addMouseWheelListener( listener );
}
/**
* Sets this digit to the value of the column in frequency that corresponds
* to the power (ie column) set for this digit. Optionally, fires a
* value change event to all listeners.
*/
public void setFrequency( int frequency, boolean fireChangeEvent )
{
//Strip the digits higher than this one
int lower = frequency % (int)( Math.pow( 10, mPower + 1) );
//Set the value to int value of dividing by 10 to this power
int value = (int)( lower / (int)( Math.pow( 10, mPower ) ) );
set( value, fireChangeEvent );
}
public int getFrequency()
{
return mValue * (int)Math.pow( 10, mPower );
}
public void increment()
{
increment( true );
}
public void increment( boolean fireChangeEvent )
{
set( mValue + 1, fireChangeEvent );
}
public void decrement()
{
decrement( true );
}
public void decrement( boolean fireChangeEvent )
{
set( mValue - 1, fireChangeEvent );
}
/**
* Convenience wrapper to change amount and fire change event
*/
private void set( int amount )
{
set( amount, true );
}
/**
* Changes the value and optionally fires change event to listeners
*/
private void set( int amount, boolean fireChangeEvent )
{
mValue = amount;
while( mValue < 0 )
{
mValue += 10;
Digit nextHigherDigit = mDigits.get( mPower + 1 );
if( nextHigherDigit != null )
{
nextHigherDigit.decrement( false );
}
}
while( mValue > 9 )
{
mValue -= 10;
Digit nextHigherDigit = mDigits.get( mPower + 1 );
if( nextHigherDigit != null )
{
nextHigherDigit.increment( false );
}
}
setText( String.valueOf( mValue ) );
repaint();
if( fireChangeEvent )
{
fireFrequencyChanged();
}
}
private class Listener implements KeyListener,
MouseListener,
MouseWheelListener
{
@Override
public void keyReleased( KeyEvent e )
{
int key = e.getKeyCode();
switch( key )
{
case KeyEvent.VK_0:
set( 0 );
Digit.this.transferFocus();
break;
case KeyEvent.VK_1:
set( 1 );
Digit.this.transferFocus();
break;
case KeyEvent.VK_2:
set( 2 );
Digit.this.transferFocus();
break;
case KeyEvent.VK_3:
set( 3 );
Digit.this.transferFocus();
break;
case KeyEvent.VK_4:
set( 4 );
Digit.this.transferFocus();
break;
case KeyEvent.VK_5:
set( 5 );
Digit.this.transferFocus();
break;
case KeyEvent.VK_6:
set( 6 );
Digit.this.transferFocus();
break;
case KeyEvent.VK_7:
set( 7 );
Digit.this.transferFocus();
break;
case KeyEvent.VK_8:
set( 8 );
Digit.this.transferFocus();
break;
case KeyEvent.VK_9:
set( 9 );
Digit.this.transferFocus();
break;
case KeyEvent.VK_LEFT:
Digit.this.transferFocusBackward();
break;
case KeyEvent.VK_RIGHT:
Digit.this.transferFocus();
break;
case KeyEvent.VK_UP:
increment();
break;
case KeyEvent.VK_DOWN:
decrement();
break;
case KeyEvent.VK_TAB:
break;
default:
set( mValue );
break;
}
repaint();
}
@Override
public void keyPressed( KeyEvent e ) {}
@Override
public void keyTyped( KeyEvent e ) {}
@Override
public void mouseClicked( MouseEvent e )
{
switch( e.getButton() )
{
case MouseEvent.BUTTON1:
increment();
break;
case MouseEvent.BUTTON2:
break;
case MouseEvent.BUTTON3:
decrement();
break;
}
}
public void mousePressed( MouseEvent e ) {}
public void mouseReleased( MouseEvent e ) {}
@Override
public void mouseEntered( MouseEvent e )
{
Digit.this.setBackground( mHighlightColor );
setCursor( mBlankCursor );
repaint();
}
@Override
public void mouseExited( MouseEvent e )
{
Digit.this.setBackground( Color.WHITE );
setCursor( Cursor.getDefaultCursor() );
repaint();
}
@Override
public void mouseWheelMoved( MouseWheelEvent e )
{
set( mValue - e.getWheelRotation() );
}
}
}
}