22 package com.realtime.crossfire.jxclient.gui.list;
32 import java.awt.Adjustable;
33 import java.awt.Component;
34 import java.awt.Dimension;
35 import java.awt.Rectangle;
36 import java.awt.Transparency;
37 import java.awt.event.MouseEvent;
38 import javax.swing.DefaultListModel;
39 import javax.swing.JList;
40 import javax.swing.JScrollPane;
41 import javax.swing.JViewport;
42 import javax.swing.ListSelectionModel;
43 import javax.swing.ScrollPaneConstants;
44 import javax.swing.border.EmptyBorder;
45 import javax.swing.event.ListSelectionListener;
46 import org.jetbrains.annotations.NotNull;
47 import org.jetbrains.annotations.Nullable;
83 private final DefaultListModel<T>
model =
new DefaultListModel<>();
133 super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT);
138 list.setCellRenderer(listCellRenderer);
139 list.setFixedCellWidth(cellWidth);
140 list.setFixedCellHeight(cellHeight);
141 list.setOpaque(
false);
142 list.setFocusable(
false);
143 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
144 list.addListSelectionListener(listSelectionListener);
146 viewport.setView(list);
147 viewport.setScrollMode(JViewport.BLIT_SCROLL_MODE);
148 viewport.setOpaque(
false);
149 viewport.setFocusable(
false);
151 scrollPane =
new JScrollPane(null, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
152 scrollPane.setViewport(viewport);
153 scrollPane.setOpaque(
false);
154 scrollPane.setFocusable(
false);
155 scrollPane.setBorder(
new EmptyBorder(0, 0, 0, 0));
159 listCellRenderer.setSize(getWidth(), cellHeight);
168 list.removeListSelectionListener(listSelectionListener);
169 synchronized (getTreeLock()) {
181 return model.get(index);
189 assert Thread.holdsLock(getTreeLock());
190 model.addElement(element);
191 list.setSize(getWidth(), Integer.MAX_VALUE);
193 if (model.getSize() == 1) {
206 assert Thread.holdsLock(getTreeLock());
207 final int index = list.getSelectedIndex();
208 final int oldSize = model.getSize();
209 if (newSize < oldSize) {
210 for (
int i = newSize; i < oldSize; i++) {
216 model.removeRange(newSize, oldSize-1);
217 list.setSize(getWidth(), Integer.MAX_VALUE);
218 if (index >= newSize && newSize > 0) {
233 synchronized (getTreeLock()) {
235 switch (list.getLayoutOrientation()) {
236 case JList.HORIZONTAL_WRAP:
237 distance = (list.getWidth()/
cellHeight)*diffLines+diffElements;
241 distance = diffLines+diffElements;
244 final int index = list.getSelectedIndex();
246 return index == -1 || index+distance < list.getModel().getSize();
250 return index == -1 || index >= -distance;
262 synchronized (getTreeLock()) {
264 switch (list.getLayoutOrientation()) {
265 case JList.HORIZONTAL_WRAP:
266 distance = (list.getWidth()/
cellHeight)*diffLines+diffElements;
270 distance = diffLines+diffElements;
273 final int index = list.getSelectedIndex();
276 newIndex = index == -1 ? 0 : Math.min(index+distance, list.getModel().getSize()-1);
277 }
else if (distance < 0) {
280 newIndex = list.getModel().getSize()-1;
282 newIndex = Math.max(index+distance, 0);
285 newIndex = index == -1 ? 0 : index;
298 synchronized (getTreeLock()) {
299 final Adjustable scrollBar = scrollPane.getVerticalScrollBar();
301 return scrollBar.getValue() < scrollBar.getMaximum()-scrollBar.getVisibleAmount();
305 return scrollBar.getValue() > scrollBar.getMinimum();
317 synchronized (getTreeLock()) {
318 final Adjustable scrollBar = scrollPane.getVerticalScrollBar();
319 final int value = scrollBar.getValue()+distance*
cellHeight;
320 scrollBar.setValue(value);
321 final int index = list.getSelectedIndex();
323 final int firstIndex = list.getFirstVisibleIndex();
324 if (index < firstIndex) {
325 switch (list.getLayoutOrientation()) {
326 case JList.HORIZONTAL_WRAP:
327 final int columns = list.getWidth()/
cellHeight;
336 final int lastIndex = list.getLastVisibleIndex();
337 if (index > lastIndex) {
338 switch (list.getLayoutOrientation()) {
339 case JList.HORIZONTAL_WRAP:
340 final int columns = list.getWidth()/
cellHeight;
341 final int newTmpColumn = lastIndex-lastIndex%columns+index%columns;
343 if (newTmpColumn <= lastIndex) {
344 newColumn = newTmpColumn;
346 newColumn = newTmpColumn >= columns ? newTmpColumn-columns : lastIndex;
376 if (doubleClickCommandList != null && e.getClickCount() > 1) {
377 doubleClickCommandList.
execute();
379 super.mouseClicked(e);
386 public void mouseEntered(@NotNull
final MouseEvent e,
final boolean debugGui) {
387 super.mouseEntered(e, debugGui);
396 super.mouseExited(e);
405 super.mouseClicked(e);
423 super.mouseClicked(e);
431 private void doSelect(@NotNull
final MouseEvent e) {
432 synchronized (getTreeLock()) {
433 setSelectedIndex(list.getFirstVisibleIndex()+list.locationToIndex(e.getPoint()));
442 synchronized (getTreeLock()) {
443 final int index = list.locationToIndex(e.getPoint());
446 tooltipRectangle = null;
451 final Rectangle rectangle = list.getCellBounds(index, index);
452 if (rectangle == null || !rectangle.contains(e.getPoint())) {
454 tooltipRectangle = null;
459 tooltipIndex = list.getFirstVisibleIndex()+index;
460 tooltipRectangle = rectangle;
470 synchronized (getTreeLock()) {
471 final int newIndex2 = Math.min(Math.max(newIndex, 0), list.getModel().getSize()-1);
472 final int index = list.getSelectedIndex();
473 if (newIndex2 == index) {
477 list.setSelectedIndex(newIndex2);
478 if (newIndex2 >= 0) {
479 list.ensureIndexIsVisible(newIndex2);
489 synchronized (getTreeLock()) {
504 if (tooltipIndex == -1) {
508 if (rectangle == null) {
514 tooltipRectangle = null;
517 updateTooltip(tooltipIndex, gui.getX()+getX()+rectangle.x, gui.getY()+getY()+rectangle.y, rectangle.width, rectangle.height);
548 protected abstract void updateTooltip(
final int index,
final int x,
final int y,
final int w,
final int h);
557 synchronized (getTreeLock()) {
558 list.setLayoutOrientation(layoutOrientation);
559 list.setVisibleRowCount(visibleRowCount);
569 synchronized (getTreeLock()) {
570 return list.getSelectedValue();
579 final Dimension result = list.getPreferredSize();
580 return result == null ? super.getPreferredSize() : result;
588 final Dimension result = list.getMinimumSize();
589 return result == null ? super.getMinimumSize() : result;
596 public void setBounds(
final int x,
final int y,
final int width,
final int height) {
597 super.setBounds(x, y, width, height);
598 scrollPane.setSize(width, height);
599 listCellRenderer.
setSize(width, cellHeight);
Listener for GUIElement related events.
static Gui getGui(@NotNull final Component element)
Returns the Gui an element is part of.
void setChanged()
Records that the contents have changed and must be repainted.
void setSelectedIndex(final int newIndex)
Update the selected list entry.
final TooltipManager tooltipManager
The TooltipManager to update.
final JList< T > list
The list used to display the cells.
final int cellHeight
The height of a list cell in pixels.
void dispose()
Releases all allocated resources.
final GUIElementListener elementListener
The GUIElementListener to notify.
void setLayoutOrientation(final int layoutOrientation, final int visibleRowCount)
Sets the layout orientation.
boolean canMoveSelection(final int diffLines, final int diffElements)
Returns whether the selection can be moved.
Dimension getMinimumSize()
void updateTooltip()
Updates the current tooltip text.
final JScrollPane scrollPane
The scroll pane instance used to display the list.
A GUIElement instance representing an in-game item.
T getElement(final int index)
Returns the GUIElement for a given index.
void dispose()
Releases all allocated resources.
GUIList(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int cellWidth, final int cellHeight, @NotNull final GUIListCellRenderer< T > listCellRenderer, @Nullable final CommandList doubleClickCommandList)
Creates a new instance.
static final long serialVersionUID
The serial version UID.
final String name
The name of this element.
A JViewport that allows updating the viewport state.
Interface defining an abstract GUI element.
int resizeElements(final int newSize)
Changes the number of list elements.
void resetScroll()
Resets the scroll index to the default value.
A GUIElement that can be set to active or inactive.
Rectangle tooltipRectangle
The location of the tooltip.
void addElement(@NotNull final T element)
Adds an GUIElement to the list.
final DefaultListModel< T > model
The list model of list.
Utility class for Gui related functions.
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...
boolean canScroll(final int distance)
Returns whether the list can be scrolled.
final CommandList doubleClickCommandList
The CommandList to execute on double-clicks or.
final GUIListCellRenderer<? extends T > listCellRenderer
The GUIListCellRenderer for the list.
Dimension getPreferredSize()
void mouseClicked(@NotNull final MouseEvent e)
Will be called when the user has clicked (pressed+released) this element.This event will be delivered...
void selectionChanged()
Called whenever the selected list entry has changed.
void doSelect(@NotNull final MouseEvent e)
Selects the list entry corresponding to a MouseEvent instance.
void update()
Updates the viewport state.
void setSize(final int width, int height)
Updates the component's size.
void moveSelection(final int diffLines, final int diffElements)
Moves the selection.
void mouseMoved(@NotNull final MouseEvent e)
Will be called when the mouse moves within this component.before. the mouse event relative to this el...
final ListSelectionListener listSelectionListener
The ListSelectionListener attached to list.
void setBounds(final int x, final int y, final int width, final int height)
Object getSelectedObject()
Returns the selected list object.
void mouseEntered(@NotNull final MouseEvent e, final boolean debugGui)
Will be called when the mouse has entered the bounding box of this element.the mouse event relative t...
A list of GUICommand instances.
void mouseExited(@NotNull final MouseEvent e)
Will be called when the mouse has left the bounding box of this element.This function will not be cal...
int tooltipIndex
The index of the currently shown tooltip.
A GUIElement that displays a list of entries.
final GUIListViewport viewport
The viewport used by scrollPane.
void execute()
Execute the command list by calling GUICommand#execute() for each command in order.
void mouseDragged(@NotNull final MouseEvent e)
Will be called when the mouse moves within this component while the button is pressed.This event will be delivered after mouseMoved(MouseEvent). Note: if the mouse leaves this element's bounding box while the mouse button is still pressed, furthermouseDragged (but nomouseMoved ) events will be generated. the mouse event relative to this element
void doTooltip(@NotNull final MouseEvent e)
Updates the tooltip text corresponding to a MouseEvent instance.
void scroll(final int distance)
Moves the list.
void mousePressed(@NotNull final MouseEvent e)
Will be called when the user has pressed the mouse inside this element.the mouse event relative to th...