Gridarta Editor
FilterComponent.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.gui.filter;
21 
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.util.HashMap;
25 import java.util.Map;
26 import javax.swing.AbstractButton;
27 import javax.swing.JCheckBoxMenuItem;
28 import javax.swing.JComponent;
29 import javax.swing.JSeparator;
30 import javax.swing.event.ChangeEvent;
31 import javax.swing.event.ChangeListener;
35 import net.sf.japi.swing.action.ActionBuilder;
36 import net.sf.japi.swing.action.ActionBuilderFactory;
37 import org.jetbrains.annotations.NotNull;
38 
45 public class FilterComponent {
46 
50  @NotNull
51  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
52 
57  @NotNull
58  private final Map<String, JComponent> content = new HashMap<>();
59 
64  @NotNull
65  private final Container component;
66 
70  @NotNull
71  private final NamedFilterConfig config;
72 
77  @NotNull
78  private final AbstractButton active = new JCheckBoxMenuItem(ActionBuilderUtils.getString(ACTION_BUILDER, "analyzeActive.text"));
79 
84  @NotNull
85  private final AbstractButton inverted = new JCheckBoxMenuItem(ActionBuilderUtils.getString(ACTION_BUILDER, "analyzeInvert.text"));
86 
93  public FilterComponent(@NotNull final Container component, @NotNull final NamedFilterConfig config) {
94  this.component = component;
95  this.config = config;
96  active.getModel().setSelected(this.config.isEnabled());
97  active.addChangeListener(new ChangeListener() {
98 
99  @Override
100  public void stateChanged(@NotNull final ChangeEvent e) {
101  FilterComponent.this.config.setEnabled(active.getModel().isSelected());
102  }
103  });
104  component.add(active);
105  inverted.getModel().setSelected(this.config.isInverted());
106  inverted.addChangeListener(new ChangeListener() {
107 
108  @Override
109  public void stateChanged(@NotNull final ChangeEvent e) {
110  FilterComponent.this.config.setInverted(inverted.getModel().isSelected());
111  }
112  });
113  component.add(inverted);
114  component.add(new JSeparator());
115  }
116 
117  public void addFilter(@NotNull final String name, @NotNull final FilterConfig<?, ?> config) {
118  if (content.containsKey(name)) {
119  return;
120  }
121 
122  final AbstractButton entry = new MenuItemCreator(config).getMenuItem();
123  entry.setVisible(true);
124  entry.setText(name);
125  content.put(name, entry);
126  component.add(entry);
127  }
128 
129  public void removeFilter(@NotNull final String name) {
130  if (!content.containsKey(name)) {
131  return;
132  }
133 
134  final Component c = content.get(name);
135  content.remove(name);
136  component.remove(c);
137  }
138 
139 }
final Map< String, JComponent > content
Maps sub-filter name to corresponding menu item.
final NamedFilterConfig config
The filter configuration that is shown in component.
Updates a Component to reflect the current state of a NamedFilterConfig.
static final ActionBuilder ACTION_BUILDER
The action builder.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
Creates menu items for net.sf.gridarta.model.filter.Filter instances.
FilterComponent(@NotNull final Container component, @NotNull final NamedFilterConfig config)
Createsa a new instance.
Utility class for ActionBuilder related functions.
JMenuItem getMenuItem()
Returns the menu item.
void removeFilter(@NotNull final String name)
final AbstractButton inverted
The checkbox menu items which shows the "inverted" state of config.
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 ...
final AbstractButton active
The checkbox menu items which shows the "active" state of config.
Interface for filter configurations.
final Container component
The components which shows the state of config.
void addFilter(@NotNull final String name, @NotNull final FilterConfig<?, ?> config)