Gridarta Editor
ViewGameObjectMatcherManager.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.match;
21 
22 import java.awt.event.ActionEvent;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import javax.swing.AbstractAction;
27 import javax.swing.Action;
28 import org.jetbrains.annotations.NotNull;
29 
35 
40  @NotNull
42 
46  @NotNull
47  private final List<GameObjectMatcherToggleAction> actions = new ArrayList<>();
48 
52  @NotNull
53  private final Action resetAction = new ResetAction(Collections.unmodifiableList(actions));
54 
60  public ViewGameObjectMatcherManager(@NotNull final MutableOrGameObjectMatcher mutableOrGameObjectMatcher) {
61  this.mutableOrGameObjectMatcher = mutableOrGameObjectMatcher;
62  }
63 
69  @NotNull
70  public Action addArchObjectMatcher(@NotNull final GameObjectMatcher gameObjectMatcher) {
71  final GameObjectMatcherToggleAction action = new GameObjectMatcherToggleAction(gameObjectMatcher, mutableOrGameObjectMatcher);
72  actions.add(action);
73  return action;
74  }
75 
80  @NotNull
81  public List<? extends Action> getAllActions() {
82  return Collections.unmodifiableList(actions);
83  }
84 
89  @NotNull
90  public Action getResetAction() {
91  return resetAction;
92  }
93 
97  private static class ResetAction extends AbstractAction {
98 
102  private static final long serialVersionUID = 1L;
103 
107  @NotNull
108  private final Iterable<? extends GameObjectMatcherToggleAction> actions;
109 
114  ResetAction(@NotNull final Iterable<? extends GameObjectMatcherToggleAction> actions) {
115  this.actions = actions;
116  }
117 
118  @Override
119  public void actionPerformed(@NotNull final ActionEvent e) {
120  for (final GameObjectMatcherToggleAction action : actions) {
121  action.setSelected(false);
122  }
123  }
124 
125  @NotNull
126  @Override
127  protected Object clone() {
128  try {
129  return super.clone();
130  } catch (final CloneNotSupportedException ex) {
131  throw new AssertionError(ex);
132  }
133  }
134 
135  }
136 
141  private static class GameObjectMatcherToggleAction extends AbstractAction {
142 
146  private static final long serialVersionUID = 1L;
147 
151  private boolean selected;
152 
157  @NotNull
159 
163  @NotNull
165 
173  GameObjectMatcherToggleAction(@NotNull final GameObjectMatcher gameObjectMatcher, @NotNull final MutableOrGameObjectMatcher mutableOrArchObjectMatcher) {
174  this.gameObjectMatcher = gameObjectMatcher;
175  this.mutableOrArchObjectMatcher = mutableOrArchObjectMatcher;
176  }
177 
178  @Override
179  public void actionPerformed(@NotNull final ActionEvent e) {
180  if (!isEnabled()) {
181  return;
182  }
183  setSelected(!selected);
184  }
185 
190  public void setSelected(final boolean selected) {
191  this.selected = selected;
192  if (selected) {
193  mutableOrArchObjectMatcher.addArchObjectMatcher(gameObjectMatcher);
194  } else {
195  mutableOrArchObjectMatcher.removeArchObjectMatcher(gameObjectMatcher);
196  }
197  }
198 
205  public boolean isSelected() {
206  return selected;
207  }
208 
209  @NotNull
210  @Override
211  protected Object clone() {
212  try {
213  return super.clone();
214  } catch (final CloneNotSupportedException ex) {
215  throw new AssertionError(ex);
216  }
217  }
218 
219  }
220 
221 }
final MutableOrGameObjectMatcher mutableOrArchObjectMatcher
A MutableOrGameObjectMatcher to add / remove GameObjectMatcher to.
Interface for classes that match GameObjects.
final List< GameObjectMatcherToggleAction > actions
The actions.
An Action to toggle an GameObjectMatcher within an MutableOrGameObjectMatcher.
void removeArchObjectMatcher(@NotNull final GameObjectMatcher gameObjectMatcher)
Removes an GameObjectMatcher.
void addArchObjectMatcher(@NotNull final GameObjectMatcher gameObjectMatcher)
Adds an GameObjectMatcher.
final Iterable<? extends GameObjectMatcherToggleAction > actions
The actions to reset.
final MutableOrGameObjectMatcher mutableOrGameObjectMatcher
The MutableOrGameObjectMatcher that is manipulated by the actions.
ViewGameObjectMatcherManager(@NotNull final MutableOrGameObjectMatcher mutableOrGameObjectMatcher)
Creates a new instance.
Action addArchObjectMatcher(@NotNull final GameObjectMatcher gameObjectMatcher)
Adds an GameObjectMatcher.