Skip to content

Instantly share code, notes, and snippets.

@kalda341
Created October 26, 2014 01:07
Show Gist options
  • Save kalda341/e66a84d5d61bdf1359aa to your computer and use it in GitHub Desktop.
Save kalda341/e66a84d5d61bdf1359aa to your computer and use it in GitHub Desktop.
package GUI;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import VideoState.DrawTextFilter;
import VideoState.Filter;
import VideoState.Flip;
import VideoState.VideoState;
/**
* JPanel containing all Filters that can be configured by the user.
*/
public class FilterPane extends JPanel {
private VideoState state;
private JPanel _listPane;
private JPanel _newFilterPane;
private JPanel _filterEditorPane;
private CardLayout _filterEditorPaneLayout;
private JList<FilterInstancePane> _filterList;
private DefaultListModel<FilterInstancePane> _filterListModel;
private JComboBox<String> _newFilterChooser;
private String[] _filters = {"Text Overlay", "Flip"};
private JButton _addFilter;
private JButton _removeFilter;
private VideoPlayerInterface _player;
/**
* @param player A videoplayer so that filters can get info about the position
* and time of the video
* @param state The video state to be edited
*/
public FilterPane(VideoState state, VideoPlayerInterface player){
this.state = state;
_player = player;
setLayout(new BorderLayout());
setPreferredSize(new Dimension(800,200));
_listPane = new JPanel();
_listPane.setLayout(new BorderLayout());
_filterListModel = new DefaultListModel<FilterInstancePane>();
_filterList = new JList<FilterInstancePane>(_filterListModel);
_filterList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
_filterList.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
FilterInstancePane filter = _filterList.getSelectedValue();
if (filter != null)
_filterEditorPaneLayout.show(_filterEditorPane, "" + filter.getIdentifier());
}});
_listPane.add(_filterList, BorderLayout.CENTER);
_newFilterPane = new JPanel();
_newFilterChooser = new JComboBox<String>(_filters);
_newFilterPane.add(_newFilterChooser, BorderLayout.PAGE_END);
_addFilter = new JButton("Add filter");
_addFilter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Object selected = _newFilterChooser.getModel().getSelectedItem();
if (selected.equals("Text Overlay")){
newFilter(new DrawTextFilter());
} else if (selected.equals("Flip")){
newFilter(new Flip());
}
}});
_newFilterPane.add(_addFilter, BorderLayout.PAGE_END);
_removeFilter = new JButton("Remove selected filter");
_removeFilter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FilterInstancePane filter = _filterList.getSelectedValue();
if (filter != null){
_filterEditorPaneLayout.removeLayoutComponent(filter);
_filterListModel.removeElement(filter);
state.removeFilter(filter.getModel());
}
}});
_listPane.add(_removeFilter, BorderLayout.PAGE_END);
_listPane.add(_newFilterPane, BorderLayout.PAGE_START);
add(_listPane, BorderLayout.LINE_START);
_filterEditorPane = new JPanel();
_filterEditorPaneLayout = new CardLayout();
_filterEditorPane.setLayout(_filterEditorPaneLayout);
add(_filterEditorPane, BorderLayout.CENTER);
}
/**
* Adds filter to GUI, without adding to state object. Ideal for loading
* filters from state
*/
private void addFilterToGui(Filter filter){
FilterInstancePane fip = null;
if (filter instanceof DrawTextFilter)
fip = new TextOverlayPane((DrawTextFilter) filter, _player);
if (filter instanceof Flip)
fip = new FlipPane((Flip) filter);
addFilterPane(fip);
}
/**
* Addes a new filter to the GUI and state
*/
private void newFilter(Filter filter){
state.addFilter(filter);
addFilterToGui(filter);
}
private void addFilterPane(FilterInstancePane filterPane){
//Add pane with a unique, identifiable name
_filterEditorPane.add(filterPane, "" + filterPane.getIdentifier());
_filterListModel.addElement(filterPane);
}
/**
* Removes all filters from the list. Does not remove from state
*/
private void clearFilterPanes(){
_filterListModel.removeAllElements();
_filterEditorPane.removeAll();
}
public void updateFromState(){
clearFilterPanes();
if (state.get_filters() != null){
for (Filter f : state.get_filters())
addFilterToGui(f);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment