Gridarta Editor
AbstractResources.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.resource;
21 
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
33 import net.sf.japi.swing.misc.Progress;
34 import org.jetbrains.annotations.NotNull;
35 
40 public abstract class AbstractResources<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
41 
45  @NotNull
47 
51  @NotNull
53 
57  private boolean loaded;
58 
62  private boolean loadedFromFiles;
63 
69  protected AbstractResources(@NotNull final GameObjectParser<G, A, R> gameObjectParser, @NotNull final ArchetypeSet<G, A, R> archetypeSet) {
70  this.gameObjectParser = gameObjectParser;
71  this.archetypeSet = archetypeSet;
72  }
73 
78  private void finishRead(@NotNull final List<G> invObjects) {
79  gameObjectParser.collectTempList(invObjects);
80  archetypeSet.connectFaces(); // attach faces to arches
81  }
82 
88  public void readFiles(@NotNull final ProjectSettings projectSettings, @NotNull final ErrorView errorView) {
89  if (loaded) {
90  throw new IllegalStateException();
91  }
92  final List<G> invObjects = new ArrayList<>();
93  readFilesInt(projectSettings, errorView, invObjects);
94  finishRead(invObjects);
95  loadedFromFiles = true;
96  loaded = true;
97  }
98 
104  public void readCollected(@NotNull final ProjectSettings projectSettings, @NotNull final ErrorView errorView) {
105  if (loaded) {
106  throw new IllegalStateException();
107  }
108  final List<G> invObjects = readCollectedInt(projectSettings, errorView);
109  finishRead(invObjects);
110  loadedFromFiles = false;
111  loaded = true;
112  }
113 
119  public boolean canWriteCollected() {
120  return loaded && loadedFromFiles;
121  }
122 
131  public void writeCollected(@NotNull final Progress progress, @NotNull final File collectedDirectory) throws IOException {
132  if (!canWriteCollected()) {
133  throw new IllegalStateException();
134  }
135  writeCollectedInt(progress, collectedDirectory);
136  }
137 
144  protected abstract void readFilesInt(@NotNull ProjectSettings projectSettings, @NotNull ErrorView errorView, @NotNull List<G> invObjects);
145 
152  @NotNull
153  protected abstract List<G> readCollectedInt(@NotNull ProjectSettings projectSettings, @NotNull ErrorView errorView);
154 
161  protected abstract void writeCollectedInt(@NotNull Progress progress, @NotNull File collectedDirectory) throws IOException;
162 
163 }
abstract List< G > readCollectedInt(@NotNull ProjectSettings projectSettings, @NotNull ErrorView errorView)
Reads the resources from a collection.
Reading and writing of maps, handling of paths.
Settings that apply to a project.
boolean canWriteCollected()
Whether the resources can be written in collected form.
boolean loaded
Whether the resources have been loaded.
Interface for classes that read or write GameObject instances.
boolean loadedFromFiles
Whether the resources have been loaded from individual files.
void finishRead(@NotNull final List< G > invObjects)
Common code to be executed after reading resources.
Interface for classes displaying error messages.
Definition: ErrorView.java:28
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
void readCollected(@NotNull final ProjectSettings projectSettings, @NotNull final ErrorView errorView)
Reads the resources from a collection.
GameObjects are the objects based on Archetypes found on maps.
final ArchetypeSet< G, A, R > archetypeSet
The ArchetypeSet to update.
abstract void readFilesInt(@NotNull ProjectSettings projectSettings, @NotNull ErrorView errorView, @NotNull List< G > invObjects)
Reads the resources from individual files.
abstract void writeCollectedInt(@NotNull Progress progress, @NotNull File collectedDirectory)
Writes the resources in collected form.
final GameObjectParser< G, A, R > gameObjectParser
The GameObjectParser to use.
void collectTempList(@NotNull List< G > objects)
Browse first through the archetype list and attach map arches to it then browse through the face list...
void readFiles(@NotNull final ProjectSettings projectSettings, @NotNull final ErrorView errorView)
Reads the resources from individual files.
AbstractResources(@NotNull final GameObjectParser< G, A, R > gameObjectParser, @NotNull final ArchetypeSet< G, A, R > archetypeSet)
Creates a new instance.
Interface that captures similarities between different ArchetypeSet implementations.
void writeCollected(@NotNull final Progress progress, @NotNull final File collectedDirectory)
Writes the resources in collected form.