Gridarta Editor
AbstractConfigSource.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.configsource;
21 
22 import java.io.File;
23 import java.io.IOException;
25 import net.sf.gridarta.utils.IOUtils;
28 import net.sf.japi.swing.action.ActionBuilder;
29 import net.sf.japi.swing.action.ActionBuilderFactory;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
37 public abstract class AbstractConfigSource implements ConfigSource {
38 
42  @NotNull
43  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
44 
45  @NotNull
46  @Override
47  public String toString() {
48  final String result = ACTION_BUILDER.getString("optionsConfigSource_" + getName());
49  return result == null ? getName() : result;
50  }
51 
52  @Nullable
53  @Override
54  public File getFile(@NotNull final ProjectSettings projectSettings, @NotNull final String type, final int index) throws IOException {
55  final String key = "configSource." + getName() + "." + type + "." + index;
56  final String spec = ACTION_BUILDER.getString(key);
57  if (spec == null) {
58  return null;
59  }
60 
61  final StringParameterBuilder stringParameterBuilder = new StringParameterBuilder();
62  stringParameterBuilder.addParameter("COLLECTED", projectSettings.getCollectedDirectory().getPath());
63  stringParameterBuilder.addParameter("ARCH", projectSettings.getArchDirectory().getPath());
64  stringParameterBuilder.addParameter("MAPS", projectSettings.getMapsDirectory().getPath());
65  final String result;
66  try {
67  result = stringParameterBuilder.replace(spec);
68  } catch (final SyntaxErrorException ex) {
69  throw new IOException(ex.getMessage() + " in " + key + "=" + spec, ex);
70  }
71  return IOUtils.getFile(null, result);
72  }
73 
74 }
File getFile(@NotNull final ProjectSettings projectSettings, @NotNull final String type, final int index)
Returns a config file.
Settings that apply to a project.
String replace(@NotNull final CharSequence spec)
Replaces all parameters in a string.
Base package of all Gridarta classes.
Utility-class for Gridarta's I/O.
Definition: IOUtils.java:40
Possible source locations for configuration files.
static File getFile(@Nullable final File dir, @NotNull final String fileName)
Returns a File instance for a resource that is a regular file on the file system. ...
Definition: IOUtils.java:124
Exception thrown for incorrect arguments.
Abstract base class for ConfigSource implementations.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
String getName()
Returns the internal name.
void addParameter(@NotNull final String key, @NotNull final String value)
Adds a parameter key/value pair.