Gridarta Editor
NamedFilter.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.filter;
21 
22 import java.util.LinkedHashMap;
23 import java.util.Map;
27 import org.apache.log4j.Category;
28 import org.apache.log4j.Logger;
29 import org.jetbrains.annotations.NotNull;
30 
39 public class NamedFilter implements Filter<NamedFilter, NamedFilterConfig> {
40 
44  @NotNull
45  private static final Category LOG = Logger.getLogger(NamedFilter.class);
46 
50  @NotNull
51  private final Map<String, Filter<?, ?>> subFilters;
52 
57  @NotNull
59 
64  public NamedFilter(@NotNull final Iterable<NamedGameObjectMatcher> matchers) {
65  subFilters = new LinkedHashMap<>();
66  for (final NamedGameObjectMatcher matcher : matchers) { // FIXME: use localized name; FIXME: sort by name
67  subFilters.put(matcher.getName(), new NamedGameObjectMatcherFilter(matcher));
68  }
69  }
70 
75  public void resetConfig(@NotNull final NamedFilterConfig config) {
76  for (final String name : subFilters.keySet()) {
77  config.setSubFilterEnabled(name, false);
78  }
79  }
80 
81  @Override
82  public boolean match(@NotNull final NamedFilterConfig config, @NotNull final GameObject<?, ?, ?> gameObject) {
83  try {
84  if (LOG.isDebugEnabled()) {
85  LOG.debug("match called on " + gameObject.getArchetype().getArchetypeName());
86  }
87  for (final String name : subFilters.keySet()) {
88  if (LOG.isDebugEnabled()) {
89  LOG.debug("checking if filter " + name + " is enabled()");
90  }
91  if (config.isSubFilterEnabled(name)) {
92  if (LOG.isDebugEnabled()) {
93  LOG.debug("enabled!");
94  }
95  if (config.getConfig(name).match(gameObject)) {
96  if (LOG.isDebugEnabled()) {
97  LOG.debug("and matched!");
98  }
99  return !config.isInverted();
100  }
101  }
102  }
103  if (LOG.isDebugEnabled()) {
104  LOG.debug("finished scanning sub filters");
105  }
106  return config.isInverted();
107  } catch (final Exception e) {
108  return false;
109  }
110  }
111 
112  @Override
113  public boolean reset(@NotNull final NamedFilterConfig config) {
114  try {
115  boolean didMatch = false;
116  for (final String name : subFilters.keySet()) {
117  final FilterConfig<?, ?> filterConfig = config.getConfig(name);
118  if (filterConfig.reset()) {
119  didMatch = true;
120  }
121  }
122  return didMatch ^ config.isInverted();
123  } catch (final Exception e) {
124  return false;
125  }
126  }
127 
128  @Override
129  public boolean hasGlobalMatch(@NotNull final NamedFilterConfig config) {
130  return false;
131  }
132 
133  @NotNull
134  @Override
136  final NamedFilterConfig filterConfig = new NamedFilterConfig(this);
137  for (final Map.Entry<String, Filter<?, ?>> filterEntry : subFilters.entrySet()) {
138  fireEvent(NamedFilterChangeType.ADD, filterEntry.getKey(), filterEntry.getValue());
139  }
140  resetConfig(filterConfig);
141  return filterConfig;
142  }
143 
149  public void addFilter(@NotNull final String name, @NotNull final Filter<?, ?> filter) {
150  if (subFilters.containsKey(name)) {
151  return;
152  }
153 
154  subFilters.put(name, filter);
155  fireEvent(NamedFilterChangeType.ADD, name, filter);
156  }
157 
162  public void removeFilter(@NotNull final String name) {
163  final Filter<?, ?> filter = subFilters.remove(name);
164  if (filter == null) {
165  return;
166  }
167 
168  fireEvent(NamedFilterChangeType.REMOVE, name, filter);
169  }
170 
177  private void fireEvent(@NotNull final NamedFilterChangeType type, @NotNull final String filterName, @NotNull final Filter<?, ?> filter) {
178  for (final NamedFilterListener listener : listenerList.getListeners()) {
179  listener.nameFilterChanged(type, filterName, filter);
180  }
181  }
182 
187  public void addFilterListener(@NotNull final NamedFilterListener listener) {
188  listenerList.add(listener);
189  }
190 
195  public void removeFilterListener(@NotNull final NamedFilterListener listener) {
196  listenerList.remove(listener);
197  }
198 
205  public boolean canShow(@NotNull final GameObject<?, ?, ?> gameObject, @NotNull final NamedFilterConfig filterOutConfig) {
206  reset(filterOutConfig);
207  if (hasGlobalMatch(filterOutConfig)) {
208  match(filterOutConfig, gameObject);
209  return !reset(filterOutConfig);
210  }
211 
212  return !match(filterOutConfig, gameObject);
213  }
214 
215 }
Interface for listeners interested in NamedFilter related events.
A Filter that aggregates named filters.
T [] getListeners()
Returns an array of all the listeners.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
Interface for Filters.
Definition: Filter.java:33
boolean reset()
Tells the filter we have finished with current map square and, perhaps, we are jumping on next one...
void removeFilter(@NotNull final String name)
Removes a sub-filter.
A Filter which filters according to a net.sf.gridarta.model.match.NamedGameObjectMatcher.
boolean reset(@NotNull final NamedFilterConfig config)
void fireEvent(@NotNull final NamedFilterChangeType type, @NotNull final String filterName, @NotNull final Filter<?, ?> filter)
Notifies all listeners about a change.
void addFilter(@NotNull final String name, @NotNull final Filter<?, ?> filter)
Adds a sub-Filter.
Decorates an arbitrary GameObjectMatcher with a localized name that is suitable for the user interfac...
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
NamedFilter(@NotNull final Iterable< NamedGameObjectMatcher > matchers)
Creates a new instance.
boolean match(@NotNull final NamedFilterConfig config, @NotNull final GameObject<?, ?, ?> gameObject)
void addFilterListener(@NotNull final NamedFilterListener listener)
Adds a NamedFilterListener to be notified about changes.
void remove(@NotNull final T listener)
Removes a listener.
void removeFilterListener(@NotNull final NamedFilterListener listener)
Removes a NamedFilterListener to be notified about changes.
GameObjects are the objects based on Archetypes found on maps.
void add(@NotNull final T listener)
Adds a listener.
boolean hasGlobalMatch(@NotNull final NamedFilterConfig config)
final Map< String, Filter<?, ?> > subFilters
The sub-filters.
Type-safe version of EventListenerList.
static final Category LOG
The Logger for printing log messages.
final EventListenerList2< NamedFilterListener > listenerList
The NamedFilterListeners to notify about changes.
boolean canShow(@NotNull final GameObject<?, ?, ?> gameObject, @NotNull final NamedFilterConfig filterOutConfig)
Returns whether this filter matches a GameObject.
void resetConfig(@NotNull final NamedFilterConfig config)
Disables all sub-filters.
Enumeration of event types of NamedFilterEvent.