Gridarta Editor
ArchetypeChooserControl.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.panel.archetypechooser;
21 
22 import java.awt.Component;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
36 import net.sf.japi.swing.action.ActionBuilder;
37 import net.sf.japi.swing.action.ActionBuilderFactory;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
40 
45 public class ArchetypeChooserControl<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Iterable<R>, ObjectChooserTab<G, A, R> {
46 
50  @NotNull
51  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
52 
56  @NotNull
57  private final Map<R, ArchetypePanel<G, A, R>> archetypes = new HashMap<>();
58 
62  @NotNull
64 
68  @NotNull
70 
77  public ArchetypeChooserControl(@NotNull final ArchetypeChooserModel<G, A, R> archetypeChooserModel, @NotNull final ArchetypeChooserView<G, A, R> archetypeChooserView) {
78  this.archetypeChooserModel = archetypeChooserModel;
79  this.archetypeChooserView = archetypeChooserView;
80 
81  for (final ArchetypeChooserPanel<G, A, R> panel : archetypeChooserModel.getPanels()) {
82  final ArchetypePanel<G, A, R> archetypePanel = archetypeChooserView.findOrCreatePanel(panel.getName());
83  final ArchetypeChooserFolder<G, A, R> folder = panel.getDefaultFolder();
84  for (final R archetype : folder.getArchetypes()) {
85  archetypes.put(archetype, archetypePanel);
86  }
87  }
88 
89  }
90 
95  public void selectArchetype(@NotNull final R archetype) {
96  final ArchetypePanel<G, A, R> panel = archetypes.get(archetype);
97  if (panel == null) {
98  return;
99  }
100 
101  archetypeChooserView.setSelectedPanel(panel);
102  panel.selectArchetype(archetype);
103  }
104 
109  @NotNull
110  @Override
111  public Iterator<R> iterator() {
112  return Collections.unmodifiableSet(archetypes.keySet()).iterator();
113  }
114 
115  @NotNull
116  @Override
117  public Component getComponent() {
118  return archetypeChooserView;
119  }
120 
121  @Override
122  public void setActive(final boolean active) {
123  // ignore
124  }
125 
126  @Override
127  public boolean isMatching(@NotNull final G gameObject) {
128  return getSelection() == gameObject.getArchetype();
129  }
130 
131  @Nullable
132  @Override
133  public R getSelection() {
134  final ArchetypeChooserPanel<G, A, R> selectedPanel = archetypeChooserModel.getSelectedPanel();
135  return selectedPanel != null ? selectedPanel.getSelectedFolder().getSelectedArchetype() : null;
136  }
137 
138  @NotNull
139  @Override
140  public List<R> getSelections() {
141  final R archObject = getSelection();
142  return archObject == null ? Collections.emptyList() : Collections.singletonList(archObject);
143  }
144 
145  @NotNull
146  @Override
147  public String getTitle() {
148  return ActionBuilderUtils.getString(ACTION_BUILDER, "objectChooser.archetypesTabTitle");
149  }
150 
151 }
String getTitle()
Returns the title to display in the object chooser.
Graphical User Interface of Gridarta.
final ArchetypeChooserModel< G, A, R > archetypeChooserModel
The archetype chooser&#39;s model.
A named panel within the ArchetypeChooserModel.
boolean isMatching(@NotNull final G gameObject)
Returns whether the current selection matches the given game object.
void setActive(final boolean active)
Called whenever this tab becomes active or inactive.
A named folder within the ArchetypeChooserModel.
ArchetypeChooserControl(@NotNull final ArchetypeChooserModel< G, A, R > archetypeChooserModel, @NotNull final ArchetypeChooserView< G, A, R > archetypeChooserView)
Creates a new instance.
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.
ArchetypeChooserPanel< G, A, R > getSelectedPanel()
Returns the selected ArchetypeChooserPanel.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Iterator< R > iterator()
Returns all archetypes in the panel.
GameObjects are the objects based on Archetypes found on maps.
Component getComponent()
Returns the component to show in the object chooser.
void selectArchetype(@NotNull final R archetype)
Select an archetype.
final Map< R, ArchetypePanel< G, A, R > > archetypes
Maps archetype to archetype panel containing the archetype.
Utility class for ActionBuilder related functions.
final ArchetypeChooserView< G, A, R > archetypeChooserView
The archetype chooser&#39;s view.
void selectArchetype(@NotNull final R archetype)
Selects an archetype.
ArchetypeChooserFolder< G, A, R > getSelectedFolder()
Returns the selected ArchetypeChooserFolder.
final void setSelectedPanel(@NotNull final ArchetypePanel< G, A, R > selectedPanel)
Selects an archetype panel.
Interface for classes being part of the object chooser.