Gridarta Editor
DefaultExiter.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.mainactions;
21 
22 import java.awt.Window;
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.prefs.Preferences;
25 import javax.swing.SwingUtilities;
26 import net.sf.gridarta.MainControl;
30 import net.sf.gridarta.utils.Exiter;
32 import net.sf.japi.swing.action.ActionBuilder;
33 import net.sf.japi.swing.action.ActionBuilderFactory;
34 import org.apache.log4j.Category;
35 import org.apache.log4j.Logger;
36 import org.jetbrains.annotations.NotNull;
37 
42 public class DefaultExiter implements Exiter {
43 
47  @NotNull
48  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
49 
53  @NotNull
54  private static final Category LOG = Logger.getLogger(DefaultExiter.class);
55 
59  @NotNull
60  private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
61 
65  @NotNull
66  private final Window mainViewFrame;
67 
71  @NotNull
73 
78  public DefaultExiter(@NotNull final Window mainViewFrame) {
79  this.mainViewFrame = mainViewFrame;
80  }
81 
82  @Override
83  public void addExiterListener(@NotNull final ExiterListener listener) {
84  exiterListeners.add(listener);
85  }
86 
87  @Override
88  public void removeExiterListener(@NotNull final ExiterListener listener) {
89  exiterListeners.remove(listener);
90  }
91 
92  @Override
93  public void doExit(final int returnCode) {
94  mainViewFrame.setEnabled(false);
95  for (final ExiterListener listener : exiterListeners.getListeners()) {
96  listener.preExitNotify();
97  }
98  for (final ExiterListener listener : exiterListeners.getListeners()) {
99  listener.appExitNotify();
100  }
101  final Thread thread = new Thread(new Runnable() {
102 
103  @Override
104  public void run() {
105  for (final ExiterListener listener : exiterListeners.getListeners()) {
106  listener.waitExitNotify();
107  }
108  try {
109  SwingUtilities.invokeAndWait(mainViewFrame::dispose);
110  } catch (final InterruptedException ex) {
111  Thread.currentThread().interrupt();
112  LOG.warn("Cannot destroy main view: " + ex.getMessage());
113  } catch (final InvocationTargetException ex) {
114  LOG.warn("Cannot destroy main view: " + ex.getMessage());
115  }
116  callExit(returnCode);
117  }
118 
119  });
120  thread.start();
121  }
122 
128  public static void callExit(final int returnCode) {
130  if (LOG.isDebugEnabled()) {
131  LOG.debug(ActionBuilderUtils.getString(ACTION_BUILDER, "logExitWithExit"));
132  }
133  System.exit(returnCode);
134  } else {
135  if (LOG.isDebugEnabled()) {
136  LOG.debug(ActionBuilderUtils.getString(ACTION_BUILDER, "logExitWithoutExit"));
137  }
138  }
139  }
140 
141 }
void addExiterListener(@NotNull final ExiterListener listener)
Adds an ExiterListener to be notified.
Graphical User Interface of Gridarta.
T [] getListeners()
Returns an array of all the listeners.
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.
void removeExiterListener(@NotNull final ExiterListener listener)
Removes an ExiterListener to be notified.
Exits the application.
Definition: Exiter.java:28
This package contains the preferences ui modules.
void remove(@NotNull final T listener)
Removes a listener.
Interface for listeners interested in Exiter related events.
Interface used as preferences location.
void add(@NotNull final T listener)
Adds a listener.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
static final Preferences PREFERENCES
The Preferences.
final EventListenerList2< ExiterListener > exiterListeners
The ExiterListeners to notify.
Preferences Module for developer preferences.
static void callExit(final int returnCode)
Calls System#exit(int) or does nothing depending on the user&#39;s settings.
Utility class for ActionBuilder related functions.
static final boolean PREFERENCES_SYSTEM_EXIT_DEFAULT
Preferences default value for using System.exit().
Type-safe version of EventListenerList.
static final String PREFERENCES_SYSTEM_EXIT
Preferences key for using System.exit().
DefaultExiter(@NotNull final Window mainViewFrame)
Creates a new instance.
void doExit(final int returnCode)
Exits the application.
final Window mainViewFrame
The main window&#39;s frame.