00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.skin.skin;
00023
00024 import com.realtime.crossfire.jxclient.gui.commandlist.CommandList;
00025 import com.realtime.crossfire.jxclient.gui.commandlist.CommandListType;
00026 import com.realtime.crossfire.jxclient.gui.gui.GUIElement;
00027 import com.realtime.crossfire.jxclient.gui.gui.Gui;
00028 import com.realtime.crossfire.jxclient.gui.keybindings.KeyBindings;
00029 import com.realtime.crossfire.jxclient.gui.label.AbstractLabel;
00030 import com.realtime.crossfire.jxclient.gui.label.TooltipManagerImpl;
00031 import com.realtime.crossfire.jxclient.settings.options.Option;
00032 import com.realtime.crossfire.jxclient.settings.options.OptionException;
00033 import com.realtime.crossfire.jxclient.settings.options.OptionManager;
00034 import com.realtime.crossfire.jxclient.skin.events.SkinEvent;
00035 import com.realtime.crossfire.jxclient.util.Resolution;
00036 import java.util.ArrayList;
00037 import java.util.Collection;
00038 import java.util.HashSet;
00039 import java.util.Iterator;
00040 import org.jetbrains.annotations.NotNull;
00041 import org.jetbrains.annotations.Nullable;
00042
00047 public class DefaultJXCSkin implements JXCSkin {
00048
00052 @NotNull
00053 private String skinName = "unknown";
00054
00058 @NotNull
00059 private Resolution minResolution = new Resolution(1, 1);
00060
00064 @NotNull
00065 private Resolution maxResolution = new Resolution(1, 1);
00066
00070 private int currentScreenWidth = 0;
00071
00075 private int currentScreenHeight = 0;
00076
00080 @NotNull
00081 private final Collection<CommandList> initEvents = new ArrayList<CommandList>();
00082
00086 @NotNull
00087 private final JXCSkinCache<CommandList> definedCommandLists = new JXCSkinCache<CommandList>("command list");
00088
00092 @NotNull
00093 private final Collection<GUIElement> guiElements = new HashSet<GUIElement>();
00094
00098 @NotNull
00099 private final Collection<SkinEvent> skinEvents = new HashSet<SkinEvent>();
00100
00104 @NotNull
00105 private final Dialogs dialogs;
00106
00110 @NotNull
00111 private final KeyBindings defaultKeyBindings;
00112
00116 @NotNull
00117 private final OptionManager optionManager;
00118
00122 @NotNull
00123 private final Collection<String> optionNames = new HashSet<String>();
00124
00128 @Nullable
00129 private AbstractLabel tooltipLabel = null;
00130
00135 @Nullable
00136 private TooltipManagerImpl tooltipManager = null;
00137
00144 public DefaultJXCSkin(@NotNull final KeyBindings defaultKeyBindings, @NotNull final OptionManager optionManager, @NotNull final Dialogs dialogs) {
00145 this.defaultKeyBindings = defaultKeyBindings;
00146 this.optionManager = optionManager;
00147 this.dialogs = dialogs;
00148 }
00149
00153 @NotNull
00154 @Override
00155 public String getSkinName() {
00156 return skinName;
00157 }
00158
00165 public void setSkinName(@NotNull final String skinName, @NotNull final Resolution minResolution, @NotNull final Resolution maxResolution) {
00166 if (minResolution.getWidth() > maxResolution.getWidth()) {
00167 throw new IllegalArgumentException("minimum width must not exceed maximum width");
00168 }
00169 if (minResolution.getHeight() > maxResolution.getHeight()) {
00170 throw new IllegalArgumentException("minimum height must not exceed maximum height");
00171 }
00172
00173 this.skinName = skinName;
00174 this.minResolution = minResolution;
00175 this.maxResolution = maxResolution;
00176 }
00177
00181 @NotNull
00182 @Override
00183 public Resolution getMinResolution() {
00184 return minResolution;
00185 }
00186
00190 @NotNull
00191 @Override
00192 public Resolution getMaxResolution() {
00193 return maxResolution;
00194 }
00195
00199 @Nullable
00200 @Override
00201 public Gui getDialogQuit() {
00202 try {
00203 return getDialog("quit");
00204 } catch (final JXCSkinException ignored) {
00205 return null;
00206 }
00207 }
00208
00212 @Nullable
00213 @Override
00214 public Gui getDialogDisconnect() {
00215 try {
00216 return getDialog("disconnect");
00217 } catch (final JXCSkinException ignored) {
00218 return null;
00219 }
00220 }
00221
00225 @Nullable
00226 @Override
00227 public Gui getDialogConnect() {
00228 try {
00229 return getDialog("connect");
00230 } catch (final JXCSkinException ignored) {
00231 return null;
00232 }
00233 }
00234
00238 @NotNull
00239 @Override
00240 public Gui getDialogKeyBind() {
00241 try {
00242 return getDialog("keybind");
00243 } catch (final JXCSkinException ex) {
00244 final AssertionError error = new AssertionError("keybind dialog does not exist");
00245 error.initCause(ex);
00246 throw error;
00247 }
00248 }
00249
00253 @NotNull
00254 @Override
00255 public Gui getDialogQuery() {
00256 try {
00257 return getDialog("query");
00258 } catch (final JXCSkinException ex) {
00259 final AssertionError error = new AssertionError("query dialog does not exist");
00260 error.initCause(ex);
00261 throw error;
00262 }
00263 }
00264
00268 @NotNull
00269 @Override
00270 public Gui getDialogBook(final int bookNo) {
00271 try {
00272 return getDialog("book");
00273 } catch (final JXCSkinException ex) {
00274 final AssertionError error = new AssertionError("book dialog does not exist");
00275 error.initCause(ex);
00276 throw error;
00277 }
00278 }
00279
00283 @NotNull
00284 @Override
00285 public Gui getMainInterface() {
00286 try {
00287 return getDialog("main");
00288 } catch (final JXCSkinException ex) {
00289 final AssertionError error = new AssertionError("main dialog does not exist");
00290 error.initCause(ex);
00291 throw error;
00292 }
00293 }
00294
00298 @NotNull
00299 @Override
00300 public Gui getMetaInterface() {
00301 try {
00302 return getDialog("meta");
00303 } catch (final JXCSkinException ex) {
00304 final AssertionError error = new AssertionError("meta dialog does not exist");
00305 error.initCause(ex);
00306 throw error;
00307 }
00308 }
00309
00313 @NotNull
00314 @Override
00315 public Gui getStartInterface() {
00316 try {
00317 return getDialog("start");
00318 } catch (final JXCSkinException ex) {
00319 final AssertionError error = new AssertionError("start dialog does not exist");
00320 error.initCause(ex);
00321 throw error;
00322 }
00323 }
00324
00328 @NotNull
00329 @Override
00330 public Gui getDialog(@NotNull final String name) throws JXCSkinException {
00331 return dialogs.lookup(name);
00332 }
00333
00337 @NotNull
00338 @Override
00339 public Iterator<Gui> iterator() {
00340 return dialogs.iterator();
00341 }
00342
00346 @NotNull
00347 @Override
00348 public CommandList getCommandList(@NotNull final String name) throws JXCSkinException {
00349 return definedCommandLists.lookup(name);
00350 }
00351
00355 @NotNull
00356 @Override
00357 public KeyBindings getDefaultKeyBindings() {
00358 return defaultKeyBindings;
00359 }
00360
00364 @Override
00365 public void attach(@NotNull final TooltipManagerImpl tooltipManager) {
00366 if (this.tooltipManager != null) {
00367 throw new IllegalStateException("skin is already attached");
00368 }
00369
00370 this.tooltipManager = tooltipManager;
00371 tooltipManager.setTooltip(tooltipLabel);
00372
00373 for (final CommandList commandList : initEvents) {
00374 commandList.execute();
00375 }
00376 }
00377
00381 @Override
00382 public void detach() {
00383 final TooltipManagerImpl tmpTooltipManager = tooltipManager;
00384 tooltipManager = null;
00385 if (tmpTooltipManager != null) {
00386 tmpTooltipManager.setTooltip(null);
00387 }
00388
00389 for (final String optionName : optionNames) {
00390 optionManager.removeOption(optionName);
00391 }
00392 optionNames.clear();
00393 for (final GUIElement guiElement : guiElements) {
00394 guiElement.dispose();
00395 }
00396 for (final SkinEvent skinEvent : skinEvents) {
00397 skinEvent.dispose();
00398 }
00399 guiElements.clear();
00400 }
00401
00405 @Override
00406 public void setScreenSize(final int screenWidth, final int screenHeight) {
00407 final int newScreenWidth = Math.max(minResolution.getWidth(), Math.min(maxResolution.getWidth(), screenWidth));
00408 final int newScreenHeight = Math.max(minResolution.getHeight(), Math.min(maxResolution.getHeight(), screenHeight));
00409 if (currentScreenWidth == newScreenWidth && currentScreenHeight == newScreenHeight) {
00410 return;
00411 }
00412 currentScreenWidth = newScreenWidth;
00413 currentScreenHeight = newScreenHeight;
00414 }
00415
00420 public void insertGuiElement(@NotNull final GUIElement guiElement) {
00421 guiElements.add(guiElement);
00422 }
00423
00428 public void addDialog(@NotNull final String dialogName) {
00429 dialogs.addDialog(dialogName);
00430 }
00431
00436 @Nullable
00437 public String getDialogToLoad() {
00438 return dialogs.getDialogToLoad();
00439 }
00440
00447 public void addCommandList(@NotNull final String commandListName, @NotNull final CommandListType commandListType) throws JXCSkinException {
00448 final CommandList commandList = new CommandList(commandListType);
00449 definedCommandLists.insert(commandListName, commandList);
00450 }
00451
00456 public void addInitEvent(@NotNull final CommandList commandList) {
00457 initEvents.add(commandList);
00458 }
00459
00467 public void addOption(@NotNull final String optionName, @NotNull final String documentation, @NotNull final Option commandCheckBoxOption) throws JXCSkinException {
00468 try {
00469 optionManager.addOption(optionName, documentation, commandCheckBoxOption);
00470 } catch (final OptionException ex) {
00471 throw new JXCSkinException(ex.getMessage());
00472 }
00473 optionNames.add(optionName);
00474 }
00475
00480 public void setTooltipLabel(@Nullable final AbstractLabel tooltipLabel) {
00481 this.tooltipLabel = tooltipLabel;
00482 }
00483
00487 @Nullable
00488 @Override
00489 public AbstractLabel getTooltipLabel() {
00490 return tooltipLabel;
00491 }
00492
00497 public void addSkinEvent(@NotNull final SkinEvent skinEvent) {
00498 skinEvents.add(skinEvent);
00499 }
00500
00501 }