wip: finished alias, alias ID and alias action editing framework.

This commit is contained in:
denny
2016-03-04 09:33:46 -05:00
parent 2c3dbe1b22
commit 00d2b43c2a
68 changed files with 1472 additions and 647 deletions

View File

@@ -1,6 +1,6 @@
/*******************************************************************************
* SDR Trunk
* Copyright (C) 2014 Dennis Sheirer
* Copyright (C) 2014-2016 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
@@ -17,9 +17,16 @@
******************************************************************************/
package alias.action.script;
import gui.editor.DocumentListenerEditor;
import gui.editor.Editor;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JButton;
@@ -27,9 +34,7 @@ import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
@@ -37,43 +42,59 @@ import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import net.miginfocom.swing.MigLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import alias.action.AliasAction;
import alias.action.RecurringAction.Interval;
import alias.action.clip.ClipActionEditor;
public class ScriptActionEditor extends JPanel implements ActionListener
public class ScriptActionEditor extends DocumentListenerEditor<AliasAction>
{
private static final long serialVersionUID = 1L;
private final static Logger mLog =
LoggerFactory.getLogger( ScriptActionEditor.class );
private ScriptActionNode mScriptActionNode;
private static final String PLEASE_SELECT_A_FILE = "Please select a file";
private static final String HELP_TEXT =
"<html><h3>Script Action</h3>"
+ "This action will run a script according to the interval<br>"
+ "that you select:<br><br>"
+ "<b>Once:</b> run the first time the alias is active and<br>"
+ "never again<br><br>"
+ "<b>Once, Reset After Delay:</b> run once and suppress<br>"
+ "subsequent runs for the specified period in seconds.<br>"
+ "After the reset period, run again when the alias is active.<br><br>"
+ "<b>Until Dismissed:</b> run every period seconds until you<br>"
+ "click OK on the dialog that appears. Alerting is suppressed<br>"
+ "for 15 seconds after you click OK.<br>"
+ "</html>";
private JComboBox<Interval> mComboInterval;
private JSpinner mSpinnerPeriod;
private JSlider mPeriodSlider;
private JLabel mPeriodSliderLabel;
private JTextField mTextFilePath;
public ScriptActionEditor( ScriptActionNode clipActionNode )
public ScriptActionEditor( AliasAction aliasAction )
{
mScriptActionNode = clipActionNode;
initGUI();
init();
setItem( aliasAction );
}
private void initGUI()
public ScriptAction getScriptAction()
{
setLayout( new MigLayout( "fill,wrap 2", "[right][left]", "[][][][][][grow]" ) );
add( new JLabel( "Action: Script" ), "span,align center" );
if( getItem() instanceof ScriptAction )
{
return (ScriptAction)getItem();
}
add( new JLabel( "Interval:" ) );
return null;
}
private void init()
{
setLayout( new MigLayout( "fill,wrap 2", "[right][left]", "[][][][][]" ) );
add( new JLabel( "Run Interval:" ) );
mComboInterval = new JComboBox<Interval>( Interval.values() );
mComboInterval.setSelectedItem( mScriptActionNode.getScriptAction().getInterval() );
mComboInterval.setToolTipText( HELP_TEXT );
mComboInterval.addActionListener( new ActionListener()
{
@Override
@@ -84,37 +105,51 @@ public class ScriptActionEditor extends JPanel implements ActionListener
if( selected != null )
{
/* Enable/disable period spinner based on selection */
mSpinnerPeriod.setEnabled( selected != Interval.ONCE );
/* Enable/disable period slider based on selection */
boolean enabled = selected != Interval.ONCE;
mScriptActionNode.getScriptAction().setInterval( selected );
mPeriodSlider.setEnabled( enabled );
mPeriodSliderLabel.setEnabled( enabled );
}
setModified( true );
}
});
add( mComboInterval, "wrap" );
add( mComboInterval );
final SpinnerModel model = new SpinnerNumberModel(
mScriptActionNode.getScriptAction().getPeriod(), 1, 30, 1 );
final SpinnerModel model = new SpinnerNumberModel( 1, 1, 30, 1 );
model.addChangeListener( new ChangeListener()
{
@Override
public void stateChanged( ChangeEvent e )
{
mScriptActionNode.getScriptAction().setPeriod( (int)model.getValue() );
setModified( true );
}
} );
mSpinnerPeriod = new JSpinner( model );
if( mScriptActionNode.getScriptAction().getInterval() == Interval.ONCE )
mPeriodSlider = new JSlider( JSlider.HORIZONTAL, 1, 60, 1 );
mPeriodSlider.setMajorTickSpacing( 10 );
mPeriodSlider.setMinorTickSpacing( 2 );
mPeriodSlider.setPaintTicks( true );
mPeriodSlider.setLabelTable( mPeriodSlider.createStandardLabels( 10, 10 ) );
mPeriodSlider.setPaintLabels( true );
mPeriodSlider.addChangeListener( new ChangeListener()
{
mSpinnerPeriod.setEnabled( false );
}
@Override
public void stateChanged( ChangeEvent e )
{
mPeriodSliderLabel.setText( "Period: " + mPeriodSlider.getValue() );
setModified( true );
}
} );
mPeriodSlider.setToolTipText( HELP_TEXT );
add( new JLabel( "Period:" ) );
add( mSpinnerPeriod, "wrap" );
mPeriodSliderLabel = new JLabel( "Period: " + mPeriodSlider.getValue() + " " );
add( mPeriodSliderLabel );
add( mPeriodSlider, "wrap,grow" );
mTextFilePath = new JTextField( PLEASE_SELECT_A_FILE );
add( mTextFilePath, "growx,span" );
JButton fileButton = new JButton( "File" );
@@ -131,13 +166,14 @@ public class ScriptActionEditor extends JPanel implements ActionListener
{
File file = chooser.getSelectedFile();
mScriptActionNode.getScriptAction().setScript( file.getAbsolutePath() );
mTextFilePath.setText( file.getAbsolutePath() );
setModified( true );
}
}
} );
add( fileButton );
add( fileButton, "grow" );
JButton testButton = new JButton( "Test" );
testButton.addActionListener( new ActionListener()
@@ -145,74 +181,115 @@ public class ScriptActionEditor extends JPanel implements ActionListener
@Override
public void actionPerformed( ActionEvent actionEvent )
{
String script = mScriptActionNode.getScriptAction().getScript();
String script = mTextFilePath.getText();
if( script == null || script.trim().isEmpty() )
{
JOptionPane.showMessageDialog( ScriptActionEditor.this,
"Please select a script file", "Please select file",
"Please select a script file", PLEASE_SELECT_A_FILE,
JOptionPane.ERROR_MESSAGE );
}
EventQueue.invokeLater( new Runnable()
else
{
@Override
public void run()
if( isModified() )
{
try
int option = JOptionPane.showConfirmDialog(
ScriptActionEditor.this,
"Settings have changed. Do you want to save these changes?",
"Save Changes?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE );
if( option == JOptionPane.YES_OPTION )
{
mScriptActionNode.getScriptAction().play();
}
catch( Exception e )
{
JOptionPane.showMessageDialog( ScriptActionEditor.this,
"Couldn't run script [" + e.getMessage() + "]",
"Error", JOptionPane.ERROR_MESSAGE );
save();
}
}
} );
new Thread( new Runnable()
{
@Override
public void run()
{
try
{
getScriptAction().play();
}
catch( Exception e1 )
{
JOptionPane.showMessageDialog( ScriptActionEditor.this,
e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE );
}
}
}).start();
}
}
} );
add( testButton, "wrap" );
add( testButton, "grow,wrap" );
mTextFilePath = new JTextField( mScriptActionNode.getScriptAction().getScript() );
add( mTextFilePath, "growx,span" );
JLabel example = new JLabel( "Help ..." );
example.setForeground( Color.BLUE.brighter() );
example.setCursor( new Cursor( Cursor.HAND_CURSOR ) );
example.addMouseListener( new MouseAdapter()
{
@Override
public void mouseClicked( MouseEvent e )
{
JOptionPane.showMessageDialog( ScriptActionEditor.this,
HELP_TEXT, "Help", JOptionPane.INFORMATION_MESSAGE );
}
} );
add( example );
}
@Override
public void setItem( AliasAction item )
{
super.setItem( item );
if( hasItem() )
{
ScriptAction script = getScriptAction();
StringBuilder sb = new StringBuilder();
sb.append( "This action will run the script according to the interval that you select.\n\n" );
sb.append( "Once - Run script the first time the alias is active and never again.\n\n" );
sb.append( "Once, Reset After Delay - Run once and suppress subsequent runs for the specified period in seconds. After the reset period, it will play again when the alias is active.\n\n" );
sb.append( "Until Dismissed - Run script every period seconds until you click OK on the dialog that appears. Alerting is suppressed for 15 seconds after you click OK." );
JTextArea description = new JTextArea( sb.toString() );
description.setLineWrap( true );
description.setBackground( getBackground() );
add( description, "growx,span" );
JButton btnSave = new JButton( "Save" );
btnSave.addActionListener( ScriptActionEditor.this );
add( btnSave, "growx,push" );
Interval interval = script.getInterval();
mComboInterval.setSelectedItem( interval );
JButton btnReset = new JButton( "Reset" );
btnReset.addActionListener( ScriptActionEditor.this );
add( btnReset, "growx,push" );
boolean enabled = interval != Interval.ONCE;
mPeriodSliderLabel.setEnabled( enabled );
mPeriodSlider.setEnabled( enabled );
mPeriodSlider.setValue( script.getPeriod() );
String filepath = script.getScript();
if( filepath != null && !filepath.isEmpty() )
{
mTextFilePath.setText( filepath );
}
else
{
mTextFilePath.setText( PLEASE_SELECT_A_FILE );
}
}
setModified( false );
}
@Override
public void actionPerformed( ActionEvent e )
{
String command = e.getActionCommand();
if( command.contentEquals( "Save" ) )
public void save()
{
if( hasItem() && isModified() )
{
mScriptActionNode.save();
mScriptActionNode.show();
ScriptAction script = getScriptAction();
script.setInterval( (Interval)mComboInterval.getSelectedItem() );
script.setPeriod( mPeriodSlider.getValue() );
script.setScript( mTextFilePath.getText() );
}
mScriptActionNode.refresh();
}
setModified( false );
}
}