Crossfire JXClient, Trunk
GUIItemList.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.gui.list;
24 
35 import java.awt.event.MouseEvent;
36 import javax.swing.JList;
37 import javax.swing.SwingUtilities;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
40 
46 public abstract class GUIItemList<T extends GUIItemItem> extends GUIList<T> {
47 
51  private static final long serialVersionUID = 1;
52 
56  @NotNull
57  private final ItemView itemView;
58 
62  @Nullable
63  private final AbstractLabel currentItem;
64 
68  @NotNull
69  private final LocationsListener locationsListener = changedSlots -> SwingUtilities.invokeLater(() -> rebuildList(changedSlots));
70 
75  @NotNull
78  setChanged();
79  };
80 
94  protected GUIItemList(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int cellWidth, final int cellHeight, @NotNull final ItemView itemView, @Nullable final AbstractLabel currentItem, @NotNull final GUIItemItem templateItem, @NotNull final GuiFactory guiFactory) {
95  super(tooltipManager, elementListener, name, cellWidth, cellHeight, new ItemItemCellRenderer<>(templateItem), null, guiFactory);
96  this.itemView = itemView;
97  this.currentItem = currentItem;
98  setLayoutOrientation(JList.HORIZONTAL_WRAP, -1);
99  this.itemView.addLocationsListener(locationsListener);
100  rebuildList(null);
101  }
102 
103  @Override
104  public void dispose() {
105  super.dispose();
107  }
108 
113  private void rebuildList(@NotNull final Integer @Nullable [] changedSlots) {
114  synchronized (getTreeLock()) {
115  final int newSize = itemView.getSize();
116  final int oldSize = resizeElements(newSize);
117  if (oldSize < newSize) {
118  for (int i = oldSize; i < newSize; i++) {
119  final T item = newItem(i);
120  addElement(item);
121  item.setChangedListener(itemChangedListener);
122  }
123  setChanged(changedSlots, oldSize);
124  } else {
125  setChanged(changedSlots, newSize);
126  }
128  }
129  setChanged();
130  }
131 
137  private void setChanged(@NotNull final Integer @Nullable [] changedSlots, final int limit) {
138  if (changedSlots == null) {
139  for (int i = 0; i < limit; i++) {
140  setChanged(i);
141  }
142  } else {
143  for (int i : changedSlots) {
144  if (i < limit) {
145  setChanged(i);
146  }
147  }
148  }
149  }
150 
155  private void setChanged(final int index) {
156  getElement(index).setChanged();
157  }
158 
159  @Override
160  protected void selectionChanged(final int selectedIndex) {
161  if (currentItem != null) {
162  final CfItem item = itemView.getItem(selectedIndex);
163  if (item == null) {
164  currentItem.setText("");
165  } else {
166  final String tooltipText1 = item.getTooltipText1();
167  final String tooltipText2 = item.getTooltipText2();
168  final String tooltipText3 = item.getTooltipText3();
169  if (tooltipText3.isEmpty()) {
170  currentItem.setText(tooltipText1+" "+tooltipText3);
171  } else {
172  currentItem.setText(tooltipText1+" ["+tooltipText2+"] "+tooltipText3);
173  }
174  }
175  }
176  }
177 
178  @Override
179  protected void activeChanged() {
180  }
181 
182  @Override
183  public void mouseClicked(@NotNull final MouseEvent e) {
184  super.mouseClicked(e);
185 
186  if (!isEnabled()) {
187  return;
188  }
189 
190  switch (e.getButton()) {
191  case MouseEvent.BUTTON1:
192  setActive(true);
193  button1Clicked(e.getModifiersEx());
194  break;
195 
196  case MouseEvent.BUTTON2:
197  button2Clicked(e.getModifiersEx());
198  break;
199 
200  case MouseEvent.BUTTON3:
201  button3Clicked(e.getModifiersEx());
202  break;
203  }
204  }
205 
210  private void button1Clicked(final int modifiers) {
211  final GUIItemItem guiItem = getSelectedItem();
212  if (guiItem == null) {
213  return;
214  }
215 
216  guiItem.button1Clicked(modifiers);
217  }
218 
223  private void button2Clicked(final int modifiers) {
224  final GUIItemItem guiItem = getSelectedItem();
225  if (guiItem == null) {
226  return;
227  }
228 
229  guiItem.button2Clicked(modifiers);
230  }
231 
236  private void button3Clicked(final int modifiers) {
237  final GUIItemItem guiItem = getSelectedItem();
238  if (guiItem == null) {
239  return;
240  }
241 
242  guiItem.button3Clicked(modifiers);
243  }
244 
249  @Nullable
251  return (GUIItemItem)getSelectedObject();
252  }
253 
259  @NotNull
260  protected abstract T newItem(final int index);
261 
262 }
com.realtime.crossfire.jxclient.gui.list.GUIList< T >::getElement
T getElement(final int index)
Definition: GUIList.java:187
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.label.AbstractLabel
Definition: AbstractLabel.java:43
com.realtime.crossfire.jxclient.items.LocationsListener
Definition: LocationsListener.java:33
com.realtime.crossfire.jxclient.gui.list.GUIItemList.serialVersionUID
static final long serialVersionUID
Definition: GUIItemList.java:51
com.realtime.crossfire.jxclient.gui.list.GUIItemList.button1Clicked
void button1Clicked(final int modifiers)
Definition: GUIItemList.java:210
com.realtime.crossfire.jxclient.gui.list.GUIList< T >::resizeElements
int resizeElements(final int newSize)
Definition: GUIList.java:212
com.realtime.crossfire.jxclient.gui.list.GUIList< T >::setLayoutOrientation
void setLayoutOrientation(final int layoutOrientation, final int visibleRowCount)
Definition: GUIList.java:576
com.realtime.crossfire.jxclient.gui.list.GUIList< T >::getSelectedObject
Object getSelectedObject()
Definition: GUIList.java:588
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.items.CfItem.getTooltipText3
String getTooltipText3()
Definition: CfItem.java:585
com.realtime.crossfire.jxclient.gui.list.GUIItemList.itemView
final ItemView itemView
Definition: GUIItemList.java:57
com.realtime.crossfire.jxclient.gui.item.GUIItem.button3Clicked
abstract void button3Clicked(final int modifiers)
com.realtime.crossfire.jxclient.gui.gui.GUIElementChangedListener
Definition: GUIElementChangedListener.java:30
com.realtime.crossfire.jxclient.gui.label
Definition: AbstractLabel.java:23
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.list.GUIList< T >::guiFactory
final GuiFactory guiFactory
Definition: GUIList.java:85
com.realtime.crossfire.jxclient.gui.list.GUIList< T >::cellHeight
final int cellHeight
Definition: GUIList.java:66
com.realtime.crossfire.jxclient.gui.list.GUIItemList
Definition: GUIItemList.java:46
com.realtime.crossfire.jxclient.gui.list.GUIItemList.rebuildList
void rebuildList(@NotNull final Integer @Nullable[] changedSlots)
Definition: GUIItemList.java:113
com.realtime.crossfire.jxclient.gui.list.GUIItemList.activeChanged
void activeChanged()
Definition: GUIItemList.java:179
com.realtime.crossfire.jxclient.gui.list.GUIItemList.locationsListener
final LocationsListener locationsListener
Definition: GUIItemList.java:69
com.realtime.crossfire.jxclient.items.CfItem.getTooltipText1
String getTooltipText1()
Definition: CfItem.java:555
com.realtime.crossfire.jxclient.gui.list.GUIItemList.getSelectedItem
GUIItemItem getSelectedItem()
Definition: GUIItemList.java:250
com.realtime.crossfire.jxclient.gui.list.GUIItemList.button2Clicked
void button2Clicked(final int modifiers)
Definition: GUIItemList.java:223
com.realtime.crossfire.jxclient.gui.list.GUIItemList.newItem
abstract T newItem(final int index)
com.realtime.crossfire.jxclient.gui.item.GUIItemItem
Definition: GUIItemItem.java:46
com.realtime.crossfire.jxclient.gui.list.GUIItemList.setChanged
void setChanged(final int index)
Definition: GUIItemList.java:155
com.realtime.crossfire.jxclient.gui.list.ItemItemCellRenderer
Definition: ItemItemCellRenderer.java:38
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.list.GUIItemList.selectionChanged
void selectionChanged(final int selectedIndex)
Definition: GUIItemList.java:160
com.realtime.crossfire.jxclient.items.CfItem
Definition: CfItem.java:37
com.realtime.crossfire.jxclient.gui.list.GUIItemList.setChanged
void setChanged(@NotNull final Integer @Nullable[] changedSlots, final int limit)
Definition: GUIItemList.java:137
com.realtime.crossfire.jxclient.gui.list.GUIItemList.dispose
void dispose()
Definition: GUIItemList.java:104
com.realtime.crossfire.jxclient.gui.list.GUIItemList.GUIItemList
GUIItemList(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int cellWidth, final int cellHeight, @NotNull final ItemView itemView, @Nullable final AbstractLabel currentItem, @NotNull final GUIItemItem templateItem, @NotNull final GuiFactory guiFactory)
Definition: GUIItemList.java:94
com.realtime.crossfire.jxclient.gui.list.GUIItemList.currentItem
final AbstractLabel currentItem
Definition: GUIItemList.java:63
com.realtime.crossfire.jxclient.gui.list.GUIList< T >::setChanged
void setChanged()
Definition: GUIList.java:529
com.realtime.crossfire.jxclient.gui.list.GUIItemList.button3Clicked
void button3Clicked(final int modifiers)
Definition: GUIItemList.java:236
com.realtime.crossfire.jxclient.gui.list.GUIItemList.mouseClicked
void mouseClicked(@NotNull final MouseEvent e)
Definition: GUIItemList.java:183
com.realtime.crossfire.jxclient.gui.gui.TooltipManager
Definition: TooltipManager.java:33
com.realtime.crossfire.jxclient.gui.gui.GUIElement
Definition: GUIElement.java:33
com.realtime.crossfire.jxclient.gui.list.GUIList
Definition: GUIList.java:56
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire.jxclient.gui.label.AbstractLabel.setText
void setText(@NotNull final String text)
Definition: AbstractLabel.java:120
com.realtime.crossfire
com.realtime.crossfire.jxclient.gui.list.GUIList< T >::selectionChanged
void selectionChanged()
Definition: GUIList.java:516
com.realtime.crossfire.jxclient.items.ItemView.getSize
int getSize()
com.realtime
com
com.realtime.crossfire.jxclient.items
Definition: AbstractItemView.java:23
com.realtime.crossfire.jxclient.gui.item
Definition: GUIItem.java:23
com.realtime.crossfire.jxclient.gui.item.GUIItem.button1Clicked
abstract void button1Clicked(final int modifiers)
com.realtime.crossfire.jxclient.gui.list.GUIItemList.itemChangedListener
final GUIElementChangedListener itemChangedListener
Definition: GUIItemList.java:76
com.realtime.crossfire.jxclient.items.ItemView
Definition: ItemView.java:32
com.realtime.crossfire.jxclient.items.CfItem.getTooltipText2
String getTooltipText2()
Definition: CfItem.java:564
com.realtime.crossfire.jxclient.gui.list.GUIList< T >::addElement
void addElement( @NotNull final T element)
Definition: GUIList.java:195
com.realtime.crossfire.jxclient.items.ItemView.removeLocationsListener
void removeLocationsListener(@NotNull LocationsListener locationsListener)
com.realtime.crossfire.jxclient.items.ItemView.getItem
CfItem getItem(int index)
com.realtime.crossfire.jxclient.gui.item.GUIItem.button2Clicked
abstract void button2Clicked(final int modifiers)
com.realtime.crossfire.jxclient.gui.gui.GUIElementListener
Definition: GUIElementListener.java:32