MCPcopy Create free account
hub / github.com/PCGen/pcgen / FilterButton

Class FilterButton

code/src/java/pcgen/gui2/filter/FilterButton.java:38–94  ·  view source on GitHub ↗

This class represents a simple filter represented as a toggle button. When the button is selected (i.e. pressed) the filter assigned to this button will become active. When deselected the filter will become inactive. Selecting and deselecting the button will trigger its FilterHandler to refilter its

Source from the content-addressed store, hash-verified

36 * to refilter its contents.
37 */
38public class FilterButton<C, E> extends JToggleButton implements DisplayableFilter<C, E>, ActionListener
39{
40
41 private FilterHandler filterHandler;
42 private Filter<C, E> filter;
43 private final PropertyContext filterContext;
44
45 public FilterButton(String prefKey)
46 {
47 this(prefKey, false);
48 }
49
50 public FilterButton(String prefKey, boolean defaultSelectedState)
51 {
52 if (StringUtils.isEmpty(prefKey))
53 {
54 throw new NullPointerException("prefKey cannot be null");
55 }
56 addActionListener(this);
57 PropertyContext baseContext = UIPropertyContext.createContext("filterPrefs");
58 filterContext = baseContext.createChildContext(prefKey);
59 setSelected(filterContext.initBoolean("active", defaultSelectedState));
60 }
61
62 @Override
63 public Component getFilterComponent()
64 {
65 return this;
66 }
67
68 @Override
69 public void setFilterHandler(FilterHandler handler)
70 {
71 this.filterHandler = handler;
72 }
73
74 public void setFilter(Filter<C, E> filter)
75 {
76 this.filter = filter;
77 }
78
79 @Override
80 public boolean accept(C context, E element)
81 {
82 //if this button is not selected treat it as if
83 //this filter always accepts
84 return !isEnabled() || !isSelected() || filter.accept(context, element);
85 }
86
87 @Override
88 public void actionPerformed(ActionEvent e)
89 {
90 filterHandler.refilter();
91 filterContext.setBoolean("active", isSelected());
92 }
93
94}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected