Crossfire JXClient, Trunk  R20561
DefaultJXCSkin.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.skin.skin;
23 
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.HashSet;
39 import java.util.Iterator;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
42 
47 public class DefaultJXCSkin implements JXCSkin {
48 
52  @NotNull
53  private String skinName = "unknown";
54 
58  @NotNull
59  private Resolution minResolution = new Resolution(1, 1);
60 
64  @NotNull
65  private Resolution maxResolution = new Resolution(1, 1);
66 
70  private int currentScreenWidth;
71 
75  private int currentScreenHeight;
76 
80  @NotNull
81  private final Collection<CommandList> initEvents = new ArrayList<>();
82 
86  @NotNull
87  private final JXCSkinCache<CommandList> definedCommandLists = new JXCSkinCache<>("command list");
88 
92  @NotNull
93  private final Collection<GUIElement> guiElements = new HashSet<>();
94 
98  @NotNull
99  private final Collection<SkinEvent> skinEvents = new HashSet<>();
100 
104  @NotNull
105  private final Dialogs dialogs;
106 
110  @NotNull
112 
116  @NotNull
118 
122  @NotNull
123  private final Collection<String> optionNames = new HashSet<>();
124 
128  @Nullable
130 
135  @Nullable
137 
144  public DefaultJXCSkin(@NotNull final KeyBindings defaultKeyBindings, @NotNull final OptionManager optionManager, @NotNull final Dialogs dialogs) {
145  this.defaultKeyBindings = defaultKeyBindings;
146  this.optionManager = optionManager;
147  this.dialogs = dialogs;
148  }
149 
153  @NotNull
154  @Override
155  public String getSkinName() {
156  return skinName;
157  }
158 
165  public void setSkinName(@NotNull final String skinName, @NotNull final Resolution minResolution, @NotNull final Resolution maxResolution) {
166  if (minResolution.getWidth() > maxResolution.getWidth()) {
167  throw new IllegalArgumentException("minimum width must not exceed maximum width");
168  }
169  if (minResolution.getHeight() > maxResolution.getHeight()) {
170  throw new IllegalArgumentException("minimum height must not exceed maximum height");
171  }
172 
173  this.skinName = skinName;
174  this.minResolution = minResolution;
175  this.maxResolution = maxResolution;
176  }
177 
181  @NotNull
182  @Override
184  return minResolution;
185  }
186 
190  @NotNull
191  @Override
193  return maxResolution;
194  }
195 
199  @Nullable
200  @Override
201  public Gui getDialogQuit() {
202  try {
203  return getDialog("quit");
204  } catch (final JXCSkinException ignored) {
205  return null;
206  }
207  }
208 
212  @Nullable
213  @Override
215  try {
216  return getDialog("disconnect");
217  } catch (final JXCSkinException ignored) {
218  return null;
219  }
220  }
221 
225  @Nullable
226  @Override
228  try {
229  return getDialog("connect");
230  } catch (final JXCSkinException ignored) {
231  return null;
232  }
233  }
234 
238  @NotNull
239  @Override
241  try {
242  return getDialog("keybind");
243  } catch (final JXCSkinException ex) {
244  final AssertionError error = new AssertionError("keybind dialog does not exist");
245  error.initCause(ex);
246  throw error;
247  }
248  }
249 
253  @NotNull
254  @Override
255  public Gui getDialogQuery() {
256  try {
257  return getDialog("query");
258  } catch (final JXCSkinException ex) {
259  final AssertionError error = new AssertionError("query dialog does not exist");
260  error.initCause(ex);
261  throw error;
262  }
263  }
264 
268  @NotNull
269  @Override
270  public Gui getDialogBook(final int bookNo) {
271  try {
272  return getDialog("book");
273  } catch (final JXCSkinException ex) {
274  final AssertionError error = new AssertionError("book dialog does not exist");
275  error.initCause(ex);
276  throw error;
277  }
278  }
279 
283  @NotNull
284  @Override
286  try {
287  return getDialog("main");
288  } catch (final JXCSkinException ex) {
289  final AssertionError error = new AssertionError("main dialog does not exist");
290  error.initCause(ex);
291  throw error;
292  }
293  }
294 
298  @NotNull
299  @Override
301  try {
302  return getDialog("meta");
303  } catch (final JXCSkinException ex) {
304  final AssertionError error = new AssertionError("meta dialog does not exist");
305  error.initCause(ex);
306  throw error;
307  }
308  }
309 
313  @NotNull
314  @Override
316  try {
317  return getDialog("start");
318  } catch (final JXCSkinException ex) {
319  final AssertionError error = new AssertionError("start dialog does not exist");
320  error.initCause(ex);
321  throw error;
322  }
323  }
324 
328  @NotNull
329  @Override
330  public Gui getDialog(@NotNull final String name) throws JXCSkinException {
331  return dialogs.lookup(name);
332  }
333 
337  @NotNull
338  @Override
339  public Iterator<Gui> iterator() {
340  return dialogs.iterator();
341  }
342 
346  @NotNull
347  @Override
348  public CommandList getCommandList(@NotNull final String name) throws JXCSkinException {
349  return definedCommandLists.lookup(name);
350  }
351 
355  @NotNull
356  @Override
358  return defaultKeyBindings;
359  }
360 
364  @Override
365  public void attach(@NotNull final TooltipManagerImpl tooltipManager) {
366  //noinspection VariableNotUsedInsideIf
367  if (this.tooltipManager != null) {
368  throw new IllegalStateException("skin is already attached");
369  }
370 
371  this.tooltipManager = tooltipManager;
372  tooltipManager.setTooltip(tooltipLabel);
373 
374  initEvents.forEach(CommandList::execute);
375  }
376 
380  @Override
381  public void detach() {
382  final TooltipManagerImpl tmpTooltipManager = tooltipManager;
383  tooltipManager = null;
384  if (tmpTooltipManager != null) {
385  tmpTooltipManager.setTooltip(null);
386  }
387 
388  optionNames.forEach(optionManager::removeOption);
389  optionNames.clear();
390  guiElements.forEach(GUIElement::dispose);
391  skinEvents.forEach(SkinEvent::dispose);
392  guiElements.clear();
393  }
394 
398  @Override
399  public void setScreenSize(final int screenWidth, final int screenHeight) {
400  final int newScreenWidth = Math.max(minResolution.getWidth(), Math.min(maxResolution.getWidth(), screenWidth));
401  final int newScreenHeight = Math.max(minResolution.getHeight(), Math.min(maxResolution.getHeight(), screenHeight));
402  if (currentScreenWidth == newScreenWidth && currentScreenHeight == newScreenHeight) {
403  return;
404  }
405  currentScreenWidth = newScreenWidth;
406  currentScreenHeight = newScreenHeight;
407  }
408 
413  public void insertGuiElement(@NotNull final GUIElement guiElement) {
414  guiElements.add(guiElement);
415  }
416 
421  public void addDialog(@NotNull final String dialogName) {
422  dialogs.addDialog(dialogName);
423  }
424 
429  @Nullable
430  public String getDialogToLoad() {
431  return dialogs.getDialogToLoad();
432  }
433 
440  public void addCommandList(@NotNull final String commandListName, @NotNull final CommandListType commandListType) throws JXCSkinException {
441  final CommandList commandList = new CommandList(commandListType);
442  definedCommandLists.insert(commandListName, commandList);
443  }
444 
449  public void addInitEvent(@NotNull final CommandList commandList) {
450  initEvents.add(commandList);
451  }
452 
460  public void addOption(@NotNull final String optionName, @NotNull final String documentation, @NotNull final Option commandCheckBoxOption) throws JXCSkinException {
461  try {
462  optionManager.addOption(optionName, documentation, commandCheckBoxOption);
463  } catch (final OptionException ex) {
464  throw new JXCSkinException(ex.getMessage(), ex);
465  }
466  optionNames.add(optionName);
467  }
468 
473  public void setTooltipLabel(@Nullable final AbstractLabel tooltipLabel) {
474  this.tooltipLabel = tooltipLabel;
475  }
476 
480  @Nullable
481  @Override
483  return tooltipLabel;
484  }
485 
490  public void addSkinEvent(@NotNull final SkinEvent skinEvent) {
491  skinEvents.add(skinEvent);
492  }
493 
494 }
String getDialogToLoad()
Returns one dialog pending loading.
Definition: Dialogs.java:112
final Collection< CommandList > initEvents
All "event init" commands in execution order.
Abstract base class for all label classes.
void attach(@NotNull final TooltipManagerImpl tooltipManager)
Attaches this skin to a gui manager.the tooltip manager to attach to
Combines a list of GUIElements to for a gui.
Definition: Gui.java:43
Gui getMetaInterface()
Returns the server selection window.the dialog
Interface for events attached to skins.
Definition: SkinEvent.java:28
final Collection< String > optionNames
The defined option names.
void dispose()
Releases all allocated resources.
void addDialog(@NotNull final String dialogName)
Defines a new dialog.
String getSkinName()
Returns a short name for the skin.It is used to construct path or file names. the skin name ...
void insertGuiElement(@NotNull final GUIElement guiElement)
Adds a new GUIElement to this skin.
void setSkinName(@NotNull final String skinName, @NotNull final Resolution minResolution, @NotNull final Resolution maxResolution)
Sets the skin name.
void addOption(@NotNull final String optionName, @NotNull final String documentation, @NotNull final Option commandCheckBoxOption)
Add a new option.
Gui addDialog(@NotNull final String name)
Creates a new dialog instance.
Definition: Dialogs.java:91
void setTooltipLabel(@Nullable final AbstractLabel tooltipLabel)
Sets the AbstractLabel that is used to display tooltips.
final OptionManager optionManager
The OptionManager to use.
void addCommandList(@NotNull final String commandListName, @NotNull final CommandListType commandListType)
Defines a new command list.
TooltipManagerImpl tooltipManager
The TooltipManagerImpl currently attached to or.
void addOption(@NotNull final String optionName, @NotNull final String documentation, @NotNull final Option option)
Adds a new option.
final KeyBindings defaultKeyBindings
The default key bindings.
Gui getDialogQuery()
Returns the dialog for query text input.the dialog
int getWidth()
Returns the width in pixels.
Definition: Resolution.java:87
final JXCSkinCache< CommandList > definedCommandLists
All defined command lists.
Gui getDialogQuit()
Returns the "really quit?" dialog.It is opened when the user presses ESCAPE. the dialog ornull if th...
final Collection< SkinEvent > skinEvents
All SkinEvents attached to this instance.
Defines a JXClient skin consisting of a main Gui and zero or more dialog Guis.
Definition: JXCSkin.java:40
Gui getDialogDisconnect()
Returns the "disconnect from server?" dialog.It is opened when the user presses ESCAPE. the dialog ornull if the dialog does not exist
int getHeight()
Returns the height in pixels.
Definition: Resolution.java:95
CommandList getCommandList(@NotNull final String name)
Returns a named command list.the name of the command list the command list if the command list does n...
Interface defining an abstract GUI element.
Definition: GUIElement.java:32
Gui getDialogBook(final int bookNo)
Returns the popup dialog for readables.the book ID the dialog
Gui lookup(@NotNull final String name)
Returns a dialog instance by dialog name.
Definition: Dialogs.java:81
void insert(@NotNull final String name, @NotNull final T t)
Adds a new element to the cache.
Iterator< Gui > iterator()
Returns all gui instances of this skin.The instances has no defined order. an iterator returning all ...
final Collection< GUIElement > guiElements
All GUI elements.
void addInitEvent(@NotNull final CommandList commandList)
Adds a command list to be executed on "init" events.
Resolution getMaxResolution()
Returns the maximal resolution of this skin.the maximal resolution
KeyBindings getDefaultKeyBindings()
Returns the default key bindings for this skin.the default key bindings
String getDialogToLoad()
Returns one dialog pending loading.
void setScreenSize(final int screenWidth, final int screenHeight)
Updates the skin&#39;s gui elements to a screen size.the new screen width the new screen height ...
Implements a cache for elements identified by name.
Exception thrown if a skin related problem occurs.
DefaultJXCSkin(@NotNull final KeyBindings defaultKeyBindings, @NotNull final OptionManager optionManager, @NotNull final Dialogs dialogs)
Creates a new instance.
Gui getStartInterface()
Returns the start window.the dialog
void addSkinEvent(@NotNull final SkinEvent skinEvent)
Records a SkinEvent attached to this instance.
Information about JXClient&#39;s screen/window resolution.
Definition: Resolution.java:35
Gui getDialogKeyBind()
Returns the key bindings dialog.the dialog
void dispose()
Will be called when the skin is disposed.
AbstractLabel getTooltipLabel()
Returns the AbstractLabel that is used to display tooltips.the label ornull if tooltips are disabled...
void execute()
Execute the command list by calling GUICommand#execute() for each command in order.
Maintains a set of Gui instances representing dialog windows.
Definition: Dialogs.java:38
T lookup(@NotNull final String name)
Looks up an element by name.
void setTooltip(@Nullable final AbstractLabel tooltip)
Sets the tooltip label.
Resolution getMinResolution()
Returns the minimal resolution of this skin.the minimal resolution
Gui getDialogConnect()
Returns the "connection in progress" dialog.It is opened while the connection establishment is in pro...
Gui getDialog(@NotNull final String name)
Returns a dialog by name.the dialog&#39;s name the dialog if the dialog does not exist ...
Gui getMainInterface()
Returns the main window.the dialog