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-2023 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 
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) {
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) {
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 }
net.sf.gridarta.model.mapmanager
Definition: AbstractMapManager.java:20
net.sf.gridarta.model.errorview.ErrorViewCollector.addWarning
void addWarning(@NotNull final ErrorViewCategory category)
Adds a warning message.
Definition: ErrorViewCollector.java:68
net.sf.gridarta.model.mapmanager.MapManager
A MapManager manages all opened maps.
Definition: MapManager.java:37
net.sf.gridarta.gui.mapfiles.Loader
Loader for pickmaps from a directory.
Definition: Loader.java:40
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.mapfiles.Loader.Loader
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
files
Standard Edition Runtime Environment README Import and export control rules on cryptographic software vary from country to country The Java Cryptography Java provides two different sets of cryptographic policy files
Definition: README.txt:26
net.sf.gridarta.model.io.MapReaderFactory
A factory for creating MapReader instances.
Definition: MapReaderFactory.java:32
net.sf.gridarta.gui.mapfiles.MapFolderTree< G, A, R >
net.sf
net.sf.gridarta.model.errorview.ErrorView
Interface for classes displaying error messages.
Definition: ErrorView.java:28
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.gui.mapfiles.MapFolderTree.addMapFolder
synchronized void addMapFolder(@NotNull final MapFolder< G, A, R > mapFolder)
Adds a map folder to this model.
Definition: MapFolderTree.java:90
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.gui.mapfiles.Loader.mapViewsManager
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager.
Definition: Loader.java:70
net.sf.gridarta.gui.mapfiles.Loader.mapReaderFactory
final MapReaderFactory< G, A > mapReaderFactory
The MapReaderFactory for loading map files.
Definition: Loader.java:58
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.gui.mapfiles.Loader.load
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
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.errorview
Definition: ErrorView.java:20
net.sf.gridarta.gui.mapfiles.Loader.pickmapManager
final MapManager< G, A, R > pickmapManager
The MapManager for loading pickmaps.
Definition: Loader.java:64
net.sf.gridarta.model.errorview.ErrorViewCollector
Convenience class for adding messages to a ErrorView instance using a fixed category name.
Definition: ErrorViewCollector.java:31
net.sf.gridarta.gui.mapfiles.MapFolder.getName
String getName()
Returns the name.
Definition: MapFolder.java:130
net.sf.gridarta.gui.mapfiles.InvalidNameException
Indicates that a folder or pickmap name is invalid.
Definition: InvalidNameException.java:28
net.sf.gridarta.model.errorview.ErrorViewCategory
Defines possible error categories for ErrorView instances.
Definition: ErrorViewCategory.java:28
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.gui.map.mapview
Definition: AbstractMapView.java:20
net.sf.gridarta.gui.mapfiles.MapFolder< G, A, R >
net.sf.gridarta.gui.mapfiles.MapFolder.addPickmap
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
net.sf.gridarta.gui.mapfiles.Loader.mapFolderTree
final MapFolderTree< G, A, R > mapFolderTree
The model to add the loaded pickmaps to.
Definition: Loader.java:52
net.sf.gridarta.model.io
Reading and writing of maps, handling of paths.
Definition: AbstractAnimationObjectsReader.java:20
net.sf.gridarta.gui.mapfiles.DuplicateMapFolderException
Indicates that a folder name is not unique.
Definition: DuplicateMapFolderException.java:28
net.sf.gridarta.model.errorview.ErrorViewCategory.PICKMAPS_DIR_INVALID
PICKMAPS_DIR_INVALID
Definition: ErrorViewCategory.java:74
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.errorview.ErrorViewCategory.PICKMAPS_FILE_INVALID
PICKMAPS_FILE_INVALID
Definition: ErrorViewCategory.java:76
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.gui.mapfiles.Loader.errorView
final ErrorView errorView
The ErrorView for reporting errors.
Definition: Loader.java:46
net.sf.gridarta.gui.mapfiles.MapFolderTree.getBaseDir
File getBaseDir()
Returns the base directory for creating new map folders.
Definition: MapFolderTree.java:79
net.sf.gridarta.gui.mapfiles.Loader.load
void load()
Loads all pickmap files from a directory and its sub-directories.
Definition: Loader.java:91
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.mapfiles.MapFolder.getPickmaps
int getPickmaps()
Returns the number of pickmaps in this folder.
Definition: MapFolder.java:221
net.sf.gridarta.gui.mapfiles.MapFolderTree.setActiveMapFolder
synchronized void setActiveMapFolder(@NotNull final MapFolder< G, A, R > mapFolder)
Sets the active map folder.
Definition: MapFolderTree.java:157
net.sf.gridarta.gui.map.mapview.MapViewsManager
Stores all existing MapViews.
Definition: MapViewsManager.java:47