com.realtime.crossfire.jxclient.gui.gui
Class JXCWindowRenderer

java.lang.Object
  extended by com.realtime.crossfire.jxclient.gui.gui.JXCWindowRenderer

public class JXCWindowRenderer
extends java.lang.Object

Renders a Gui instance into a Frame.


Nested Class Summary
private  class JXCWindowRenderer.OpenDialogsIterator
          An Iterator that returns all open dialogs in painting order.
 
Field Summary
private  java.awt.event.ActionListener actionListener
          Called periodically to update the display contents.
private  java.awt.image.BufferStrategy bufferStrategy
          The current BufferStrategy.
private  CrossfireUpdateMapListener crossfireUpdateMapListener
          The listener to detect map model changes.
private  Gui currentGui
          The currently displayed Gui.
private  boolean currentGuiChanged
          If set, currentGui has changed.
private  java.io.Writer debugScreen
          The Writer to write screen debug to or null.
private  java.awt.DisplayMode defaultDisplayMode
          The default screen mode that was active when the client did start.
private  boolean forcePaint
          If set, force a full repaint.
private  java.awt.Frame frame
          The associated Frame.
private  java.awt.GraphicsDevice graphicsDevice
          The used GraphicsDevice.
private  java.awt.GraphicsEnvironment graphicsEnvironment
          The used GraphicsEnvironment.
private  GuiAutoCloseListener guiAutoCloseListener
          The GuiAutoCloseListener used to track auto-closing dialogs.
private  boolean inhibitPaintIconified
          If set, do not repaint anything.
private  boolean inhibitPaintMapUpdate
          If set, do not repaint anything.
private  boolean isFullScreen
          Records whether full-screen mode is active.
private  java.awt.event.MouseListener mouseTracker
          The MouseTracker instance.
private  int offsetH
          The y-difference between the visible window and the real window.
private  int offsetW
          The x-difference between the visible window to the real window.
private  int offsetX
          The x-offset of the visible window.
private  int offsetY
          The y-offset of the visible window.
private  java.util.concurrent.CopyOnWriteArrayList<Gui> openDialogs
          Currently opened dialogs.
private  boolean openDialogsChanged
          If set, the content of openDialogs has changed.
private  java.lang.Object redrawSemaphore
          The semaphore used to synchronize map model updates and map view redraws.
private  RendererGuiState rendererGuiState
          The current gui state.
private  java.util.Collection<RendererGuiStateListener> rendererGuiStateListeners
          Listeners to be notified about rendererGuiState changes.
private  java.text.DateFormat simpleDateFormat
          A formatter for timestamps.
private  boolean skippedPaint
          If set, at least one call to redrawGUI() has been dropped while inhibitPaintMapUpdate or inhibitPaintIconified was set.
private  javax.swing.Timer timer
          The timer used to update the display contents.
private  GUIElement tooltip
          The tooltip to use, or null if no tooltips should be shown.
private  boolean wasDisplayed
          Records whether the frame has been displayed before.
private  int windowHeight
          The height of the client area in pixels.
private  int windowWidth
          The width of the client area in pixels.
 
Constructor Summary
JXCWindowRenderer(java.awt.event.MouseListener mouseTracker, java.lang.Object redrawSemaphore, CrossfireServerConnection crossfireServerConnection, java.io.Writer debugScreen)
          Creates a new instance.
 
Method Summary
 GUIText activateCommandInput()
          Activates the command input text field.
 void addGuiStateListener(RendererGuiStateListener listener)
          Adds a gui state listener to be notified about rendererGuiState changes.
 void clearGUI(Gui gui)
          Sets a gui to display and clears the display.
 void closeDialog(Gui dialog)
          Closes a dialog.
 boolean deactivateCommandInput()
          Deactivates the command input text field.
private  void debugScreenWrite(java.lang.CharSequence message)
          Writes a message to the screen debug.
 void endRendering()
          Ends rendering and reverts the display settings.
 GUIElement findElement(java.awt.event.MouseEvent e)
          Finds the gui element for a given MouseEvent.
 Buffer getActiveMessageBuffer()
          Returns the active message buffer.
private static Buffer getActiveMessageBuffer(Gui gui)
          Returns the active message buffer for a Gui instance.
 RendererGuiState getGuiState()
          Returns the current gui state.
private  java.awt.Dimension getMaxWindowDimension(java.awt.Insets frameInsets)
          Returns the maximum dimension of a frame to fit on the screen.
 int getOffsetX()
          Returns the x-offset of the visible window.
 int getOffsetY()
          Returns the y-offset of the visible window.
 java.lang.Iterable<Gui> getOpenDialogs()
          Returns all open dialogs in reverse painting order; the first element is the top-most dialog.
 int getWindowHeight()
          Returns the height of the client area.
 int getWindowWidth()
          Returns the width of the client area.
 boolean handleKeyPress(java.awt.event.KeyEvent e)
           
 boolean handleKeyTyped(java.awt.event.KeyEvent e)
           
 boolean isDialogOpen(Gui dialog)
          Returns whether a given dialog is currently visible.
private  GUIElement manageMouseEvents(Gui gui, java.awt.event.MouseEvent e)
           
private  boolean needRedraw()
          Checks whether any gui element has changed and needs a redraw.
 boolean openDialog(Gui dialog, boolean autoCloseOnDeactivate)
          Opens a dialog.
private  void openDialogsAdd(Gui dialog)
          Adds a dialog to openDialogs.
private  boolean openDialogsRemove(Gui dialog)
          Removes a dialog to openDialogs.
 void raiseDialog(Gui dialog)
          Raises an already opened dialog.
 void redraw(java.awt.Graphics g)
          Paints the view into the given graphics instance.
private  void redrawBlack(java.awt.Graphics g)
           
private  void redrawGUI()
          Redraws the current gui.
private  void redrawGUIBasic(java.awt.Graphics g)
           
private  void redrawGUIDialog(java.awt.Graphics g)
           
private  void redrawTooltip(java.awt.Graphics g)
           
 void repaint()
          Repaints the window.
 void setCurrentGui(Gui gui)
          Sets the Gui to display.
 boolean setFullScreenMode(java.awt.Frame frame, Resolution resolution)
          Tries to switch to the given resolution.
 void setGuiState(RendererGuiState rendererGuiState)
          Sets the current gui state.
 void setInhibitPaintIconified(boolean inhibitPaintIconified)
          Inhibits or allows painting while the main window is iconified.
private  void setResolutionPost(java.awt.Window frame, java.awt.Dimension dimension)
          Tries to switch to the given resolution.
private  void setResolutionPre(java.awt.Window frame)
          Tries to switch to the given resolution.
 void setSelectedHostname(java.lang.String serverName)
          Selects a server entry.
 void setTooltip(GUIElement tooltip)
          Sets the tooltip to use, or null if no tooltips should be shown.
 void setWindowMode(java.awt.Frame frame, Resolution resolution, Resolution minResolution, boolean fixedSize)
          Tries to switch to the given resolution.
private  void showDialog(Gui dialog)
          Makes sure the dialog is fully visible.
 void showDialog(Gui dialog, int x, int y)
          Sets the position of a dialog but makes sure the dialog is fully visible.
 void showDialogAuto(Gui dialog)
          Auto-resizes a dialog (or does nothing for non-auto-size dialogs).
 void start()
          Starts repainting the window.
 void stop()
          Stops repainting the window.
 boolean toggleDialog(Gui dialog)
          Toggles a dialog: if the dialog is not shown, show it; else hide it.
 void updateWindowSize(int windowWidth, int windowHeight)
          Updates the window size for rendering from the main window size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

actionListener

@NotNull
private final java.awt.event.ActionListener actionListener
Called periodically to update the display contents.


bufferStrategy

@Nullable
private java.awt.image.BufferStrategy bufferStrategy
The current BufferStrategy. Set to null until setFullScreenMode(Frame, Resolution) or setWindowMode(Frame, Resolution, Resolution, boolean) has been called.


crossfireUpdateMapListener

@NotNull
private final CrossfireUpdateMapListener crossfireUpdateMapListener
The listener to detect map model changes.


currentGui

@NotNull
private Gui currentGui
The currently displayed Gui.


currentGuiChanged

private boolean currentGuiChanged
If set, currentGui has changed.


debugScreen

@Nullable
private final java.io.Writer debugScreen
The Writer to write screen debug to or null.


defaultDisplayMode

@NotNull
private final java.awt.DisplayMode defaultDisplayMode
The default screen mode that was active when the client did start.


forcePaint

private volatile boolean forcePaint
If set, force a full repaint.


frame

@Nullable
private java.awt.Frame frame
The associated Frame. Set to null while not visible.


graphicsDevice

@NotNull
private final java.awt.GraphicsDevice graphicsDevice
The used GraphicsDevice.


graphicsEnvironment

@NotNull
private final java.awt.GraphicsEnvironment graphicsEnvironment
The used GraphicsEnvironment.


guiAutoCloseListener

@NotNull
private final GuiAutoCloseListener guiAutoCloseListener
The GuiAutoCloseListener used to track auto-closing dialogs.


inhibitPaintIconified

private volatile boolean inhibitPaintIconified
If set, do not repaint anything. It it set while the main window is iconified.


inhibitPaintMapUpdate

private volatile boolean inhibitPaintMapUpdate
If set, do not repaint anything. It it set while a map update is in progress.


isFullScreen

private boolean isFullScreen
Records whether full-screen mode is active.


mouseTracker

@NotNull
private final java.awt.event.MouseListener mouseTracker
The MouseTracker instance.


offsetH

private int offsetH
The y-difference between the visible window and the real window.


offsetW

private int offsetW
The x-difference between the visible window to the real window.


offsetX

private int offsetX
The x-offset of the visible window.


offsetY

private int offsetY
The y-offset of the visible window.


openDialogs

@NotNull
private final java.util.concurrent.CopyOnWriteArrayList<Gui> openDialogs
Currently opened dialogs. The ordering is the painting order: the topmost dialog is at the end.


openDialogsChanged

private boolean openDialogsChanged
If set, the content of openDialogs has changed.


redrawSemaphore

@NotNull
private final java.lang.Object redrawSemaphore
The semaphore used to synchronize map model updates and map view redraws.


rendererGuiState

@NotNull
private RendererGuiState rendererGuiState
The current gui state.


rendererGuiStateListeners

@NotNull
private final java.util.Collection<RendererGuiStateListener> rendererGuiStateListeners
Listeners to be notified about rendererGuiState changes.


simpleDateFormat

@NotNull
private final java.text.DateFormat simpleDateFormat
A formatter for timestamps.


skippedPaint

private volatile boolean skippedPaint
If set, at least one call to redrawGUI() has been dropped while inhibitPaintMapUpdate or inhibitPaintIconified was set.


timer

@NotNull
private final javax.swing.Timer timer
The timer used to update the display contents.


tooltip

@Nullable
private GUIElement tooltip
The tooltip to use, or null if no tooltips should be shown.


wasDisplayed

private boolean wasDisplayed
Records whether the frame has been displayed before.


windowHeight

private int windowHeight
The height of the client area in pixels.


windowWidth

private int windowWidth
The width of the client area in pixels.

Constructor Detail

JXCWindowRenderer

public JXCWindowRenderer(@NotNull
                         java.awt.event.MouseListener mouseTracker,
                         @NotNull
                         java.lang.Object redrawSemaphore,
                         @NotNull
                         CrossfireServerConnection crossfireServerConnection,
                         @Nullable
                         java.io.Writer debugScreen)
Creates a new instance.

Parameters:
mouseTracker - the mouse tracker instance
redrawSemaphore - the semaphore used to synchronized map model updates and map view redraws
crossfireServerConnection - the server connection to monitor
debugScreen - the writer to write screen debug to or null
Method Detail

activateCommandInput

@Nullable
public GUIText activateCommandInput()
Activates the command input text field. If more than one input field exists, the first matching one is selected.

Returns:
the command input text field or null if no command input text field exists

addGuiStateListener

public void addGuiStateListener(@NotNull
                                RendererGuiStateListener listener)
Adds a gui state listener to be notified about rendererGuiState changes.

Parameters:
listener - the listener to add

clearGUI

public void clearGUI(@NotNull
                     Gui gui)
Sets a gui to display and clears the display.

Parameters:
gui - the gui to set

closeDialog

public void closeDialog(@NotNull
                        Gui dialog)
Closes a dialog. Does nothing if the given dialog is not open.

Parameters:
dialog - the dialog to close

deactivateCommandInput

public boolean deactivateCommandInput()
Deactivates the command input text field. Does nothing if the command input text field is not active.

Returns:
whether the command input text field has been deactivated

debugScreenWrite

private void debugScreenWrite(@NotNull
                              java.lang.CharSequence message)
Writes a message to the screen debug.

Parameters:
message - the message to write

endRendering

public void endRendering()
Ends rendering and reverts the display settings.


findElement

@Nullable
public GUIElement findElement(@NotNull
                                       java.awt.event.MouseEvent e)
Finds the gui element for a given MouseEvent. If a gui element was found, update the event mouse coordinates to be relative to the gui element.

Parameters:
e - the mouse event to process
Returns:
the gui element found, or null if none was found

getActiveMessageBuffer

@Nullable
public Buffer getActiveMessageBuffer()
Returns the active message buffer.

Returns:
the active message buffer or null if none is active

getActiveMessageBuffer

@Nullable
private static Buffer getActiveMessageBuffer(@NotNull
                                                      Gui gui)
Returns the active message buffer for a Gui instance.

Parameters:
gui - the gui instance
Returns:
the active message buffer or null

getGuiState

@NotNull
public RendererGuiState getGuiState()
Returns the current gui state.

Returns:
the gui state

getMaxWindowDimension

@NotNull
private java.awt.Dimension getMaxWindowDimension(@NotNull
                                                         java.awt.Insets frameInsets)
Returns the maximum dimension of a frame to fit on the screen.

Parameters:
frameInsets - the frame's insets
Returns:
the maximum dimension

getOffsetX

public int getOffsetX()
Returns the x-offset of the visible window.

Returns:
the x-offset of the visible window

getOffsetY

public int getOffsetY()
Returns the y-offset of the visible window.

Returns:
the y-offset of the visible window

getOpenDialogs

@NotNull
public java.lang.Iterable<Gui> getOpenDialogs()
Returns all open dialogs in reverse painting order; the first element is the top-most dialog.

Returns:
the open dialogs; client code must not modify this list

getWindowHeight

public int getWindowHeight()
Returns the height of the client area.

Returns:
the height in pixels

getWindowWidth

public int getWindowWidth()
Returns the width of the client area.

Returns:
the width in pixels

handleKeyPress

public boolean handleKeyPress(@NotNull
                              java.awt.event.KeyEvent e)

handleKeyTyped

public boolean handleKeyTyped(@NotNull
                              java.awt.event.KeyEvent e)

isDialogOpen

public boolean isDialogOpen(@NotNull
                            Gui dialog)
Returns whether a given dialog is currently visible.

Parameters:
dialog - the dialog to check
Returns:
whether the dialog is visible

manageMouseEvents

@Nullable
private GUIElement manageMouseEvents(@NotNull
                                              Gui gui,
                                              @NotNull
                                              java.awt.event.MouseEvent e)

needRedraw

private boolean needRedraw()
Checks whether any gui element has changed and needs a redraw.

Returns:
whether any gui element has changed

openDialog

public boolean openDialog(@NotNull
                          Gui dialog,
                          boolean autoCloseOnDeactivate)
Opens a dialog. Raises an already opened dialog.

Parameters:
dialog - the dialog to show
autoCloseOnDeactivate - whether the dialog should auto-close when it becomes inactive; ignored if the dialog is already open
Returns:
whether the dialog was opened or raised; false if the dialog already was opened as the topmost dialog

openDialogsAdd

private void openDialogsAdd(@NotNull
                            Gui dialog)
Adds a dialog to openDialogs. Generates mouse events if necessary.

Parameters:
dialog - the dialog

openDialogsRemove

private boolean openDialogsRemove(@NotNull
                                  Gui dialog)
Removes a dialog to openDialogs. Generates mouse events if necessary.

Parameters:
dialog - the dialog
Returns:
whether the dialog was opened

raiseDialog

public void raiseDialog(@NotNull
                        Gui dialog)
Raises an already opened dialog.

Parameters:
dialog - the dialog to show

redraw

public void redraw(@NotNull
                   java.awt.Graphics g)
Paints the view into the given graphics instance.

Parameters:
g - the graphics instance to paint to

redrawBlack

private void redrawBlack(@NotNull
                         java.awt.Graphics g)

redrawGUI

private void redrawGUI()
Redraws the current gui.


redrawGUIBasic

private void redrawGUIBasic(@NotNull
                            java.awt.Graphics g)

redrawGUIDialog

private void redrawGUIDialog(@NotNull
                             java.awt.Graphics g)

redrawTooltip

private void redrawTooltip(@NotNull
                           java.awt.Graphics g)

repaint

public void repaint()
Repaints the window.


setCurrentGui

public void setCurrentGui(@NotNull
                          Gui gui)
Sets the Gui to display.

Parameters:
gui - the gui to display

setFullScreenMode

public boolean setFullScreenMode(@NotNull
                                 java.awt.Frame frame,
                                 @Nullable
                                 Resolution resolution)
Tries to switch to the given resolution. If resolution switching fails, the window might be invisible.

Parameters:
frame - the associated frame
resolution - the resolution to switch to; null to keep current resolution
Returns:
whether the resolution has been changed

setGuiState

public void setGuiState(@NotNull
                        RendererGuiState rendererGuiState)
Sets the current gui state.

Parameters:
rendererGuiState - the gui state

setInhibitPaintIconified

public void setInhibitPaintIconified(boolean inhibitPaintIconified)
Inhibits or allows painting while the main window is iconified.

Parameters:
inhibitPaintIconified - whether the main window is iconified

setResolutionPost

private void setResolutionPost(@NotNull
                               java.awt.Window frame,
                               @NotNull
                               java.awt.Dimension dimension)
Tries to switch to the given resolution. If resolution switching fails, the window might be invisible.

Parameters:
frame - the associated frame
dimension - the window size to switch to

setResolutionPre

private void setResolutionPre(@NotNull
                              java.awt.Window frame)
Tries to switch to the given resolution. If resolution switching fails, the window might be invisible.

Parameters:
frame - the associated frame

setSelectedHostname

public void setSelectedHostname(@NotNull
                                java.lang.String serverName)
Selects a server entry.

Parameters:
serverName - the server name to select

setTooltip

public void setTooltip(@Nullable
                       GUIElement tooltip)
Sets the tooltip to use, or null if no tooltips should be shown.

Parameters:
tooltip - the tooltip to use, or null

setWindowMode

public void setWindowMode(@NotNull
                          java.awt.Frame frame,
                          @Nullable
                          Resolution resolution,
                          @NotNull
                          Resolution minResolution,
                          boolean fixedSize)
Tries to switch to the given resolution. If resolution switching fails, the window might be invisible.

Parameters:
frame - the associated frame
resolution - the resolution to switch to, null for default
minResolution - the minimal supported resolution
fixedSize - whether the window should have fixed size

showDialog

private void showDialog(@NotNull
                        Gui dialog)
Makes sure the dialog is fully visible.

Parameters:
dialog - the dialog to show

showDialog

public void showDialog(@NotNull
                       Gui dialog,
                       int x,
                       int y)
Sets the position of a dialog but makes sure the dialog is fully visible.

Parameters:
dialog - the dialog to show
x - the dialog's x coordinate
y - the dialog's y coordinate

showDialogAuto

public void showDialogAuto(@NotNull
                           Gui dialog)
Auto-resizes a dialog (or does nothing for non-auto-size dialogs). Then makes sure the dialog is fully visible.

Parameters:
dialog - the dialog to show

start

public void start()
Starts repainting the window.


stop

public void stop()
Stops repainting the window.


toggleDialog

public boolean toggleDialog(@NotNull
                            Gui dialog)
Toggles a dialog: if the dialog is not shown, show it; else hide it.

Parameters:
dialog - the dialog to toggle
Returns:
whether the dialog is shown

updateWindowSize

public void updateWindowSize(int windowWidth,
                             int windowHeight)
Updates the window size for rendering from the main window size.

Parameters:
windowWidth - the window width including insets
windowHeight - the window height including insets