Gridarta Editor
Loader.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.mapfiles;
21 
22 import java.io.File;
23 import java.util.Arrays;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
35 
40 public class Loader<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
41 
45  @NotNull
46  private final ErrorView errorView;
47 
51  @NotNull
53 
57  @NotNull
59 
63  @NotNull
65 
69  @NotNull
71 
80  public Loader(@NotNull final ErrorView errorView, @NotNull final MapFolderTree<G, A, R> mapFolderTree, @NotNull final MapReaderFactory<G, A> mapReaderFactory, @NotNull final MapManager<G, A, R> pickmapManager, @NotNull final MapViewsManager<G, A, R> mapViewsManager) {
81  this.errorView = errorView;
82  this.mapFolderTree = mapFolderTree;
83  this.mapReaderFactory = mapReaderFactory;
84  this.pickmapManager = pickmapManager;
85  this.mapViewsManager = mapViewsManager;
86  }
87 
91  public void load() {
92  final MapFolder<G, A, R> mapFolder = load(null, "");
93  if (mapFolder != null) {
94  mapFolderTree.setActiveMapFolder(mapFolder);
95  }
96  }
97 
105  @Nullable
106  private MapFolder<G, A, R> load(@Nullable final MapFolder<G, A, R> parent, @NotNull final String folderName) {
107  final File baseDir = mapFolderTree.getBaseDir();
108  final File dir = parent == null ? baseDir : new File(parent.getDir(), folderName);
109  final ErrorViewCollector errorViewCollector = new ErrorViewCollector(errorView, dir);
110  final File[] files = dir.listFiles();
111  if (files == null) {
112  errorViewCollector.addWarning(ErrorViewCategory.PICKMAPS_DIR_INVALID);
113  return null;
114  }
115 
116  final MapFolder<G, A, R> mapFolder;
117  try {
118  mapFolder = new MapFolder<>(parent, folderName, baseDir, mapViewsManager);
119  } catch (final InvalidNameException ex) {
120  errorViewCollector.addWarning(ErrorViewCategory.PICKMAPS_DIR_INVALID, "ignoring pickmap folder with invalid name: " + ex.getMessage());
121  return null;
122  }
123  try {
124  mapFolderTree.addMapFolder(mapFolder);
125  } catch (final DuplicateMapFolderException ex) {
126  errorViewCollector.addWarning(ErrorViewCategory.PICKMAPS_DIR_INVALID, "cannot add folder: " + ex.getMessage());
127  return null;
128  }
129 
130  Arrays.sort(files);
131  MapFolder<G, A, R> result = null;
132  for (final File file : files) {
133  if (file.isFile()) {
134  try {
135  mapFolder.addPickmap(file.getName(), mapReaderFactory, pickmapManager);
136  } catch (final InvalidNameException ex) {
137  errorViewCollector.addWarning(ErrorViewCategory.PICKMAPS_FILE_INVALID, "ignoring pickmap with invalid name: " + ex.getMessage());
138  }
139  } else if (file.isDirectory()) {
140  if (!file.getName().startsWith(".")) {
141  final MapFolder<G, A, R> tmp = load(mapFolder, file.getName());
142  if (result == null && tmp != null && tmp.getPickmaps() > 0) {
143  result = tmp;
144  }
145  }
146  }
147  }
148 
149  return mapFolder.getPickmaps() > 0 ? mapFolder : result;
150  }
151 
152 }
final ErrorView errorView
The ErrorView for reporting errors.
Definition: Loader.java:46
final MapFolderTree< G, A, R > mapFolderTree
The model to add the loaded pickmaps to.
Definition: Loader.java:52
File getBaseDir()
Returns the base directory for creating new map folders.
Convenience class for adding messages to a ErrorView instance using a fixed category name...
A MapManager manages all opened maps.
Definition: MapManager.java:37
Graphical User Interface of Gridarta.
Reading and writing of maps, handling of paths.
PickmapState< G, A, R > addPickmap(@NotNull final String name, @NotNull final MapReaderFactory< G, A > mapReaderFactory, @NotNull final MapManager< G, A, R > pickmapManager)
Adds a new PickmapState to this folder.
Definition: MapFolder.java:155
A factory for creating MapReader instances.
Defines possible error categories for ErrorView instances.
Interface for classes displaying error messages.
Definition: ErrorView.java:28
synchronized void addMapFolder(@NotNull final MapFolder< G, A, R > mapFolder)
Adds a map folder to this model.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
int getPickmaps()
Returns the number of pickmaps in this folder.
Definition: MapFolder.java:221
GameObjects are the objects based on Archetypes found on maps.
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager.
Definition: Loader.java:70
Base classes for rendering maps.
Loader(@NotNull final ErrorView errorView, @NotNull final MapFolderTree< G, A, R > mapFolderTree, @NotNull final MapReaderFactory< G, A > mapReaderFactory, @NotNull final MapManager< G, A, R > pickmapManager, @NotNull final MapViewsManager< G, A, R > mapViewsManager)
Creates a new instance.
Definition: Loader.java:80
String getName()
Returns the name.
Definition: MapFolder.java:130
final MapReaderFactory< G, A > mapReaderFactory
The MapReaderFactory for loading map files.
Definition: Loader.java:58
synchronized void setActiveMapFolder(@NotNull final MapFolder< G, A, R > mapFolder)
Sets the active map folder.
MapFolder< G, A, R > load(@Nullable final MapFolder< G, A, R > parent, @NotNull final String folderName)
Loads all pickmap files from a directory and its sub-directories.
Definition: Loader.java:106
Loader for pickmaps from a directory.
Definition: Loader.java:40
final MapManager< G, A, R > pickmapManager
The MapManager for loading pickmaps.
Definition: Loader.java:64
Indicates that a folder or pickmap name is invalid.
void load()
Loads all pickmap files from a directory and its sub-directories.
Definition: Loader.java:91