WIP - updates to the demodulator viewer app and the recently used files

menu.
This commit is contained in:
Denny
2018-02-13 12:31:50 +00:00
parent 956bf2d76a
commit 80c5ad1bc8
5 changed files with 204 additions and 117 deletions

View File

@@ -115,7 +115,7 @@ public class ComplexPhaseLineChart extends LineChart implements Listener<Complex
previous.add(ANGLE_OFFSET_45_DEGREES);
sample.multiply(previous.conjugate());
// sample.normalize();
mPhaseValues.get(x - 10).setYValue(sample.angle());
mPhaseValues.get(x - 9).setYValue(sample.angle());
}
}
}

View File

@@ -37,7 +37,7 @@ public class DemodulatorViewerFX extends Application
{
private Stage mStage;
private MenuBar mMenuBar;
private RecentFilesMenuItem mRecentFilesMenuItem;
private RecentFilesMenu mRecentFilesMenu;
private ViewerDesktop mViewerDesktop;
@Override
@@ -83,7 +83,7 @@ public class DemodulatorViewerFX extends Application
fileChooser.setTitle("Open an I/Q recording file");
File file = fileChooser.showOpenDialog(mStage);
getViewerDesktop().load(file);
getRecentFilesMenuItem().add(file);
getRecentFilesMenu().add(file);
}
});
@@ -107,9 +107,7 @@ public class DemodulatorViewerFX extends Application
}
});
mRecentFilesMenuItem = new RecentFilesMenuItem("demodulator.viewer", 5);
menuFile.getItems().addAll(menuItemOpen, getRecentFilesMenuItem(), menuItemClose, new SeparatorMenuItem(),
menuFile.getItems().addAll(menuItemOpen, getRecentFilesMenu(), menuItemClose, new SeparatorMenuItem(),
menuItemExit);
mMenuBar.getMenus().addAll(menuFile);
@@ -118,12 +116,12 @@ public class DemodulatorViewerFX extends Application
return mMenuBar;
}
private RecentFilesMenuItem getRecentFilesMenuItem()
private RecentFilesMenu getRecentFilesMenu()
{
if(mRecentFilesMenuItem == null)
if(mRecentFilesMenu == null)
{
mRecentFilesMenuItem = new RecentFilesMenuItem("demodulator.viewer", 5);
mRecentFilesMenuItem.setFileSelectionListener(new RecentFilesMenuItem.IFileSelectionListener()
mRecentFilesMenu = new RecentFilesMenu("Recent","demodulator.viewer", 5);
mRecentFilesMenu.setFileSelectionListener(new RecentFilesMenu.IFileSelectionListener()
{
@Override
public void fileSelected(File file)
@@ -133,7 +131,7 @@ public class DemodulatorViewerFX extends Application
});
}
return mRecentFilesMenuItem;
return mRecentFilesMenu;
}
public static void main(String[] args)

View File

@@ -67,6 +67,7 @@ public class PlaybackController extends HBox implements IFrameLocationListener,
public void load(File file)
{
mLog.debug("loading:" + file.getAbsolutePath());
if(file != null && file.isFile())
{
try
@@ -112,6 +113,8 @@ public class PlaybackController extends HBox implements IFrameLocationListener,
mLog.error("Error closing file", ioe);
}
}
setControlsEnabled(false);
}
/**

View File

@@ -0,0 +1,192 @@
package io.github.dsheirer.instrument.gui.viewer;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import java.io.File;
import java.util.prefs.Preferences;
public class RecentFilesMenu extends Menu implements EventHandler<ActionEvent>
{
private Preferences mPreferences = Preferences.userNodeForPackage(RecentFilesMenu.class);
private IFileSelectionListener mFileSelectionListener;
private String mIdentifier;
private int mSize;
/**
* Recently used files menu. Uses java Preferences to store recently accessed files and provides access to those
* files via child menu items to this menu.
*
* @param label to use for this menu.
* @param uniqueIdentifier to use in addition to the class path of this class to uniquely identify recently accessed
* files in a java Preferences data store.
* @param size of the maximum number of recent files to present to the user
*/
public RecentFilesMenu(String label, String uniqueIdentifier, int size)
{
super(label);
mIdentifier = uniqueIdentifier;
mSize = size;
load();
}
/**
* Registers the listener to receive file selection events when the user selects a recently used file.
* @param listener to receive file selection events
*/
public void setFileSelectionListener(IFileSelectionListener listener)
{
mFileSelectionListener = listener;
}
/**
* Adds the file to the recently accessed files list. If this file is already included in the recently files list,
* it will be promoted to the first menu item and all other recently used files will be demoted.
*
* @param file to add to the recently used files list.
*/
public void add(File file)
{
if(file != null)
{
String filePath = file.getAbsolutePath();
int index = mSize - 1;
//Check to see if this file is already in the recent files list
for(int x = mSize - 1; x >= 0; x--)
{
String existingItem = mPreferences.get(getKey(x), null);
if(existingItem != null && existingItem.equals(filePath))
{
index = x;
break;
}
}
//Shift all recent files down to make room for the newly added file at the top of the list
while(index > 0)
{
String newerItem = mPreferences.get(getKey(index - 1), null);
if(newerItem != null)
{
mPreferences.put(getKey(index), newerItem);
}
index--;
}
mPreferences.put(getKey(0), file.getAbsolutePath());
}
load();
}
/**
* Creates a unique key value for each numbered recently used file item in the preferences.
*
* @param index of recent file item.
* @return unique key to use with the preferences instance.
*/
private String getKey(int index)
{
return mIdentifier + ".recent.file." + index;
}
/**
* Clears and (re)loads all child recent file menu items.
*/
private void load()
{
getItems().clear();
for(int x = 0; x < mSize; x++)
{
String path = mPreferences.get(getKey(x), null);
if(path != null)
{
File file = new File(path);
if(file.isFile())
{
MenuItem menuItem = new MenuItem(file.getName());
menuItem.setUserData(file);
menuItem.setOnAction(this);
getItems().add(menuItem);
}
}
}
if(getItems().size() > 0)
{
setDisable(false);
getItems().add(new SeparatorMenuItem());
MenuItem clearMenuItem = new MenuItem("Clear History");
clearMenuItem.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
clearHistory();
}
});
getItems().add(clearMenuItem);
}
else
{
setDisable(true);
}
}
/**
* Clears the recently used files history and removes all items from the preferences instance
*/
private void clearHistory()
{
for(int x = 0; x < mSize; x++)
{
mPreferences.remove(getKey(x));
}
load();
}
/**
* Event handler for all recent file child menu items. Extracts the selected file stored as the user data object
* in each of the menu items and notifies the listener that the recent file has been selected.
*
* @param event from a recent file menu item child of this menu.
*/
@Override
public void handle(ActionEvent event)
{
if(mFileSelectionListener != null)
{
if(event.getSource() instanceof MenuItem)
{
Object fileObject = ((MenuItem)event.getSource()).getUserData();
if(fileObject instanceof File)
{
mFileSelectionListener.fileSelected((File)fileObject);
}
}
}
}
/**
* Listener interface for file selection events.
*/
public interface IFileSelectionListener
{
void fileSelected(File file);
}
}

View File

@@ -1,106 +0,0 @@
package io.github.dsheirer.instrument.gui.viewer;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.prefs.Preferences;
public class RecentFilesMenuItem extends Menu implements EventHandler<ActionEvent>
{
private final static Logger mLog = LoggerFactory.getLogger(RecentFilesMenuItem.class);
private Preferences mPreferences = Preferences.userNodeForPackage(RecentFilesMenuItem.class);
private IFileSelectionListener mFileSelectionListener;
private String mIdentifier;
private int mSize;
public RecentFilesMenuItem(String identifier, int size)
{
super("Recent");
mIdentifier = identifier;
mSize = size;
load();
}
public void setFileSelectionListener(IFileSelectionListener listener)
{
mFileSelectionListener = listener;
}
public void add(File file)
{
if(file != null)
{
int index = mSize - 1;
while(index > 0)
{
String newerItem = mPreferences.get(getKey(index - 1), null);
if(newerItem != null)
{
mPreferences.put(getKey(index), newerItem);
}
index--;
}
mPreferences.put(getKey(0), file.getAbsolutePath());
}
load();
}
private String getKey(int index)
{
return mIdentifier + ".recent." + index;
}
private void load()
{
getItems().clear();
for(int x = 0; x < mSize; x++)
{
String path = mPreferences.get(getKey(x), null);
if(path != null)
{
File file = new File(path);
if(file.isFile())
{
MenuItem menuItem = new MenuItem(file.getName());
menuItem.setUserData(file);
menuItem.setOnAction(this);
getItems().add(menuItem);
}
}
}
}
@Override
public void handle(ActionEvent event)
{
if(mFileSelectionListener != null)
{
Object fileObject = ((MenuItem)event.getSource()).getUserData();
if(fileObject instanceof File)
{
mFileSelectionListener.fileSelected((File)fileObject);
}
}
}
public interface IFileSelectionListener
{
void fileSelected(File file);
}
}