Gridarta Editor
NamedFilterConfig.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.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import org.apache.log4j.Category;
26 import org.apache.log4j.Logger;
27 import org.jetbrains.annotations.NotNull;
28 
34 public class NamedFilterConfig extends AbstractFilterConfig<NamedFilter, NamedFilterConfig> {
35 
39  @NotNull
40  private static final Category LOG = Logger.getLogger(NamedFilterConfig.class);
41 
46  // TODO fix potential concurrency issues
47  private boolean inverted;
48 
49  @NotNull
50  private final Map<String, FilterConfig<?, ?>> map = new HashMap<>();
51 
52  @NotNull
54 
55  @Override
56  public void configChanged(@NotNull final FilterConfigChangeType filterConfigChangeType, @NotNull final FilterConfig<?, ?> filterConfig) {
57  // XXX What's with newConfigEvent?
58  // Not used, so why is it created?
59  // This also has no side effect, so is this code wrong and fireEvent(newConfigEvent) should be invoked instead of fireEvent(event)?
60  // If so, why is the argument ignored in the new event construction?
61  //final ConfigEvent<?, ?> newConfigEvent = new ConfigEvent<NamedFilter, NamedFilterConfig>(FilterConfigChangeType.CHANGE, NamedFilterConfig.this);
62  fireEvent(filterConfigChangeType, filterConfig);
63  }
64 
65  };
66 
71  public NamedFilterConfig(@NotNull final NamedFilter owner) {
72  super(owner);
73 
74  final NamedFilterListener namedFilterListener = new NamedFilterListener() {
75 
76  @Override
77  public void nameFilterChanged(@NotNull final NamedFilterChangeType type, @NotNull final String filterName, @NotNull final Filter<?, ?> filter) {
78  switch (type) {
79  case REMOVE:
80  final FilterConfig<?, ?> oldConfig = map.get(filterName);
81  if (oldConfig != null) {
82  oldConfig.removeConfigChangeListener(filterConfigListener);
83  }
84  if (LOG.isDebugEnabled()) {
85  LOG.debug("removing config for " + filterName);
86  }
87  map.remove(filterName);
88  break;
89 
90  case ADD:
91  final FilterConfig<?, ?> newConfig = filter.createConfig();
92  newConfig.addConfigChangeListener(filterConfigListener);
93  if (LOG.isDebugEnabled()) {
94  LOG.debug("adding config for " + filterName);
95  }
96  map.put(filterName, newConfig);
97  break;
98  }
100  }
101 
102  };
103  owner.addFilterListener(namedFilterListener);
104  }
105 
106  @NotNull
107  @Override
109  return this;
110  }
111 
118  public boolean isInverted() {
119  return inverted;
120  }
121 
128  public void setInverted(final boolean inverted) {
129  if (this.inverted == inverted) {
130  return;
131  }
132  this.inverted = inverted;
134  }
135 
141  @NotNull
142  public FilterConfig<?, ?> getConfig(@NotNull final String name) {
143  final FilterConfig<?, ?> filterConfig = map.get(name);
144  if (filterConfig == null) {
145  throw new IllegalArgumentException();
146  }
147  return filterConfig;
148  }
149 
155  public boolean isSubFilterEnabled(@NotNull final String name) {
156  return getConfig(name).isEnabled();
157  }
158 
164  public void setSubFilterEnabled(@NotNull final String name, final boolean enabled) {
165  if (LOG.isDebugEnabled()) {
166  LOG.debug("setSubFilterEnabled(" + name + ", " + enabled + ")");
167  }
168 
169  getConfig(name).setEnabled(enabled);
171  }
172 
173  @Override
174  public void accept(@NotNull final FilterConfigVisitor visitor) {
175  visitor.visit(this);
176  }
177 
182  @NotNull
183  public Map<String, FilterConfig<?, ?>> getEntries() {
184  return Collections.unmodifiableMap(map);
185  }
186 
187 }
Interface for listeners interested in NamedFilter related events.
A Filter that aggregates named filters.
void fireEvent( @NotNull final FilterConfigChangeType filterConfigChangeType, @NotNull final FilterConfig<?, ?> filterConfig)
Notify all listeners that a FilterConfig has happened.
Abstract base class for filter configurations.
Interface for Filters.
Definition: Filter.java:33
void setSubFilterEnabled(@NotNull final String name, final boolean enabled)
Sets whether a sub-filter is enabled.
void setEnabled(boolean enabled)
Enables or disables the filter.
boolean isSubFilterEnabled(@NotNull final String name)
Returns whether a sub-filter is enabled.
final Map< String, FilterConfig<?, ?> > map
void accept(@NotNull final FilterConfigVisitor visitor)
Interface for visitors of filter configs.
boolean isEnabled()
Returns whether the filter is enabled.
static final Category LOG
The Logger for printing log messages.
NamedFilterConfig(@NotNull final NamedFilter owner)
Creates a new instance.
boolean inverted
Whether the filter should match if all sub-filters match (.
FilterConfig<?, ?> getConfig(@NotNull final String name)
Returns the FilterConfig for a sub-filter.
Map< String, FilterConfig<?, ?> > getEntries()
Returns a Map containing all configurations of sub-filters.
void addConfigChangeListener(FilterConfigListener listener)
Adds a FilterConfigListener to be notified about changes.
void setInverted(final boolean inverted)
Sets whether the filter should match if all sub-filters match or if at least one sub-filter does not ...
boolean isInverted()
Returns whether the filter should match if all sub-filters match or if at least one sub-filter does n...
void removeConfigChangeListener(FilterConfigListener listener)
Removes a FilterConfigListener to be notified about changes.
Enumeration of event types of NamedFilterEvent.
Interface for listeners interested in FilterConfig related changes.