Crossfire JXClient, Trunk
GuiFactory.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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.skin.skin;
24 
29 import java.awt.Component;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import javax.swing.GroupLayout;
33 import javax.swing.JComponent;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
41 public class GuiFactory {
42 
47 
51  @NotNull
52  private final Collection<Gui> guis = new ArrayList<>();
53 
59  this.guiCommandFactory = guiCommandFactory;
60  }
61 
67  @NotNull
68  public Gui newGui(@NotNull final String name) {
69  final Gui gui = new Gui(name, new KeyBindings(null, null, guiCommandFactory));
70  final JComponent component = gui.getComponent();
71  component.setLayout(new GroupLayout(component));
72  guis.add(gui);
73  return gui;
74  }
75 
81  public int getElementX(@NotNull final AbstractGUIElement element) {
82  final Gui gui = getGui(element);
83  int x = gui == null ? 0 : gui.getComponent().getX();
84  for (Component component = element; component != null && getGuiFromComponent(component) == null; component = component.getParent()) {
85  x += component.getX();
86  }
87  return x;
88  }
89 
95  public int getElementY(@NotNull final AbstractGUIElement element) {
96  final Gui gui = getGui(element);
97  int y = gui == null ? 0 : gui.getComponent().getY();
98  for (Component component = element; component != null && getGuiFromComponent(component) == null; component = component.getParent()) {
99  y += component.getY();
100  }
101  return y;
102  }
103 
109  @Nullable
110  public Gui getGui(@NotNull final AbstractGUIElement element) {
111  for (Component component = element; component != null; component = component.getParent()) {
112  final Gui gui = getGuiFromComponent(component);
113  if (gui != null) {
114  return gui;
115  }
116  }
117  return null;
118  }
119 
125  @Nullable
126  private Gui getGuiFromComponent(@NotNull final Component component) {
127  for (final Gui gui : guis) {
128  if (gui.getComponent() == component) {
129  return gui;
130  }
131 
132  final AbstractGUIElement help = gui.getHelp();
133  if (help == component) {
134  return gui;
135  }
136  }
137  return null;
138  }
139 
140 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.skin.skin.GuiFactory.newGui
Gui newGui(@NotNull final String name)
Definition: GuiFactory.java:68
com.realtime.crossfire.jxclient.gui.gui.Gui
Definition: Gui.java:49
com.realtime.crossfire.jxclient.skin.skin.GuiFactory.guis
final Collection< Gui > guis
Definition: GuiFactory.java:52
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.commandlist
Definition: CommandList.java:23
com.realtime.crossfire.jxclient.gui.keybindings
Definition: InvalidKeyBindingException.java:23
com.realtime.crossfire.jxclient.skin.skin.GuiFactory.getGuiFromComponent
Gui getGuiFromComponent(@NotNull final Component component)
Definition: GuiFactory.java:126
com.realtime.crossfire.jxclient.skin.skin.GuiFactory.getGui
Gui getGui(@NotNull final AbstractGUIElement element)
Definition: GuiFactory.java:110
com.realtime.crossfire.jxclient.gui.commandlist.GUICommandFactory
Definition: GUICommandFactory.java:32
com.realtime.crossfire.jxclient.gui.gui.Gui.getComponent
JComponent getComponent()
Definition: Gui.java:161
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.skin.skin.GuiFactory.GuiFactory
GuiFactory(@NotNull final GUICommandFactory guiCommandFactory)
Definition: GuiFactory.java:58
com.realtime.crossfire.jxclient.skin.skin.GuiFactory.getElementY
int getElementY(@NotNull final AbstractGUIElement element)
Definition: GuiFactory.java:95
com.realtime.crossfire.jxclient.skin.skin.GuiFactory.getElementX
int getElementX(@NotNull final AbstractGUIElement element)
Definition: GuiFactory.java:81
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.gui.keybindings.KeyBindings
Definition: KeyBindings.java:47
com.realtime.crossfire.jxclient.skin.skin.GuiFactory.guiCommandFactory
final GUICommandFactory guiCommandFactory
Definition: GuiFactory.java:46
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement
Definition: AbstractGUIElement.java:37