Crossfire JXClient, Trunk
GUICharOptionsComboBox.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.combobox;
24 
32 import java.awt.Component;
33 import java.util.ArrayList;
34 import java.util.Comparator;
35 import java.util.Iterator;
36 import java.util.List;
37 import javax.swing.JLabel;
38 import javax.swing.JList;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
41 
47 public class GUICharOptionsComboBox extends GUIComboBox<Integer> {
48 
52  private static final long serialVersionUID = 1L;
53 
57  @NotNull
58  private final NewCharModel newCharModel;
59 
63  @NotNull
64  private final JLabel renderer = new JLabel();
65 
69  @NotNull
71 
72  @Override
73  public void changed() {
74  updateModel();
76  }
77 
78  };
79 
89  public GUICharOptionsComboBox(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final NewCharModel newCharModel, @Nullable final GUILabelLog label, @NotNull final GuiFactory guiFactory) {
90  super(tooltipManager, elementListener, name, label, guiFactory);
91  this.newCharModel = newCharModel;
93  updateModel();
95  }
96 
97  @Override
98  public void dispose() {
99  super.dispose();
101  }
102 
103  @Override
104  public void notifyOpen() {
105  }
106 
111  private void updateModel() {
112  final Choice option = newCharModel.getOption();
113  @Nullable final List<Integer> model;
114  if (option == null) {
115  model = null;
116  } else {
117  model = new ArrayList<>();
118  final List<String> choices = new ArrayList<>(option.getChoices().values());
119  for (int i = 0; i < choices.size(); i++) {
120  model.add(i);
121  }
122  model.sort(Comparator.comparing(choices::get));
123  }
126  }
127 
128  @NotNull
129  @Override
130  protected Component getListCellRendererComponent(@NotNull final JList<? extends Integer> list, @Nullable final Integer value, final int index, final boolean selected, final boolean cellHasFocus) {
131  final Choice option = newCharModel.getOption();
132  final String text;
133  if (option == null || value == null) {
134  text = "";
135  } else {
136  final Iterator<String> it = option.getChoices().values().iterator();
137  for (int i = 0; i < value; i++) {
138  if (it.hasNext()) {
139  it.next();
140  }
141  }
142  text = it.hasNext() ? it.next() : "";
143  }
144  renderer.setText(text);
145  return renderer;
146  }
147 
148  @Override
149  protected void updateSelectedItem(@Nullable final Integer item) {
150  newCharModel.setOptionIndex(item == null ? -1 : item);
151  }
152 
153  @NotNull
154  @Override
155  protected String getDescription(@Nullable final Integer item) {
156  return "";
157  }
158 
159 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.renderer
final JLabel renderer
Definition: GUICharOptionsComboBox.java:64
com.realtime.crossfire.jxclient.gui.label.NewCharModelListener
Definition: NewCharModelListener.java:30
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.character.Choice
Definition: Choice.java:34
com.realtime.crossfire.jxclient.gui.combobox.GUIComboBox< Integer >::setSelectedItem
void setSelectedItem( @NotNull final T item)
Definition: GUIComboBox.java:221
com.realtime.crossfire.jxclient.gui.label
Definition: AbstractLabel.java:23
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.serialVersionUID
static final long serialVersionUID
Definition: GUICharOptionsComboBox.java:52
com.realtime.crossfire.jxclient.gui.label.NewCharModel.addListener
void addListener(@NotNull final NewCharModelListener listener)
Definition: NewCharModel.java:208
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.log.GUILabelLog
Definition: GUILabelLog.java:38
com.realtime.crossfire.jxclient.gui.log
Definition: Buffer.java:23
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.getDescription
String getDescription(@Nullable final Integer item)
Definition: GUICharOptionsComboBox.java:155
com.realtime.crossfire.jxclient.gui.combobox.GUIComboBox
Definition: GUIComboBox.java:50
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.notifyOpen
void notifyOpen()
Definition: GUICharOptionsComboBox.java:104
com.realtime.crossfire.jxclient.gui.combobox.GUIComboBox< Integer >::model
final DefaultComboBoxModel< T > model
Definition: GUIComboBox.java:73
com.realtime.crossfire.jxclient.gui.combobox.GUIComboBox< Integer >::updateSelectedItem
void updateSelectedItem()
Definition: GUIComboBox.java:195
com.realtime.crossfire.jxclient.gui.combobox.GUIComboBox< Integer >::label
final GUILabelLog label
Definition: GUIComboBox.java:79
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.newCharModelListener
final NewCharModelListener newCharModelListener
Definition: GUICharOptionsComboBox.java:70
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.newCharModel
final NewCharModel newCharModel
Definition: GUICharOptionsComboBox.java:58
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.updateModel
void updateModel()
Definition: GUICharOptionsComboBox.java:111
com.realtime.crossfire.jxclient.gui.gui.TooltipManager
Definition: TooltipManager.java:33
com.realtime.crossfire.jxclient.gui.label.NewCharModel
Definition: NewCharModel.java:43
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox
Definition: GUICharOptionsComboBox.java:47
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire.jxclient.gui.label.NewCharModel.setOptionIndex
void setOptionIndex(final int optionIndex)
Definition: NewCharModel.java:454
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.getListCellRendererComponent
Component getListCellRendererComponent(@NotNull final JList<? extends Integer > list, @Nullable final Integer value, final int index, final boolean selected, final boolean cellHasFocus)
Definition: GUICharOptionsComboBox.java:130
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.character.Choice.getChoices
Map< String, String > getChoices()
Definition: Choice.java:89
com.realtime.crossfire.jxclient.character
Definition: Choice.java:23
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.updateSelectedItem
void updateSelectedItem(@Nullable final Integer item)
Definition: GUICharOptionsComboBox.java:149
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getOptionIndex
int getOptionIndex()
Definition: NewCharModel.java:446
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.dispose
void dispose()
Definition: GUICharOptionsComboBox.java:98
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getOption
Choice getOption()
Definition: NewCharModel.java:438
com.realtime.crossfire.jxclient.gui.combobox.GUICharOptionsComboBox.GUICharOptionsComboBox
GUICharOptionsComboBox(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final NewCharModel newCharModel, @Nullable final GUILabelLog label, @NotNull final GuiFactory guiFactory)
Definition: GUICharOptionsComboBox.java:89
com.realtime.crossfire.jxclient.gui.label.NewCharModel.removeListener
void removeListener(@NotNull final NewCharModelListener listener)
Definition: NewCharModel.java:216
com.realtime.crossfire.jxclient.gui.gui.GUIElementListener
Definition: GUIElementListener.java:32