/*******************************************************************************
* SDR Trunk
* 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
* 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
******************************************************************************/
package module.decode;
import gui.editor.Editor;
import gui.editor.EditorValidationException;
import gui.editor.EmptyValidatingEditor;
import gui.editor.ValidatingEditor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import controller.channel.Channel;
import controller.channel.map.ChannelMapModel;
public class DecodeConfigurationEditor extends ValidatingEditor
{
private final static Logger mLog = LoggerFactory.getLogger( DecodeConfigurationEditor.class );
private static final long serialVersionUID = 1L;
private JComboBox mComboDecoders;
private ValidatingEditor mCurrentEditor = new EmptyValidatingEditor<>();
public DecodeConfigurationEditor( final ChannelMapModel channelMapModel )
{
init();
}
private void init()
{
setLayout( new MigLayout( "fill,wrap 3", "[right][left][grow,fill]", "[][grow]" ) );
mComboDecoders = new JComboBox();
mComboDecoders.setEnabled( false );
DefaultComboBoxModel model = new DefaultComboBoxModel();
for( DecoderType type: DecoderType.getPrimaryDecoders() )
{
model.addElement( type );
}
mComboDecoders.setModel( model );
mComboDecoders.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
DecoderType selected = mComboDecoders.getItemAt( mComboDecoders.getSelectedIndex() );
setEditor( DecoderFactory.getEditor( selected ) );
}
});
add( new JLabel( "Decoder:" ) );
add( mComboDecoders );
add( mCurrentEditor );
}
private void setEditor( ValidatingEditor editor )
{
if( mCurrentEditor != editor )
{
remove( mCurrentEditor );
mCurrentEditor = editor;
add( mCurrentEditor );
revalidate();
repaint();
}
}
@Override
public void save()
{
// if( mEditor != null )
// {
// mEditor.save();
// }
//
// mChannel.setDecodeConfiguration( mEditor.getConfig() );
}
@Override
public void setItem( Channel channel )
{
super.setItem( channel );
if( hasItem() )
{
mComboDecoders.setEnabled( true );
mCurrentEditor.setItem( channel );
}
else
{
mComboDecoders.setEnabled( false );
}
}
@Override
public boolean isValid( Editor editor ) throws EditorValidationException
{
return mCurrentEditor.isValid( editor );
}
}