Gridarta Editor
StatusBar.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.gui.misc;
21 
22 import java.awt.Dimension;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Point;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.event.MouseEvent;
29 import java.awt.event.MouseMotionListener;
30 import java.text.NumberFormat;
31 import javax.swing.JLabel;
32 import javax.swing.JPanel;
33 import javax.swing.Timer;
34 import javax.swing.border.BevelBorder;
53 import net.sf.japi.swing.action.ActionBuilder;
54 import net.sf.japi.swing.action.ActionBuilderFactory;
55 import org.jetbrains.annotations.NotNull;
56 import org.jetbrains.annotations.Nullable;
57 
68 public class StatusBar<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends JPanel {
69 
73  private static final long serialVersionUID = 1L;
74 
78  @NotNull
79  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
80 
85  @NotNull
87 
92  @NotNull
94 
99  @NotNull
100  private final JLabel mouse;
101 
106  @NotNull
107  private final JLabel cursor;
108 
113  @NotNull
114  private final JLabel status;
115 
120  @NotNull
121  private final JLabel memory;
122 
126  @NotNull
127  private final Point mouseMapTmp = new Point();
128 
133  @Nullable
135 
139  @NotNull
141 
142  @Override
143  public void mapCursorChangedPos(@NotNull final Point location) {
144  mapCursorChanged(mapView == null ? null : mapView.getMapCursor());
145  }
146 
147  @Override
148  public void mapCursorChangedMode() {
149  mapCursorChanged(mapView == null ? null : mapView.getMapCursor());
150  }
151 
152  @Override
153  public void mapCursorChangedGameObject(@Nullable final MapSquare<G, A, R> mapSquare, @Nullable final G gameObject) {
154  // ignore
155  }
156 
157  @Override
158  public void mapCursorChangedSize() {
159  // ignore
160  }
161 
162  };
163 
167  @NotNull
169 
170  @Override
171  public void currentMapChanged(@Nullable final MapControl<G, A, R> mapControl) {
172  // ignore
173  }
174 
175  @Override
176  public void mapCreated(@NotNull final MapControl<G, A, R> mapControl, final boolean interactive) {
177  setStatusText("Creating new map " + mapControl.getMapModel().getMapArchObject().getMapName() + ".");
178  mapControl.addMapControlListener(mapControlListener);
179  }
180 
181  @Override
182  public void mapClosing(@NotNull final MapControl<G, A, R> mapControl) {
183  // ignore
184  }
185 
186  @Override
187  public void mapClosed(@NotNull final MapControl<G, A, R> mapControl) {
188  mapControl.removeMapControlListener(mapControlListener);
189  }
190 
191  };
192 
196  @NotNull
198 
199  @Override
200  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
201  setCurrentMapView(mapView);
202  }
203 
204  @Override
205  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
206  // ignore
207  }
208 
209  @Override
210  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
211  // ignore
212  }
213 
214  };
215 
219  @NotNull
221 
222  @Override
223  public void saved(@NotNull final DefaultMapControl<G, A, R> mapControl) {
224  final String mapType = mapControl.isPickmap() ? "map" : "pickmap";
225  setStatusText("Saved " + mapType + " '" + mapControl.getMapModel().getMapArchObject().getMapName() + "'.");
226  }
227 
228  };
229 
234  @NotNull
235  private final MouseMotionListener mouseMotionListener = new MouseMotionListener() {
236 
237  @Override
238  public void mouseDragged(@NotNull final MouseEvent e) {
239  mousePosChanged(e);
240  }
241 
242  @Override
243  public void mouseMoved(@NotNull final MouseEvent e) {
244  mousePosChanged(e);
245  }
246 
247  };
248 
253  @NotNull
254  private final ActionListener statusBarUpdate = new ActionListener() {
255 
256  @Override
257  public void actionPerformed(@NotNull final ActionEvent e) {
258  final int archetypeCount = archetypeSet.getArchetypeCount();
259 
260  final int faceObjectsCount = faceObjects.size();
261 
262  final Runtime runtime = Runtime.getRuntime();
263  final long freeMem = runtime.freeMemory();
264  final long totMem = runtime.totalMemory();
265  final long usedMem = totMem - freeMem;
266 
267  memory.setText(ACTION_BUILDER.format("memory", archetypeCount, faceObjectsCount, getMemoryString(usedMem), getMemoryString(freeMem), getMemoryString(totMem)));
268  }
269 
270  };
271 
280  public StatusBar(@NotNull final MapManager<G, A, R> mapManager, @NotNull final MapViewManager<G, A, R> mapViewManager, @NotNull final ArchetypeSet<G, A, R> archetypeSet, @NotNull final NamedObjects<FaceObject> faceObjects) {
281  this.archetypeSet = archetypeSet;
282  this.faceObjects = faceObjects;
283  setLayout(new GridBagLayout());
284  setBorder(new BevelBorder(BevelBorder.LOWERED));
285 
286  final GridBagConstraints gbc = new GridBagConstraints();
287  gbc.fill = GridBagConstraints.HORIZONTAL;
288  gbc.anchor = GridBagConstraints.WEST;
289  gbc.ipadx = 1;
290 
291  gbc.weightx = 0.0;
292  mouse = new JLabel(" ");
293  mouse.setBorder(new BevelBorder(BevelBorder.LOWERED));
294  add(mouse, gbc);
295 
296  cursor = new JLabel(" ");
297  cursor.setBorder(new BevelBorder(BevelBorder.LOWERED));
298  add(cursor, gbc);
299 
300  gbc.weightx = 5.0;
301  status = new JLabel(" ");
302  status.setBorder(new BevelBorder(BevelBorder.LOWERED));
303  add(status, gbc);
304 
305  gbc.weightx = 0.0;
306  memory = new JLabel(" ");
307  memory.setBorder(new BevelBorder(BevelBorder.LOWERED));
308  add(memory, gbc);
309 
310  mapManager.addMapManagerListener(mapManagerListener);
311  mapViewManager.addMapViewManagerListener(mapViewManagerListener);
312 
313  new Timer(5000, statusBarUpdate).start();
314  }
315 
320  public void setStatusText(final String text) {
321  status.setText(text);
322  }
323 
328  @NotNull
329  private static final NumberFormat FORMAT = NumberFormat.getInstance();
330 
331  static {
332  FORMAT.setMinimumFractionDigits(1);
333  FORMAT.setMaximumFractionDigits(1);
334  }
335 
339  @NotNull
340  private static final String[] UNITS = { "Bytes", "KB", "MB", "GB" };
341 
348  @Nullable
349  private static String getMemoryString(final long mem) {
350  for (int i = UNITS.length - 1; i >= 0; i--) {
351  final long m = 1L << (long) (i * 10);
352  if (mem > m) {
353  return FORMAT.format((double) mem / (double) m) + UNITS[i];
354  }
355  }
356  assert false;
357  return null;
358  }
359 
365  private void mapCursorChanged(@Nullable final MapCursor<G, A, R> mapCursor) {
366  final String formatCursor;
367  if (mapCursor == null) {
368  formatCursor = "";
369  } else {
370  final Point pos = mapCursor.getLocation();
371  final int cursorX = pos.x;
372  final int cursorY = pos.y;
373  if (mapCursor.isDragging()) {
374  final Dimension offset = mapCursor.getDragOffset();
375  assert offset != null;
376  final int offsetX = Math.abs(offset.width) + 1;
377  final int offsetY = Math.abs(offset.height) + 1;
378  formatCursor = ACTION_BUILDER.format("statusCursorDragging", cursorX, cursorY, offsetX, offsetY);
379  } else {
380  formatCursor = ACTION_BUILDER.format("statusCursorActive", cursorX, cursorY);
381  }
382  }
383  cursor.setText(formatCursor);
384  }
385 
390  private void mousePosChanged(@NotNull final MouseEvent e) {
391  final MapRenderer renderer = (MapRenderer) e.getSource();
392  if (renderer.getSquareLocationAt(e.getPoint(), mouseMapTmp)) {
393  final int mouseMapX = mouseMapTmp.x;
394  final int mouseMapY = mouseMapTmp.y;
395  mouse.setText(ACTION_BUILDER.format("statusMouseOn", mouseMapX, mouseMapY));
396  } else {
397  mouse.setText(ACTION_BUILDER.format("statusMouseOff"));
398  }
399  }
400 
401  private void setCurrentMapView(@Nullable final MapView<G, A, R> mapView) {
402  if (this.mapView != null) {
403  this.mapView.getMapCursor().removeMapCursorListener(mapCursorListener);
404  this.mapView.getRenderer().removeMouseMotionListener(mouseMotionListener);
405  }
406 
407  this.mapView = mapView;
408 
409  if (this.mapView != null) {
410  this.mapView.getMapCursor().addMapCursorListener(mapCursorListener);
411  this.mapView.getRenderer().addMouseMotionListener(mouseMotionListener);
412  }
413 
414  mapCursorChanged(mapView == null ? null : mapView.getMapCursor());
415  mouse.setText("");
416  }
417 
418 }
final MouseMotionListener mouseMotionListener
The MouseMotionListener for tracking the mouse position in map windows.
Definition: StatusBar.java:235
Implements the main status bar of the application.
Definition: StatusBar.java:68
The data package contains classes for handling data that is organized in a tree.
A MapManager manages all opened maps.
Definition: MapManager.java:37
Graphical User Interface of Gridarta.
StatusBar(@NotNull final MapManager< G, A, R > mapManager, @NotNull final MapViewManager< G, A, R > mapViewManager, @NotNull final ArchetypeSet< G, A, R > archetypeSet, @NotNull final NamedObjects< FaceObject > faceObjects)
Constructs a status bar that has the given main controller object set as its controller.
Definition: StatusBar.java:280
This class manages NamedObjects, managing their tree as well as providing a method for showing a dial...
static final String [] UNITS
The units used by getMemoryString(long).
Definition: StatusBar.java:340
void setCurrentMapView(@Nullable final MapView< G, A, R > mapView)
Definition: StatusBar.java:401
Common interface for FaceObject.
Definition: FaceObject.java:30
final ActionListener statusBarUpdate
The action listener which is registered to periodically update the status bar.
Definition: StatusBar.java:254
int size()
Get the number of objects.
final MapCursorListener< G, A, R > mapCursorListener
The map cursor listener to detect map cursor changes.
Definition: StatusBar.java:140
final JLabel memory
The label that shows the memory status.
Definition: StatusBar.java:121
MapCursor provides methods to move and drag on map.
Definition: MapCursor.java:57
static String getMemoryString(final long mem)
Returns the given memory amount as a string scales the value to be bytes, kilobytes or megabytes...
Definition: StatusBar.java:349
Base package of all Gridarta classes.
void addMapCursorListener(@NotNull final MapCursorListener< G, A, R > listener)
Register a MapCursorListener.
Definition: MapCursor.java:419
Reflects a game object (object on a map).
Definition: GameObject.java:36
int getArchetypeCount()
Returns the number of Archetypes available.
static final NumberFormat FORMAT
The DecimalFormat to use for formatting the numbers in getMemoryString(long).
Definition: StatusBar.java:329
Interface for listeners listening to MapManager changes.
Interface for listeners listening on changes in MapControl instances.
final Point mouseMapTmp
Temporary used to get map coordinates.
Definition: StatusBar.java:127
GameObjects are the objects based on Archetypes found on maps.
Interface for listeners interested in events related to MapViewManager instances. ...
final MapManagerListener< G, A, R > mapManagerListener
The map manager listener to detect current map changes.
Definition: StatusBar.java:168
final MapControlListener< G, A, R > mapControlListener
The MapControlListener used to detect saved maps.
Definition: StatusBar.java:220
final MapViewManagerListener< G, A, R > mapViewManagerListener
The map view manager listener to detect current map changes.
Definition: StatusBar.java:197
void setStatusText(final String text)
Sets the level status text, which usually displays arch numbers.
Definition: StatusBar.java:320
void mousePosChanged(@NotNull final MouseEvent e)
Set new mouse and map coordinates to mouse label.
Definition: StatusBar.java:390
Base classes for rendering maps.
final ArchetypeSet< G, A, R > archetypeSet
The ArchetypeSet.
Definition: StatusBar.java:86
static final long serialVersionUID
The serial Version UID.
Definition: StatusBar.java:73
final JLabel mouse
The label that shows the mouse.
Definition: StatusBar.java:100
MapView< G, A, R > mapView
The map view for which mapCursorListener is registered, or.
Definition: StatusBar.java:134
boolean getSquareLocationAt(@NotNull Point point, @NotNull Point retPoint)
Returns the map location at the given point.
The face is the appearance of an object.
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
static final ActionBuilder ACTION_BUILDER
Action Builder.
Definition: StatusBar.java:79
final JLabel cursor
The label that shows the cursor.
Definition: StatusBar.java:107
Common interface for renderers of map control instances.
Interface that captures similarities between different ArchetypeSet implementations.
Interface for listeners listening to MapCursor related events.
void mapCursorChanged(@Nullable final MapCursor< G, A, R > mapCursor)
Sets the coordinates of the MapCursor to cursor label and the offset when in drag mode...
Definition: StatusBar.java:365
final NamedObjects< FaceObject > faceObjects
The NamedObjects instance to use.
Definition: StatusBar.java:93
final JLabel status
The label that shows the one line text message.
Definition: StatusBar.java:114