Crossfire JXClient, Trunk  R20561
GUIComboBox.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.combobox;
23 
30 import java.awt.Component;
31 import java.awt.Dimension;
32 import java.awt.Transparency;
33 import java.awt.event.ActionListener;
34 import javax.swing.DefaultComboBoxModel;
35 import javax.swing.JComboBox;
36 import javax.swing.JList;
37 import javax.swing.ListCellRenderer;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
40 
46 public abstract class GUIComboBox<T> extends AbstractGUIElement {
47 
51  private static final long serialVersionUID = 1;
52 
56  @NotNull
57  private final DefaultComboBoxModel<T> model = new DefaultComboBoxModel<>();
58 
62  @Nullable
63  private final GUILabelLog label;
64 
68  @NotNull
69  private final JComboBox<T> comboBox = new JComboBox<>(model);
70 
74  @NotNull
75  @SuppressWarnings("FieldCanBeLocal")
76  private final ListCellRenderer<T> renderer = this::getListCellRendererComponent;
77 
81  @NotNull
82  private final ActionListener actionListener = e -> updateSelectedItem();
83 
91  protected GUIComboBox(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final GUILabelLog label) {
92  super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT);
93  this.label = label;
94  comboBox.setFocusable(false);
95  comboBox.setRenderer(renderer);
96  comboBox.addActionListener(actionListener);
97  add(comboBox);
98  }
99 
103  @Override
104  public void dispose() {
105  super.dispose();
106  comboBox.removeActionListener(actionListener);
107  }
108 
112  @Override
113  public Dimension getPreferredSize() {
114  final Dimension result = comboBox.getPreferredSize();
115  return result == null ? super.getPreferredSize() : result;
116  }
117 
121  @Override
122  public Dimension getMinimumSize() {
123  final Dimension result = comboBox.getMinimumSize();
124  return result == null ? super.getMinimumSize() : result;
125  }
126 
130  @Override
131  public void setBounds(final int x, final int y, final int width, final int height) {
132  super.setBounds(x, y, width, height);
133  comboBox.setSize(width, height);
134  }
135 
140  protected void updateModel(@NotNull final Iterable<T> elements) {
141  model.removeAllElements();
142  for (final T element : elements) {
143  model.addElement(element);
144  }
145  }
146 
156  @NotNull
157  protected abstract Component getListCellRendererComponent(@NotNull final JList<? extends T> list, @Nullable final T value, final int index, final boolean selected, final boolean cellHasFocus);
158 
162  protected void updateSelectedItem() {
163  if (label == null) {
164  return;
165  }
166 
167  setChanged();
168  @SuppressWarnings("unchecked") final T item = (T)comboBox.getSelectedItem();
169  label.updateText(item == null ? "" : getDescription(item));
170  }
171 
177  @NotNull
178  protected abstract String getDescription(@NotNull T item);
179 
180 }
final JComboBox< T > comboBox
The Swing component that implements the combo box.
final TooltipManager tooltipManager
The TooltipManager to update.
final GUILabelLog label
The GUILog to update or.
void setChanged()
Records that the contents have changed and must be repainted.
void dispose()
Releases all allocated resources.
void updateModel(@NotNull final Iterable< T > elements)
Updates entries shown in the combo box.
final GUIElementListener elementListener
The GUIElementListener to notify.
void updateText(@NotNull final CharSequence string)
Sets the displayed text by parsing a string.
A gui element implementing a static text field which may contain media tags.
abstract Component getListCellRendererComponent(@NotNull final JList<? extends T > list, @Nullable final T value, final int index, final boolean selected, final boolean cellHasFocus)
Returns a Component that displays the.
void setBounds(final int x, final int y, final int width, final int height)
Interface defining an abstract GUI element.
Definition: GUIElement.java:32
static final long serialVersionUID
The serial version UID.
final ListCellRenderer< T > renderer
The ListCellRenderer for comboBox.
abstract String getDescription(@NotNull T item)
Returns the description for an item.
void updateSelectedItem()
Called whenever the selected item has changed.
final DefaultComboBoxModel< T > model
The model for comboBox.
final ActionListener actionListener
The ActionListener for comboBox.
A GUIElement that displays a combo box.
Abstract base class for GUI elements to be shown in Guis.
Abstract base class for gui elements implementing text fields.
Definition: GUILog.java:48