Gridarta Editor
MapParameter.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.plugin.parameter;
21 
22 import java.io.File;
23 import java.io.IOException;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
37 public class MapParameter<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractStringPluginParameter<G, A, R, MapControl<G, A, R>> {
38 
42  @NotNull
43  public static final String PARAMETER_TYPE = MapControl.class.getName();
44 
48  @NotNull
49  private static final String CURRENT_MAP = "[Current Map]";
50 
55  @NotNull
57 
62  @NotNull
63  private final PathManager pathManager;
64 
72  public MapParameter(@NotNull final MapManager<G, A, R> mapManager, @NotNull final PathManager pathManager) {
73  super(""); // XXX
74  this.mapManager = mapManager;
75  this.pathManager = pathManager;
76  }
77 
78  @NotNull
79  @Override
80  public String getParameterType() {
81  return PARAMETER_TYPE;
82  }
83 
84  @NotNull
85  @Override
86  public <T> T visit(@NotNull final PluginParameterVisitor<G, A, R, T> visitor) {
87  return visitor.visit(this);
88  }
89 
90  @NotNull
91  @Override
93  final String stringValue = getStringValue();
94  if (CURRENT_MAP.equals(stringValue)) {
95  final MapControl<G, A, R> currentMap = mapManager.getCurrentMap();
96  if (currentMap == null) {
97  throw new InvalidValueException("no map is opened");
98  }
99  currentMap.acquire(); // XXX: this causes a leak
100  return currentMap;
101  }
102 
103  final MapFile mapFile = pathManager.getMapFile(new File(stringValue));
104  final MapControl<G, A, R> mapControl;
105  try {
106  mapControl = mapManager.openMapFile(mapFile, false);
107  } catch (final IOException ex) {
108  throw new InvalidValueException("map " + stringValue + " does not exist: " + ex.getMessage(), ex);
109  }
110  return mapControl;
111  }
112 
113  @Nullable
114  @Override
116  try {
117  return getValue();
118  } catch (final InvalidValueException ignored) {
119  return null;
120  }
121  }
122 
126  public void setValueToCurrent() {
127  setStringValue(CURRENT_MAP);
128  }
129 
134  public boolean isCurrentMap() {
135  return getStringValue().equals(CURRENT_MAP);
136  }
137 
138 }
final MapManager< G, A, R > mapManager
The MapManager for converting map paths to MapControl instances.
A PluginParameter that holds a MapControl value.
void setValueToCurrent()
Selects the current map.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
A MapManager manages all opened maps.
Definition: MapManager.java:37
Reading and writing of maps, handling of paths.
final PathManager pathManager
The PathManager instance for converting Files into path names.
Abstract base class for PluginParameter implementations for which the value is calculated from the st...
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
boolean isCurrentMap()
Returns whether the current map is selected.
static final String CURRENT_MAP
The string representation for the current map.
GameObjects are the objects based on Archetypes found on maps.
Interface for visitors of PluginParameter instances.
MapControl< G, A, R > getCurrentMap()
Returns the current map.
void acquire()
Increases the use counter.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
An exception that is thrown if the string representation of value in a PluginParameter cannot be conv...
MapControl< G, A, R > openMapFile(@NotNull MapFile mapFile, boolean interactive)
Loads a map file.
static final String PARAMETER_TYPE
The string representation of this parameter type.
MapParameter(@NotNull final MapManager< G, A, R > mapManager, @NotNull final PathManager pathManager)
Creates a new instance.
The location of a map file with a map directory.
Definition: MapFile.java:31