Gridarta Editor
UpdaterManager.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.updater;
21 
22 import java.awt.Component;
23 import java.util.prefs.Preferences;
24 import javax.swing.Action;
25 import net.sf.gridarta.MainControl;
28 import net.sf.gridarta.utils.Exiter;
29 import net.sf.japi.swing.action.ActionBuilder;
30 import net.sf.japi.swing.action.ActionBuilderFactory;
31 import net.sf.japi.swing.action.ActionMethod;
32 import org.jetbrains.annotations.NotNull;
33 
38 public class UpdaterManager implements EditorAction {
39 
43  @NotNull
44  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
45 
49  @NotNull
50  private static final Preferences PREFERENCES = Preferences.userNodeForPackage(MainControl.class);
51 
55  @NotNull
56  public static final String AUTO_CHECK_KEY = "UpdaterAutoCheck";
57 
61  public static final boolean AUTO_CHECK_DEFAULT = false;
62 
66  @NotNull
67  public static final String INTERVAL_KEY = "UpdaterInterval";
68 
72  public static final int INTERVAL_DEFAULT = 2;
73 
77  private static final long[] UPDATE_TIMES = { 0L, // Every startup
78  86400000L, // Once a day
79  604800000L, // Once a week
80  2419200000L, // Once a month
81  };
82 
86  @NotNull
87  private final Exiter exiter;
88 
92  @NotNull
94 
98  @NotNull
99  private final Component parentComponent;
100 
104  @NotNull
105  private final String updateFileName;
106 
110  private final boolean hasUpdateURL;
111 
119  public UpdaterManager(@NotNull final Exiter exiter, @NotNull final MapManager<?, ?, ?> mapManager, @NotNull final Component parentComponent, @NotNull final String updateFileName) {
120  this.mapManager = mapManager;
121  this.exiter = exiter;
122  this.parentComponent = parentComponent;
123  this.updateFileName = updateFileName;
124 
125  final String propUrl = ACTION_BUILDER.getString("update.url");
126  hasUpdateURL = propUrl != null && !propUrl.isEmpty();
127  }
128 
132  public void startup() {
133  if (!hasUpdateURL || !PREFERENCES.getBoolean(AUTO_CHECK_KEY, AUTO_CHECK_DEFAULT)) {
134  return;
135  }
136  final long timeDifference = UPDATE_TIMES[PREFERENCES.getInt(INTERVAL_KEY, INTERVAL_DEFAULT)];
137  final long lastUpdate = PREFERENCES.getLong(Updater.LAST_UPDATE_KEY, 0L);
138  if (System.currentTimeMillis() > lastUpdate + timeDifference) {
139  update();
140  }
141  }
142 
146  @ActionMethod
147  public void update() {
148  if (mapManager.getOpenedMaps().isEmpty()) {
149  new Thread(new Updater(parentComponent, exiter, updateFileName)).start();
150  } else {
151  ACTION_BUILDER.showMessageDialog(parentComponent, "updateCloseMaps");
152  }
153  }
154 
155  @Override
156  public void setAction(@NotNull final Action action, @NotNull final String name) {
157  action.setEnabled(hasUpdateURL);
158  }
159 
160 }
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
final MapManager<?, ?, ?> mapManager
The map manager to use.
A MapManager manages all opened maps.
Definition: MapManager.java:37
final Component parentComponent
The component to show dialogs on.
final boolean hasUpdateURL
Whether an update URL is defined.
static final String LAST_UPDATE_KEY
Preferences key for last update.
Definition: Updater.java:82
This class handles updating the map editor.
Definition: Updater.java:58
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
final Exiter exiter
The Exiter for terminating the application.
static final long [] UPDATE_TIMES
The times for update calculation.
Base package of all Gridarta classes.
Exits the application.
Definition: Exiter.java:28
A global editor action.
static final boolean AUTO_CHECK_DEFAULT
Preferences default value for AUTO_CHECK_KEY.
This class handles updating the map editor.
Interface used as preferences location.
static final String AUTO_CHECK_KEY
Preferences key whether to automatically check for updates.
final String updateFileName
The file to update.
static final String INTERVAL_KEY
Preferences key for selected update interval.
static final int INTERVAL_DEFAULT
Preferences default value for INTERVAL_KEY.
static final Preferences PREFERENCES
Preferences.
UpdaterManager(@NotNull final Exiter exiter, @NotNull final MapManager<?, ?, ?> mapManager, @NotNull final Component parentComponent, @NotNull final String updateFileName)
Creates a new instance.
void startup()
Eventually perform an update during startup.
List< MapControl< G, A, R > > getOpenedMaps()
Returns all opened maps.