Crossfire JXClient, Trunk  R20561
DialogFactory.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.factory;
23 
30 import java.awt.Color;
31 import java.awt.Font;
32 import java.awt.Image;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
42 public class DialogFactory {
43 
47  @NotNull
48  private final Image frameNW;
49 
53  @NotNull
54  private final Image frameN;
55 
59  @NotNull
60  private final Image frameNE;
61 
65  @NotNull
66  private final Image frameW;
67 
71  @NotNull
72  private final Image frameC;
73 
77  @NotNull
78  private final Image frameE;
79 
83  @NotNull
84  private final Image frameSW;
85 
89  @NotNull
90  private final Image frameS;
91 
95  @NotNull
96  private final Image frameSE;
97 
101  private final Font titleFont;
102 
106  private final Color titleColor;
107 
111  @Nullable
112  private final Color titleBackgroundColor;
113 
118  private final float frameAlpha;
119 
137  public DialogFactory(@NotNull final Image frameNW, @NotNull final Image frameN, @NotNull final Image frameNE, @NotNull final Image frameW, @NotNull final Image frameC, @NotNull final Image frameE, @NotNull final Image frameSW, @NotNull final Image frameS, @NotNull final Image frameSE, @NotNull final Font titleFont, @NotNull final Color titleColor, @NotNull final Color titleBackgroundColor, final float frameAlpha) {
138  this.frameNW = frameNW;
139  this.frameN = frameN;
140  this.frameNE = frameNE;
141  this.frameW = frameW;
142  this.frameC = frameC;
143  this.frameE = frameE;
144  this.frameSW = frameSW;
145  this.frameS = frameS;
146  this.frameSE = frameSE;
147  this.frameAlpha = frameAlpha;
148  if (frameAlpha < 0.0F || frameAlpha > 1.0F) {
149  throw new IllegalArgumentException("alpha transparency should be between 0 and 1 inclusive");
150  }
151  final int sizeN = frameN.getHeight(null);
152  final int sizeS = frameS.getHeight(null);
153  final int sizeW = frameW.getWidth(null);
154  final int sizeE = frameE.getWidth(null);
155  if (frameNW.getHeight(null) != sizeN) {
156  throw new IllegalArgumentException("height of NW ("+frameNW.getHeight(null)+") does not match height of N ("+sizeN+")");
157  }
158  if (frameNE.getHeight(null) != sizeN) {
159  throw new IllegalArgumentException("height of NE ("+frameNE.getHeight(null)+") does not match height of N ("+sizeN+")");
160  }
161  if (frameSW.getHeight(null) != sizeS) {
162  throw new IllegalArgumentException("height of SW ("+frameSW.getHeight(null)+") does not match height of N ("+sizeS+")");
163  }
164  if (frameSE.getHeight(null) != sizeS) {
165  throw new IllegalArgumentException("height of SE ("+frameSE.getHeight(null)+") does not match height of N ("+sizeS+")");
166  }
167  if (frameNW.getWidth(null) != sizeW) {
168  throw new IllegalArgumentException("width of NW ("+frameNW.getWidth(null)+") does not match width of W");
169  }
170  if (frameNE.getWidth(null) != sizeE) {
171  throw new IllegalArgumentException("width of NE ("+frameNE.getWidth(null)+") does not match width of E");
172  }
173  if (frameSW.getWidth(null) != sizeW) {
174  throw new IllegalArgumentException("width of SW ("+frameSW.getWidth(null)+") does not match width of W");
175  }
176  if (frameSE.getWidth(null) != sizeE) {
177  throw new IllegalArgumentException("width of SE ("+frameSE.getWidth(null)+") does not match width of E");
178  }
179  final int contentWidth = frameC.getWidth(null);
180  final int contentHeight = frameC.getHeight(null);
181  if (frameN.getWidth(null) != contentWidth) {
182  throw new IllegalArgumentException("width of N ("+frameN.getWidth(null)+") does not match width of C ("+contentWidth+")");
183  }
184  if (frameS.getWidth(null) != contentWidth) {
185  throw new IllegalArgumentException("width of S ("+frameS.getWidth(null)+") does not match width of C ("+contentWidth+")");
186  }
187  if (frameW.getHeight(null) != contentHeight) {
188  throw new IllegalArgumentException("width of W ("+frameW.getHeight(null)+") does not match height of C ("+contentHeight+")");
189  }
190  if (frameE.getHeight(null) != contentHeight) {
191  throw new IllegalArgumentException("width of E ("+frameE.getHeight(null)+") does not match height of C ("+contentHeight+")");
192  }
193  this.titleFont = titleFont;
194  this.titleColor = titleColor;
195  final int intAlpha = (int)(255*frameAlpha);
196  this.titleBackgroundColor = intAlpha == 0 ? null : new Color(titleBackgroundColor.getRed(), titleBackgroundColor.getGreen(), titleBackgroundColor.getBlue(), intAlpha);
197  }
198 
207  @NotNull
208  public Iterable<AbstractGUIElement> newDialog(@NotNull final TooltipManager tooltipManager, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final GUIElementListener elementListener, @NotNull final String title) {
209  final Collection<AbstractGUIElement> result = new ArrayList<>();
210  result.add(new GUIDialogBackground(tooltipManager, elementListener, "dialog_background", frameAlpha, frameNW, frameN, frameNE, frameW, frameC, frameE, frameSW, frameS, frameSE));
211 
212  if (!title.isEmpty()) {
213  result.add(new GUIDialogTitle(tooltipManager, windowRenderer, elementListener, "dialog_title", titleFont, titleColor, titleBackgroundColor, title));
214  }
215 
216  return result;
217  }
218 
223  public float getFrameAlpha() {
224  return frameAlpha;
225  }
226 
227 }
final Image frameSE
The south-east frame picture.
Iterable< AbstractGUIElement > newDialog(@NotNull final TooltipManager tooltipManager, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final GUIElementListener elementListener, @NotNull final String title)
Creates a new dialog.
float getFrameAlpha()
Returns the alpha value for the frame background.
final Image frameNE
The north-east frame picture.
final Image frameSW
The south-west frame picture.
A dialog title that allows to move the dialog.
final float frameAlpha
The alpha transparency for the dialog background, 1 is opaque and 0 is transparent.
final Image frameNW
The north-west frame picture.
final Color titleBackgroundColor
The background color for the dialog title.
Abstract base class for GUI elements to be shown in Guis.
DialogFactory(@NotNull final Image frameNW, @NotNull final Image frameN, @NotNull final Image frameNE, @NotNull final Image frameW, @NotNull final Image frameC, @NotNull final Image frameE, @NotNull final Image frameSW, @NotNull final Image frameS, @NotNull final Image frameSE, @NotNull final Font titleFont, @NotNull final Color titleColor, @NotNull final Color titleBackgroundColor, final float frameAlpha)
Creates a new instance.
final Font titleFont
The font for the dialog title.
A factory class to create "textbutton" instances.
final Color titleColor
The color for the dialog title.