Gridarta Editor
AbstractArchetypeSetCollectable.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.collectable;
21 
22 import java.io.BufferedWriter;
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStreamWriter;
27 import java.io.Writer;
28 import java.util.Collection;
35 import net.sf.gridarta.utils.IOUtils;
36 import net.sf.japi.swing.action.ActionBuilder;
37 import net.sf.japi.swing.action.ActionBuilderFactory;
38 import net.sf.japi.swing.misc.Progress;
39 import org.jetbrains.annotations.NotNull;
40 
47 public abstract class AbstractArchetypeSetCollectable<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Collectable {
48 
52  @NotNull
53  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
54 
58  @NotNull
60 
64  @NotNull
65  private final String archFile;
66 
72  protected AbstractArchetypeSetCollectable(@NotNull final ArchetypeSet<G, A, R> archetypeSet, @NotNull final String archFile) {
73  this.archetypeSet = archetypeSet;
74  this.archFile = archFile;
75  }
76 
77  @Override
78  public void collect(@NotNull final Progress progress, @NotNull final File collectedDirectory) throws IOException {
79  final File file = new File(collectedDirectory, archFile);
80  try (FileOutputStream fos = new FileOutputStream(file)) {
81  try (OutputStreamWriter osw = new OutputStreamWriter(fos, IOUtils.MAP_ENCODING)) {
82  try (Writer out = new BufferedWriter(osw)) {
83  collect(progress, out);
84  }
85  }
86  }
87  }
88 
95  private void collect(@NotNull final Progress progress, @NotNull final Writer writer) throws IOException {
96  final Collection<R> archetypes = archetypeSet.getArchetypes();
97  progress.setLabel(ActionBuilderUtils.getString(ACTION_BUILDER, "archCollectArches"), archetypes.size());
98  int artifactCount = 0;
99  int count = 0;
100  for (final R archetype : archetypes) {
101  if (archetype.isArtifact()) {
102  artifactCount++;
103  continue;
104  }
105  if (archetype.isUndefinedArchetype()) {
106  continue;
107  }
108 
109  if (archetype.isTail()) {
110  continue;
111  }
112 
113  if (archetype.getArchetypeName().equals(ArchetypeParser.START_ARCH_NAME)) {
114  collectStartArch(archetype, writer);
115  count++;
116  } else {
117  count += collectArchetype(archetype, writer);
118  }
119 
120  if (count % 100 == 0) {
121  progress.setValue(count);
122  }
123  }
124 
125  if ((count + artifactCount) - archetypeSet.getArchetypeCount() != 0) {
126  ACTION_BUILDER.showMessageDialog(progress.getParentComponent(), "archCollectWarningMissed", archetypeSet.getArchetypeCount() - count - artifactCount);
127  }
128  progress.setValue(archetypes.size());
129  }
130 
137  private static void collectStartArch(@NotNull final Archetype<?, ?, ?> archetype, @NotNull final Appendable out) throws IOException {
138  out.append("Object ").append(archetype.getArchetypeName()).append('\n');
139 
140  if (archetype.getArchetypeName().equals(ArchetypeParser.START_ARCH_NAME)) {
141  out.append("x ").append(Integer.toString(archetype.getMultiX())).append('\n');
142  out.append("y ").append(Integer.toString(archetype.getMultiY())).append('\n');
143  }
144 
145  out.append(archetype.getObjectText());
146 
147  out.append("end\n");
148  }
149 
157  protected abstract int collectArchetype(@NotNull R archetype, @NotNull Writer out) throws IOException;
158 
159 }
net.sf.gridarta.model.collectable.AbstractArchetypeSetCollectable.AbstractArchetypeSetCollectable
AbstractArchetypeSetCollectable(@NotNull final ArchetypeSet< G, A, R > archetypeSet, @NotNull final String archFile)
Definition: AbstractArchetypeSetCollectable.java:72
net.sf.gridarta.model.collectable.AbstractArchetypeSetCollectable
Definition: AbstractArchetypeSetCollectable.java:47
net.sf.gridarta
net.sf.gridarta.model.collectable.Collectable
Definition: Collectable.java:33
net.sf
net.sf.gridarta.model.collectable.AbstractArchetypeSetCollectable.collect
void collect(@NotNull final Progress progress, @NotNull final File collectedDirectory)
Definition: AbstractArchetypeSetCollectable.java:78
net.sf.gridarta.model.collectable.AbstractArchetypeSetCollectable.archetypeSet
final ArchetypeSet< G, A, R > archetypeSet
Definition: AbstractArchetypeSetCollectable.java:59
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Definition: GameObject.java:36
net.sf.gridarta.model.collectable.AbstractArchetypeSetCollectable.collect
void collect(@NotNull final Progress progress, @NotNull final Writer writer)
Definition: AbstractArchetypeSetCollectable.java:95
net.sf.gridarta.model.io.ArchetypeParser
Definition: ArchetypeParser.java:36
net.sf.gridarta.model.io.ArchetypeParser.START_ARCH_NAME
String START_ARCH_NAME
Definition: ArchetypeParser.java:41
net.sf.gridarta.model.gameobject
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.archetypeset.ArchetypeSet.getArchetypeCount
int getArchetypeCount()
net.sf.gridarta.model.collectable.AbstractArchetypeSetCollectable.collectStartArch
static void collectStartArch(@NotNull final Archetype<?, ?, ?> archetype, @NotNull final Appendable out)
Definition: AbstractArchetypeSetCollectable.java:137
net.sf.gridarta.model.maparchobject.MapArchObject
Definition: MapArchObject.java:40
net.sf.gridarta.model.archetypeset.ArchetypeSet
Definition: ArchetypeSet.java:37
net.sf.gridarta.model.collectable.AbstractArchetypeSetCollectable.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Definition: AbstractArchetypeSetCollectable.java:53
net.sf.gridarta.utils.ActionBuilderUtils.getString
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Definition: ActionBuilderUtils.java:71
net.sf.gridarta.model.collectable.AbstractArchetypeSetCollectable.archFile
final String archFile
Definition: AbstractArchetypeSetCollectable.java:65
net.sf.gridarta.model.io
Definition: AbstractArchetypeParser.java:20
net.sf.gridarta.model.archetypeset.ArchetypeSet.getArchetypes
Collection< R > getArchetypes()
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Definition: Archetype.java:41
net.sf.gridarta.model.collectable.AbstractArchetypeSetCollectable.collectArchetype
abstract int collectArchetype(@NotNull R archetype, @NotNull Writer out)
net.sf.gridarta.utils.IOUtils
Definition: IOUtils.java:40
net.sf.gridarta.utils.ActionBuilderUtils
Definition: ActionBuilderUtils.java:31
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.archetypeset
Definition: ArchetypeSet.java:20
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.utils.IOUtils.MAP_ENCODING
static final String MAP_ENCODING
Definition: IOUtils.java:52