fix: Adds separators in listview combobox

- Height of listview compobox popup adjusts to number of gamelist views
This commit is contained in:
lantzelot-swe 2021-04-07 23:29:49 +02:00
parent e46308da94
commit 80221c523f
2 changed files with 65 additions and 24 deletions

View File

@ -4,6 +4,7 @@ import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
@ -15,17 +16,17 @@ public class GameListDataRenderer extends DefaultListCellRenderer
private Color fav1Color = Color.BLACK;
private Color fav1ColorSelected = Color.WHITE;
//Orange
private Color fav2Color = new Color(255, 106, 0);
private Color fav2ColorSelected = new Color(255, 163, 132);
private Color fav2Color = new Color(0, 38, 255);
private Color fav2ColorSelected = new Color(186, 202, 255);
//Blue
private Color fav3Color = new Color(0, 38, 255);
private Color fav3ColorSelected = new Color(186, 202, 255);
private Color fav3Color = new Color(255, 106, 0);
private Color fav3ColorSelected = new Color(255, 163, 132);
//Red
private Color fav4Color = Color.RED;
private Color fav4ColorSelected = Color.PINK;
private Color fav4Color = Color.GREEN.darker();
private Color fav4ColorSelected = Color.GREEN;
//Green
private Color fav5Color = Color.GREEN.darker();
private Color fav5ColorSelected = Color.GREEN;
private Color fav5Color = Color.RED;
private Color fav5ColorSelected = Color.PINK;
private final Font bold;
private final Font plain;
@ -51,7 +52,7 @@ public class GameListDataRenderer extends DefaultListCellRenderer
}
else
{
handleGameListView(value, isSelected);
handleGameListView(value, isSelected, index);
}
return listCellRendererComponent;
}
@ -90,13 +91,18 @@ public class GameListDataRenderer extends DefaultListCellRenderer
}
}
private void handleGameListView(Object value, boolean isSelected)
private void handleGameListView(Object value, boolean isSelected, int index)
{
this.setBorder(null);
GameView view = (GameView) value;
if (view.getGameViewId() == GameView.FAVORITES_ID)
{
this.setFont(bold);
this.setForeground(isSelected ? fav1ColorSelected : fav1Color);
if(index > -1)
{
this.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.LIGHT_GRAY));
}
}
else if (view.getGameViewId() == GameView.FAVORITES_2_ID)
{
@ -117,6 +123,10 @@ public class GameListDataRenderer extends DefaultListCellRenderer
{
this.setFont(bold);
this.setForeground(isSelected ? fav5ColorSelected : fav5Color);
if(index > -1)
{
this.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY));
}
}
else
{

View File

@ -3,6 +3,7 @@ package se.lantz.gui;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@ -14,6 +15,7 @@ import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
@ -24,6 +26,9 @@ import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.basic.ComboPopup;
import se.lantz.gui.gameview.GameViewManager;
import se.lantz.model.MainViewModel;
@ -168,6 +173,31 @@ public class ListPanel extends JPanel
});
listViewComboBox.setModel(uiModel.getGameViewModel());
listViewComboBox.setRenderer(new GameListDataRenderer());
listViewComboBox.addPopupMenuListener(new PopupMenuListener()
{
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e)
{
ComboPopup popup = (ComboPopup) listViewComboBox.getUI().getAccessibleChild(listViewComboBox, 0);
//Each row is 14 pixels
int height = listViewComboBox.getModel().getSize() * 14 + 6;
((JComponent) popup)
.setPreferredSize(new Dimension(listViewComboBox.getSize().width, Math.min(height, 700)));
((JComponent) popup).setLayout(new GridLayout(1, 1));
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
{
//Empty
}
@Override
public void popupMenuCanceled(PopupMenuEvent e)
{
//Empty
}
});
}
return listViewComboBox;
}
@ -176,10 +206,12 @@ public class ListPanel extends JPanel
{
return getList().getSelectedValuesList();
}
boolean isSingleGameSelected()
{
return getList().getSelectedIndices().length == 1;
}
int getSelectedIndexInList()
{
return getList().getSelectedIndex();
@ -275,9 +307,9 @@ public class ListPanel extends JPanel
super.removeSelectionInterval(start, end);
}
}
@Override
public void addSelectionInterval(int anchor, int lead)
public void addSelectionInterval(int anchor, int lead)
{
if (!uiModel.isDataChanged())
{
@ -384,7 +416,8 @@ public class ListPanel extends JPanel
@Override
public void mouseClicked(MouseEvent e)
{
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2 && list.getSelectionModel().getSelectedItemsCount() == 1)
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2 &&
list.getSelectionModel().getSelectedItemsCount() == 1)
{
//trigger run game...
MainWindow.getInstance().getMainPanel().runCurrentGame();
@ -443,57 +476,55 @@ public class ListPanel extends JPanel
{
if (!uiModel.isDataChanged())
{
for (GameListData glData : list.getSelectedValuesList())
for (GameListData glData : list.getSelectedValuesList())
{
uiModel.toggleFavorite(glData);
}
mainPanel.repaintAfterModifications();
}
}
public void toggleFavorite2()
{
if (!uiModel.isDataChanged())
{
for (GameListData glData : list.getSelectedValuesList())
for (GameListData glData : list.getSelectedValuesList())
{
uiModel.toggleFavorite2(glData);
}
mainPanel.repaintAfterModifications();
}
}
public void toggleFavorite3()
{
if (!uiModel.isDataChanged())
{
for (GameListData glData : list.getSelectedValuesList())
for (GameListData glData : list.getSelectedValuesList())
{
uiModel.toggleFavorite3(glData);
}
mainPanel.repaintAfterModifications();
}
}
public void toggleFavorite4()
{
if (!uiModel.isDataChanged())
{
for (GameListData glData : list.getSelectedValuesList())
for (GameListData glData : list.getSelectedValuesList())
{
uiModel.toggleFavorite4(glData);
}
mainPanel.repaintAfterModifications();
}
}
public void toggleFavorite5()
{
if (!uiModel.isDataChanged())
{
for (GameListData glData : list.getSelectedValuesList())
for (GameListData glData : list.getSelectedValuesList())
{
uiModel.toggleFavorite5(glData);
}