Gridarta Editor
CollectArchesAction.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.action;
21 
22 import java.awt.Component;
23 import javax.swing.Action;
24 import javax.swing.JFrame;
34 import net.sf.gridarta.utils.Exiter;
36 import net.sf.japi.swing.action.ActionBuilder;
37 import net.sf.japi.swing.action.ActionBuilderFactory;
38 import net.sf.japi.swing.action.ActionMethod;
39 import net.sf.japi.swing.misc.Progress;
40 import net.sf.japi.swing.misc.ProgressDisplay;
41 import org.apache.log4j.Category;
42 import org.apache.log4j.Logger;
43 import org.jetbrains.annotations.NotNull;
44 import org.jetbrains.annotations.Nullable;
45 
50 public class CollectArchesAction<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction {
51 
55  @NotNull
56  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
57 
61  @NotNull
62  private static final Category LOG = Logger.getLogger(CollectArchesAction.class);
63 
67  @NotNull
69 
73  @NotNull
75 
79  @NotNull
80  private final JFrame parent;
81 
86  @Nullable
87  private volatile Collector collector;
88 
93  @Nullable
94  private Action action;
95 
103  public CollectArchesAction(@NotNull final ProjectSettings projectSettings, @NotNull final AbstractResources<G, A, R> resources, @NotNull final Exiter exiter, @NotNull final JFrame parent) {
104  this.projectSettings = projectSettings;
105  this.resources = resources;
106  this.parent = parent;
107  exiter.addExiterListener(newExiterListener());
108  }
109 
113  @ActionMethod
114  public void collectArches() {
115  doCollectArches(true);
116  }
117 
118  @Override
119  public void setAction(@NotNull final Action action, @NotNull final String name) {
120  this.action = action;
121  }
122 
128  private boolean doCollectArches(final boolean performAction) {
129  synchronized (this) {
130  if (collector != null || !resources.canWriteCollected()) {
131  return false;
132  }
133 
134  if (performAction) {
135  final Progress progress = new AsynchronousProgress(new ProgressDisplay(parent, ActionBuilderUtils.getString(ACTION_BUILDER, "archCollectTitle"), 0, ActionBuilderUtils.getString(ACTION_BUILDER, "archCollectArches")));
136  final Progress collectArches = new Progress() {
137 
138  @Override
139  public void finished() {
140  progress.finished();
141  collector = null;
142  updateAction();
143  }
144 
145  @Override
146  public Component getParentComponent() {
147  return progress.getParentComponent();
148  }
149 
150  @Override
151  public void setLabel(final String msg, final int max) {
152  progress.setLabel(msg, max);
153  }
154 
155  @Override
156  public void setValue(final int value) {
157  progress.setValue(value);
158  }
159 
160  };
161 
162  collector = new Collector(collectArches, resources, projectSettings.getCollectedDirectory());
163  assert collector != null;
164  collector.start();
165  updateAction();
166  }
167  }
168 
169  return true;
170  }
171 
175  private void updateAction() {
176  if (action != null) {
177  //noinspection ConstantConditions
178  action.setEnabled(doCollectArches(false));
179  }
180  }
181 
187  @NotNull
189  return new ExiterListener() {
190 
191  @Override
192  public void preExitNotify() {
193  // ignore
194  }
195 
196  @Override
197  public void appExitNotify() {
198  // ignore
199  }
200 
201  @Override
202  public void waitExitNotify() {
203  final Collector tmp = collector;
204  if (tmp != null) {
205  try {
206  tmp.waitUntilFinished();
207  } catch (final InterruptedException ignored) {
208  Thread.currentThread().interrupt();
209  LOG.warn("DelayedMapModelListenerManager was interrupted");
210  }
211  }
212  }
213 
214  };
215  }
216 
217 }
void updateAction()
Updates the action&#39;s enabled state.
A Collector is capable of iterating over a collection of Collectables and collecting them in a separa...
Definition: Collector.java:37
void waitUntilFinished()
Waits until collection has finished.
Definition: Collector.java:96
Graphical User Interface of Gridarta.
void collectArches()
Runs the archetype collection.
Action action
The action associated with this editor action.
Settings that apply to a project.
boolean canWriteCollected()
Whether the resources can be written in collected form.
final JFrame parent
The parent component for dialog windows.
void start()
Starts collecting.
Definition: Collector.java:87
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
static final Category LOG
The Logger for printing log messages.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
Base package of all Gridarta classes.
final AbstractResources< G, A, R > resources
The AbstractResources to collect.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Exits the application.
Definition: Exiter.java:28
A global editor action.
CollectArchesAction(@NotNull final ProjectSettings projectSettings, @NotNull final AbstractResources< G, A, R > resources, @NotNull final Exiter exiter, @NotNull final JFrame parent)
Creates a new instance.
Interface for listeners interested in Exiter related events.
ExiterListener newExiterListener()
Creates a new ExiterListener that delays application exit until archetype collection has finished...
GameObjects are the objects based on Archetypes found on maps.
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
An EditorAction that runs the archetype collection.
boolean doCollectArches(final boolean performAction)
Executes the "collect arches" action.
Utility class for ActionBuilder related functions.
Implements a Progress that forwards to another.
File getCollectedDirectory()
Returns the directory where collected archetypes are stored.
volatile Collector collector
The Collector if an archetype collection is running, or else.
final ProjectSettings projectSettings
The project settings instance.