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-2023 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 @NotNull [] 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() {
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 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.model.mapmanager
Definition: AbstractMapManager.java:20
net.sf.gridarta.updater.UpdaterManager.UpdaterManager
UpdaterManager(@NotNull final Exiter exiter, @NotNull final MapManager<?, ?, ?> mapManager, @NotNull final Component parentComponent, @NotNull final String updateFileName)
Creates a new instance.
Definition: UpdaterManager.java:119
net.sf.gridarta.model.mapmanager.MapManager
A MapManager manages all opened maps.
Definition: MapManager.java:37
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.updater.UpdaterManager.INTERVAL_KEY
static final String INTERVAL_KEY
Preferences key for selected update interval.
Definition: UpdaterManager.java:67
net.sf.gridarta.updater.UpdaterManager.update
void update()
Perform an update.
Definition: UpdaterManager.java:147
net.sf
net.sf.gridarta.updater.Updater.LAST_UPDATE_KEY
static final String LAST_UPDATE_KEY
The preferences key for last update.
Definition: Updater.java:82
net.sf.gridarta.updater.UpdaterManager.exiter
final Exiter exiter
The Exiter for terminating the application.
Definition: UpdaterManager.java:87
net.sf.gridarta.updater.UpdaterManager.PREFERENCES
static final Preferences PREFERENCES
Preferences.
Definition: UpdaterManager.java:50
net.sf.gridarta.model.mapmanager.MapManager.getOpenedMaps
List< MapControl< G, A, R > > getOpenedMaps()
Returns all opened maps.
net.sf.gridarta.updater.UpdaterManager.startup
void startup()
Eventually perform an update during startup.
Definition: UpdaterManager.java:132
net.sf.gridarta.updater.UpdaterManager.mapManager
final MapManager<?, ?, ?> mapManager
The map manager to use.
Definition: UpdaterManager.java:93
net.sf.gridarta.updater.UpdaterManager.parentComponent
final Component parentComponent
The component to show dialogs on.
Definition: UpdaterManager.java:99
net
net.sf.gridarta.updater.UpdaterManager.UPDATE_TIMES
static final long[] UPDATE_TIMES
The times for update calculation.
Definition: UpdaterManager.java:77
net.sf.gridarta.updater.UpdaterManager.setAction
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
Definition: UpdaterManager.java:156
net.sf.gridarta.updater.UpdaterManager.AUTO_CHECK_DEFAULT
static final boolean AUTO_CHECK_DEFAULT
Preferences default value for AUTO_CHECK_KEY.
Definition: UpdaterManager.java:61
net.sf.gridarta.updater.UpdaterManager
This class handles updating the map editor.
Definition: UpdaterManager.java:38
net.sf.gridarta.updater.UpdaterManager.updateFileName
final String updateFileName
The file to update.
Definition: UpdaterManager.java:105
net.sf.gridarta.updater.UpdaterManager.AUTO_CHECK_KEY
static final String AUTO_CHECK_KEY
Preferences key whether to automatically check for updates.
Definition: UpdaterManager.java:56
net.sf.gridarta.updater.UpdaterManager.hasUpdateURL
final boolean hasUpdateURL
Whether an update URL is defined.
Definition: UpdaterManager.java:110
net.sf.gridarta.model
net.sf.gridarta.updater.UpdaterManager.INTERVAL_DEFAULT
static final int INTERVAL_DEFAULT
Preferences default value for INTERVAL_KEY.
Definition: UpdaterManager.java:72
net.sf.gridarta.utils.EditorAction
A global editor action.
Definition: EditorAction.java:29
net.sf.gridarta.updater.Updater
This class handles updating the map editor.
Definition: Updater.java:58
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.updater.UpdaterManager.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
Definition: UpdaterManager.java:44
net.sf.gridarta.utils.Exiter
Exits the application.
Definition: Exiter.java:28
net.sf.gridarta.MainControl
Interface used as preferences location.
Definition: MainControl.java:27