Crossfire JXClient, Trunk  R20561
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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.list;
23 
33 import java.awt.event.MouseEvent;
34 import javax.swing.JList;
35 import javax.swing.SwingUtilities;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
44 public abstract class GUIItemList<T extends GUIItemItem> extends GUIList<T> {
45 
49  private static final long serialVersionUID = 1;
50 
54  @NotNull
55  private final ItemView itemView;
56 
60  @Nullable
61  private final AbstractLabel currentItem;
62 
66  @NotNull
67  private final LocationsListener locationsListener = changedSlots -> SwingUtilities.invokeLater(() -> rebuildList(changedSlots));
68 
73  @NotNull
76  setChanged();
77  };
78 
91  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) {
92  super(tooltipManager, elementListener, name, cellWidth, cellHeight, new ItemItemCellRenderer<>(templateItem), null);
93  this.itemView = itemView;
94  this.currentItem = currentItem;
95  setLayoutOrientation(JList.HORIZONTAL_WRAP, -1);
96  this.itemView.addLocationsListener(locationsListener);
97  rebuildList(null);
98  }
99 
103  @Override
104  public void dispose() {
105  super.dispose();
106  itemView.removeLocationsListener(locationsListener);
107  }
108 
113  private void rebuildList(@Nullable final Integer[] 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(@Nullable final Integer[] changedSlots, final int limit) {
138  if (changedSlots == null) {
139  for (int i = 0; i < limit; i++) {
140  setChanged(i);
141  }
142  } else {
143  for (final 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 
162  @Override
163  protected void selectionChanged(final int selectedIndex) {
164  if (currentItem != null) {
165  final CfItem item = itemView.getItem(selectedIndex);
166  if (item == null) {
167  currentItem.setText("");
168  currentItem.setTooltipText("");
169  } else {
170  final String tooltipText1 = item.getTooltipText1();
171  final String tooltipText2 = item.getTooltipText2();
172  final String tooltipText3 = item.getTooltipText3();
173  if (tooltipText3.isEmpty()) {
174  currentItem.setText(tooltipText1+" "+tooltipText3);
175  } else {
176  currentItem.setText(tooltipText1+" ["+tooltipText2+"] "+tooltipText3);
177  }
178  currentItem.setTooltipText(item.getTooltipText());
179  }
180  }
181  }
182 
186  @Override
187  protected void updateTooltip(final int index, final int x, final int y, final int w, final int h) {
188  final CfItem item = itemView.getItem(index);
189  setTooltipText(item == null ? null : item.getTooltipText(), x, y, w, h);
190  }
191 
195  @Override
196  protected void activeChanged() {
197  }
198 
202  @Override
203  public void mouseClicked(@NotNull final MouseEvent e) {
204  super.mouseClicked(e);
205  switch (e.getButton()) {
206  case MouseEvent.BUTTON1:
207  setActive(true);
208  button1Clicked(e.getModifiersEx());
209  break;
210 
211  case MouseEvent.BUTTON2:
212  button2Clicked(e.getModifiersEx());
213  break;
214 
215  case MouseEvent.BUTTON3:
216  button3Clicked(e.getModifiersEx());
217  break;
218  }
219  }
220 
225  private void button1Clicked(final int modifiers) {
226  final GUIItemItem guiItem = getSelectedItem();
227  if (guiItem == null) {
228  return;
229  }
230 
231  guiItem.button1Clicked(modifiers);
232  }
233 
238  private void button2Clicked(final int modifiers) {
239  final GUIItemItem guiItem = getSelectedItem();
240  if (guiItem == null) {
241  return;
242  }
243 
244  guiItem.button2Clicked(modifiers);
245  }
246 
251  private void button3Clicked(final int modifiers) {
252  final GUIItemItem guiItem = getSelectedItem();
253  if (guiItem == null) {
254  return;
255  }
256 
257  guiItem.button3Clicked(modifiers);
258  }
259 
264  @Nullable
266  return (GUIItemItem)getSelectedObject();
267  }
268 
274  @NotNull
275  protected abstract T newItem(final int index);
276 
277 }
String getTooltipText1()
Returns the first line of the tooltip text.
Definition: CfItem.java:504
Abstract base class for all label classes.
String getTooltipText()
Returns a description suitable for a tooltip text.
Definition: CfItem.java:483
void setText(@NotNull final String text)
The label text.
void rebuildList(@Nullable final Integer[] changedSlots)
Rebuilds the list cells.
GUIItemItem getSelectedItem()
Returns the selected GUIItemItem instance.
abstract void button1Clicked(final int modifiers)
Called when the left mouse button was pressed.
final int cellHeight
The height of a list cell in pixels.
Definition: GUIList.java:64
void updateTooltip(final int index, final int x, final int y, final int w, final int h)
A ListCellRenderer that renders GUIMetaElement instances.
void setLayoutOrientation(final int layoutOrientation, final int visibleRowCount)
Sets the layout orientation.
Definition: GUIList.java:556
String getTooltipText3()
Returns the third line of the tooltip text.
Definition: CfItem.java:534
void setChanged(@Nullable final Integer[] changedSlots, final int limit)
Marks some slots as modified.
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)
Creates a new instance.
void button3Clicked(final int modifiers)
Called if the user has clicked the right mouse button.
void button2Clicked(final int modifiers)
Called if the user has clicked the middle mouse button.
A GUIElement instance representing an in-game item.
T getElement(final int index)
Returns the GUIElement for a given index.
Definition: GUIList.java:180
void removeLocationsListener(@NotNull LocationsListener locationsListener)
Removes a LocationsListener to be notified when any displayed item has changed.
void mouseClicked(@NotNull final MouseEvent e)
abstract void button2Clicked(final int modifiers)
Called when the middle mouse button was pressed.
Interface defining an abstract GUI element.
Definition: GUIElement.java:32
int resizeElements(final int newSize)
Changes the number of list elements.
Definition: GUIList.java:205
final ItemView itemView
The ItemView to monitor.
Interface for listeners interested in changed item locations.
final GUIElementChangedListener itemChangedListener
The GUIElementChangedListener attached to all GUIItemItem instances in the list.
abstract void button3Clicked(final int modifiers)
Called when the right mouse button was pressed.
void addElement( @NotNull final T element)
Adds an GUIElement to the list.
Definition: GUIList.java:188
void setChanged(final int index)
Marks one slot as modified.
A GUIList instance that displays GUIItemItem instances.
void setTooltipText(@Nullable final String tooltipText)
Sets the tooltip text to show when the mouse is inside this element.the text to show ornull to disab...
int getSize()
Returns the number of items.
String getTooltipText2()
Returns the second line of the tooltip text.
Definition: CfItem.java:513
void selectionChanged()
Called whenever the selected list entry has changed.
Definition: GUIList.java:488
Interface for listeners interested in the changed flag of GUIElement instances.
CfItem getItem(int index)
Returns the CfItem in a given slot.
abstract T newItem(final int index)
Creates a new GUIElement instance.
Object getSelectedObject()
Returns the selected list object.
Definition: GUIList.java:568
static final long serialVersionUID
The serial version UID.
void button1Clicked(final int modifiers)
Called if the user has clicked the left mouse button.
The representation of a Crossfire Item, client-side.
Definition: CfItem.java:36
final LocationsListener locationsListener
The LocationsListener to be notified about changes.
A GUIElement that displays a list of entries.
Definition: GUIList.java:54
final AbstractLabel currentItem
The label to update with information about the selected item.