Gridarta Editor
ToolSelector.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.tools;
21 
22 import java.awt.BorderLayout;
23 import java.awt.CardLayout;
24 import java.awt.Component;
25 import java.awt.Container;
26 import java.awt.FlowLayout;
27 import java.awt.Insets;
28 import java.awt.event.ActionEvent;
29 import java.util.HashMap;
30 import java.util.Map;
31 import javax.swing.AbstractAction;
32 import javax.swing.AbstractButton;
33 import javax.swing.Action;
34 import javax.swing.ButtonGroup;
35 import javax.swing.JPanel;
36 import javax.swing.JToggleButton;
47 import net.sf.japi.swing.action.ActionBuilder;
48 import net.sf.japi.swing.action.ActionBuilderFactory;
49 import org.jetbrains.annotations.NotNull;
50 import org.jetbrains.annotations.Nullable;
51 
57 public class ToolSelector<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends JPanel {
58 
62  private static final long serialVersionUID = 1L;
63 
67  @NotNull
68  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta.gui.panel.tools");
69 
73  @NotNull
74  private final Container selectionPane = new JPanel(new FlowLayout());
75 
79  @NotNull
80  private final CardLayout optionCards = new CardLayout();
81 
85  @NotNull
86  private final ButtonGroup selectionButtonGroup = new ButtonGroup();
87 
91  @NotNull
92  private final Container optionsPane = new JPanel(optionCards);
93 
97  @NotNull
99 
103  @NotNull
104  private final Map<String, Tool<G, A, R>> tools = new HashMap<>();
105 
109  private static final Insets EMPTY_MARGIN = new Insets(0, 0, 0, 0);
110 
123  public ToolSelector(@NotNull final String defaultTool, @NotNull final MapViewSettings mapViewSettings, @NotNull final SelectedSquareModel<G, A, R> selectedSquareModel, @NotNull final ObjectChooser<G, A, R> objectChooser, @NotNull final PickmapSettings pickmapSettings, @Nullable final GameObjectMatcher floorGameObjectMatcher, @Nullable final GameObjectMatcher wallGameObjectMatcher, @Nullable final GameObjectMatcher monsterGameObjectMatcher, @NotNull final InsertionModeSet<G, A, R> insertionModeSet) {
124  createUI();
125  addTool(new VoidTool<>(), defaultTool);
126  addTool(new SelectionTool<>(objectChooser, insertionModeSet), defaultTool);
127  addTool(new DeletionTool<>(mapViewSettings, objectChooser, pickmapSettings, floorGameObjectMatcher, wallGameObjectMatcher, monsterGameObjectMatcher), defaultTool);
128  addTool(new InsertionTool<>(selectedSquareModel, objectChooser, pickmapSettings, insertionModeSet, mapViewSettings), defaultTool);
129  }
130 
136  private void addTool(@NotNull final Tool<G, A, R> tool, @NotNull final String defaultTool) {
137  add(tool, tool.getId().equals(defaultTool));
138  }
139 
143  private void createUI() {
144  setLayout(new BorderLayout());
145  add(selectionPane, BorderLayout.NORTH);
146  add(optionsPane, BorderLayout.CENTER);
147  }
148 
153  @SuppressWarnings("MethodOverloadsMethodOfSuperclass")
154  public void add(@NotNull final Tool<G, A, R> tool) {
155  add(tool, false);
156  }
157 
164  private void add(@NotNull final Tool<G, A, R> tool, final boolean selected) {
165  @NotNull final Action selectionAction = new SelectionAction(tool);
166  @NotNull final AbstractButton toggleButton = new JToggleButton(selectionAction);
167  toggleButton.setMargin(EMPTY_MARGIN);
168  @NotNull final Component optionsView = createOptionsView(tool);
169  selectionButtonGroup.add(toggleButton);
170  selectionPane.add(toggleButton);
171  final Container panel = new JPanel(new BorderLayout());
172  panel.add(optionsView, BorderLayout.NORTH);
173  optionsPane.add(panel, tool.getId());
174  toggleButton.setSelected(selected);
175  tools.put(tool.getId(), tool);
176  if (selected) {
177  selectedTool = tool;
178  optionCards.show(optionsPane, tool.getId());
179  }
180  }
181 
190  @NotNull
191  private static <G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> Component createOptionsView(@NotNull final Tool<G, A, R> tool) {
192  @Nullable final Component optionsView = tool.createOptionsView();
193  return optionsView != null ? optionsView : new JPanel();
194  }
195 
202  private void setSelectedTool(@NotNull final Tool<G, A, R> tool) {
203  selectedTool = tool;
204  optionCards.show(optionsPane, tool.getId());
205  }
206 
216  public void setSelectedTool(@NotNull final String id) {
217  setSelectedTool(tools.get(id));
218  }
219 
224  @NotNull
226  return selectedTool;
227  }
228 
232  private class SelectionAction extends AbstractAction {
233 
237  private static final long serialVersionUID = 1L;
238 
242  @NotNull
243  private final Tool<G, A, R> tool;
244 
249  private SelectionAction(@NotNull final Tool<G, A, R> tool) {
250  this.tool = tool;
251  ACTION_BUILDER.initAction(false, this, tool.getId());
252  }
253 
254  @Override
255  public void actionPerformed(@NotNull final ActionEvent e) {
256  setSelectedTool(tool);
257  }
258 
259  @NotNull
260  @Override
261  public Object clone() throws CloneNotSupportedException {
262  return super.clone();
263  }
264 
265  }
266 
267 }
void setSelectedTool(@NotNull final Tool< G, A, R > tool)
Makes a tool the currently selected tool to edit its options.
MouseOpListener< G, A, R > getSelectedTool()
Returns the tool that is currently selected.
SelectionAction(@NotNull final Tool< G, A, R > tool)
Creates a SelectionAction.
final Tool< G, A, R > tool
The tool to select with this action.
ToolSelector(@NotNull final String defaultTool, @NotNull final MapViewSettings mapViewSettings, @NotNull final SelectedSquareModel< G, A, R > selectedSquareModel, @NotNull final ObjectChooser< G, A, R > objectChooser, @NotNull final PickmapSettings pickmapSettings, @Nullable final GameObjectMatcher floorGameObjectMatcher, @Nullable final GameObjectMatcher wallGameObjectMatcher, @Nullable final GameObjectMatcher monsterGameObjectMatcher, @NotNull final InsertionModeSet< G, A, R > insertionModeSet)
Creates a new instance.
final ButtonGroup selectionButtonGroup
The ButtonGroup for the toggle buttons.
Graphical User Interface of Gridarta.
Interface for classes that match GameObjects.
This package contains classes related to matching GameObjects, so called GameObjectMatchers.
void add(@NotNull final Tool< G, A, R > tool)
Adds a tool to this tool selector.
static final ActionBuilder ACTION_BUILDER
The Action Builder.
final Container selectionPane
The pane with the selections.
Tool< G, A, R > selectedTool
The currently selected tool.
final Map< String, Tool< G, A, R > > tools
The tools.
Base package of all Gridarta classes.
static final Insets EMPTY_MARGIN
Empty margin.
Reflects a game object (object on a map).
Definition: GameObject.java:36
User interface for selecting a tool and displaying its options.
void addTool(@NotNull final Tool< G, A, R > tool, @NotNull final String defaultTool)
Adds a tool to this tool selector.
void setSelectedTool(@NotNull final String id)
Makes a tool the currently selected tool to edit its options.
Container for settings that affect the rendering of maps.
GameObjects are the objects based on Archetypes found on maps.
Displays the contents of the currently selected map square.
Base classes for rendering maps.
The VoidTool is a tool that does nothing.
Definition: VoidTool.java:35
final Container optionsPane
The pane with the options of a tool.
final CardLayout optionCards
The CardLayout for the pane that shows a tool&#39;s options.
Common base interface for ObjectChoosers.
The model component of the selected square control.
void createUI()
Creates the user interface elements of the ToolSelector.
static final long serialVersionUID
The serial version UID.
void add(@NotNull final Tool< G, A, R > tool, final boolean selected)
Adds a tool to this tool selector.
Container for settings that affect pickmaps.